Browse Source

+试图支持ET宏ENABLE_CODES,坑多待填

大爷 2 years ago
parent
commit
3880b7df9c
1 changed files with 63 additions and 52 deletions
  1. 63 52
      Unity/Assets/Scripts/Loader/CodeLoader.cs

+ 63 - 52
Unity/Assets/Scripts/Loader/CodeLoader.cs

@@ -12,58 +12,27 @@ namespace ET
 {
 	public class CodeLoader: Singleton<CodeLoader>
 	{
-		public void Start()
+        public void Start()
 		{
-			if (Define.EnableCodes)
-			{
-				GlobalConfig globalConfig = Resources.Load<GlobalConfig>("GlobalConfig");
-				if (globalConfig.CodeMode != CodeMode.ClientServer)
-				{
-					throw new Exception("ENABLE_CODES mode must use ClientServer code mode!");
-				}
-
-                GameObject.Find("HotfixUI").SetActive(false);
-                Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
-				Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(assemblies);
-				EventSystem.Instance.Add(types);
-
-                bool bStart = false;
-				foreach (Assembly ass in assemblies)
-				{
-					string name = ass.GetName().Name;
-					if (name == "Unity.Model.Codes")
-					{
-                        bStart = true;
-                        IStaticMethod start = new StaticMethod(ass, "ET.Entry", "Start");
-                        start.Run();
-                        break;
-                    }
-				}
-
-                if(!bStart)
-                {
-                    throw new Exception("not found Assembly: 'Unity.Model.Codes'");
-                }
-			}
-			else
-			{
-                LoadHotfix();
-            }
-		}
-
-        //编译时会检查这个方法,需保留
-        public void LoadHotfix()
-        {
-            AsyncLoadHotfix().Forget();
+            AsyncLoadResAndCode().Forget();
         }
 
-        private async UniTaskVoid AsyncLoadHotfix()
+        private async UniTaskVoid AsyncLoadResAndCode()
         {
-            GlobalConfig globalConfig = Resources.Load<GlobalConfig>("GlobalConfig");
-            Log.Info($"start mode: {globalConfig.PlayMode}");
+            YooAssets.EPlayMode playMode;
+            if (Define.EnableCodes)
+            {
+                playMode = YooAssets.EPlayMode.EditorSimulateMode;
+            }
+            else
+            {
+                GlobalConfig globalConfig = Resources.Load<GlobalConfig>("GlobalConfig");
+                playMode = globalConfig.PlayMode;
+            }
+            Log.Info($"start mode: {playMode}");
 
-            // 启动YooAsset引擎,并在初始化完毕后进行热更代码加载
-            await YooAssetProxy.StartYooAssetEngine(globalConfig.PlayMode);
+            // 启动YooAsset引擎
+            await YooAssetProxy.StartYooAssetEngine(playMode);
 
             // Shader Warm Up
             ShaderVariantCollection shaderVariantCollection =
@@ -76,11 +45,16 @@ namespace ET
             stopwatch.Stop();
             Log.Info($"Shader Warm Up完成, 耗时: {stopwatch.ElapsedMilliseconds}ms,shaderCount: {shaderVariantCollection.shaderCount} variantCount: {shaderVariantCollection.variantCount}");
 
-            //给mscorlib.dll等AOT补充元数据
-            await LoadMetadataForAOT();
-            await LoadHotfixCode();
-
-            GameObject.Find("HotfixUI").SetActive(false);
+            //加载代码入口
+            if (Define.EnableCodes)
+            {
+                LoadLocalCode();
+            }
+            else
+            {
+                await LoadMetadataForAOT();
+                await LoadHotfixCode();
+            }
         }
 
 		// 加载assembly对应的dll,会自动为它hook。一旦aot泛型函数的native函数不存在,用解释器版本代码
@@ -129,5 +103,42 @@ namespace ET
             IStaticMethod start = new MonoStaticMethod(modelAssembly, "ET.Entry", "Start");
             start.Run();
         }
+
+        //不使用dll,打开了EnableCode,使用Editor编辑模式
+        private void LoadLocalCode()
+        {
+            //GlobalConfig globalConfig = Resources.Load<GlobalConfig>("GlobalConfig");
+            //if (globalConfig.CodeMode != CodeMode.ClientServer)
+            //{
+            //	throw new Exception("ENABLE_CODES mode must use ClientServer code mode!");
+            //}
+
+            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
+            Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(assemblies);
+            EventSystem.Instance.Add(types);
+
+            bool bStart = false;
+            foreach (Assembly ass in assemblies)
+            {
+                string name = ass.GetName().Name;
+                if (name == "Unity.Model.Codes")
+                {
+                    bStart = true;
+                    IStaticMethod start = new StaticMethod(ass, "ET.Entry", "Start");
+                    start.Run();
+                    break;
+                }
+            }
+
+            if (!bStart)
+            {
+                throw new Exception("not found Assembly: 'Unity.Model.Codes'");
+            }
+        }
+
+        //编译时会检查这个方法,需保留
+        public void LoadHotfix()
+        {
+        }
     }
 }