Esempio n. 1
0
    // Awake
    void Awake()
    {
        // TODO: Checks...
        instance = this;

        // This will be reset later if -batchmode was specified
        #if UNITY_STANDALONE_WIN
        isTestServer = true;
        #else
        isTestServer = false;
        #endif

        // Default values
        serverPort = defaultServerPort;
        partyCount = 1;

        // -party0 account1 -party1 account2
        // -batchmode -port7100
        // taskkill /IM
        LogManager.General.Log("Parsing command line arguments");
        string[] args = System.Environment.GetCommandLineArgs();
        int partyId = GameServerParty.Undefined;

        foreach(string arg in args) {
            LogManager.General.Log("Command line argument: '" + arg + "'");

            // Overwrite port
            if(arg.StartsWith("-port") && arg.Length > 5) {
                serverPort = int.Parse(arg.Substring(5));
            // Batchmode
            } else if(arg == "-batchmode") {
                batchMode = true;
                isTestServer = false;
            // Party count
            } else if(arg.StartsWith("-partycount") && arg.Length > 6) {
                partyCount = int.Parse(arg.Substring("-partycount".Length));

                LogManager.General.Log(string.Format("Creating parties: {0}", partyCount));
                GameServerParty.partyList.Clear();
                GameServerParty.CreateParties(partyCount);
            // Teams
            } else if(arg.StartsWith("-party") && arg.Length > 6) {
                partyId = int.Parse(arg.Substring("-party".Length));
                restrictedAccounts = true;
            // Map
            } else if(arg.StartsWith("-map") && arg.Length > 4) {
                mapName = arg.Substring("-map".Length);
            // Server type
            } else if(arg.StartsWith("-type") && arg.Length > 5) {
                string serverTypeString = arg.Substring("-type".Length);
                switch(serverTypeString) {
                    case "Arena":
                        GameManager.serverType = ServerType.Arena;
                        break;

                    case "Town":
                        GameManager.serverType = ServerType.Town;
                        break;

                    case "FFA":
                        GameManager.serverType = ServerType.FFA;
                        break;

                    case "World":
                        GameManager.serverType = ServerType.World;
                        break;
                }
            // Account ID
            } else {
                if(partyId >= 0 && partyId < GameServerParty.partyList.Count) {
                    var currentParty = GameServerParty.partyList[partyId];
                    accountToParty[arg] = currentParty;
                    currentParty.expectedMemberCount += 1;
                }
            }
        }

        // For testing
        if(isTestServer)
            GameManager.serverType = testServerType;

        // Create at least 1 party if no party count has been specified
        if(GameServerParty.partyList.Count != partyCount) {
            switch(GameManager.serverType) {
                case ServerType.Arena:
                    partyCount = 2;
                    break;

                case ServerType.FFA:
                    partyCount = 10;
                    break;
            }

            LogManager.General.Log(string.Format("Creating parties: {0}", partyCount));
            GameServerParty.partyList.Clear();
            GameServerParty.CreateParties(partyCount);
        }

        // Server type
        LogManager.General.Log("Server type: " + GameManager.serverType);
        MapManager.InitPhysics(GameManager.serverType);

        if(restrictedAccounts) {
            LogManager.General.Log("Server is restricted to the following accounts: " + accountToParty.Keys);
        }

        if(GameManager.isArena) {
            QueueSettings.queueIndex = accountToParty.Count / 2 - 1;
            LogManager.General.Log("Queue type is: " + (QueueSettings.queueIndex + 1) + "v" + (QueueSettings.queueIndex + 1));
        } else if(GameManager.isFFA) {
            QueueSettings.queueIndex = 0;
            LogManager.General.Log("Queue type is: " + (QueueSettings.queueIndex + 1) + "v" + (QueueSettings.queueIndex + 1));
        }

        // Server batchmode
        //if(batchMode) {
        LogManager.General.Log("Batchmode is being used: Destroy the camera and disable renderers");

        // Destroy the camera
        Destroy(Camera.main);

        // Disable renderers in batchmode
        DisableRenderers(creatorPrefab);

        // No statistics GUI
        #if UNITY_EDITOR
        GetComponent<uLinkStatisticsGUI>().enabled = true;
        #endif

        // No audio
        AudioListener.pause = true;

        // Disable all game modes just to be safe
        var gameModes = GetComponents<GameMode>();
        foreach(var mode in gameModes) {
            mode.enabled = false;
        }

        // Pick correct game mode
        int maxPlayerCount = 0;
        switch(GameManager.serverType) {
            case ServerType.Arena:
                gameMode = GetComponent<ArenaGameMode>();
                int maxSpectators = 10;
                maxPlayerCount = 10 + maxSpectators;
                break;

            case ServerType.Town:
                gameMode = GetComponent<TownGameMode>();
                maxPlayerCount = 1024;
                break;

            case ServerType.FFA:
                gameMode = GetComponent<FFAGameMode>();
                maxPlayerCount = 10;
                break;

            case ServerType.World:
                gameMode = GetComponent<WorldGameMode>();
                maxPlayerCount = 1024;
                break;
        }

        // FFA
        if(GameManager.isFFA) {
            GameServerParty.partyList.Clear();
            GameServerParty.CreateParties(10, 1);
        }

        // Load map
        StartCoroutine(MapManager.LoadMapAsync(
            mapName,

            // Map is loaded asynchronously...
            // When it's finished, we use the callback:
            () => {
                // Register codecs for serialization
                GameDB.InitCodecs();

                // Init uZone
                if(uZone.Instance.wasStartedByuZone) {
                    uZone.Instance.GlobalEvents events = new uZone.Instance.GlobalEvents();
                    events.onInitialized = uZone_OnInitialized;

                    // This will tell uZone that the instance is ready
                    // and we can let other players connect to it
                    LogManager.General.Log("Initializing the uZone instance");
                    uZone.Instance.Initialize(events);
                }

                // Server port
                if(!isTestServer) {
                    try {
                        serverPort = uZone.Instance.port;
                        LogManager.General.Log("Using port assigned from uZone: " + serverPort);
                    } catch {
                        LogManager.General.Log("Failed to retrieve port info from uZone! Using port " + serverPort);
                    }
                } else {
                    LogManager.General.Log("Using test server port: " + serverPort);
                }

                // Init server
                LogManager.General.Log("Initializing the server on port " + serverPort);
                uLink.Network.InitializeServer(maxPlayerCount, serverPort);

                // Encryption
                LogManager.General.Log("Initializing security");
                uLink.Network.InitializeSecurity(true);

        #if !UNITY_EDITOR
                // Clean up
                DestroyServerAssets();
        #endif
            }
        ));
    }
