#if UNITY_EDITOR using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using ILCrossBinding; using Snxxz.UI; using UnityEditor; using UnityEngine; using UnityEngine.UI; [System.Reflection.Obfuscation(Exclude = true)] public class ILRuntimeCLRBinding { const string genPath = "Assets/ILRuntime/Generated"; //需要生成绑定代码的类, litjson 和 c# system 下的代码不要在这生成,会有问题 //此处配置的类,生成代码会覆盖自动生成的。 static List types = new List { //Unity相关 typeof(Transform), typeof(RectTransform), typeof(GameObject), typeof(Component), typeof(UnityEngine.Object), typeof(MonoBehaviour), typeof(Behaviour), typeof(Text), typeof(Button), typeof(Image), typeof(Toggle), //主工程相关 typeof(Redpoint), typeof(Int2), typeof(Int3), typeof(UILoader), typeof(ComponentExtersion), typeof(TransformExtension), typeof(LocalSave), typeof(ILBehaviourProxy), typeof(WidgetBehavior), typeof(ILWindowProxy), typeof(ILOneLevelWindowProxy), typeof(ModelCenter), typeof(Singleton), typeof(WindowCenter), typeof(SingletonMonobehaviour), typeof(WindowJumpMgr), typeof(Singleton), typeof(DebugEx), typeof(TextEx), typeof(ButtonEx), typeof(ImageEx), typeof(TimeUtility), typeof(HttpRequest), typeof(SingletonMonobehaviour), typeof(SysNotifyMgr), typeof(SingletonMonobehaviour), typeof(PackageRegedit), typeof(GameNetSystem), typeof(Singleton), typeof(GameNetPackBasic), typeof(OperationTimeHepler), typeof(Singleton), typeof(DtcBasic), typeof(PlayerDatas), typeof(Singleton), typeof(PlayerBaseData), typeof(Language), typeof(TimeMgr), typeof(SingletonMonobehaviour), typeof(CommonItemBaisc), typeof(ItemCell), typeof(ItemTipUtility), typeof(SkillModel), typeof(VipModel), typeof(ItemCellModel), typeof(ItemModel), typeof(PackModel), typeof(EquipModel), typeof(RedpointCenter), typeof(Singleton), typeof(ItemLogicUtility), typeof(Singleton), typeof(ItemOperateUtility), typeof(Singleton), typeof(Equation), typeof(Singleton), typeof(SkillHelper), typeof(Singleton), typeof(OpenServerActivityCenter), typeof(Singleton), typeof(GlobalTimeEvent), typeof(SingletonMonobehaviour), typeof(UIHelper), typeof(FindPreciousModel), typeof(DungeonModel), //表 typeof(ItemConfig), typeof(FuncConfigConfig), typeof(SysInfoConfig), typeof(InvestConfig), typeof(VipPrivilegeConfig), typeof(DungeonOpenTimeConfig), typeof(RealmConfig), typeof(MapConfig), typeof(OrderInfoConfig), typeof(CTGConfig), typeof(SkillConfig), typeof(PetInfoConfig), typeof(HorseConfig), typeof(NPCConfig), typeof(PlayerPropertyConfig), typeof(SpiritWeaponConfig), typeof(FuncOpenLVConfig), typeof(FightPowerParamConfig), typeof(EquipGSParamConfig), }; //需要排除的方法 static List excludeMethods = new List() { new object[]{typeof(Text),"OnRebuildRequested"}, new object[]{typeof(MonoBehaviour),"get_runInEditMode"}, new object[]{typeof(MonoBehaviour),"set_runInEditMode"}, }; static HashSet GetExcludeMethod() { var set = new HashSet(); foreach (var objs in excludeMethods) { var type = (Type)objs[0]; var m = type.GetMethod((string)objs[1]); set.Add(m); } return set; } [MenuItem("ILRuntime/清除CLR绑定")] static void ClearCLRBinding() { if (Directory.Exists(genPath)) { Directory.Delete(genPath, true); } AssetDatabase.Refresh(); } [MenuItem("ILRuntime/生成CLR绑定")] public static void GenerateCLRBinding() { ClearCLRBinding(); GenerateCLRBindingByAnalysis(); GenerateCLRBindingByTypes(); MergeGenerated(); AssetDatabase.Refresh(); DebugEx.Log("ILRuntime CLR绑定代码生成成功!"); } // [MenuItem("ILRuntime/自动分析Dll生成CLR绑定")] static void GenerateCLRBindingByAnalysis() { var path = genPath + "/Analysis"; //用热更dll调用引用来生成绑定代码 ILRuntime.Runtime.Enviorment.AppDomain domain = new ILRuntime.Runtime.Enviorment.AppDomain(); using (System.IO.FileStream fs = new System.IO.FileStream("Assets/ResourcesOut/logic/Logic.dll.bytes", System.IO.FileMode.Open, System.IO.FileAccess.Read)) { domain.LoadAssembly(fs); //Crossbind Adapter is needed to generate the correct binding code InitILRuntime(domain); ILRuntime.Runtime.CLRBinding.BindingCodeGenerator.GenerateBindingCode(domain, path); } } // [MenuItem("ILRuntime/根据配置列表生成CLR绑定")] static void GenerateCLRBindingByTypes() { var path = genPath + "/Types"; //用自定义的列表来生成绑定代码 ILRuntime.Runtime.CLRBinding.BindingCodeGenerator.GenerateBindingCode(types, path, GetExcludeMethod()); } //合并生成的代码 static void MergeGenerated() { var exist = new HashSet(); var added = new List(); var path1 = System.Environment.CurrentDirectory + "/" + genPath + "/Types"; var dic1 = new DirectoryInfo(path1); //获取types生成的列表 foreach (var file in dic1.GetFiles()) { exist.Add(file.Name); } var path2 = System.Environment.CurrentDirectory + "/" + genPath + "/Analysis"; var dic2 = new DirectoryInfo(path2); //dll存在和types一样的代码,就删除,其余的视为新增 foreach (var file in dic2.GetFiles()) { if (exist.Contains(file.Name)) file.Delete(); else { var className = GetClassName(file.FullName); if (!string.IsNullOrEmpty(className)) added.Add(className); } } //将dll新增代码写入到CLRBindings中注册 var lines = File.ReadAllLines(path1 + "/CLRBindings.cs").ToList(); int index = 0; for (int i = 0; i < lines.Count; i++) { if (lines[i].Contains("public static void Initialize")) { index = i; break; } } var space = " "; lines.Insert(index + 2, space + "//配置列表的CLR绑定类"); foreach (var name in added) { lines.Insert(index + 2, space + name + ".Register(app);"); } lines.Insert(index + 2, space + "//自动分析dll新增的CLR绑定类"); File.WriteAllLines(path1 + "/CLRBindings.cs", lines); } //获取文件的类名 static string GetClassName(string path) { var lines = File.ReadAllLines(path).ToList(); foreach (var line in lines) { if (line.Contains("class")) { var strs = line.Split(' '); return strs[strs.Length - 1]; } } return null; } static void InitILRuntime(ILRuntime.Runtime.Enviorment.AppDomain domain) { //这里需要注册所有热更DLL中用到的跨域继承Adapter,否则无法正确抓取引用 ILLauncherProxy.RegisterCrossBindingAdaptor(domain); } } #endif