Exemple #1
0
        public PlayerManager(
            PacketManager packetManager,
            Settings.GameSettings gameSettings,
            Dictionary <ushort, ClientPlayerData> playerData
            )
        {
            _gameSettings = gameSettings;

            _skinManager = new SkinManager();

            _playerData = playerData;

            _inactivePlayers = new Queue <GameObject>();
            _activePlayers   = new Dictionary <ushort, GameObject>();

            On.HeroController.Start += (orig, self) => {
                orig(self);

                if (_playerContainerPrefab == null)
                {
                    CreatePlayerPool();
                }
            };

            // Register packet handlers
            packetManager.RegisterClientPacketHandler <ClientPlayerTeamUpdate>(ClientPacketId.PlayerTeamUpdate,
                                                                               OnPlayerTeamUpdate);
            packetManager.RegisterClientPacketHandler <ClientPlayerSkinUpdate>(ClientPacketId.PlayerSkinUpdate,
                                                                               OnPlayerSkinUpdate);
        }
Exemple #2
0
        public ServerManager(NetworkManager networkManager, Settings.GameSettings gameSettings, PacketManager packetManager, ServerKnightsManager serverKnightsManager)
        {
            _netServer            = networkManager.GetNetServer();
            _gameSettings         = gameSettings;
            _serverKnightsSession = serverKnightsManager.loadSession();
            _playerData           = new ConcurrentDictionary <ushort, ServerPlayerData>();

            // Register packet handlers
            packetManager.RegisterServerPacketHandler <HelloServer>(ServerPacketId.HelloServer, OnHelloServer);
            packetManager.RegisterServerPacketHandler <ServerPlayerEnterScene>(ServerPacketId.PlayerEnterScene, OnClientEnterScene);
            packetManager.RegisterServerPacketHandler(ServerPacketId.PlayerLeaveScene, OnClientLeaveScene);
            packetManager.RegisterServerPacketHandler <PlayerUpdate>(ServerPacketId.PlayerUpdate, OnPlayerUpdate);
            packetManager.RegisterServerPacketHandler <EntityUpdate>(ServerPacketId.EntityUpdate, OnEntityUpdate);
            packetManager.RegisterServerPacketHandler(ServerPacketId.PlayerDisconnect, OnPlayerDisconnect);
            packetManager.RegisterServerPacketHandler(ServerPacketId.PlayerDeath, OnPlayerDeath);
            packetManager.RegisterServerPacketHandler <ServerPlayerTeamUpdate>(ServerPacketId.PlayerTeamUpdate, OnPlayerTeamUpdate);
            packetManager.RegisterServerPacketHandler <ServerServerKnightUpdate>(ServerPacketId.ServerKnightUpdate, OnServerKnightUpdate);

            // Register a heartbeat handler
            _netServer.RegisterOnClientHeartBeat(OnClientHeartBeat);

            // Register server shutdown handler
            _netServer.RegisterOnShutdown(OnServerShutdown);

            // Register application quit handler
            ModHooks.Instance.ApplicationQuitHook += OnApplicationQuit;
        }
Exemple #3
0
        /// <summary>
        /// Constructs the server manager.
        /// </summary>
        /// <param name="netServer">The net server instance.</param>
        /// <param name="gameSettings">The server game settings.</param>
        /// <param name="packetManager">The packet manager instance.</param>
        protected ServerManager(
            NetServer netServer,
            Settings.GameSettings gameSettings,
            PacketManager packetManager
            )
        {
            _netServer   = netServer;
            GameSettings = gameSettings;
            _playerData  = new ConcurrentDictionary <ushort, ServerPlayerData>();

            CommandManager = new ServerCommandManager();
            var eventAggregator = new EventAggregator();

            var serverApi = new ServerApi(this, CommandManager, _netServer, eventAggregator);

            AddonManager = new ServerAddonManager(serverApi);

            // Load the lists
            _whiteList      = WhiteList.LoadFromFile();
            _authorizedList = AuthKeyList.LoadFromFile(AuthorizedFileName);
            _banList        = BanList.LoadFromFile();

            // Register packet handlers
            packetManager.RegisterServerPacketHandler <HelloServer>(ServerPacketId.HelloServer, OnHelloServer);
            packetManager.RegisterServerPacketHandler <ServerPlayerEnterScene>(ServerPacketId.PlayerEnterScene,
                                                                               OnClientEnterScene);
            packetManager.RegisterServerPacketHandler(ServerPacketId.PlayerLeaveScene, OnClientLeaveScene);
            packetManager.RegisterServerPacketHandler <PlayerUpdate>(ServerPacketId.PlayerUpdate, OnPlayerUpdate);
            packetManager.RegisterServerPacketHandler <EntityUpdate>(ServerPacketId.EntityUpdate, OnEntityUpdate);
            packetManager.RegisterServerPacketHandler(ServerPacketId.PlayerDisconnect, OnPlayerDisconnect);
            packetManager.RegisterServerPacketHandler(ServerPacketId.PlayerDeath, OnPlayerDeath);
            packetManager.RegisterServerPacketHandler <ServerPlayerTeamUpdate>(ServerPacketId.PlayerTeamUpdate,
                                                                               OnPlayerTeamUpdate);
            packetManager.RegisterServerPacketHandler <ServerPlayerSkinUpdate>(ServerPacketId.PlayerSkinUpdate,
                                                                               OnPlayerSkinUpdate);
            packetManager.RegisterServerPacketHandler <ChatMessage>(ServerPacketId.ChatMessage, OnChatMessage);

            // Register a timeout handler
            _netServer.ClientTimeoutEvent += OnClientTimeout;

            // Register server shutdown handler
            _netServer.ShutdownEvent += OnServerShutdown;

            // Register a handler for when a client wants to login
            _netServer.LoginRequestEvent += OnLoginRequest;
        }
Exemple #4
0
        public ModServerManager(
            NetServer netServer,
            Settings.GameSettings gameSettings,
            PacketManager packetManager,
            UiManager uiManager
            ) : base(netServer, gameSettings, packetManager)
        {
            // Start addon loading once all mods have finished loading
            ModHooks.FinishedLoadingModsHook += AddonManager.LoadAddons;

            // Register handlers for UI events
            uiManager.ConnectInterface.StartHostButtonPressed += Start;
            uiManager.ConnectInterface.StopHostButtonPressed  += Stop;

            // Register application quit handler
            ModHooks.ApplicationQuitHook += Stop;
        }
Exemple #5
0
        public MapManager(NetworkManager networkManager, Settings.GameSettings gameSettings)
        {
            _netClient    = networkManager.GetNetClient();
            _gameSettings = gameSettings;

            _mapIcons = new ConcurrentDictionary <int, GameObject>();

            _netClient.RegisterOnDisconnect(OnDisconnect);

            // Register a hero controller update callback, so we can update the map icon position
            On.HeroController.Update += HeroControllerOnUpdate;

            // Register when the player closes their map, so we can hide the icons
            On.GameMap.CloseQuickMap += OnCloseQuickMap;

            // Register when the player opens their map, which is when the compass position is calculated
            On.GameMap.PositionCompass += OnPositionCompass;
        }
Exemple #6
0
        public PlayerManager(NetworkManager networkManager, Settings.GameSettings gameSettings, ModSettings settings, ServerKnightsManager serverKnightsManager)
        {
            _gameSettings = gameSettings;

            _playerData           = new Dictionary <ushort, ClientPlayerData>();
            _serverKnightsManager = serverKnightsManager;
            // Create the player prefab, used to instantiate player objects
            _playerPrefab = new GameObject(
                "PlayerPrefab",
                typeof(BoxCollider2D),
                typeof(DamageHero),
                typeof(EnemyHitEffectsUninfected),
                typeof(MeshFilter),
                typeof(MeshRenderer),
                typeof(NonBouncer),
                typeof(SpriteFlash),
                typeof(tk2dSprite),
                typeof(tk2dSpriteAnimator),
                typeof(CoroutineCancelComponent)
                )
            {
                layer = 9
            };

            // Add some extra gameObject related to animation effects
            new GameObject("Attacks")
            {
                layer = 9
            }.transform.SetParent(_playerPrefab.transform);
            new GameObject("Effects")
            {
                layer = 9
            }.transform.SetParent(_playerPrefab.transform);
            new GameObject("Spells")
            {
                layer = 9
            }.transform.SetParent(_playerPrefab.transform);

            _playerPrefab.SetActive(false);
            Object.DontDestroyOnLoad(_playerPrefab);
        }
