summaryrefslogtreecommitdiffstats
path: root/Scripts/YOTSNDMFGenerator.cs
blob: 338aebc13d350811da05141876446497f096df96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#if UNITY_EDITOR

using UnityEngine;
using nadena.dev.ndmf;
using nadena.dev.ndmf.builtin;
using nadena.dev.ndmf.localization;
using nadena.dev.ndmf.VRChat;
using VRC.SDK3.Avatars.ScriptableObjects;
using UnityEditor;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Animations;

[assembly: ExportsPlugin(typeof(YOTS.YOTSNDMFGenerator))]

namespace YOTS
{
  public class YOTSNDMFGenerator : Plugin<YOTSNDMFGenerator> {
    private readonly Localizer lcl = new Localizer("en-us", () =>
        new List<(string, Func<string, string>)> {
        ("en-us", key => {
          switch (key) {
            case "json_missing": return "YOTS configuration JSON file is missing";
            case "config_missing": return "YOTS config component not found on avatar";
            case "descriptor_missing": return "VRC Avatar Descriptor is missing from avatar";
            case "expressions_missing": return "Avatar is missing Expression Parameters or Expression Menu";
            case "param_exists": return "Parameter '{0}' already exists in FX animator";
            case "layer_exists": return "Layer '{0}' already exists in FX animator";
            case "config_error": return "{0}";
            default: return null;
          }
        })
        });

    public override string DisplayName => "YOTS Animator Generator";

    protected override void Configure() {
      // First pass: Retrieve and stash configuration. By the time we're in the
      // Transforming phase, we can no longer access the YOTSConfig object.
      InPhase(BuildPhase.Resolving)
        .Run("Cache YOTS Config", ctx => {
          var configs = ctx.AvatarRootObject.GetComponentsInChildren<YOTSConfig>();
          if (configs.Length == 0) {
            ctx.GetState<YOTSBuildState>().skipGeneration = true;
            Debug.Log("No YOTS config found - skipping.");
            return;
          }
          var mergedConfig = new AnimatorConfigFile();
          foreach (var config in configs) {
            if (config.jsonConfig == null) {
              ctx.GetState<YOTSBuildState>().skipGeneration = true;
              ErrorReport.ReportError(lcl, ErrorSeverity.Error, "json_missing",
                  config.gameObject);
              return;
            }
            var parsed = JsonUtility.FromJson<AnimatorConfigFile>(config.jsonConfig.text);
            mergedConfig.toggles.AddRange(parsed.toggles);
            if (parsed.api_version != null) {
              mergedConfig.api_version = parsed.api_version;
            }
          }
          ctx.GetState<YOTSBuildState>().jsonConfig = JsonUtility.ToJson(mergedConfig);
        })
        // Shoutsout anatawa12/AvatarOptimizer
        .BeforePass(RemoveEditorOnlyPass.Instance);

      // Second pass: Generate and merge animator
      InPhase(BuildPhase.Transforming)
        .Run("Generate YOTS Animator", ctx => {
          var config = ctx.GetState<YOTSBuildState>();
          if (config.skipGeneration) {
            return;
          }
          if (config == null) {
            ErrorReport.ReportError(lcl, ErrorSeverity.Error, "config_missing",
                ctx.AvatarRootObject);
            return;
          }
          if (config.jsonConfig == null) {
            ErrorReport.ReportError(lcl, ErrorSeverity.Error, "json_missing",
                ctx.AvatarRootObject);
            return;
          }

          // Get menu and parameters
          var descriptor = ctx.AvatarDescriptor;
          if (descriptor == null) {
            ErrorReport.ReportError(lcl, ErrorSeverity.Error, "descriptor_missing",
                ctx.AvatarRootObject);
            return;
          }
          RuntimeAnimatorController originalAnimator = descriptor.baseAnimationLayers[4].animatorController;
          var menu = descriptor.expressionsMenu;
          var parameters = descriptor.expressionParameters;
          if (parameters == null || menu == null) {
            ErrorReport.ReportError(lcl, ErrorSeverity.Error, "expressions_missing",
                descriptor);
            return;
          }
          // Create copies so the originals don't get modified
          menu = DeepCopyMenu(menu);
          parameters = UnityEngine.Object.Instantiate(parameters);
          descriptor.expressionsMenu = menu;
          descriptor.expressionParameters = parameters;

          // Resolve bare names to full hierarchy paths.
          var resolvedJson = ResolveNames(config.jsonConfig, ctx.AvatarRootObject.transform);

          // Generate the YOTS animator.
          RuntimeAnimatorController generatedAnimator = null;
          try {
            generatedAnimator = YOTSCore.GenerateAnimator(
                resolvedJson,
                parameters,
                menu
            );
          } catch (ArgumentException e) {
            ErrorReport.ReportError(lcl, ErrorSeverity.Error, "config_error",
                e.Message, ctx.AvatarRootObject);
            return;
          } catch (Exception e) {
            ErrorReport.ReportException(e);
            return;
          }

          // If no original animator, just assign the generated one.
          if (originalAnimator == null) {
            descriptor.baseAnimationLayers[4].animatorController = generatedAnimator;
            return;
          }
          // Else append the generated animator to the original.
          AnimatorController originalController = originalAnimator as AnimatorController;
          AnimatorController generatedController = generatedAnimator as AnimatorController;
          MergeAnimatorControllers(originalController, generatedController);
          descriptor.baseAnimationLayers[4].animatorController = generatedController;
        });
    }

