yum-slop/YOTS
an optimized toggle system for vrchat
git clone https://git.yummers.dev/yum-slop/YOTS
4cb2a2b
v1.1.0
1#if UNITY_EDITOR 2 3using UnityEngine ; 4using nadena . dev . ndmf ; 5using nadena . dev . ndmf . builtin ; 6using nadena . dev . ndmf . localization ; 7using nadena . dev . ndmf . VRChat ; 8using VRC . SDK3 . Avatars . ScriptableObjects ; 9using UnityEditor ; 10using System ; 11using System . Collections . Generic ; 12using System . Linq ; 13using UnityEditor . Animations ; 14 15[ assembly : ExportsPlugin ( typeof ( YOTS . YOTSNDMFGenerator ))] 16 17namespace YOTS 18{ 19public class YOTSNDMFGenerator : Plugin < YOTSNDMFGenerator > { 20private readonly Localizer localizer = new Localizer ( "en-us" , () => 21new List < ( string , Func < string , string > ) > { 22( "en-us" , key => key ) 23}) ; 24 25public override string DisplayName => "YOTS Animator Generator" ; 26 27protected override void Configure () { 28// First pass: Retrieve and stash configuration 29InPhase ( BuildPhase . Resolving ) 30. Run ( "Cache YOTS Config" , ctx => { 31var config = ctx . AvatarRootObject . GetComponentInChildren < YOTSNDMFConfig > () ; 32if ( config == null ) { 33ctx . GetState < YOTSBuildState > () . skipGeneration = true ; 34Debug . Log ( "No YOTS config found - skipping." ) ; 35return ; 36} 37if ( config . jsonConfig == null ) { 38ctx . GetState < YOTSBuildState > () . skipGeneration = true ; 39ErrorReport . WithContextObject ( ctx . AvatarRootObject , () => { 40ErrorReport . ReportException ( 41new Exception ( "No YOTS config found" ) , 42"Missing required YOTS configuration" 43) ; 44}) ; 45return ; 46} 47ctx . GetState < YOTSBuildState > () . jsonConfig = config . jsonConfig . text ; 48}) 49// Shoutsout anatawa12/AvatarOptimizer 50. BeforePass ( RemoveEditorOnlyPass . Instance ) ; 51 52// Second pass: Generate and merge animator 53InPhase ( BuildPhase . Transforming ) 54. Run ( "Generate YOTS Animator" , ctx => { 55var config = ctx . GetState < YOTSBuildState > () ; 56if ( config . skipGeneration ) { 57return ; 58} 59if ( config == null ) { 60ErrorReport . WithContextObject ( ctx . AvatarRootObject , () => { 61ErrorReport . ReportException ( 62new Exception ( "No YOTS config component found" ) , 63"Missing required YOTS configuration" 64) ; 65}) ; 66return ; 67} 68if ( config . jsonConfig == null ) { 69ErrorReport . WithContextObject ( ctx . AvatarRootObject , () => { 70ErrorReport . ReportException ( 71new Exception ( "Missing JSON config file" ) , 72"YOTS config component is missing required JSON configuration" 73) ; 74}) ; 75return ; 76} 77 78// Get menu and parameters 79var descriptor = ctx . AvatarDescriptor ; 80if ( descriptor == null ) { 81ErrorReport . WithContextObject ( ctx . AvatarRootObject , () => { 82ErrorReport . ReportException ( 83new Exception ( "Avatar descriptor is missing" ) , 84"Cannot find VRC Avatar Descriptor" 85) ; 86}) ; 87return ; 88} 89RuntimeAnimatorController originalAnimator = descriptor . baseAnimationLayers [ 4 ] . animatorController ; 90var menu = descriptor . expressionsMenu ; 91var parameters = descriptor . expressionParameters ; 92if ( parameters == null || menu == null ) { 93ErrorReport . WithContextObject ( descriptor , () => { 94ErrorReport . ReportException ( 95new Exception ( "Missing required VRC assets" ) , 96"Avatar is missing required Expression Parameters or Menu" 97) ; 98}) ; 99return ; 100} 101// Create copies so the originals don't get modified 102menu = DeepCopyMenu ( menu ) ; 103parameters = UnityEngine . Object . Instantiate ( parameters ) ; 104descriptor . expressionsMenu = menu ; 105descriptor . expressionParameters = parameters ; 106 107// Generate the YOTS animator. 108RuntimeAnimatorController generatedAnimator = YOTSCore . GenerateAnimator ( 109config . jsonConfig , 110parameters , 111menu 112) ; 113if ( generatedAnimator == null ) { 114ErrorReport . WithContextObject ( ctx . AvatarRootObject , () => { 115ErrorReport . ReportException ( 116new Exception ( "Failed to generate animator" ) , 117"YOTS animator generation failed" 118) ; 119}) ; 120return ; 121} 122 123// If no original animator, just assign the generated one. 124if ( originalAnimator == null ) { 125descriptor . baseAnimationLayers [ 4 ] . animatorController = generatedAnimator ; 126return ; 127} 128// Else append the generated animator to the original. 129AnimatorController originalController = originalAnimator as AnimatorController ; 130AnimatorController generatedController = generatedAnimator as AnimatorController ; 131MergeAnimatorControllers ( localizer , originalController , generatedController ) ; 132descriptor . baseAnimationLayers [ 4 ] . animatorController = originalController ; 133}) ; 134} 135 136// Simply append generated params and layers to the original animator. 137private static void MergeAnimatorControllers ( Localizer localizer , AnimatorController original , AnimatorController generated ) { 138// Merge parameters from generated into original. 139foreach ( var genParam in generated . parameters ) { 140// This is an O(m*n) check but m and n should be small enough to not matter. 141if ( original . parameters . Any ( p => p . name == genParam . name )) { 142ErrorReport . WithContextObject ( original , () => { 143ErrorReport . ReportException ( 144new Exception ( $"Parameter ' { genParam . name } ' already exists" ) , 145"Parameter name conflict in animator" 146) ; 147}) ; 148return ; 149} 150original . AddParameter ( genParam ) ; 151} 152 153// Append each YOTS layer after the original layers. 154foreach ( var genLayer in generated . layers ) { 155// This isn't strictly an error but if someone already has layers named 156// YOTS_* that's probably not on purpose. 157if ( original . layers . Any ( l => l . name == genLayer . name )) { 158ErrorReport . WithContextObject ( original , () => { 159ErrorReport . ReportException ( 160new Exception ( $"Layer ' { genLayer . name } ' already exists" ) , 161"Layer name conflict in animator" 162) ; 163}) ; 164return ; 165} 166var newLayer = new AnimatorControllerLayer { 167name = genLayer . name , 168defaultWeight = genLayer . defaultWeight , 169stateMachine = genLayer . stateMachine 170} ; 171original . AddLayer ( newLayer ) ; 172} 173} 174 175private class YOTSBuildState { 176public string jsonConfig ; 177public bool skipGeneration ; 178} 179 180private static VRCExpressionsMenu DeepCopyMenu ( VRCExpressionsMenu sourceMenu ) { 181var copiedMenu = UnityEngine . Object . Instantiate ( sourceMenu ) ; 182// Deep copy all submenu references 183for ( int i = 0 ; i < copiedMenu . controls . Count ; i ++ ) { 184var control = copiedMenu . controls [ i ] ; 185if ( control . type == VRCExpressionsMenu . Control . ControlType . SubMenu ) { 186control . subMenu = DeepCopyMenu ( control . subMenu ) ; 187} 188} 189return copiedMenu ; 190} 191} 192} 193 194#endif // UNITY_EDITOR