Ejemplo n.º 1
0
    public static void InitServerSystems()
    {
        serverWorld = new World("ServerWorld");
        serverSimulationSystemGroup = serverWorld.GetOrCreateSystem <ServerSimulationSystemGroup>();

        foreach (var sys in allSystems)
        {
            var groups = sys.GetCustomAttributes(typeof(UpdateInGroupAttribute), true);
            foreach (var grp in groups)
            {
                var group = grp as UpdateInGroupAttribute;

                if (group.GroupType == typeof(ServerSimulationSystemGroup) ||
                    group.GroupType == typeof(ClientAndServerSimulationSystemGroup))
                {
                    serverSimulationSystemGroup.AddSystemToUpdateList(serverWorld.GetOrCreateSystem(sys) as ComponentSystemBase);
                }
                else if (group.GroupType == typeof(ClientSimulationSystemGroup) ||
                         group.GroupType == typeof(ClientPresentationSystemGroup))
                {
                    // do nothing
                }
                else
                {
                    var mask = GetTopLevelWorldMask(group.GroupType);
                    if ((mask & WorldType.ServerWorld) != 0)
                    {
                        var groupSys = serverWorld.GetOrCreateSystem(group.GroupType) as ComponentSystemGroup;
                        groupSys.AddSystemToUpdateList(serverWorld.GetOrCreateSystem(sys) as ComponentSystemBase);
                    }
                }
            }
        }

        serverSimulationSystemGroup.SortSystemUpdateList();
        World.Active.GetOrCreateSystem <TickServerSimulationSystem>().AddSystemToUpdateList(serverSimulationSystemGroup);
    }
