コード例 #1
0
        public ClientManager(
            NetClient netClient,
            ServerManager serverManager,
            PacketManager packetManager,
            UiManager uiManager,
            Settings.GameSettings gameSettings,
            ModSettings modSettings
            )
        {
            _netClient     = netClient;
            _serverManager = serverManager;
            _uiManager     = uiManager;
            _gameSettings  = gameSettings;
            _modSettings   = modSettings;

            _playerData = new Dictionary <ushort, ClientPlayerData>();

            _playerManager    = new PlayerManager(packetManager, gameSettings, _playerData);
            _animationManager = new AnimationManager(netClient, _playerManager, packetManager, gameSettings);
            _mapManager       = new MapManager(netClient, gameSettings);

            _entityManager = new EntityManager(netClient);

            new PauseManager(netClient).RegisterHooks();

            _commandManager = new ClientCommandManager();
            var eventAggregator = new EventAggregator();

            RegisterCommands();

            var clientApi = new ClientApi(this, _commandManager, uiManager, netClient, eventAggregator);

            _addonManager = new ClientAddonManager(clientApi);

            ModHooks.FinishedLoadingModsHook += _addonManager.LoadAddons;

            // Check if there is a valid authentication key and if not, generate a new one
            if (!AuthUtil.IsValidAuthKey(modSettings.AuthKey))
            {
                modSettings.AuthKey = AuthUtil.GenerateAuthKey();
            }

            // Then authorize the key on the locally hosted server
            serverManager.AuthorizeKey(modSettings.AuthKey);

            // Register packet handlers
            packetManager.RegisterClientPacketHandler <HelloClient>(ClientPacketId.HelloClient, OnHelloClient);
            packetManager.RegisterClientPacketHandler <ServerClientDisconnect>(ClientPacketId.ServerClientDisconnect,
                                                                               OnDisconnect);
            packetManager.RegisterClientPacketHandler <PlayerConnect>(ClientPacketId.PlayerConnect, OnPlayerConnect);
            packetManager.RegisterClientPacketHandler <ClientPlayerDisconnect>(ClientPacketId.PlayerDisconnect,
                                                                               OnPlayerDisconnect);
            packetManager.RegisterClientPacketHandler <ClientPlayerEnterScene>(ClientPacketId.PlayerEnterScene,
                                                                               OnPlayerEnterScene);
            packetManager.RegisterClientPacketHandler <ClientPlayerAlreadyInScene>(ClientPacketId.PlayerAlreadyInScene,
                                                                                   OnPlayerAlreadyInScene);
            packetManager.RegisterClientPacketHandler <GenericClientData>(ClientPacketId.PlayerLeaveScene,
                                                                          OnPlayerLeaveScene);
            packetManager.RegisterClientPacketHandler <PlayerUpdate>(ClientPacketId.PlayerUpdate, OnPlayerUpdate);
            packetManager.RegisterClientPacketHandler <EntityUpdate>(ClientPacketId.EntityUpdate, OnEntityUpdate);
            packetManager.RegisterClientPacketHandler <GameSettingsUpdate>(ClientPacketId.GameSettingsUpdated,
                                                                           OnGameSettingsUpdated);
            packetManager.RegisterClientPacketHandler <ChatMessage>(ClientPacketId.ChatMessage, OnChatMessage);

            // Register handlers for events from UI
            uiManager.ConnectInterface.ConnectButtonPressed     += Connect;
            uiManager.ConnectInterface.DisconnectButtonPressed  += () => Disconnect();
            uiManager.SettingsInterface.OnTeamRadioButtonChange += InternalChangeTeam;
            uiManager.SettingsInterface.OnSkinIdChange          += InternalChangeSkin;

            UiManager.InternalChatBox.ChatInputEvent += OnChatInput;

            netClient.ConnectEvent       += response => uiManager.OnSuccessfulConnect();
            netClient.ConnectFailedEvent += uiManager.OnFailedConnect;

            // Register the Hero Controller Start, which is when the local player spawns
            On.HeroController.Start += (orig, self) => {
                // Execute the original method
                orig(self);
                // If we are connect to a server, add a username to the player object
                if (netClient.IsConnected)
                {
                    _playerManager.AddNameToPlayer(
                        HeroController.instance.gameObject,
                        _username,
                        _playerManager.LocalPlayerTeam
                        );
                }
            };

            // Register handlers for scene change and player update
            UnityEngine.SceneManagement.SceneManager.activeSceneChanged += OnSceneChange;
            On.HeroController.Update += OnPlayerUpdate;

            // Register client connect and timeout handler
            netClient.ConnectEvent += OnClientConnect;
            netClient.TimeoutEvent += OnTimeout;

            // Register application quit handler
            ModHooks.ApplicationQuitHook += OnApplicationQuit;
        }
