Created
March 12, 2023 02:43
-
-
Save TrevTV/586f50874e03f3bf819a7510bce3a5dd to your computer and use it in GitHub Desktop.
A mess of decompiled Unity code in an attempt to help create custom stripped versions of libunity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor; | |
using System.Reflection; | |
using System; | |
using System.Text; | |
using Unity.VisualScripting; | |
using System.IO; | |
using System.Linq; | |
public class StripSTuff : MonoBehaviour | |
{ | |
[MenuItem("Stripping/UnityType.GetTypes()")] | |
public static void GetTypes() | |
{ | |
Assembly asm = typeof(AndroidBlitType).Assembly; | |
Type unityType = asm.GetType("UnityEditor.UnityType"); | |
MethodInfo getTypes = unityType.GetMethod("GetTypes", BindingFlags.Static | BindingFlags.Public); | |
dynamic typeCollection = getTypes.Invoke(null, new object[] { }); | |
foreach (object a in typeCollection) | |
{ | |
/*string nativeNamespace = (string)unityType.GetProperty("nativeNamespace").GetValue(a); | |
string name = (string)unityType.GetProperty("name").GetValue(a); | |
string qualifiedName = (string)unityType.GetProperty("qualifiedName").GetValue(a); | |
Debug.Log($"{nativeNamespace} : {name} : {qualifiedName} :::: {unityType.GetProperty("module").GetValue(a)}");*/ | |
Debug.Log(unityType.GetProperty("module").GetValue(a)); | |
} | |
} | |
[MenuItem("Stripping/Generate ClassRegistration")] | |
public static void GenClassReg() | |
{ | |
// WriteModuleAndClassRegistrationFile | |
Assembly asm = typeof(AndroidBlitType).Assembly; | |
Type unityType = asm.GetType("UnityEditor.UnityType"); | |
MethodInfo getTypes = unityType.GetMethod("GetTypes", BindingFlags.Static | BindingFlags.Public); | |
dynamic typeCollection = getTypes.Invoke(null, new object[] { }); | |
StringBuilder w = new StringBuilder(); | |
#region WriteFunctionRegisterStaticallyLinkedModulesGranular | |
/* Native Module Generation Code */ | |
HashSet<string> nativeModules = new HashSet<string>(); | |
foreach (object a in typeCollection) | |
{ | |
string name = (string)unityType.GetProperty("module").GetValue(a); | |
if (name != "undefined" | |
&& !(bool)unityType.GetProperty("isEditorOnly").GetValue(a) | |
&& name != "Editor" // hardcoded to prevent errors during libunity compilation | |
&& name != "ClusterInput") | |
nativeModules.Add(name); | |
} | |
/* End Module Generation Code */ | |
Type comparerType = asm.GetType("UnityEditor.CodeStrippingUtils").GetNestedType("ModuleDependencyComparer"); | |
w.AppendLine("extern \"C\" void RegisterStaticallyLinkedModulesGranular()"); | |
w.AppendLine("{"); | |
IOrderedEnumerable<string> orderedEnumerable = Enumerable.OrderBy<string, string>(nativeModules, (string x) => x, (IComparer<string>)Activator.CreateInstance(comparerType)); | |
foreach (string str in orderedEnumerable) | |
{ | |
w.AppendLine("\tvoid RegisterModule_" + str + "();"); | |
w.AppendLine("\tRegisterModule_" + str + "();"); | |
w.AppendLine(); | |
} | |
w.AppendLine("}"); | |
w.AppendLine(); | |
#endregion | |
#region WriteStaticallyLinkedModuleClassRegistration | |
w.AppendLine("template <typename T> void RegisterUnityClass(const char*);"); | |
w.AppendLine("template <typename T> void RegisterStrippedType(int, const char*, const char*);"); | |
w.AppendLine(); | |
// START <CodeStrippingUtils.WriteFunctionInvokeRegisterStaticallyLinkedModuleClasses(w, nativeClasses)> | |
w.AppendLine("void InvokeRegisterStaticallyLinkedModuleClasses()"); | |
w.AppendLine("{"); | |
w.AppendLine("\tvoid RegisterStaticallyLinkedModuleClasses();"); | |
w.AppendLine("\tRegisterStaticallyLinkedModuleClasses();"); | |
w.AppendLine("}"); | |
w.AppendLine(); | |
// END <CodeStrippingUtils.WriteFunctionInvokeRegisterStaticallyLinkedModuleClasses(w, nativeClasses)> | |
foreach (object a in typeCollection) | |
{ | |
if (unityType.GetProperty("baseClass").GetValue(a) == null | |
|| ((bool)unityType.GetProperty("isEditorOnly").GetValue(a)) == true) | |
continue; | |
bool hasNativeNamespace = (bool)unityType.GetProperty("hasNativeNamespace").GetValue(a); | |
if (hasNativeNamespace) | |
{ | |
w.AppendFormat("namespace {0} {{ class {1}; }} ", unityType.GetProperty("nativeNamespace").GetValue(a), unityType.GetProperty("name").GetValue(a)); | |
} | |
else | |
{ | |
w.AppendFormat("class {0}; ", unityType.GetProperty("name").GetValue(a)); | |
} | |
w.AppendLineFormat("template <> void RegisterUnityClass<{0}>(const char*);", unityType.GetProperty("qualifiedName").GetValue(a)); | |
} | |
w.AppendLine(); | |
w.AppendLine("void RegisterAllClasses()"); | |
w.AppendLine("{"); | |
bool wantCustomStrip = true; | |
if (!wantCustomStrip) | |
{ | |
w.AppendLine("\tvoid RegisterAllClassesGranular();"); | |
w.AppendLine("\tRegisterAllClassesGranular();"); | |
} | |
else | |
{ | |
w.AppendLine("void RegisterBuiltinTypes();"); | |
w.AppendLine("RegisterBuiltinTypes();"); | |
w.AppendLineFormat("\t//Total: {0} non stripped classes", (int)typeCollection.GetType().GetProperty("Count").GetValue(typeCollection)); | |
int num = 0; | |
foreach (object type in typeCollection) | |
{ | |
string qualName = (string)unityType.GetProperty("qualifiedName").GetValue(type); | |
w.AppendLineFormat("\t//{0}. {1}", num, qualName); | |
if ((string)unityType.GetProperty("module").GetValue(type) == "undefined") | |
{ | |
w.AppendLine("\t// Skipping, undefined module"); | |
num++; | |
continue; | |
} | |
if ((bool)unityType.GetProperty("isEditorOnly").GetValue(type)) | |
{ | |
w.AppendLine("\t// Skipping, editor only"); | |
num++; | |
continue; | |
} | |
if (qualName.Contains("Fake") | |
|| qualName.Contains("Test") | |
|| qualName == "AudioMixerController" | |
|| qualName == "AudioMixerGroupController" | |
|| qualName == "AudioMixerSnapshotController" | |
|| qualName == "MovieTexture" | |
|| qualName == "VisualEffectSubgraph" | |
|| qualName == "ClusterInputManager" | |
|| qualName == "NativeObjectType" | |
|| qualName == "SerializableManagedHost" | |
) | |
{ | |
w.AppendLine("\t// Skipping, on blacklist"); | |
num++; | |
continue; | |
} | |
if (unityType.GetProperty("baseClass").GetValue(type) != null) | |
w.AppendLineFormat("\tRegisterUnityClass<{0}>(\"{1}\");", qualName, unityType.GetProperty("module").GetValue(type)); | |
else | |
w.AppendLine("\t// Skipping, no base class"); | |
num++; | |
} | |
w.AppendLine(); | |
} | |
w.AppendLine("}"); | |
#endregion | |
File.WriteAllText("D:\\CustomGen-UnityClassRegistration.cpp", w.ToString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment