/// <summary> /// 对每个Mod的系统根据运行于的世界和时间分类 /// </summary> private void SortModSystem(Type system, NativeModSystems mod) { //获得系统运行于的世界 var worldtype = GetSystemWorldType(system); if (worldtype == WorldTypes.DefaultWorld) { //是默认状态则添加到DefaultSystem中,由于默认状态的int为0所以要直接用=比较 mod.DefaultWorldSystem.Add(system); //如果所属系统组为Presentation组,加入列表,将在服务端禁用 var attr = TypeManager.GetSystemAttributes(system, typeof(UpdateInGroupAttribute)); if (attr.Length != 0 && (attr[0] as UpdateInGroupAttribute).GroupType.Equals(typeof(PresentationSystemGroup))) { mod.DefaultPresentationSystems.Add(system); } return;//且不需要继续比较 } if ((worldtype & WorldTypes.ClientWorld) == WorldTypes.ClientWorld) { //添加到客户端世界 mod.ClientSystems.Add(system); } if ((worldtype & WorldTypes.ServerWorld) == WorldTypes.ServerWorld) { //添加到服务端世界 mod.ServerSystems.Add(system); } if ((worldtype & WorldTypes.ExplicitDefaultWorld) == WorldTypes.ExplicitDefaultWorld) { //添加到默认世界 ExplicitDefaultWorldSystems.Add(system); } }
/// <summary> /// 创建服务端世界 /// </summary> public World CreateServerWorld(string ServerWorldName, params string[] EnableMods) { //创建世界 World ServerWorld = new World(ServerWorldName); EntityManager manager = ServerWorld.EntityManager; //添加世界信息组件 Entity e = manager.CreateEntity(ComponentType.ReadWrite <WorldTypeInfo>()); manager.SetComponentData(e, new WorldTypeInfo() { type = WorldTypes.ServerWorld }); manager.SetName(e, "WorldTypeInfo"); //为接下来将在ServerWorld中禁用的Presentation组的系统做缓存,节省查找开销 List <Type>[] types = new List <Type> [EnableMods.Length]; //轮询添加系统 for (int i = 0; i < EnableMods.Length; i++) { NativeModSystems mod = ModSystems[EnableMods[i]]; DefaultWorldInitialization.AddSystemsToRootLevelSystemGroups(ServerWorld, mod.DefaultWorldSystem); DefaultWorldInitialization.AddSystemsToRootLevelSystemGroups(ServerWorld, mod.ServerSystems); //缓存 types[i] = mod.DefaultPresentationSystems; } ScriptBehaviourUpdateOrder.AddWorldToCurrentPlayerLoop(ServerWorld); //获取ServerWorld中的PresentationSystemGroup并禁止其运行 for (int i = 0; i < types.Length; i++) { for (int j = 0; j < types[i].Count; j++) { ServerWorld.GetExistingSystem(types[i][j]).Enabled = false; } } return(ServerWorld); }
/// <summary> /// 对原生系统进行分类 /// </summary> public void SortSystems(IReadOnlyList <Type> systems) { //初始化 ExplicitDefaultWorldSystems = new List <Type>(); ModSystems = new Dictionary <string, NativeModSystems>(); //提前缓存基本系统所处的分类表,避免之后多次查询 var GameCoreSystems = new NativeModSystems("GameCore"); ModSystems.Add("GameCore", GameCoreSystems); int i = 0; for (i = 0; i < systems.Count; i++) { //获得系统所属命名空间名 string[] SystemName = TypeManager.GetSystemName(systems[i]).Split('.'); //Mod的命名空间名格式为Mod.XX, string FirstName = SystemName[0]; //避免出现名为Mod.XX的类导致错误分类,如果类名为Mod.XX将视作基本系统 if (FirstName == "Mod" && SystemName.Length > 2) { //分类Mod系统 string SecondName = SystemName[1]; NativeModSystems mod; if (ModSystems.TryGetValue(SecondName, out mod)) { //如果找到了就直接分类Mod内部系统,没有找到新建一个再分类 SortModSystem(systems[i], mod); } else { mod = new NativeModSystems(SecondName); SortModSystem(systems[i], mod); ModSystems.Add(SecondName, mod); } } else { //分类基本系统 SortModSystem(systems[i], GameCoreSystems); } } }
/// <summary> /// 创建客户端世界 /// </summary> public World CreateClientWorld(string ClientWorldName, params string[] EnableMods) { //创建世界 World ClientWorld = new World(ClientWorldName); EntityManager manager = ClientWorld.EntityManager; //添加世界信息组件 Entity e = manager.CreateEntity(ComponentType.ReadWrite <WorldTypeInfo>()); manager.SetComponentData(e, new WorldTypeInfo() { type = WorldTypes.ClientWorld }); manager.SetName(e, "WorldTypeInfo"); //轮询添加系统 for (int i = 0; i < EnableMods.Length; i++) { NativeModSystems mod = ModSystems[EnableMods[i]]; DefaultWorldInitialization.AddSystemsToRootLevelSystemGroups(ClientWorld, mod.DefaultWorldSystem); DefaultWorldInitialization.AddSystemsToRootLevelSystemGroups(ClientWorld, mod.ClientSystems); } //添加到Update列表 ScriptBehaviourUpdateOrder.AddWorldToCurrentPlayerLoop(ClientWorld); return(ClientWorld); }