private static void DoObfuscateByConfig(string[] assemblyPath) { ObfuscatorConfig obfuscatorConfig = AssetDatabase.LoadAssetAtPath <ObfuscatorConfig>(Const.ConfigAssetPath); if (obfuscatorConfig == null) { Debug.Log(Const.ConfigAssetPath + "不存在"); return; } if (!obfuscatorConfig.enableCodeObfuscator) { return; } string uselessCodeLibAssemblyPath = obfuscatorConfig.uselessCodeLibPath; int randomSeed = obfuscatorConfig.randomSeed; if (obfuscatorConfig.useTimeSpan) { randomSeed = (int)DateTime.Now.Ticks; } bool enableNameObfuscate = obfuscatorConfig.enableNameObfuscate; bool enableCodeInject = obfuscatorConfig.enableCodeInject; ObfuscateType nameObfuscateType = obfuscatorConfig.nameObfuscateType; ObfuscateType codeInjectObfuscateType = obfuscatorConfig.codeInjectType; ObfuscateNameType obfuscateNameType = obfuscatorConfig.obfuscateNameType; int garbageMethodMultiplePerClass = obfuscatorConfig.GarbageMethodMultiplePerClass; int insertMethodCountPerMethod = obfuscatorConfig.InsertMethodCountPerMethod; DoObfuscate(assemblyPath, uselessCodeLibAssemblyPath, randomSeed, enableNameObfuscate, enableCodeInject, nameObfuscateType, codeInjectObfuscateType, obfuscateNameType, garbageMethodMultiplePerClass, insertMethodCountPerMethod); }
private void DrawDllPathItem(string[] paths, int index) { ObfuscatorConfig obfuscatorConfig = ((ObfuscatorConfig)target); EditorGUILayout.BeginHorizontal(); paths[index] = EditorGUILayout.TextField(paths[index]); if (GUILayout.Button("-", new GUILayoutOption[1] { GUILayout.Width(20f) })) { RemovePath(index); } GUILayout.Space(2f); if (GUILayout.Button("Browse...", new GUILayoutOption[1] { GUILayout.Width(80f) })) { paths[index] = CheckPath(EditorUtility.OpenFilePanel("", Application.dataPath, "dll")); } EditorGUILayout.EndHorizontal(); }
private void AddPath() { ObfuscatorConfig obfuscatorConfig = ((ObfuscatorConfig)target); string[] lastData = obfuscatorConfig.obfuscateDllPaths; if (lastData != null && lastData.Length > 0) { string[] newData = new string[lastData.Length + 1]; for (int i = 0; i < lastData.Length; i++) { newData[i] = lastData[i]; } obfuscatorConfig.obfuscateDllPaths = newData; } else { obfuscatorConfig.obfuscateDllPaths = new string[1] { "" } }; }
private void RemovePath(int index) { ObfuscatorConfig obfuscatorConfig = ((ObfuscatorConfig)target); if (obfuscatorConfig.obfuscateDllPaths == null || index > obfuscatorConfig.obfuscateDllPaths.Length - 1) { return; } string[] lastData = obfuscatorConfig.obfuscateDllPaths; string[] newData = new string[lastData.Length - 1]; for (int i = 0; i < newData.Length; i++) { if (i < index) { newData[i] = lastData[i]; } else { newData[i] = lastData[i + 1]; } } obfuscatorConfig.obfuscateDllPaths = newData; }
public override void OnInspectorGUI() { if (box == null) { box = new GUIStyle(GUI.skin.box); box.margin.left = 2; box.margin.right = 2; } //base.OnInspectorGUI(); serializedObject.Update(); ObfuscatorConfig obfuscatorConfig = ((ObfuscatorConfig)target); GUILayout.Space(10f); obfuscatorConfig.enableCodeObfuscator = EditorGUILayout.ToggleLeft("Enable Code Obfuscator", obfuscatorConfig.enableCodeObfuscator, EditorStyles.boldLabel); GUILayout.Space(10f); using (new EditorGUI.DisabledGroupScope(!obfuscatorConfig.enableCodeObfuscator)) { EditorGUILayout.BeginHorizontal(); using (new EditorGUI.DisabledGroupScope(obfuscatorConfig.useTimeSpan)) { EditorGUILayout.LabelField("Random Seed", new GUILayoutOption[1] { GUILayout.Width(100f) }); obfuscatorConfig.randomSeed = EditorGUILayout.IntField(obfuscatorConfig.randomSeed); } obfuscatorConfig.useTimeSpan = EditorGUILayout.ToggleLeft("Use Time Stamp", obfuscatorConfig.useTimeSpan); EditorGUILayout.EndHorizontal(); GUILayout.Space(5f); Header("Obfuscate Name", obfuscatorConfig.enableNameObfuscate, (enable) => obfuscatorConfig.enableNameObfuscate = enable); using (new EditorGUI.DisabledGroupScope(!obfuscatorConfig.enableNameObfuscate)) { DrawLeft(); GUILayout.Space(5f); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Filter Type", new GUILayoutOption[1] { GUILayout.Width(80f) }); obfuscatorConfig.nameObfuscateType = (ObfuscateType)EditorGUILayout.Popup((int)obfuscatorConfig.nameObfuscateType, ObfuscateTypeStr); EditorGUILayout.EndHorizontal(); GUILayout.Space(5f); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Name Source", new GUILayoutOption[1] { GUILayout.Width(80f) }); obfuscatorConfig.obfuscateNameType = (ObfuscateNameType)EditorGUILayout.Popup((int)obfuscatorConfig.obfuscateNameType, ObfuscateNameTypeStr); EditorGUILayout.EndHorizontal(); GUILayout.Space(5f); DrawRight(); } Header("Inject Code", obfuscatorConfig.enableCodeInject, (enable) => obfuscatorConfig.enableCodeInject = enable); using (new EditorGUI.DisabledGroupScope(!obfuscatorConfig.enableCodeInject)) { DrawLeft(); GUILayout.Space(5f); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Filter Type", new GUILayoutOption[1] { GUILayout.Width(80f) }); obfuscatorConfig.codeInjectType = (ObfuscateType)EditorGUILayout.Popup((int)obfuscatorConfig.codeInjectType, ObfuscateTypeStr); EditorGUILayout.EndHorizontal(); GUILayout.Space(2f); obfuscatorConfig.GarbageMethodMultiplePerClass = EditorGUILayout.IntField(new GUIContent("Generate Useless Method Multiple"), obfuscatorConfig.GarbageMethodMultiplePerClass); GUILayout.Space(2f); obfuscatorConfig.InsertMethodCountPerMethod = EditorGUILayout.IntField(new GUIContent("Call Useless Method Per Method"), obfuscatorConfig.InsertMethodCountPerMethod); GUILayout.Space(5f); DrawRight(); } } //DLL路径设置 GUILayout.Space(10f); Header("DLL Path Setting"); DrawLeft(); GUILayout.Space(5f); for (int i = 0; i < obfuscatorConfig.obfuscateDllPaths.Length; i++) { DrawDllPathItem(obfuscatorConfig.obfuscateDllPaths, i); GUILayout.Space(2f); } if (GUILayout.Button("+")) { AddPath(); } GUILayout.Space(5f); DrawRight(); //垃圾代码库路径 GUILayout.Space(10f); Header("Useless Code Library Path"); DrawLeft(); GUILayout.Space(5f); EditorGUILayout.BeginHorizontal(); obfuscatorConfig.uselessCodeLibPath = EditorGUILayout.TextField(obfuscatorConfig.uselessCodeLibPath); if (GUILayout.Button("Browse...", new GUILayoutOption[1] { GUILayout.Width(80f) })) { obfuscatorConfig.uselessCodeLibPath = CheckPath(EditorUtility.OpenFilePanel("", Application.dataPath, "dll")); } EditorGUILayout.EndHorizontal(); GUILayout.Space(5f); DrawRight(); //测试面板 GUILayout.Space(10f); Header("Test"); DrawLeft(); GUILayout.Space(5f); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Output Path", new GUILayoutOption[1] { GUILayout.Width(80f) }); obfuscatorConfig.testOutputPath = EditorGUILayout.TextField(obfuscatorConfig.testOutputPath); if (GUILayout.Button("Browse...", new GUILayoutOption[1] { GUILayout.Width(80f) })) { obfuscatorConfig.testOutputPath = CheckPath(EditorUtility.OpenFolderPanel("", Application.dataPath, "dll")); } EditorGUILayout.EndHorizontal(); GUILayout.Space(5f); EditorGUILayout.BeginHorizontal(); GUILayout.Space(5f); if (GUILayout.Button("Obfuscate", new GUILayoutOption[1] { GUILayout.Height(60f) })) { ProcessObfuscator.TestObfuscate(); } GUILayout.Space(5f); EditorGUILayout.EndHorizontal(); GUILayout.Space(5f); DrawRight(); EditorUtility.SetDirty(obfuscatorConfig); }
//测试混淆(不需要Build,直接输出) public static void TestObfuscate() { ObfuscatorConfig obfuscatorConfig = AssetDatabase.LoadAssetAtPath <ObfuscatorConfig>(Const.ConfigAssetPath); string[] pathsConfig = obfuscatorConfig.obfuscateDllPaths; if (pathsConfig == null) { Debug.LogError("目标DLL路径为空"); return; } if (!Directory.Exists(obfuscatorConfig.testOutputPath)) { Debug.LogError(string.Format("找不到混淆测试输出路径:{0}", obfuscatorConfig.testOutputPath)); return; } FileInfo[] fileInfos = new FileInfo[pathsConfig.Length]; for (int i = 0; i < pathsConfig.Length; i++) { string path = pathsConfig[i]; //有可能记录的是相对路径,这里如果找不到对应路径就找相对路径 path = File.Exists(path) ? path : (Application.dataPath.Substring(0, Application.dataPath.Length - 6) + path); bool exists = File.Exists(path); if (!exists) { Debug.Log(string.Format("找不到混淆目标文件:{0}", path)); return; } FileInfo fileInfo = new FileInfo(path); string oringalMDBPath = fileInfo.FullName + ".mdb"; if (!File.Exists(oringalMDBPath)) { Debug.Log(string.Format("找不到该MDB文件:{0}", oringalMDBPath)); return; } fileInfos[i] = fileInfo; } string[] copyDllPaths = new string[pathsConfig.Length]; for (int i = 0; i < fileInfos.Length; i++) { string targetPath = obfuscatorConfig.testOutputPath + "/" + fileInfos[i].Name; string oringalMDBPath = fileInfos[i].FullName + ".mdb"; string targetMDBPath = obfuscatorConfig.testOutputPath + "/" + fileInfos[i].Name + ".mdb"; if (File.Exists(targetPath)) { File.Delete(targetPath); } if (File.Exists(targetMDBPath)) { File.Delete(targetMDBPath); } File.Copy(fileInfos[i].FullName, targetPath); File.Copy(oringalMDBPath, targetMDBPath); copyDllPaths[i] = targetPath; } DoObfuscateByConfig(copyDllPaths); }
private static void DoObfuscate() { ObfuscatorConfig obfuscatorConfig = AssetDatabase.LoadAssetAtPath <ObfuscatorConfig>(Const.ConfigAssetPath); DoObfuscateByConfig(obfuscatorConfig.obfuscateDllPaths); }