-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathObfuscator.cs
More file actions
305 lines (273 loc) · 12.2 KB
/
Copy pathObfuscator.cs
File metadata and controls
305 lines (273 loc) · 12.2 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#if UNITY_EDITOR
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
using UnityEngine.Serialization;
using VRC.SDK3.Avatars.Components;
namespace Shell.Protector
{
public class Obfuscator : ScriptableObject
{
OutputPaths outputPaths;
AssetWriter assetWriter;
List<int> obfuscatedBlendShapeIndex = new List<int>();
Dictionary<string, string> obfuscatedBlendShapeNames = new Dictionary<string, string>(); // before, after
Dictionary<AnimationClip, AnimationClip> obfuscatedClip = new Dictionary<AnimationClip, AnimationClip>(); // before, after
HashSet<string> mmdShapes = new HashSet<string>();
[FormerlySerializedAs("clone")]
public bool Clone = true;
[FormerlySerializedAs("bPreserveMMD")]
public bool PreserveMmd = true;
public Obfuscator()
{
string[] shapes =
{
"まばたき", "笑い", "ウィンク", "ウィンク右", "ウィンク2",
"ウィンク2右", "なごみ", "はぅ", "びっくり", "じと目", "キリッ",
"はちゅ目", "星目", "はぁと", "瞳小", "瞳大", "恐ろしい子!",
"ハイライト消し", "あ", "い", "えー", "う", "え", "お", "ワ",
"ω", "ω□", "∧", "▲", "はんっ!", "にやり", "にっこり",
"ぺろっ", "てへぺろ", "てへぺろ2", "口角上げ", "口角下げ",
"口横広げ", "真面目", "困る", "にこり", "怒り", "上", "下",
"頬染め", "がーん", "青ざめ", "涙", "ジト目", "△", "Λ",
"□", "にやり2", "照れ", "ん", "あ2", "白目", "あ2", "口角広げ"
};
foreach (string str in shapes)
{
mmdShapes.Add(str);
}
}
public void Clean()
{
Clone = true;
outputPaths = null;
assetWriter = null;
obfuscatedBlendShapeNames.Clear();
obfuscatedBlendShapeIndex.Clear();
obfuscatedClip.Clear();
}
static string GetAnimationPath(Transform transform)
{
var names = new List<string>();
Transform current = transform;
while (current != null && current.parent != null)
{
names.Add(current.name);
current = current.parent;
}
names.Reverse();
return string.Join("/", names);
}
public Mesh ObfuscateBlendShapeMesh(Mesh mesh, OutputPaths paths, AssetWriter writer)
{
Mesh obfuscatedMesh = Instantiate(mesh);
obfuscatedMesh.ClearBlendShapes();
for (int shapeIndex = 0; shapeIndex < mesh.blendShapeCount; shapeIndex++)
obfuscatedBlendShapeIndex.Add(shapeIndex);
//shuffle
var rng = new System.Random();
int n = obfuscatedBlendShapeIndex.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
int value = obfuscatedBlendShapeIndex[k];
obfuscatedBlendShapeIndex[k] = obfuscatedBlendShapeIndex[n];
obfuscatedBlendShapeIndex[n] = value;
}
foreach (int shapeIndex in obfuscatedBlendShapeIndex)
{
for (var frameIndex = 0; frameIndex < mesh.GetBlendShapeFrameCount(shapeIndex); frameIndex++)
{
Vector3[] deltaVertices = new Vector3[mesh.vertexCount];
Vector3[] deltaNormals = new Vector3[mesh.vertexCount];
Vector3[] deltaTangents = new Vector3[mesh.vertexCount];
mesh.GetBlendShapeFrameVertices(shapeIndex, frameIndex, deltaVertices, deltaNormals, deltaTangents);
float weight = mesh.GetBlendShapeFrameWeight(shapeIndex, frameIndex);
string blendShapeName = mesh.GetBlendShapeName(shapeIndex);
if (PreserveMmd)
{
if (mmdShapes.Contains(blendShapeName))
{
obfuscatedMesh.AddBlendShapeFrame(blendShapeName, weight, deltaVertices, deltaNormals, deltaTangents);
continue;
}
}
if (obfuscatedBlendShapeNames.ContainsKey(blendShapeName))
{
blendShapeName = obfuscatedBlendShapeNames[blendShapeName];
}
else
{
string obfuscatedBlendShapeName = GUID.Generate().ToString();
obfuscatedBlendShapeNames.Add(blendShapeName, obfuscatedBlendShapeName);
blendShapeName = obfuscatedBlendShapeName;
}
obfuscatedMesh.AddBlendShapeFrame(blendShapeName, weight, deltaVertices, deltaNormals, deltaTangents);
}
}
Debug.LogFormat("Obfuscator blendshapes : {0}", string.Join(", ", obfuscatedBlendShapeNames.Select(kv => $"{kv.Key}: {kv.Value}")));
writer.CreateAssetInFolder(obfuscatedMesh, paths.Folders.MeshGuid, paths.MeshAssetName(mesh));
AssetDatabase.Refresh();
return obfuscatedMesh;
}
public void ChangeObfuscatedBlendShapeInDescriptor(VRCAvatarDescriptor descriptor)
{
for (int i = 0; i < descriptor.VisemeBlendShapes.Length; i++)
{
if (obfuscatedBlendShapeNames.ContainsKey(descriptor.VisemeBlendShapes[i]))
descriptor.VisemeBlendShapes[i] = obfuscatedBlendShapeNames[descriptor.VisemeBlendShapes[i]];
}
for (int i = 0; i < descriptor.customEyeLookSettings.eyelidsBlendshapes.Length; i++)
{
int idx = descriptor.customEyeLookSettings.eyelidsBlendshapes[i];
descriptor.customEyeLookSettings.eyelidsBlendshapes[i] = obfuscatedBlendShapeIndex.FindIndex(0, obfuscatedBlendShapeIndex.Count,
x =>
{
return x == idx;
}
);
}
}
public void ObfuscateBlendshapeInAnim(AnimatorController anim, GameObject obj, OutputPaths paths, AssetWriter writer)
{
if (anim == null)
return;
outputPaths = paths;
assetWriter = writer;
var layers = anim.layers;
foreach (var layer in layers)
{
if (layer.name == "ShellProtectorDriver")
continue;
if (layer.name == "ShellProtector")
continue;
var stateMachine = layer.stateMachine;
if (stateMachine == null)
continue;
SearchStateMachine(stateMachine, obj);
}
}
void SearchStateMachine(AnimatorStateMachine stateMachine, GameObject obj)
{
for (int i = 0; i < stateMachine.states.Length; i++)
{
ChildAnimatorState state = stateMachine.states[i];
AnimationClip clip = SearchMotion(state.state.motion, obj);
if (clip != null)
{
state.state.motion = clip;
stateMachine.states[i] = state;
}
}
foreach (ChildAnimatorStateMachine childStateMachine in stateMachine.stateMachines)
{
SearchStateMachine(childStateMachine.stateMachine, obj);
}
}
AnimationClip SearchMotion(Motion motion, GameObject obj)
{
if (motion is AnimationClip clip)
{
var tmp = ChangeBlendShapeInClip(clip, obj);
if (clip == tmp)
return null;
return tmp;
}
else if (motion is BlendTree blendTree)
{
for (int i = 0; i < blendTree.children.Length; ++i)
{
ChildMotion childMotion = blendTree.children[i];
var result = SearchMotion(childMotion.motion, obj);
if (result != null)
{
childMotion.motion = result;
ChildMotion[] newChildren = new ChildMotion[blendTree.children.Length];
blendTree.children.CopyTo(newChildren, 0);
newChildren[i] = childMotion;
blendTree.children = newChildren;
}
}
}
return null;
}
public AnimationClip ChangeBlendShapeInClip(AnimationClip clip, GameObject obj)
{
bool detect = false;
EditorCurveBinding[] bindings = AnimationUtility.GetCurveBindings(clip);
foreach (EditorCurveBinding binding in bindings)
{
if (binding.type == typeof(SkinnedMeshRenderer) && binding.propertyName.StartsWith("blendShape."))
{
string hierarchyPath = GetAnimationPath(obj.transform);
if (binding.path != hierarchyPath)
continue;
string blendShapeName = binding.propertyName.Substring("blendShape.".Length);
if (obfuscatedBlendShapeNames.ContainsKey(blendShapeName))
{
Debug.LogFormat("find: {0},{1} in {2}", binding.path, blendShapeName, clip.name);
detect = true;
break;
}
}
}
if (!detect)
{
return clip;
}
AnimationClip newClip = clip;
if (Clone)
{
if (obfuscatedClip.ContainsKey(clip))
newClip = obfuscatedClip[clip];
else
{
newClip = Instantiate(clip);
assetWriter.CreateAssetInFolder(newClip, outputPaths.Folders.AnimGuid, outputPaths.AnimationClipName(clip, "_obfuscated"));
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
obfuscatedClip.Add(clip, newClip);
}
}
bindings = AnimationUtility.GetCurveBindings(newClip);
foreach (EditorCurveBinding binding in bindings)
{
if (binding.type == typeof(SkinnedMeshRenderer) && binding.propertyName.StartsWith("blendShape."))
{
string hierarchyPath = GetAnimationPath(obj.transform);
string blendShapeName = binding.propertyName.Substring("blendShape.".Length);
if (binding.path != hierarchyPath)
continue;
if (!obfuscatedBlendShapeNames.ContainsKey(blendShapeName))
continue;
string newBlendShapeName = obfuscatedBlendShapeNames[blendShapeName];
EditorCurveBinding newBinding = new EditorCurveBinding
{
path = binding.path,
propertyName = $"blendShape.{newBlendShapeName}",
type = binding.type
};
AnimationCurve curve = AnimationUtility.GetEditorCurve(newClip, binding);
AnimationUtility.SetEditorCurve(newClip, binding, null);
AnimationUtility.SetEditorCurve(newClip, newBinding, curve);
}
}
return newClip;
}
public IList<int> GetObfuscatedBlendShapeIndex()
{
return obfuscatedBlendShapeIndex.AsReadOnly();
}
public string GetOriginalBlendShapeName(string obfuscatedBlendShape)
{
if (!obfuscatedBlendShapeNames.ContainsKey(obfuscatedBlendShape))
return null;
return obfuscatedBlendShapeNames[obfuscatedBlendShape];
}
}
}
#endif