Ejemplo n.º 2
0
    public List <Type> Initialize(List <Type> systems)
    {
        // Workaround for initialization being called multiple times when using game object conversion
#if !UNITY_SERVER
        if (clientWorld != null)
        {
            return(systems);
        }
#endif
#if !UNITY_CLIENT || UNITY_SERVER || UNITY_EDITOR
        if (serverWorld != null)
        {
            return(systems);
        }
#endif

#if !UNITY_SERVER
#if UNITY_EDITOR
        int numClientWorlds = UnityEditor.EditorPrefs.GetInt("MultiplayerPlayMode_" + UnityEngine.Application.productName + "_NumClients");
        if (numClientWorlds < 1)
        {
            numClientWorlds = 1;
        }
        if (numClientWorlds > 8)
        {
            numClientWorlds = 8;
        }
        int playModeType = UnityEditor.EditorPrefs.GetInt("MultiplayerPlayMode_" + UnityEngine.Application.productName + "_Type");
#else
        int numClientWorlds = 1;
#endif
#endif

        var defaultBootstrap = new List <Type>();
#if !UNITY_SERVER
        clientWorld = null;
        ClientInitializationSystemGroup[] clientInitializationSystemGroup = null;
        ClientSimulationSystemGroup[]     clientSimulationSystemGroup     = null;
        ClientPresentationSystemGroup[]   clientPresentationSystemGroup   = null;
#if UNITY_EDITOR
        if (playModeType != 2)
#endif
        {
            clientWorld = new World[numClientWorlds];
            clientInitializationSystemGroup = new ClientInitializationSystemGroup[clientWorld.Length];
            clientSimulationSystemGroup     = new ClientSimulationSystemGroup[clientWorld.Length];
            clientPresentationSystemGroup   = new ClientPresentationSystemGroup[clientWorld.Length];
            for (int i = 0; i < clientWorld.Length; ++i)
            {
                clientWorld[i] = new World("ClientWorld" + i);
                clientInitializationSystemGroup[i] = clientWorld[i].GetOrCreateSystem <ClientInitializationSystemGroup>();
                clientSimulationSystemGroup[i]     = clientWorld[i].GetOrCreateSystem <ClientSimulationSystemGroup>();
#if UNITY_EDITOR
                clientSimulationSystemGroup[i].ClientWorldIndex = i;
#endif
                clientPresentationSystemGroup[i] = clientWorld[i].GetOrCreateSystem <ClientPresentationSystemGroup>();
            }
        }
#endif
#if !UNITY_CLIENT || UNITY_SERVER || UNITY_EDITOR
        serverWorld = null;
        ServerInitializationSystemGroup serverInitializationSystemGroup = null;
        ServerSimulationSystemGroup     serverSimulationSystemGroup     = null;
#if UNITY_EDITOR
        if (playModeType != 1)
#endif
        {
            serverWorld = new World("ServerWorld");
            serverInitializationSystemGroup = serverWorld.GetOrCreateSystem <ServerInitializationSystemGroup>();
            serverSimulationSystemGroup     = serverWorld.GetOrCreateSystem <ServerSimulationSystemGroup>();
        }
#endif
        foreach (var type in systems)
        {
            if (type.GetCustomAttributes(typeof(NotClientServerSystemAttribute), true).Length > 0 ||
                type == typeof(InitializationSystemGroup) ||
                type == typeof(SimulationSystemGroup) ||
                type == typeof(PresentationSystemGroup))
            {
                defaultBootstrap.Add(type);
                continue;
            }
            var groups = type.GetCustomAttributes(typeof(UpdateInGroupAttribute), true);
            if (groups.Length == 0)
            {
                defaultBootstrap.Add(type);
                // Treat the default group as both client and server
                groups = new object[] { new UpdateInGroupAttribute(typeof(ClientAndServerSimulationSystemGroup)) };
            }

            foreach (var grp in groups)
            {
                var group = grp as UpdateInGroupAttribute;
                if (group.GroupType == typeof(ClientAndServerSimulationSystemGroup) || group.GroupType == typeof(SimulationSystemGroup))
                {
                    if (group.GroupType == typeof(SimulationSystemGroup))
                    {
                        defaultBootstrap.Add(type);
                    }
#if !UNITY_CLIENT || UNITY_SERVER || UNITY_EDITOR
                    if (serverWorld != null)
                    {
                        var defaultWorld = World.Active;
                        World.Active = serverWorld;
                        serverSimulationSystemGroup.AddSystemToUpdateList(
                            serverWorld.GetOrCreateSystem(type));
                        World.Active = defaultWorld;
                    }
#endif
#if !UNITY_SERVER
                    if (clientWorld != null)
                    {
                        var defaultWorld = World.Active;
                        for (int i = 0; i < clientSimulationSystemGroup.Length; ++i)
                        {
                            World.Active = clientWorld[i];
                            clientSimulationSystemGroup[i]
                            .AddSystemToUpdateList(clientWorld[i].GetOrCreateSystem(type));
                        }
                        World.Active = defaultWorld;
                    }
#endif
                }
                else if (group.GroupType == typeof(ClientAndServerInitializationSystemGroup) || group.GroupType == typeof(InitializationSystemGroup))
                {
                    if (group.GroupType == typeof(InitializationSystemGroup))
                    {
                        defaultBootstrap.Add(type);
                    }
#if !UNITY_CLIENT || UNITY_SERVER || UNITY_EDITOR
                    if (serverWorld != null)
                    {
                        var defaultWorld = World.Active;
                        World.Active = serverWorld;
                        serverInitializationSystemGroup.AddSystemToUpdateList(
                            serverWorld.GetOrCreateSystem(type));
                        World.Active = defaultWorld;
                    }
#endif
#if !UNITY_SERVER
                    if (clientWorld != null)
                    {
                        var defaultWorld = World.Active;
                        for (int i = 0; i < clientInitializationSystemGroup.Length; ++i)
                        {
                            World.Active = clientWorld[i];
                            clientInitializationSystemGroup[i]
                            .AddSystemToUpdateList(clientWorld[i].GetOrCreateSystem(type));
                        }
                        World.Active = defaultWorld;
                    }
#endif
                }
                else if (group.GroupType == typeof(ServerInitializationSystemGroup))
                {
#if !UNITY_CLIENT || UNITY_SERVER || UNITY_EDITOR
                    if (serverWorld != null)
                    {
                        var defaultWorld = World.Active;
                        World.Active = serverWorld;
                        serverInitializationSystemGroup.AddSystemToUpdateList(
                            serverWorld.GetOrCreateSystem(type));
                        World.Active = defaultWorld;
                    }
#endif
                }
                else if (group.GroupType == typeof(ClientInitializationSystemGroup))
                {
#if !UNITY_SERVER
                    if (clientWorld != null)
                    {
                        var defaultWorld = World.Active;
                        for (int i = 0; i < clientInitializationSystemGroup.Length; ++i)
                        {
                            World.Active = clientWorld[i];
                            clientInitializationSystemGroup[i]
                            .AddSystemToUpdateList(clientWorld[i].GetOrCreateSystem(type));
                        }
                        World.Active = defaultWorld;
                    }
#endif
                }
                else if (group.GroupType == typeof(ServerSimulationSystemGroup))
                {
#if !UNITY_CLIENT || UNITY_SERVER || UNITY_EDITOR
                    if (serverWorld != null)
                    {
                        var defaultWorld = World.Active;
                        World.Active = serverWorld;
                        serverSimulationSystemGroup.AddSystemToUpdateList(
                            serverWorld.GetOrCreateSystem(type));
                        World.Active = defaultWorld;
                    }
#endif
                }
                else if (group.GroupType == typeof(ClientSimulationSystemGroup))
                {
#if !UNITY_SERVER
                    if (clientWorld != null)
                    {
                        var defaultWorld = World.Active;
                        for (int i = 0; i < clientSimulationSystemGroup.Length; ++i)
                        {
                            World.Active = clientWorld[i];
                            clientSimulationSystemGroup[i]
                            .AddSystemToUpdateList(clientWorld[i].GetOrCreateSystem(type));
                        }
                        World.Active = defaultWorld;
                    }
#endif
                }
                else if (group.GroupType == typeof(ClientPresentationSystemGroup) || group.GroupType == typeof(PresentationSystemGroup))
                {
                    if (group.GroupType == typeof(PresentationSystemGroup))
                    {
                        defaultBootstrap.Add(type);
                    }
#if !UNITY_SERVER
                    if (clientWorld != null)
                    {
                        var defaultWorld = World.Active;
                        for (int i = 0; i < clientPresentationSystemGroup.Length; ++i)
                        {
                            World.Active = clientWorld[i];
                            clientPresentationSystemGroup[i]
                            .AddSystemToUpdateList(clientWorld[i].GetOrCreateSystem(type));
                        }
                        World.Active = defaultWorld;
                    }
#endif
                }
                else
                {
                    var mask = GetTopLevelWorldMask(group.GroupType);
                    if ((mask & WorldType.DefaultWorld) != 0)
                    {
                        defaultBootstrap.Add(type);
                    }
#if !UNITY_SERVER
                    if ((mask & WorldType.ClientWorld) != 0 && clientWorld != null)
                    {
                        var defaultWorld = World.Active;
                        for (int i = 0; i < clientWorld.Length; ++i)
                        {
                            World.Active = clientWorld[i];
                            var groupSys = clientWorld[i].GetOrCreateSystem(group.GroupType) as ComponentSystemGroup;
                            groupSys.AddSystemToUpdateList(clientWorld[i].GetOrCreateSystem(type));
                        }
                        World.Active = defaultWorld;
                    }
#endif
#if !UNITY_CLIENT || UNITY_SERVER || UNITY_EDITOR
                    if ((mask & WorldType.ServerWorld) != 0 && serverWorld != null)
                    {
                        var defaultWorld = World.Active;
                        World.Active = serverWorld;
                        var groupSys = serverWorld.GetOrCreateSystem(group.GroupType) as ComponentSystemGroup;
                        groupSys.AddSystemToUpdateList(serverWorld.GetOrCreateSystem(type));
                        World.Active = defaultWorld;
                    }
#endif
                }
            }
        }
#if !UNITY_CLIENT || UNITY_SERVER || UNITY_EDITOR
        if (serverWorld != null)
        {
            serverInitializationSystemGroup.SortSystemUpdateList();
            serverSimulationSystemGroup.SortSystemUpdateList();
            World.Active.GetOrCreateSystem <TickServerInitializationSystem>().AddSystemToUpdateList(serverInitializationSystemGroup);
            World.Active.GetOrCreateSystem <TickServerSimulationSystem>().AddSystemToUpdateList(serverSimulationSystemGroup);
        }
#endif
#if !UNITY_SERVER
        if (clientWorld != null)
        {
            for (int i = 0; i < clientWorld.Length; ++i)
            {
                clientInitializationSystemGroup[i].SortSystemUpdateList();
                clientSimulationSystemGroup[i].SortSystemUpdateList();
                clientPresentationSystemGroup[i].SortSystemUpdateList();
                World.Active.GetOrCreateSystem <TickClientInitializationSystem>().AddSystemToUpdateList(clientInitializationSystemGroup[i]);
                World.Active.GetOrCreateSystem <TickClientSimulationSystem>().AddSystemToUpdateList(clientSimulationSystemGroup[i]);
                World.Active.GetOrCreateSystem <TickClientPresentationSystem>().AddSystemToUpdateList(clientPresentationSystemGroup[i]);
            }
        }
#endif
        return(defaultBootstrap);
    }