Exemplo n.º 1
0
    public bool Init(string[] args)
    {
        m_StateMachine = new StateMachine <ClientState>();
        m_StateMachine.Add(ClientState.Browsing, EnterBrowsingState, UpdateBrowsingState, LeaveBrowsingState);
        m_StateMachine.Add(ClientState.Connecting, EnterConnectingState, UpdateConnectingState, null);
        m_StateMachine.Add(ClientState.Loading, EnterLoadingState, UpdateLoadingState, null);
        m_StateMachine.Add(ClientState.Playing, EnterPlayingState, UpdatePlayingState, LeavePlayingState);

#if UNITY_EDITOR
        Game.game.levelManager.UnloadLevel();
        World.DisposeAllWorlds();
#endif
        m_GameWorld = new GameWorld("ClientWorld");

#if USE_UNET
        m_NetworkTransport = new UNETTransport();
        if (!m_NetworkTransport.Init())
        {
            return(false);
        }
        m_NetworkClient = new NetworkClient(m_NetworkTransport);
#else
        m_NetworkTransport = new SocketTransport();
        m_NetworkClient    = new NetworkClient(m_NetworkTransport);
#endif

        if (Application.isEditor || Game.game.buildId == "AutoBuild")
        {
            NetworkClient.clientVerifyProtocol.Value = "0";
        }

        m_NetworkClient.UpdateClientConfig();
        m_NetworkStatistics = new NetworkStatisticsClient(m_NetworkClient);
        m_ChatSystem        = new ChatSystemClient(m_NetworkClient);

#if USE_UNET
        //m_BroadcastListener = new UNETBroadcastListener();
        //m_BroadcastListener.config = new UNETBroadcastConfig();
        //m_BroadcastListener.Init();
        m_ServerListClient = new ServerListClient(ServerListConfig.BasicConfig("da76f801-609f-47c8-b711-a54234e2b7be"));
#endif
        GameDebug.Log("Network client initialized");

        m_requestedPlayerSettings.playerName = clientPlayerName.Value;
        m_requestedPlayerSettings.teamId     = -1;

        Console.AddCommand("disconnect", CmdDisconnect, "Disconnect from server if connected", this.GetHashCode());
        Console.AddCommand("prediction", CmdTogglePrediction, "Toggle prediction", this.GetHashCode());
        Console.AddCommand("runatserver", CmdRunAtServer, "Run command at server", this.GetHashCode());
        Console.AddCommand("respawn", CmdRespawn, "Force a respawn", this.GetHashCode());
        Console.AddCommand("nextchar", CmdNextChar, "Select next character", this.GetHashCode());
        Console.AddCommand("nextteam", CmdNextTeam, "Select next character", this.GetHashCode());
        Console.AddCommand("spectator", CmdSpectator, "Select spectator cam", this.GetHashCode());
        Console.AddCommand("matchmake", CmdMatchmake, "matchmake <ip:port>: Find and join a server", this.GetHashCode());

        if (args.Length > 0)
        {
            targetServer = args[0];
            m_StateMachine.SwitchTo(ClientState.Connecting);
        }
        else
        {
            m_StateMachine.SwitchTo(ClientState.Browsing);
        }

        GameDebug.Log("Client initialized");

        return(true);
    }
Exemplo n.º 2
0
    public bool Init(string[] args)
    {
        // Set up statemachine for ServerGame
        m_StateMachine = new StateMachine <ServerState>();
        m_StateMachine.Add(ServerState.Idle, null, UpdateIdleState, null);
        m_StateMachine.Add(ServerState.Loading, null, UpdateLoadingState, null);
        m_StateMachine.Add(ServerState.Active, EnterActiveState, UpdateActiveState, LeaveActiveState);

        m_StateMachine.SwitchTo(ServerState.Idle);

#if USE_UNET
        m_NetworkTransport = new UNETTransport();
        if (!m_NetworkTransport.Init(serverPort.IntValue, 32))
        {
            return(false);
        }
        m_NetworkServer = new NetworkServer(m_NetworkTransport);

        m_ServerBroadcast = new UNETServerBroadcast(m_NetworkTransport.hostId, new UNETBroadcastConfig(), 7913);

        // TODO (petera) what if these (max clients and name) changes during play
        m_ServerBroadcast.gameInfo.maxPlayers = serverMaxClients.IntValue;
        if (serverServerName.Value.Length == 0)
        {
            serverServerName.Value = MakeServername();
        }
        m_ServerBroadcast.gameInfo.servername = serverServerName.Value;
        GameDebug.Log("Server name: '" + m_ServerBroadcast.gameInfo.servername + "'");

        m_ServerBroadcast.gameInfo.levelname = "-";
        m_ServerBroadcast.gameInfo.gamemode  = "-";
        m_ServerBroadcast.Start();
#else
        m_NetworkTransport = new SocketTransport(serverPort.IntValue);
        GameDebug.Log("Listening on " + string.Join(", ", NetworkUtils.GetLocalInterfaceAddresses()) + " on port " + serverPort.IntValue);
        m_NetworkServer = new NetworkServer(m_NetworkTransport);

        if (Game.game.clientFrontend != null)
        {
            var serverPanel = Game.game.clientFrontend.serverPanel;
            serverPanel.SetPanelActive(true);
            serverPanel.serverInfo.text += "Listening on:\n";
            foreach (var a in NetworkUtils.GetLocalInterfaceAddresses())
            {
                serverPanel.serverInfo.text += a + ":" + serverPort.IntValue + "\n";
            }
        }
#endif
        m_NetworkServer.UpdateClientInfo();
        m_NetworkServer.clientInfo.compressionModel = m_Model;

        m_ServerQueryProtocolServer = new SQP.SQPServer(serverSQPPort.IntValue);
        var info = m_ServerQueryProtocolServer.ServerInfoData;
        info.BuildId        = Game.game.buildId;
        info.Port           = (ushort)serverPort.IntValue;
        info.CurrentPlayers = 0;
        info.GameType       = "Deathmatch";
        info.Map            = "";
        info.MaxPlayers     = 32;
        info.ServerName     = MakeServername();

#if UNITY_EDITOR
        Game.game.levelManager.UnloadLevel();
        World.DisposeAllWorlds();
#endif
        m_GameWorld = new GameWorld("ServerWorld");

        m_NetworkStatistics = new NetworkStatisticsServer(m_NetworkServer);

        m_ChatSystem = new ChatSystemServer(m_Clients, m_NetworkServer);

        GameDebug.Log("Network server initialized");

        Console.AddCommand("load", CmdLoad, "Load a named scene", this.GetHashCode());
        Console.AddCommand("unload", CmdUnload, "Unload current scene", this.GetHashCode());
        Console.AddCommand("respawn", CmdRespawn, "Respawn character (usage : respawn playername|playerId)", this.GetHashCode());
        Console.AddCommand("servername", CmdSetServerName, "Set name of the server", this.GetHashCode());
        Console.AddCommand("beginnetworkprofile", CmdBeginNetworkProfile, "begins a network profile", this.GetHashCode());
        Console.AddCommand("endnetworkprofile", CmdEndNetworkProfile, "Ends a network profile and analyzes. [optional] filepath for model data", this.GetHashCode());
        Console.AddCommand("loadcompressionmodel", CmdLoadNetworkCompressionModel, "Loads a network compression model from a filepath", this.GetHashCode());
        Console.AddCommand("list", CmdList, "List clients", this.GetHashCode());

        CmdLoad(args);
        Game.SetMousePointerLock(false);

        m_ServerStartTime = Time.time;

        GameDebug.Log("Server initialized");
        Console.SetOpen(false);
        return(true);
    }