Esempio n. 2
0
    // Awake
    void Awake()
    {
        // TODO: Checks...
        instance = this;

        // This will be reset later if -batchmode was specified
#if UNITY_STANDALONE_WIN
        isTestServer = true;
#else
        isTestServer = false;
#endif

        // Default values
        serverPort = defaultServerPort;
        partyCount = 1;

        // -party0 account1 -party1 account2
        // -batchmode -port7100
        // taskkill /IM
        LogManager.General.Log("Parsing command line arguments");
        string[] args    = System.Environment.GetCommandLineArgs();
        int      partyId = GameServerParty.Undefined;

        foreach (string arg in args)
        {
            LogManager.General.Log("Command line argument: '" + arg + "'");

            // Overwrite port
            if (arg.StartsWith("-port") && arg.Length > "-port".Length)
            {
                serverPort = int.Parse(arg.Substring(5));
                // Batchmode
            }
            else if (arg == "-batchmode")
            {
                batchMode    = true;
                isTestServer = false;
                // Party count
            }
            else if (arg.StartsWith("-partycount") && arg.Length > "-partycount".Length)
            {
                partyCount = int.Parse(arg.Substring("-partycount".Length));

                LogManager.General.Log(string.Format("Creating parties: {0}", partyCount));
                GameServerParty.partyList.Clear();
                GameServerParty.CreateParties(partyCount);
                // Teams
            }
            else if (arg.StartsWith("-party") && arg.Length > "-party".Length)
            {
                partyId            = int.Parse(arg.Substring("-party".Length));
                restrictedAccounts = true;
                // Map
            }
            else if (arg.StartsWith("-map") && arg.Length > "-map".Length)
            {
                mapName = arg.Substring("-map".Length);
                // Lobby IP
            }
            else if (arg.StartsWith("-lobbyIP") && arg.Length > "-lobbyIP".Length)
            {
                lobbyIP = arg.Substring("-lobbyIP".Length);
                // Lobby Port
            }
            else if (arg.StartsWith("-lobbyPort") && arg.Length > "-lobbyPort".Length)
            {
                lobbyPort = int.Parse(arg.Substring("-lobbyPort".Length));
                // Server type
            }
            else if (arg.StartsWith("-type") && arg.Length > "-type".Length)
            {
                string serverTypeString = arg.Substring("-type".Length);
                switch (serverTypeString)
                {
                case "Arena":
                    GameManager.serverType = ServerType.Arena;
                    break;

                case "Town":
                    GameManager.serverType = ServerType.Town;
                    break;

                case "FFA":
                    GameManager.serverType = ServerType.FFA;
                    break;

                case "World":
                    GameManager.serverType = ServerType.World;
                    break;
                }
                // Database IP
            }
            else if (arg.StartsWith("-databaseIP") && arg.Length > "-databaseIP".Length)
            {
                databaseIP = arg.Substring("-databaseIP".Length);
                // Database Port
            }
            else if (arg.StartsWith("-databasePort") && arg.Length > "-databasePort".Length)
            {
                databasePort = int.Parse(arg.Substring("-databasePort".Length));
                // Account ID
            }
            else
            {
                if (partyId >= 0 && partyId < GameServerParty.partyList.Count)
                {
                    var currentParty = GameServerParty.partyList[partyId];
                    accountToParty[arg] = currentParty;
                    currentParty.expectedMemberCount += 1;
                }
            }
        }

        // For testing
        if (isTestServer)
        {
            GameManager.serverType = testServerType;
        }
        else
        {
            if (!string.IsNullOrEmpty(databaseIP))
            {
                Database.AddNode("riak", databaseIP, databasePort, 10, Defaults.WriteTimeout, Defaults.ReadTimeout);
                Database.Connect();
            }
            else
            {
                LogManager.DB.LogError("No database address specified, can't connect to the database");
            }
        }

        // Create at least 1 party if no party count has been specified
        if (GameServerParty.partyList.Count != partyCount)
        {
            switch (GameManager.serverType)
            {
            case ServerType.Arena:
                partyCount = 2;
                break;

            case ServerType.FFA:
                partyCount = 10;
                break;
            }

            LogManager.General.Log(string.Format("Creating parties: {0}", partyCount));
            GameServerParty.partyList.Clear();
            GameServerParty.CreateParties(partyCount);
        }

        // Server type
        LogManager.General.Log("Server type: " + GameManager.serverType);
        MapManager.InitPhysics(GameManager.serverType);

        if (restrictedAccounts)
        {
            LogManager.General.Log("Server is restricted to the following accounts: " + accountToParty.Keys);
        }

        if (GameManager.isArena)
        {
            QueueSettings.queueIndex = accountToParty.Count / 2 - 1;
            LogManager.General.Log("Queue type is: " + (QueueSettings.queueIndex + 1) + "v" + (QueueSettings.queueIndex + 1));
        }
        else if (GameManager.isFFA)
        {
            QueueSettings.queueIndex = 0;
            LogManager.General.Log("Queue type is: " + (QueueSettings.queueIndex + 1) + "v" + (QueueSettings.queueIndex + 1));
        }

        // Server batchmode
        //if(batchMode) {
        LogManager.General.Log("Batchmode is being used: Destroy the camera and disable renderers");

        // Destroy the camera
        Destroy(Camera.main);

        // Disable renderers in batchmode
        DisableRenderers(creatorPrefab);

        // No statistics GUI
#if UNITY_EDITOR
        GetComponent <uLinkStatisticsGUI>().enabled = true;
#endif

        // No audio
        AudioListener.pause = true;

        // Disable all game modes just to be safe
        var gameModes = GetComponents <GameMode>();
        foreach (var mode in gameModes)
        {
            mode.enabled = false;
        }

        // Pick correct game mode
        int maxPlayerCount = 0;
        switch (GameManager.serverType)
        {
        case ServerType.Arena:
            gameMode = GetComponent <ArenaGameMode>();
            int maxSpectators = 10;
            maxPlayerCount = 10 + maxSpectators;
            break;

        case ServerType.Town:
            gameMode       = GetComponent <TownGameMode>();
            maxPlayerCount = 1024;
            break;

        case ServerType.FFA:
            gameMode       = GetComponent <FFAGameMode>();
            maxPlayerCount = 10;
            break;

        case ServerType.World:
            gameMode       = GetComponent <WorldGameMode>();
            maxPlayerCount = 1024;
            break;
        }

        // FFA
        if (GameManager.isFFA)
        {
            GameServerParty.partyList.Clear();
            GameServerParty.CreateParties(10, 1);
        }

        // Public key
        NetworkHelper.InitPublicLobbyKey();

        // Started by uZone?
        if (uZone.Instance.wasStartedByuZone)
        {
            // Listen to lobby events and RPCs
            Lobby.AddListener(this);

            IPAndPortCallBack connectToServices = (host, port) => {
                // Connect to lobby
                LogManager.General.Log(string.Format("Connecting to lobby as server {0}:{1}", host, port));
                Lobby.ConnectAsServer(host, port);

                // Update members in case we downloaded the info
                lobbyIP   = host;
                lobbyPort = port;

                // Set up callback
                uZone.Instance.GlobalEvents events = new uZone.Instance.GlobalEvents();
                events.onInitialized = uZone_OnInitialized;

                // This will tell uZone that the instance is ready
                // and we can let other players connect to it
                LogManager.General.Log("Initializing the uZone instance");
                uZone.Instance.Initialize(events);
            };

            // uLobby and uZone
#if UNITY_STANDALONE_WIN
            lobbyIP   = "127.0.0.1";
            lobbyPort = 1310;

            connectToServices(lobbyIP, lobbyPort);
#else
            if (string.IsNullOrEmpty(lobbyIP))
            {
                StartCoroutine(NetworkHelper.DownloadIPAndPort("https://battleofmages.com/scripts/login-server-ip.php", connectToServices));
            }
            else
            {
                connectToServices(lobbyIP, lobbyPort);
            }
#endif
        }

        // Load map
        StartCoroutine(MapManager.LoadMapAsync(
                           mapName,

                           // Map is loaded asynchronously...
                           // When it's finished, we use the callback:
                           () => {
            // Register codecs for serialization
            GameDB.InitCodecs();

            // Server port
            if (!isTestServer)
            {
                try {
                    serverPort = uZone.Instance.port;
                    LogManager.General.Log("Using port assigned from uZone: " + serverPort);
                } catch {
                    LogManager.General.Log("Failed to retrieve port info from uZone! Using port " + serverPort);
                }
            }
            else
            {
                LogManager.General.Log("Using test server port: " + serverPort);
            }

            // Init server
            LogManager.General.Log("Initializing the server on port " + serverPort);
            uLink.Network.InitializeServer(maxPlayerCount, serverPort);

            // Encryption
            LogManager.General.Log("Initializing security");
            uLink.Network.InitializeSecurity(true);

#if !UNITY_EDITOR
            // Clean up
            DestroyServerAssets();
#endif
            // Send ready message if we didn't do it yet
            SendReadyMessage();
        }
                           ));
    }