コード例 #2
0
        //private event Action ServerKnightChangeEvent;

        public ClientManager(
            NetworkManager networkManager,
            PlayerManager playerManager,
            AnimationManager animationManager,
            MapManager mapManager,
            Game.Settings.GameSettings gameSettings,
            PacketManager packetManager
            )
        {
            _netClient        = networkManager.GetNetClient();
            _playerManager    = playerManager;
            _animationManager = animationManager;
            _mapManager       = mapManager;
            _gameSettings     = gameSettings;

            _entityManager = new EntityManager(_netClient);

            new PauseManager(_netClient).RegisterHooks();

            _heartBeatReceiveStopwatch = new Stopwatch();

            // Register packet handlers
            packetManager.RegisterClientPacketHandler(ClientPacketId.ServerShutdown, OnServerShutdown);
            packetManager.RegisterClientPacketHandler <PlayerConnect>(ClientPacketId.PlayerConnect, OnPlayerConnect);
            packetManager.RegisterClientPacketHandler <ClientPlayerDisconnect>(ClientPacketId.PlayerDisconnect,
                                                                               OnPlayerDisconnect);
            packetManager.RegisterClientPacketHandler <ClientPlayerEnterScene>(ClientPacketId.PlayerEnterScene,
                                                                               OnPlayerEnterScene);
            packetManager.RegisterClientPacketHandler <ClientPlayerAlreadyInScene>(ClientPacketId.PlayerAlreadyInScene,
                                                                                   OnPlayerAlreadyInScene);
            packetManager.RegisterClientPacketHandler <GenericClientData>(ClientPacketId.PlayerLeaveScene,
                                                                          OnPlayerLeaveScene);
            packetManager.RegisterClientPacketHandler <PlayerUpdate>(ClientPacketId.PlayerUpdate, OnPlayerUpdate);
            packetManager.RegisterClientPacketHandler <EntityUpdate>(ClientPacketId.EntityUpdate, OnEntityUpdate);
            packetManager.RegisterClientPacketHandler <GameSettingsUpdate>(ClientPacketId.GameSettingsUpdated,
                                                                           OnGameSettingsUpdated);

            // Register the Hero Controller Start, which is when the local player spawns
            On.HeroController.Start += (orig, self) => {
                // Execute the original method
                orig(self);
                // If we are connect to a server, add a username to the player object
                if (networkManager.GetNetClient().IsConnected)
                {
                    _playerManager.AddNameToPlayer(HeroController.instance.gameObject, _username,
                                                   _playerManager.LocalPlayerTeam);
                }
            };

            // Register handlers for scene change and player update
            UnityEngine.SceneManagement.SceneManager.activeSceneChanged += OnSceneChange;
            On.HeroController.Update += OnPlayerUpdate;

            // Register client connect handler
            _netClient.RegisterOnConnect(OnClientConnect);

            _netClient.RegisterOnHeartBeat(OnHeartBeat);

            // Register application quit handler
            ModHooks.Instance.ApplicationQuitHook += OnApplicationQuit;
        }