Просмотр исходного кода

加入ILRuntime Binding Editor

tanghai 4 лет назад
Родитель
Сommit
4bca55dd50

+ 5 - 3
Unity/Assets/Editor/BuildEditor/BuildAssemblieEditor.cs

@@ -11,7 +11,7 @@ namespace ET
 {
     public static class BuildAssemblieEditor
     {
-        private const string ScriptAssembliesDir = "Temp/MyAssembly/";
+        public const string ScriptAssembliesDir = "Temp/MyAssembly/";
         private const string CodeDir = "Assets/Bundles/Code/";
 
         [MenuItem("Tools/BuildDll")]
@@ -124,19 +124,21 @@ namespace ET
                 await Task.Delay(100);
             }
             
-            Debug.Log("copy Code.dll to Bundles/Code");
+            
             Directory.CreateDirectory(CodeDir);
             File.Copy(Path.Combine(ScriptAssembliesDir, "Code.dll"), Path.Combine(CodeDir, "Code.dll.bytes"), true);
             File.Copy(Path.Combine(ScriptAssembliesDir, "Code.pdb"), Path.Combine(CodeDir, "Code.pdb.bytes"), true);
             AssetDatabase.Refresh();
+            Debug.Log("copy Code.dll to Bundles/Code success!");
             
             // 设置ab包
             AssetImporter assetImporter1 = AssetImporter.GetAtPath("Assets/Bundles/Code/Code.dll.bytes");
             assetImporter1.assetBundleName = "Code.unity3d";
             AssetImporter assetImporter2 = AssetImporter.GetAtPath("Assets/Bundles/Code/Code.pdb.bytes");
             assetImporter2.assetBundleName = "Code.unity3d";
-            
             AssetDatabase.Refresh();
+            Debug.Log("set assetbundle success!");
+            
             Debug.Log("build success!");
         }
     }

+ 8 - 0
Unity/Assets/Editor/ILRuntimeEditor.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: e12bb51d60b44ef46b74ac2a299ca195
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 44 - 0
Unity/Assets/Editor/ILRuntimeEditor/ILRuntimeCLRBinding.cs

@@ -0,0 +1,44 @@
+using UnityEditor;
+using UnityEngine;
+
+namespace ET
+{
+    public static class ILRuntimeCLRBinding
+    {
+        [MenuItem("Tools/ILRuntime/通过自动分析热更DLL生成CLR绑定")]
+        private static void GenerateCLRBindingByAnalysis()
+        {
+            //用新的分析热更dll调用引用来生成绑定代码
+            ILRuntime.Runtime.Enviorment.AppDomain domain = new ILRuntime.Runtime.Enviorment.AppDomain();
+            using (System.IO.FileStream fs = new System.IO.FileStream(BuildAssemblieEditor.ScriptAssembliesDir + "Code.dll", System.IO.FileMode.Open,
+                System.IO.FileAccess.Read))
+            {
+                domain.LoadAssembly(fs);
+                
+                ILHelper.RegisterAdaptor(domain);
+                
+                ILRuntime.Runtime.CLRBinding.BindingCodeGenerator.GenerateBindingCode(domain, "Assets/Mono/ILRuntime/Generate");
+            }
+
+            AssetDatabase.Refresh();
+
+            Debug.Log("生成CLR绑定文件完成");
+        }
+
+        [MenuItem("Tools/ILRuntime/生成跨域继承适配器")]
+        private static void GenerateCrossbindAdapter()
+        {
+            //由于跨域继承特殊性太多,自动生成无法实现完全无副作用生成,所以这里提供的代码自动生成主要是给大家生成个初始模版,简化大家的工作
+            //大多数情况直接使用自动生成的模版即可,如果遇到问题可以手动去修改生成后的文件,因此这里需要大家自行处理是否覆盖的问题
+            using (System.IO.StreamWriter sw = new System.IO.StreamWriter("Assets/Mono/ILRuntime/ISupportInitializeAdapter.cs"))
+            {
+                sw.WriteLine(ILRuntime.Runtime.Enviorment.CrossBindingCodeGenerator.GenerateCrossBindingAdapterCode(
+                    typeof (System.ComponentModel.ISupportInitialize), "ET"));
+            }
+
+            AssetDatabase.Refresh();
+
+            Debug.Log("生成适配器完成");
+        }
+    }
+}

+ 12 - 0
Unity/Assets/Editor/ILRuntimeEditor/ILRuntimeCLRBinding.cs.meta

@@ -0,0 +1,12 @@
+fileFormatVersion: 2
+guid: 970393f1bc06b0a4a8572b5eae410a9a
+timeCreated: 1481621267
+licenseType: Free
+MonoImporter:
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 2 - 2
Unity/Assets/Editor/Unity.Editor.asmdef