Exemple #7
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;
        }
Exemple #8
0
        public ClientManager(NetworkManager networkManager, PlayerManager playerManager,
                             AnimationManager animationManager, MapManager mapManager, Settings.GameSettings gameSettings,
                             PacketManager packetManager)
        {
            _netClient        = networkManager.GetNetClient();
            _playerManager    = playerManager;
            _animationManager = animationManager;
            _mapManager       = mapManager;
            _gameSettings     = gameSettings;

            _entityManager = new EntityManager(_netClient);

            _heartBeatReceiveStopwatch = new Stopwatch();

            // Register packet handlers
            packetManager.RegisterClientPacketHandler <ServerShutdownPacket>(PacketId.ServerShutdown, OnServerShutdown);
            packetManager.RegisterClientPacketHandler <ClientPlayerConnectPacket>(PacketId.PlayerConnect, OnPlayerConnect);
            packetManager.RegisterClientPacketHandler <ClientPlayerDisconnectPacket>(PacketId.PlayerDisconnect, OnPlayerDisconnect);
            packetManager.RegisterClientPacketHandler <ClientAlreadyInScenePacket>(PacketId.AlreadyInScene, OnAlreadyInScene);
            packetManager.RegisterClientPacketHandler <ClientPlayerEnterScenePacket>(PacketId.PlayerEnterScene,
                                                                                     OnPlayerEnterScene);
            packetManager.RegisterClientPacketHandler <ClientPlayerLeaveScenePacket>(PacketId.PlayerLeaveScene,
                                                                                     OnPlayerLeaveScene);
            packetManager.RegisterClientPacketHandler <ClientUpdatePacket>(
                PacketId.PlayerUpdate, OnUpdatePacket);
            packetManager.RegisterClientPacketHandler <ClientPlayerTeamUpdatePacket>(PacketId.PlayerTeamUpdate, OnPlayerTeamUpdate);
            packetManager.RegisterClientPacketHandler <GameSettingsUpdatePacket>(PacketId.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);
                }
            };
            networkManager.GetNetClient().RegisterOnConnect(() => {
                // We should only be able to connect during a gameplay scene,
                // which is when the player is spawned already, so we can add the username
                _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);

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

            // Prevent changing the timescale if the client is connected to ensure synchronisation between clients
            On.GameManager.SetTimeScale_float += (orig, self, scale) => {
                if (!_netClient.IsConnected)
                {
                    orig(self, scale);
                }
                else
                {
                    // Always put the time scale to 1.0, thus never allowing the game to change speed
                    // This is to prevent desyncs in multiplayer
                    orig(self, 1.0f);
                }
            };
            // Register pause callback to make sure the player doesn't keep dashing or moving
            On.HeroController.Pause += (orig, self) => {
                if (!_netClient.IsConnected)
                {
                    orig(self);
                    return;
                }

                // We simply call the private ResetInput method to prevent the knight from continuing movement
                // while the game is paused
                typeof(HeroController).InvokeMember(
                    "ResetInput",
                    BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod,
                    null,
                    HeroController.instance,
                    null
                    );
            };

            // To make sure that if we are paused, and we enter a screen transition,
            // we still go through it. So we unpause first, then execute the original method
            On.TransitionPoint.OnTriggerEnter2D += (orig, self, obj) => {
                // Unpause if paused
                if (UIManager.instance != null)
                {
                    if (UIManager.instance.uiState.Equals(UIState.PAUSED))
                    {
                        UIManager.instance.TogglePauseGame();
                    }
                }

                // Execute original method
                orig(self, obj);
            };
        }
Exemple #9
0
 public SettingsCommand(ServerManager serverManager, Settings.GameSettings gameSettings)
 {
     _serverManager = serverManager;
     GameSettings   = gameSettings;
 }