    // Simply append generated params and layers to the original animator.
    private void MergeAnimatorControllers(AnimatorController from, AnimatorController to) {
      // Merge parameters from from into to.
      foreach (var genParam in from.parameters) {
        // This is an O(m*n) check but m and n should be small enough to not matter.
        if (to.parameters.Any(p => p.name == genParam.name)) {
          ErrorReport.ReportError(lcl, ErrorSeverity.Error,
              "param_exists",
              genParam.name, to);
          return;
        }
        to.AddParameter(genParam);
      }

      // Append each YOTS layer after the to layers.
      foreach (var genLayer in from.layers) {
        // This isn't strictly an error but if someone already has layers named
        // YOTS_* that's probably not on purpose.
        if (to.layers.Any(l => l.name == genLayer.name)) {
          ErrorReport.ReportError(lcl, ErrorSeverity.Error,
              "layer_exists",
              genLayer.name, to);
          return;
        }
        var newLayer = new AnimatorControllerLayer {
          name = genLayer.name,
          defaultWeight = genLayer.defaultWeight,
          stateMachine = genLayer.stateMachine
        };
        to.AddLayer(newLayer);
      }
    }

    private class YOTSBuildState {
      public string jsonConfig;
      public bool skipGeneration;
    }

    // Resolve bare names (no '/') to full hierarchy paths. Names containing
    // '/' are kept as explicit paths.
    private static string ResolveNames(string jsonConfig, Transform avatarRoot) {
      var config = JsonUtility.FromJson<AnimatorConfigFile>(jsonConfig);
      var nameToPathsMap = BuildNameToPathsMap(avatarRoot);
      foreach (var toggle in config.toggles) {
        toggle.meshToggles = ExpandNames(toggle.meshToggles, nameToPathsMap);
        toggle.inverseMeshToggles = ExpandNames(toggle.inverseMeshToggles, nameToPathsMap);
        foreach (var bs in toggle.blendShapes) {
          bs.path = ExpandName(bs.path, nameToPathsMap);
          bs.paths = ExpandNames(bs.paths, nameToPathsMap);
        }
        foreach (var st in toggle.shaderToggles) {
          st.path = ExpandName(st.path, nameToPathsMap);
          st.paths = ExpandNames(st.paths, nameToPathsMap);
        }
      }
      return JsonUtility.ToJson(config);
    }

    private static string ExpandName(string name, Dictionary<string, List<string>> nameToPathsMap) {
      if (string.IsNullOrEmpty(name) || name.Contains('/')) return name;
      if (nameToPathsMap.TryGetValue(name, out var paths) && paths.Count == 1) return paths[0];
      return name;
    }

    private static List<string> ExpandNames(List<string> names, Dictionary<string, List<string>> nameToPathsMap) {
      if (names == null) return null;
      var resolved = new List<string>();
      foreach (var name in names) {
        if (name.Contains('/')) {
          resolved.Add(name);
        } else if (nameToPathsMap.TryGetValue(name, out var paths)) {
          resolved.AddRange(paths);
        } else {
          resolved.Add(name);
        }
      }
      return resolved;
    }

    private static Dictionary<string, List<string>> BuildNameToPathsMap(Transform root) {
      var map = new Dictionary<string, List<string>>();
      CollectPaths(root, "", map);
      return map;
    }

    private static void CollectPaths(Transform current, string currentPath, Dictionary<string, List<string>> map) {
      foreach (Transform child in current) {
        string childPath = currentPath == "" ? child.name : currentPath + "/" + child.name;
        if (!map.ContainsKey(child.name))
          map[child.name] = new List<string>();
        map[child.name].Add(childPath);
        CollectPaths(child, childPath, map);
      }
    }

    private static VRCExpressionsMenu DeepCopyMenu(VRCExpressionsMenu sourceMenu) {
        var copiedMenu = UnityEngine.Object.Instantiate(sourceMenu);
        // Deep copy all submenu references
        for (int i = 0; i < copiedMenu.controls.Count; i++) {
            var control = copiedMenu.controls[i];
            if (control.type == VRCExpressionsMenu.Control.ControlType.SubMenu) {
                control.subMenu = DeepCopyMenu(control.subMenu);
            }
        }
        return copiedMenu;
    }
  }
}

#endif  // UNITY_EDITOR