讲过master客户端的框架初始化,再来说一下服务端的初始化流程吧,因为很多客户端代码在服务端是通用的,所以理解起来也不算困难,就像这样

先来到程序入口

然后就是框架的初始化了(因为很多细节在客户端那篇讲过了,所以就不在这赘述了,内部实现是一样的)
1 2 3 4 5 6 7 8 9 10 11 12 13
   |   Game.EventSystem.Add(DLLType.Model, typeof(Game).Assembly);
  Game.EventSystem.Add(DLLType.Hotfix, DllHelper.GetHotfixAssembly());
  Options options = Game.Scene.AddComponent<OptionComponent, string[]>(args).Options;
  StartConfig startConfig = Game.Scene.AddComponent<StartConfigComponent, string, int> if (!options.AppType.Is(startConfig.AppType)) {     Log.Error("命令行参数apptype与配置不一致");     return; }
   | 
 
1 2 3 4 5 6 7 8 9 10
   |   Game.Scene.AddComponent<TimerComponent>();
  Game.Scene.AddComponent<OpcodeTypeComponent>();
  Game.Scene.AddComponent<MessageDispatcherComponent>();
  OuterConfig outerConfig = startConfig.GetComponent<OuterConfig>(); InnerConfig innerConfig = startConfig.GetComponent<InnerConfig>(); ClientConfig clientConfig = startConfig.GetComponent<ClientConfig>();
   | 
 
然后是最为重要的服务端组件的添加 ,这里直接拿****case AppType.AllServer:中的组件举例,因为它几乎包括了所有组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
   |   Game.Scene.AddComponent<ActorMessageSenderComponent>();
  Game.Scene.AddComponent<ActorLocationSenderComponent>();
  Game.Scene.AddComponent<DBComponent>();
  Game.Scene.AddComponent<DBProxyComponent>();
  Game.Scene.AddComponent<LocationComponent>();
  Game.Scene.AddComponent<LocationProxyComponent>();
  Game.Scene.AddComponent<MailboxDispatcherComponent>(); Game.Scene.AddComponent<ActorMessageDispatcherComponent>();
  Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
  Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
  Game.Scene.AddComponent<AppManagerComponent>(); Game.Scene.AddComponent<RealmGateAddressComponent>(); Game.Scene.AddComponent<GateSessionKeyComponent>();
  Game.Scene.AddComponent<ConfigComponent>();
  Game.Scene.AddComponent<PathfindingComponent>();
  Game.Scene.AddComponent<PlayerComponent>();
  Game.Scene.AddComponent<UnitComponent>(); Game.Scene.AddComponent<ConsoleComponent>();
   | 
 
** 最后通过while循环让整个框架跑起来,线程沉睡一毫秒的作用是防止直接吃满CPU**
1 2 3 4 5 6 7 8 9 10 11 12 13
   | 	while (true) { 	try 	{ 		Thread.Sleep(1); 		OneThreadSynchronizationContext.Instance.Update(); 		Game.EventSystem.Update(); 	} 	catch (Exception e) 	{ 		Log.Error(e); 	} }
   |