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

调整代码编译以及初始化方式,使用回调方式初始化代码,可以选择客户端代码编译方式,有三种:
Client 仅编译客户端代码,仅做客户端使用
Server 仅编译服务端代码,可以把Unity打包成服务端,很多项目想用Unity当server,可以使用这种模式
ClientServer 编译客户端跟服务端代码,该模式下,客户端里面带有一个服务端,可以自己连接自己身上的服务端,就不用额外启动一个服务端了

tanghai 3 лет назад
Родитель
Сommit
1be8d015fc

+ 6 - 28
DotNet/Hotfix/AppStart_Init.cs → Codes/Hotfix/Server/Demo/InitServer.cs

@@ -1,23 +1,12 @@
 using System.Net;
-using ET.Server;
-using ET.Client;
 
-namespace ET
+namespace ET.Server
 {
-    [Event(SceneType.Process)]
-    public class AppStart_Init: AEvent<Scene, ET.EventType.AppStart>
+    [Callback(CallbackType.InitServer)]
+    public class InitServer: IFunc<ETTask>
     {
-        protected override async ETTask Run(Scene scene, ET.EventType.AppStart args)
+        public async ETTask Handle()
         {
-            Game.Scene.AddComponent<ConfigComponent>();
-            await ConfigComponent.Instance.LoadAsync();
-
-            StartProcessConfig processConfig = StartProcessConfigCategory.Instance.Get(Game.Options.Process);
-
-            Game.Scene.AddComponent<TimerComponent>();
-            Game.Scene.AddComponent<OpcodeTypeComponent>();
-            Game.Scene.AddComponent<MessageDispatcherComponent>();
-            Game.Scene.AddComponent<CoroutineLockComponent>();
             // 发送普通actor消息
             Game.Scene.AddComponent<ActorMessageSenderComponent>();
             // 发送location actor消息
@@ -25,24 +14,13 @@ namespace ET
             // 访问location server的组件
             Game.Scene.AddComponent<LocationProxyComponent>();
             Game.Scene.AddComponent<ActorMessageDispatcherComponent>();
-            // 数值订阅组件
-            Game.Scene.AddComponent<NumericWatcherComponent>();
             
-            Game.Scene.AddComponent<AIDispatcherComponent>();
-
-            #region 机器人使用
-
-            Game.Scene.AddComponent<ClientSceneManagerComponent>();
             Game.Scene.AddComponent<RobotCaseDispatcherComponent>();
             Game.Scene.AddComponent<RobotCaseComponent>();
 
-            #endregion
-
-            
-            Game.Scene.AddComponent<NetThreadComponent>();
-            
             Game.Scene.AddComponent<NavmeshComponent>();
 
+            StartProcessConfig processConfig = StartProcessConfigCategory.Instance.Get(Game.Options.Process);
             switch (Game.Options.AppType)
             {
                 case AppType.Server:
@@ -52,7 +30,7 @@ namespace ET
                     var processScenes = StartSceneConfigCategory.Instance.GetByProcess(Game.Options.Process);
                     foreach (StartSceneConfig startConfig in processScenes)
                     {
-                        await Server.SceneFactory.Create(Game.Scene, startConfig.Id, startConfig.InstanceId, startConfig.Zone, startConfig.Name,
+                        await SceneFactory.Create(Game.Scene, startConfig.Id, startConfig.InstanceId, startConfig.Zone, startConfig.Name,
                             startConfig.Type, startConfig);
                     }
 

+ 20 - 0
Codes/Hotfix/Share/Demo/InitShare.cs

@@ -0,0 +1,20 @@
+namespace ET
+{
+    [Callback(CallbackType.InitShare)]
+    public class InitShare: IFunc<ETTask>
+    {
+        public async ETTask Handle()
+        {
+            Game.Scene.AddComponent<TimerComponent>();
+            Game.Scene.AddComponent<OpcodeTypeComponent>();
+            Game.Scene.AddComponent<MessageDispatcherComponent>();
+            Game.Scene.AddComponent<CoroutineLockComponent>();
+            Game.Scene.AddComponent<NetThreadComponent>();
+            Game.Scene.AddComponent<NumericWatcherComponent>();
+            Game.Scene.AddComponent<AIDispatcherComponent>();
+            Game.Scene.AddComponent<ClientSceneManagerComponent>();
+            
+            await Game.Scene.AddComponent<ConfigComponent>().LoadAsync();
+        }
+    }
+}

+ 2 - 2
Codes/Hotfix/Share/Module/Config/ConfigComponentSystem.cs

@@ -40,7 +40,7 @@ namespace ET
 			HashSet<Type> types = Game.EventSystem.GetTypes(typeof (ConfigAttribute));
 			
 			Dictionary<string, byte[]> configBytes = new Dictionary<string, byte[]>();
-			Game.EventSystem.Callback(CallbackType.GetAllConfigBytes, configBytes);
+			Game.EventSystem.Callback(CallbackType.GetAllConfigBytes, self, configBytes);
 
 			foreach (Type type in types)
 			{
@@ -54,7 +54,7 @@ namespace ET
 			HashSet<Type> types = Game.EventSystem.GetTypes(typeof (ConfigAttribute));
 			
 			Dictionary<string, byte[]> configBytes = new Dictionary<string, byte[]>();
-			Game.EventSystem.Callback(CallbackType.GetAllConfigBytes, configBytes);
+			Game.EventSystem.Callback(CallbackType.GetAllConfigBytes, self, configBytes);
 
 			using (ListComponent<Task> listTasks = ListComponent<Task>.Create())
 			{

+ 1 - 1
Codes/Hotfix/Client/Demo/Scene/ClientSceneFlagComponentSystem.cs → Codes/Hotfix/Share/Module/Scene/ClientSceneFlagComponentSystem.cs

@@ -1,4 +1,4 @@
-namespace ET.Client
+namespace ET
 {
     [ObjectSystem]
     public class ClientSceneFlagComponentDestroySystem: DestroySystem<ClientSceneFlagComponent>

+ 1 - 1
Codes/Hotfix/Client/Demo/Scene/ClientSceneManagerComponentSystem.cs → Codes/Hotfix/Share/Module/Scene/ClientSceneManagerComponentSystem.cs

@@ -1,6 +1,6 @@
 using System;
 
-namespace ET.Client
+namespace ET
 {
     [FriendOf(typeof(ClientSceneManagerComponent))]
     public static class ClientSceneManagerComponentSystem

+ 1 - 1
Codes/Hotfix/Client/Demo/Scene/CurrentScenesComponentSystem.cs → Codes/Hotfix/Share/Module/Scene/CurrentScenesComponentSystem.cs

@@ -1,6 +1,6 @@
 using System;
 
-namespace ET.Client
+namespace ET
 {
     public static class CurrentScenesComponentSystem
     {

+ 0 - 34
Codes/HotfixView/Client/AppStart_Init.cs

@@ -1,34 +0,0 @@
-namespace ET.Client
-{
-    [Event(SceneType.Process)]
-    public class AppStart_Init: AEvent<Scene, EventType.AppStart>
-    {
-        protected override async ETTask Run(Scene scene, EventType.AppStart args)
-        {
-            Game.Scene.AddComponent<TimerComponent>();
-            Game.Scene.AddComponent<CoroutineLockComponent>();
-
-            // 加载配置
-            Game.Scene.AddComponent<ResourcesComponent>();
-            await ResourcesComponent.Instance.LoadBundleAsync("config.unity3d");
-            Game.Scene.AddComponent<ConfigComponent>();
-            ConfigComponent.Instance.Load();
-            ResourcesComponent.Instance.UnloadBundle("config.unity3d");
-            
-            Game.Scene.AddComponent<OpcodeTypeComponent>();
-            Game.Scene.AddComponent<MessageDispatcherComponent>();
-            
-            Game.Scene.AddComponent<NetThreadComponent>();
-            Game.Scene.AddComponent<ClientSceneManagerComponent>();
-            
-            Game.Scene.AddComponent<GlobalComponent>();
-            Game.Scene.AddComponent<NumericWatcherComponent>();
-            Game.Scene.AddComponent<AIDispatcherComponent>();
-            await ResourcesComponent.Instance.LoadBundleAsync("unit.unity3d");
-            
-            Scene clientScene = Client.SceneFactory.CreateClientScene(1, "Game", Game.Scene);
-            
-            await Game.EventSystem.PublishAsync(clientScene, new EventType.AppStartInitFinish());
-        }
-    }
-}

+ 23 - 0
Codes/HotfixView/Client/Demo/InitClient.cs

@@ -0,0 +1,23 @@
+using System;
+using System.IO;
+
+namespace ET.Client
+{
+    [Callback(CallbackType.InitClient)]
+    public class InitClient: IFunc<ETTask>
+    {
+        public async ETTask Handle()
+        {
+            // 加载配置
+            Game.Scene.AddComponent<ResourcesComponent>();
+            
+            Game.Scene.AddComponent<GlobalComponent>();
+
+            await ResourcesComponent.Instance.LoadBundleAsync("unit.unity3d");
+            
+            Scene clientScene = SceneFactory.CreateClientScene(1, "Game", Game.Scene);
+            
+            await Game.EventSystem.PublishAsync(clientScene, new EventType.AppStartInitFinish());
+        }
+    }
+}

+ 8 - 2
Codes/Model/Server/Entry.cs

@@ -9,6 +9,11 @@ namespace ET.Server
     public static class Entry
     {
         public static void Start()
+        {
+            StartAsync().Coroutine();
+        }
+        
+        private static async ETTask StartAsync()
         {
             AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
             {
@@ -38,8 +43,9 @@ namespace ET.Server
             LogManager.Configuration.Variables["appIdFormat"] = $"{Game.Options.Process:000000}";
 				
             Log.Console($"app start: {Game.Scene.Id} options: {JsonHelper.ToJson(Game.Options)} ");
-
-            Game.EventSystem.Publish(Game.Scene, new ET.EventType.AppStart());
+            
+            await Game.EventSystem.Callback<ETTask>(CallbackType.InitShare);
+            await Game.EventSystem.Callback<ETTask>(CallbackType.InitServer);
         }
     }
 }

+ 6 - 0
Codes/Model/Share/CallbackType.cs

@@ -7,11 +7,17 @@
         public const int SessionStreamDispatcherServerOuter = 2;
         public const int SessionStreamDispatcherServerInner = 3;
 
+        public const int InitShare = 5;
+        public const int InitClient = 6;
+        public const int InitServer = 7;
+
         public const int GetAllConfigBytes = 11;
         public const int GetOneConfigBytes = 12;
 
         public const int RecastFileLoader = 13;
         
+        
+        
         // 框架层100-200,逻辑层的timer type从200起
         public const int WaitTimer = 100;
         public const int SessionIdleChecker = 101;

+ 1 - 1
Codes/Model/Client/Demo/Scene/ClientSceneFlagComponent.cs → Codes/Model/Share/Module/Scene/ClientSceneFlagComponent.cs

@@ -1,6 +1,6 @@
 using System.Collections.Generic;
 
-namespace ET.Client
+namespace ET
 {
     [ComponentOf(typeof(Scene))]
     public class ClientSceneFlagComponent: Entity, IAwake, IDestroy

+ 0 - 0
Codes/Model/Client/Demo/Scene/ClientSceneFlagComponent.cs.meta → Codes/Model/Share/Module/Scene/ClientSceneFlagComponent.cs.meta


+ 1 - 1
Codes/Model/Client/Demo/Scene/ClientSceneManagerComponent.cs → Codes/Model/Share/Module/Scene/ClientSceneManagerComponent.cs

@@ -1,6 +1,6 @@
 using System.Collections.Generic;
 
-namespace ET.Client
+namespace ET
 {
     [ComponentOf(typeof(Scene))]
     public class ClientSceneManagerComponent: Entity, IAwake, IDestroy

+ 0 - 0
Codes/Model/Client/Demo/Scene/ClientSceneManagerComponent.cs.meta → Codes/Model/Share/Module/Scene/ClientSceneManagerComponent.cs.meta


+ 1 - 1
Codes/Model/Client/Demo/Scene/CurrentScenesComponent.cs → Codes/Model/Share/Module/Scene/CurrentScenesComponent.cs

@@ -1,4 +1,4 @@
-namespace ET.Client
+namespace ET
 {
     // 可以用来管理多个客户端场景,比如大世界会加载多块场景
     [ComponentOf(typeof(Scene))]

+ 18 - 11
Codes/ModelView/Client/Demo/Config/ConfigLoader.cs

@@ -1,31 +1,38 @@
+using System;
 using System.Collections.Generic;
 using UnityEngine;
 
 namespace ET.Client
 {
     [Callback(CallbackType.GetAllConfigBytes)]
-    public class GetAllConfigBytes: IAction<Dictionary<string, byte[]>>
+    public class GetAllConfigBytes: IAction<ConfigComponent, Dictionary<string, byte[]>>
     {
-        public void Handle(Dictionary<string, byte[]> output)
+        public void Handle(ConfigComponent configComponent, Dictionary<string, byte[]> output)
         {
-            Dictionary<string, UnityEngine.Object> keys = ResourcesComponent.Instance.GetBundleAll("config.unity3d");
-
-            foreach (var kv in keys)
+            using (Game.Scene.AddComponent<ResourcesComponent>())
             {
-                TextAsset v = kv.Value as TextAsset;
-                string key = kv.Key;
-                output[key] = v.bytes;
+                const string configBundleName = "config.unity3d";
+                ResourcesComponent.Instance.LoadBundle(configBundleName);
+                Dictionary<string, UnityEngine.Object> keys = ResourcesComponent.Instance.GetBundleAll(configBundleName);
+
+                foreach (var kv in keys)
+                {
+                    TextAsset v = kv.Value as TextAsset;
+                    string key = kv.Key;
+                    output[key] = v.bytes;
+                }
             }
         }
     }
-    
+
     [Callback(CallbackType.GetOneConfigBytes)]
     public class GetOneConfigBytes: IFunc<string, byte[]>
     {
         public byte[] Handle(string configName)
         {
-            TextAsset v = ResourcesComponent.Instance.GetAsset("config.unity3d", configName) as TextAsset;
-            return v.bytes;
+            //TextAsset v = ResourcesComponent.Instance.GetAsset("config.unity3d", configName) as TextAsset;
+            //return v.bytes;
+            throw new NotImplementedException("client cant use LoadOneConfig");
         }
     }
 }

+ 24 - 2
Codes/ModelView/Client/Entry.cs

@@ -6,10 +6,15 @@ namespace ET.Client
 	public static class Entry
 	{
 		public static void Start()
+		{
+			StartAsync().Coroutine();
+		}
+		
+		private static async ETTask StartAsync()
 		{
 			try
 			{
-				System.AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
+				AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
 				{
 					Log.Error(e.ExceptionObject.ToString());
 				};
@@ -28,7 +33,24 @@ namespace ET.Client
 
 				Options.Instance = new Options();
 
-				Game.EventSystem.Publish(Game.Scene, new EventType.AppStart());
+				await Game.EventSystem.Callback<ETTask>(CallbackType.InitShare);
+				
+				switch (CodeLoader.Instance.GlobalConfig.CodeMode)
+				{
+					case CodeMode.Client:
+						await Game.EventSystem.Callback<ETTask>(CallbackType.InitClient);
+						break;
+					case CodeMode.Server:
+						await Game.EventSystem.Callback<ETTask>(CallbackType.InitServer);
+						break;
+					case CodeMode.ClientServer:
+						await Game.EventSystem.Callback<ETTask>(CallbackType.InitServer);
+						await Game.EventSystem.Callback<ETTask>(CallbackType.InitClient);
+						break;
+					default:
+						throw new ArgumentOutOfRangeException();
+				}
+				Log.Info("Init Finish!");
 			}
 			catch (Exception e)
 			{

+ 1 - 2
Codes/ModelView/Client/Module/Resource/ResourcesComponent.cs

@@ -136,7 +136,6 @@ namespace ET.Client
             protected override void Awake(ResourcesComponent self)
             {
                 ResourcesComponent.Instance = self;
-
                 if (Define.IsAsync)
                 {
                     self.LoadOneBundle("StreamingAssets");
@@ -600,7 +599,7 @@ namespace ET.Client
         }
     }
     
-    [ComponentOf(typeof(Scene))]
+    [ComponentOf]
     public class ResourcesComponent: Entity, IAwake, IDestroy
     {
         public static ResourcesComponent Instance { get; set; }

+ 5 - 0
DotNet/Hotfix/DotNet.Hotfix.csproj

@@ -37,4 +37,9 @@
         <ProjectReference Include="..\..\Codes\Analyzer\Share.Analyzer.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
         <ProjectReference Include="..\Model\DotNet.Model.csproj" />
     </ItemGroup>
+    <ItemGroup>
+      <Folder Include="..\..\Codes\Hotfix\Share\Demo">
+        <Link>Share\Demo</Link>
+      </Folder>
+    </ItemGroup>
 </Project>

+ 2 - 2
Codes/Model/Server/Module/Config/ConfigLoader.cs → DotNet/Model/ConfigLoader.cs

@@ -4,9 +4,9 @@ using System.IO;
 namespace ET.Server
 {
     [Callback(CallbackType.GetAllConfigBytes)]
-    public class GetAllConfigBytes: IAction<Dictionary<string, byte[]>>
+    public class GetAllConfigBytes: IAction<ConfigComponent, Dictionary<string, byte[]>>
     {
-        public void Handle(Dictionary<string, byte[]> output)
+        public void Handle(ConfigComponent configComponent, Dictionary<string, byte[]> output)
         {
             foreach (string file in Directory.GetFiles($"../Config", "*.bytes"))
             {

+ 4 - 0
DotNet/Model/DotNet.Model.csproj

@@ -42,6 +42,10 @@
         <Compile Include="..\..\Codes\Generate\Server\**\*.cs">
             <Link>Generate\%(RecursiveDir)%(FileName)%(Extension)</Link>
         </Compile>
+
+        <Compile Update="ConfigLoader.cs">
+          <Link>Server\Module\Config\ConfigLoader.cs</Link>
+        </Compile>
         
     </ItemGroup> 
     <ItemGroup> 

+ 11 - 4
Unity/Assets/Scripts/Editor/BuildEditor/BuildEditor.cs

@@ -36,6 +36,8 @@ namespace ET
 		private BuildOptions buildOptions;
 		private BuildAssetBundleOptions buildAssetBundleOptions = BuildAssetBundleOptions.None;
 
+		private GlobalConfig globalConfig;
+
 		[MenuItem("ET/Build Tool")]
 		public static void ShowWindow()
 		{
@@ -43,7 +45,9 @@ namespace ET
 		}
 
         private void OnEnable()
-        {
+		{
+			globalConfig = Resources.Load<GlobalConfig>("GlobalConfig");
+			
 #if UNITY_ANDROID
 			activePlatform = PlatformType.Android;
 #elif UNITY_IOS
@@ -107,21 +111,24 @@ namespace ET
 			}
 			
 			GUILayout.Label("");
+			GUILayout.Label("代码编译:");
+			
+			this.globalConfig.LoadMode = (LoadMode)EditorGUILayout.EnumPopup("LoadMode: ", this.globalConfig.LoadMode);
+			
+			this.globalConfig.CodeMode = (CodeMode)EditorGUILayout.EnumPopup("CodeMode: ", this.globalConfig.CodeMode);
+			
 			if (GUILayout.Button("BuildCode"))
 			{
-				GlobalConfig globalConfig = Resources.Load<GlobalConfig>("GlobalConfig");
 				BuildAssemblieEditor.BuildCode(this.codeOptimization, globalConfig);
 			}
 			
 			if (GUILayout.Button("BuildModel"))
 			{
-				GlobalConfig globalConfig = Resources.Load<GlobalConfig>("GlobalConfig");
 				BuildAssemblieEditor.BuildModel(this.codeOptimization, globalConfig);
 			}
 			
 			if (GUILayout.Button("BuildHotfix"))
 			{
-				GlobalConfig globalConfig = Resources.Load<GlobalConfig>("GlobalConfig");
 				BuildAssemblieEditor.BuildHotfix(this.codeOptimization, globalConfig);
 			}