static void CreateBindingAdapterFile(Type type) { string content = CrossBindingCodeGenerator.GenerateCrossBindingAdapterCode(type, type.Namespace); string filePath = $"{ILRDefine.StrMyAdapterFolderPath}/{type.Name}Adapter.cs"; File.WriteAllText(filePath, content, Encoding.UTF8); UnityEngine.Debug.Log($"Create ILRuntime adapter file : {filePath}"); }
public static void GenerateCrossbindAdapter() { //由于跨域继承特殊性太多,自动生成无法实现完全无副作用生成,所以这里提供的代码自动生成主要是给大家生成个初始模版,简化大家的工作 //大多数情况直接使用自动生成的模版即可,如果遇到问题可以手动去修改生成后的文件,因此这里需要大家自行处理是否覆盖的问题 using (StreamWriter sw = new StreamWriter("Assets/Scripts/Runtime/Core/Manager/ILRuntime/Adapter/MonoBehaviourAdapter.cs")) { sw.WriteLine(CrossBindingCodeGenerator.GenerateCrossBindingAdapterCode(typeof(MonoBehaviour), "Model")); } AssetDatabase.Refresh(); }
//------------------------------------------------------------------------------------------------------------ protected static void generateAdapter(Type type, string nameSpace = "HotFix") { string prePath = FrameDefine.P_SCRIPT_GAME_PATH + "ILRuntime/GeneratedCrossBinding/"; string fullFileName = prePath + type.Name + "Adapter.cs"; FileUtility.createDir(prePath); using (StreamWriter sw = new StreamWriter(fullFileName)) { sw.WriteLine(CrossBindingCodeGenerator.GenerateCrossBindingAdapterCode(type, nameSpace)); } // 生成热更工程中对应的派生类,用于重写所有的虚函数,只是因为ILR本身的原因需要对虚函数进行重写 ClassTypeGenerator generator = new ClassTypeGenerator(); generator.generateILRClass(type, FrameDefine.F_HOT_FIX_PATH + "Game/ILRInherit_AutoGenerated/"); }
private static void WriteOneAdapter(Type type) { m_StringBuilder = m_StringBuilder?.Clear() ?? new StringBuilder(); m_StringBuilder.Append($"Assets/ILRuntime/Adapter/{type.Name}"); foreach (Type genericType in type.GetGenericArguments()) { m_StringBuilder.Append($"_{genericType.Name}"); } m_StringBuilder.Append("Adapter.cs"); string filePath = m_StringBuilder.ToString(); if (File.Exists(filePath)) { return; } using (StreamWriter sw = new StreamWriter(filePath)) { sw.WriteLine("#pragma warning disable CS0108, CS0649"); sw.WriteLine(CrossBindingCodeGenerator.GenerateCrossBindingAdapterCode(type, "Encoder.ILAdapter")); sw.WriteLine("#pragma warning restore CS0108, CS0649"); } }
private static void OnGenerateAdapter(AppDomain domain, string path) { ILRuntimeAppEditor app = ILRuntimeAppEditor.GetInstance(); IHotFixConfig config = app.GetHotFixConfig(); string[] splits = path.Split('.'); splits = splits[0].Split('/'); string name = splits[splits.Length - 1]; string code = string.Empty; var className = config.AutoAdapterGenerates; bool hasAdapterConfig = false; foreach (var item in className) { if (item.Key == name) { hasAdapterConfig = true; if (path.Contains(".cs")) { using (StreamWriter sw = new StreamWriter(path)) { sw.WriteLine(CrossBindingCodeGenerator.GenerateCrossBindingAdapterCode(item.Value, config.SpaceName)); } } else { string info = string.Format("选定的文件 {0} 不是cs代码文件", path); EditorUtility.DisplayDialog("生成错误", info, "朕知道了"); throw new Exception(info); } } } if (!hasAdapterConfig) { string info = "选定的文件可能未包含在适配器的配置中,请通过修改 AppHotFixConfigBase 或其子类中的 GetAutoAdapterGenerates() 方法增加定义之后重试"; EditorUtility.DisplayDialog("生成错误", info, "朕知道了"); throw new Exception(info); } }
private void GenAdapter() { //获取主工程DLL的类 Type t = null; if (_class.Contains("UnityEngine") || _assembly.Contains("Unity")) { t = Type.GetType($"{_class},UnityEngine"); } else { t = Type.GetType(_class); } if (t == null) { if (File.Exists(new DirectoryInfo(Application.dataPath).Parent?.FullName + $"/Library/ScriptAssemblies/{_assembly}.dll")) { t = Assembly .LoadFile(new DirectoryInfo(Application.dataPath).Parent?.FullName + $"/Library/ScriptAssemblies/{_assembly}.dll").GetType(_class); if (t == null) { if (File.Exists(new DirectoryInfo(Application.dataPath).Parent?.FullName + $"/HotUpdateScripts/Dlls/{_assembly}.dll")) { t = Assembly .LoadFile(new DirectoryInfo(Application.dataPath).Parent?.FullName + $"/HotUpdateScripts/Dlls/{_assembly}.dll").GetType(_class); } } } } //判断空 if (t == null) { EditorUtility.DisplayDialog("Error", $"Invalid Class {_class}!\r\n{_class}类不存在!", "Ok"); return; } _class = t.FullName.Replace(".", "_"); if (!Directory.Exists(OUTPUT_PATH)) { Directory.CreateDirectory(OUTPUT_PATH); } //如果有先删除 if (File.Exists($"{OUTPUT_PATH}/{_class}Adapter.cs")) { File.Delete($"{OUTPUT_PATH}/{_class}Adapter.cs"); if (File.Exists($"{OUTPUT_PATH}/{_class}Adapter.cs.meta")) { File.Delete($"{OUTPUT_PATH}/{_class}Adapter.cs.meta"); } AssetDatabase.Refresh(); } if (!Directory.Exists(OUTPUT_PATH + "/MonoMethods")) { Directory.CreateDirectory(OUTPUT_PATH + "/MonoMethods"); } if (File.Exists($"{OUTPUT_PATH}/MonoMethods/{_class}Adapter.MonoMethods.cs")) { File.Delete($"{OUTPUT_PATH}/MonoMethods/{_class}Adapter.MonoMethods.cs"); if (File.Exists($"{OUTPUT_PATH}/MonoMethods/{_class}Adapter.MonoMethods.cs.meta")) { File.Delete($"{OUTPUT_PATH}/MonoMethods/{_class}Adapter.MonoMethods.cs.meta"); } AssetDatabase.Refresh(); } if (!Directory.Exists(OUTPUT_PATH + "/Editor")) { Directory.CreateDirectory(OUTPUT_PATH + "/Editor"); } //如果有先删除 if (File.Exists($"{OUTPUT_PATH}/Editor/{_class}AdapterEditor.cs")) { File.Delete($"{OUTPUT_PATH}/Editor/{_class}AdapterEditor.cs"); if (File.Exists($"{OUTPUT_PATH}/Editor/{_class}AdapterEditor.cs.meta")) { File.Delete($"{OUTPUT_PATH}/Editor/{_class}AdapterEditor.cs.meta"); } AssetDatabase.Refresh(); } //生成适配器 FileStream stream = new FileStream($"{OUTPUT_PATH}/{_class}Adapter.cs", FileMode.Append, FileAccess.Write); StreamWriter sw = new StreamWriter(stream); Stopwatch watch = new Stopwatch(); sw.WriteLine( CrossBindingCodeGenerator.GenerateCrossBindingAdapterCode(t, _namespace)); watch.Stop(); Log.Print($"Generated {OUTPUT_PATH}/{_class}Adapter.cs in: " + watch.ElapsedMilliseconds + " ms."); sw.Dispose(); stream = new FileStream($"{OUTPUT_PATH}/MonoMethods/{_class}Adapter.MonoMethods.cs", FileMode.Append, FileAccess.Write); sw = new StreamWriter(stream); watch = new Stopwatch(); sw.WriteLine( GenerateCrossBindingAdapterMonoMethods(t, _namespace)); watch.Stop(); Log.Print($"Generated {OUTPUT_PATH}/MonoMethods/{_class}Adapter.MonoMethods.cs in: " + watch.ElapsedMilliseconds + " ms."); sw.Dispose(); //生成编辑器 string editorText = GenerateCrossBindingAdapterEditorCode(t, _namespace); if (editorText != null) { stream = new FileStream($"{OUTPUT_PATH}/Editor/{_class}AdapterEditor.cs", FileMode.Append, FileAccess.Write); sw = new StreamWriter(stream); watch = new Stopwatch(); sw.WriteLine(editorText); watch.Stop(); Log.Print($"Generated {OUTPUT_PATH}/Editor/{_class}AdapterEditor.cs in: " + watch.ElapsedMilliseconds + " ms."); sw.Dispose(); } window.Close(); AssetDatabase.Refresh(); }
void OnGUI() { GUILayout.BeginHorizontal(); GUILayout.Label("生成适配器"); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); _namespace = EditorGUILayout.TextField("命名空间", _namespace); _class = EditorGUILayout.TextField("类名", _class); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); if (GUILayout.Button(new GUIContent("生成"))) { string path = "Library/ScriptAssemblies/Unity.Model.dll"; Type type = Assembly.LoadFile(path).GetType($"{_namespace}.{_class}"); if (type == null) { ShowNotification(new GUIContent("没有此脚本")); return; } if (File.Exists($"Assets/Scripts/Runtime/Core/Manager/ILRuntime/Adapter/{_class}Adapter.cs")) { File.Delete($"Assets/Scripts/Runtime/Core/Manager/ILRuntime/Adapter/{_class}Adapter.cs"); } FileUtil.SaveAsset($"Assets/Scripts/Runtime/Core/Manager/ILRuntime/Adapter/{_class}Adapter.cs", CrossBindingCodeGenerator.GenerateCrossBindingAdapterCode(type, _namespace)); AssetDatabase.Refresh(); } GUILayout.EndHorizontal(); }
public void BuildCrossBinding() { if (string.IsNullOrEmpty(assets)) { editorWindow.ShowNotification(new GUIContent("请输入程序集")); return; } if (string.IsNullOrEmpty(className)) { editorWindow.ShowNotification(new GUIContent("请输入脚本名")); return; } string path = $"Library/ScriptAssemblies/{assets}.dll"; if (!File.Exists(path)) { editorWindow.ShowNotification(new GUIContent("程序集路径错误")); return; } Type type; if (string.IsNullOrEmpty(namespaceName)) { type = Assembly.LoadFile(path).GetType(className); } else { type = Assembly.LoadFile(path).GetType($"{namespaceName}.{className}"); } if (type == null) { editorWindow.ShowNotification(new GUIContent("没有此脚本")); return; } if (File.Exists($"Assets/Scripts/Runtime/Core/Manager/ILRuntime/Adapter/{className}Adapter.cs")) { File.Delete($"Assets/Scripts/Runtime/Core/Manager/ILRuntime/Adapter/{className}Adapter.cs"); } FileUtil.SaveAsset($"Assets/Scripts/Runtime/Core/Manager/ILRuntime/Adapter/{className}Adapter.cs", CrossBindingCodeGenerator.GenerateCrossBindingAdapterCode(type, "LccModel")); AssetDatabase.Refresh(); }