@@ -1,13 +1,13 @@
 {
     "name": "Unity.Editor",
-    "rootNamespace": "ET",
     "references": [
         "Unity.Model",
         "Unity.ThirdParty",
         "Unity.ModelView",
         "Unity.Hotfix",
         "Unity.HotfixView",
-        "Unity.Mono"
+        "Unity.Mono",
+        "ILRuntime"
     ],
     "includePlatforms": [
         "Editor"

+ 0 - 0
Unity/Assets/Mono/Helper/AssetsBundleHelper.cs → Unity/Assets/Mono/AssetsBundleHelper.cs


+ 1 - 1
Unity/Assets/Mono/Helper/AssetsBundleHelper.cs.meta → Unity/Assets/Mono/AssetsBundleHelper.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: 59177274cf204f04cb378cf66aa62218
+guid: b4497d7c724c1164a9ef9b95ebbe0fc3
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 0 - 0
Unity/Assets/Mono/Helper/CodeLoader.cs → Unity/Assets/Mono/CodeLoader.cs


+ 1 - 1
Unity/Assets/Mono/Helper/CodeLoader.cs.meta → Unity/Assets/Mono/CodeLoader.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: f552377e6dfa5ac4da1f2430f31664eb
+guid: 2d06e8cf45c18764db16886c76475f5e
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 0 - 10
Unity/Assets/Mono/Helper/DllHelper.cs

@@ -1,10 +0,0 @@
-namespace ET
-{
-    public static class DllHelper
-    {
-        public static void GetHotfixAssembly()
-        {
-            
-        }
-    }
-}

+ 0 - 56
Unity/Assets/Mono/Helper/ILHelper.cs

@@ -1,56 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Reflection;
-using ILRuntime.CLR.Method;
-using ILRuntime.CLR.TypeSystem;
-using ILRuntime.Runtime.Enviorment;
-using ILRuntime.Runtime.Intepreter;
-using UnityEngine;
-
-namespace ET
-{
-    public static class ILHelper
-    {
-        public static void InitILRuntime(ILRuntime.Runtime.Enviorment.AppDomain appdomain)
-        {
-            /*
-            // 注册重定向函数
-
-            // 注册委托
-            appdomain.DelegateManager.RegisterMethodDelegate<List<object>>();
-            appdomain.DelegateManager.RegisterMethodDelegate<AChannel, System.Net.Sockets.SocketError>();
-            appdomain.DelegateManager.RegisterMethodDelegate<byte[], int, int>();
-            appdomain.DelegateManager.RegisterMethodDelegate<IResponse>();
-            appdomain.DelegateManager.RegisterMethodDelegate<Session, object>();
-            appdomain.DelegateManager.RegisterMethodDelegate<Session, ushort, MemoryStream>();
-            appdomain.DelegateManager.RegisterMethodDelegate<Session>();
-            appdomain.DelegateManager.RegisterMethodDelegate<ILTypeInstance>();
-            appdomain.DelegateManager.RegisterFunctionDelegate<Google.Protobuf.Adapt_IMessage.Adaptor>();
-            appdomain.DelegateManager.RegisterMethodDelegate<Google.Protobuf.Adapt_IMessage.Adaptor>();
-
-            CLRBindings.Initialize(appdomain);
-
-            // 注册适配器
-            Assembly assembly = typeof(Init).Assembly;
-            foreach (Type type in assembly.GetTypes())
-            {
-                object[] attrs = type.GetCustomAttributes(typeof(ILAdapterAttribute), false);
-                if (attrs.Length == 0)
-                {
-                    continue;
-                }
-                object obj = Activator.CreateInstance(type);
-                CrossBindingAdaptor adaptor = obj as CrossBindingAdaptor;
-                if (adaptor == null)
-                {
-                    continue;
-                }
-                appdomain.RegisterCrossBindingAdaptor(adaptor);
-            }
-
-            LitJson.JsonMapper.RegisterILRuntimeCLRRedirection(appdomain);
-            */
-        }
-    }
-}

+ 8 - 0
Unity/Assets/Mono/ILRuntime.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: a4e25534a4b8b3b43be6776da6904c5e
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 8 - 0
Unity/Assets/Mono/ILRuntime/Generate.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: 12fa5c98d89043b4789788bd9de58c3b
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 10 - 0
Unity/Assets/Mono/ILRuntime/Generate/CLRBindings.cs

@@ -0,0 +1,10 @@
+namespace ILRuntime.Runtime.Generated
+{
+    public class CLRBindings
+    {
+        public static void Initialize(ILRuntime.Runtime.Enviorment.AppDomain app)
+        {
+            
+        }
+    }
+}

+ 1 - 1
Unity/Assets/Mono/Helper/DllHelper.cs.meta → Unity/Assets/Mono/ILRuntime/Generate/CLRBindings.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: 42cae7cf9d71fa74a99dbd6e30692202
+guid: 599f6bae92a430245b87897a4294f80f
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 78 - 0
Unity/Assets/Mono/ILRuntime/IAsyncStateMachineAdaptor.cs

@@ -0,0 +1,78 @@
+using System;
+using System.Runtime.CompilerServices;
+using ILRuntime.CLR.Method;
+using ILRuntime.Runtime.Enviorment;
+using ILRuntime.Runtime.Intepreter;
+using AppDomain = ILRuntime.Runtime.Enviorment.AppDomain;
+
+namespace ET
+{
+    /// <summary>
+    /// 用于async await适配
+    /// </summary>
+    public class IAsyncStateMachineClassInheritanceAdaptor: CrossBindingAdaptor
+    {
+        public override Type BaseCLRType => typeof (IAsyncStateMachine);
+
+        public override Type AdaptorType => typeof (IAsyncStateMachineAdaptor);
+
+        public override object CreateCLRInstance(AppDomain appdomain, ILTypeInstance instance)
+        {
+            return new IAsyncStateMachineAdaptor(appdomain, instance);
+        }
+
+        public class IAsyncStateMachineAdaptor: IAsyncStateMachine, CrossBindingAdaptorType
+        {
+            private ILTypeInstance instance;
+            private AppDomain appDomain;
+
+            private IMethod mMoveNext;
+            private IMethod mSetStateMachine;
+            private readonly object[] param1 = new object[1];
+
+            public IAsyncStateMachineAdaptor()
+            {
+            }
+
+            public IAsyncStateMachineAdaptor(AppDomain appDomain, ILTypeInstance instance)
+            {
+                this.appDomain = appDomain;
+                this.instance = instance;
+            }
+
+            public ILTypeInstance ILInstance => instance;
+
+            public void MoveNext()
+            {
+                if (this.mMoveNext == null)
+                {
+                    mMoveNext = instance.Type.GetMethod("MoveNext", 0);
+                }
+
+                this.appDomain.Invoke(mMoveNext, instance, null);
+            }
+
+            public void SetStateMachine(IAsyncStateMachine stateMachine)
+            {
+                if (this.mSetStateMachine == null)
+                {
+                    mSetStateMachine = instance.Type.GetMethod("SetStateMachine");
+                }
+
+                this.appDomain.Invoke(mSetStateMachine, instance, stateMachine);
+            }
+
+            public override string ToString()
+            {
+                IMethod m = this.appDomain.ObjectType.GetMethod("ToString", 0);
+                m = instance.Type.GetVirtualMethod(m);
+                if (m == null || m is ILMethod)
+                {
+                    return instance.ToString();
+                }
+
+                return instance.Type.FullName;
+            }
+        }
+    }
+}

+ 11 - 0
Unity/Assets/Mono/ILRuntime/IAsyncStateMachineAdaptor.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: f393d1dfe75a70c40a3a1caa0b7660b5
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 63 - 0
Unity/Assets/Mono/ILRuntime/ILHelper.cs

@@ -0,0 +1,63 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Net;
+using System.Reflection;
+using ILRuntime.CLR.Method;
+using ILRuntime.CLR.TypeSystem;
+using ILRuntime.Runtime.Enviorment;
+using ILRuntime.Runtime.Generated;
+using ILRuntime.Runtime.Intepreter;
+using UnityEngine;
+
+namespace ET
+{
+    public static class ILHelper
+    {
+        public static void InitILRuntime(ILRuntime.Runtime.Enviorment.AppDomain appdomain)
+        {
+            
+            // 注册重定向函数
+
+            // 注册委托
+            appdomain.DelegateManager.RegisterMethodDelegate<List<object>>();
+            appdomain.DelegateManager.RegisterMethodDelegate<object>();
+            appdomain.DelegateManager.RegisterMethodDelegate<bool>();
+            appdomain.DelegateManager.RegisterMethodDelegate<string>();
+            appdomain.DelegateManager.RegisterMethodDelegate<float>();
+            appdomain.DelegateManager.RegisterMethodDelegate<long, int>();
+            appdomain.DelegateManager.RegisterMethodDelegate<long, MemoryStream>();
+            appdomain.DelegateManager.RegisterMethodDelegate<long, IPEndPoint>();
+            appdomain.DelegateManager.RegisterMethodDelegate<ILTypeInstance>();
+            
+            
+            appdomain.DelegateManager.RegisterFunctionDelegate<UnityEngine.Events.UnityAction>();
+            appdomain.DelegateManager.RegisterFunctionDelegate<System.Object, ET.ETTask>();
+            appdomain.DelegateManager.RegisterFunctionDelegate<ILTypeInstance, bool>();
+            appdomain.DelegateManager.RegisterFunctionDelegate<System.Collections.Generic.KeyValuePair<System.String, System.Int32>, System.String>();
+            appdomain.DelegateManager.RegisterFunctionDelegate<System.Collections.Generic.KeyValuePair<System.Int32, System.Int32>, System.Boolean>();
+            appdomain.DelegateManager.RegisterFunctionDelegate<System.Collections.Generic.KeyValuePair<System.String, System.Int32>, System.Int32>();
+            appdomain.DelegateManager.RegisterFunctionDelegate<List<int>, int>();
+            appdomain.DelegateManager.RegisterFunctionDelegate<List<int>, bool>();
+            appdomain.DelegateManager.RegisterFunctionDelegate<int, bool>();//Linq
+            appdomain.DelegateManager.RegisterFunctionDelegate<int, int, int>();//Linq
+            appdomain.DelegateManager.RegisterFunctionDelegate<KeyValuePair<int, List<int>>, bool>();
+            appdomain.DelegateManager.RegisterFunctionDelegate<KeyValuePair<int, int>, KeyValuePair<int, int>, int>();
+
+            // 注册适配器
+            RegisterAdaptor(appdomain);
+
+            //LitJson.JsonMapper.RegisterILRuntimeCLRRedirection(appdomain);
+            
+            CLRBindings.Initialize(appdomain);
+        }
+        
+        public static void RegisterAdaptor(ILRuntime.Runtime.Enviorment.AppDomain appdomain)
+        {
+            //注册自己写的适配器
+            appdomain.RegisterCrossBindingAdaptor(new IAsyncStateMachineClassInheritanceAdaptor());
+            appdomain.RegisterCrossBindingAdaptor(new ISupportInitializeAdapter());
+            
+        }
+    }
+}

+ 1 - 1
Unity/Assets/Mono/Helper/ILHelper.cs.meta → Unity/Assets/Mono/ILRuntime/ILHelper.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: 52103238670c697448bb34a589d392ab
+guid: b6243574cb0dfff4c94600d04b1ab682
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 0 - 0
Unity/Assets/Mono/Helper/ILStaticMethod.cs → Unity/Assets/Mono/ILRuntime/ILStaticMethod.cs


+ 1 - 1
Unity/Assets/Mono/Helper/ILStaticMethod.cs.meta → Unity/Assets/Mono/ILRuntime/ILStaticMethod.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: 1bb3fd9d20ec4ae4e8409d656c1f2117
+guid: b71d91990e4a80a4d8944e042593b5bf
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 63 - 0
Unity/Assets/Mono/ILRuntime/ISupportInitializeAdapter.cs

@@ -0,0 +1,63 @@
+using System;
+using System.ComponentModel;
+using ILRuntime.CLR.Method;
+using ILRuntime.Runtime.Enviorment;
+using ILRuntime.Runtime.Intepreter;
+using AppDomain = ILRuntime.Runtime.Enviorment.AppDomain;
+
+namespace ET
+{
+    public class ISupportInitializeAdapter: CrossBindingAdaptor
+    {
+        private static CrossBindingMethodInfo mBeginInit_0 = new CrossBindingMethodInfo("BeginInit");
+        private static CrossBindingMethodInfo mEndInit_1 = new CrossBindingMethodInfo("EndInit");
+        public override Type BaseCLRType => typeof (ISupportInitialize);
+
+        public override Type AdaptorType => typeof (Adapter);
+
+        public override object CreateCLRInstance(AppDomain appdomain, ILTypeInstance instance)
+        {
+            return new Adapter(appdomain, instance);
+        }
+
+        public class Adapter: ISupportInitialize, CrossBindingAdaptorType
+        {
+            private ILTypeInstance instance;
+            private AppDomain appdomain;
+
+            public Adapter()
+            {
+            }
+
+            public Adapter(AppDomain appdomain, ILTypeInstance instance)
+            {
+                this.appdomain = appdomain;
+                this.instance = instance;
+            }
+
+            public ILTypeInstance ILInstance => instance;
+
+            public void BeginInit()
+            {
+                mBeginInit_0.Invoke(this.instance);
+            }
+
+            public void EndInit()
+            {
+                mEndInit_1.Invoke(this.instance);
+            }
+
+            public override string ToString()
+            {
+                IMethod m = appdomain.ObjectType.GetMethod("ToString", 0);
+                m = instance.Type.GetVirtualMethod(m);
+                if (m == null || m is ILMethod)
+                {
+                    return instance.ToString();
+                }
+
+                return this.instance.Type.FullName;
+            }
+        }
+    }
+}

+ 11 - 0
Unity/Assets/Mono/ILRuntime/ISupportInitializeAdapter.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 174347fd8f382304f827d9e6d996d6e0
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: