public static void EnemyPosition(byte fromClient, Packet packet)
        {
            //if (!Server.clients[fromClient].player.isHost) return;
            byte toClient = packet.ReadByte();

            if (Server.clients[fromClient].player.activeScene == Server.clients[toClient].player.activeScene)
            {
                return;
            }

            Vector3 position = packet.ReadVector3();
            int     id       = packet.ReadInt();

            ServerSend.EnemyPosition(toClient, position, id);
        }
        public static void EnemyScale(byte fromClient, Packet packet)
        {
            //if (!Server.clients[fromClient].player.isHost) return;
            byte toClient = packet.ReadByte();

            if (Server.clients[fromClient].player.activeScene != Server.clients[toClient].player.activeScene)
            {
                return;
            }

            Vector3 scale = packet.ReadVector3();
            int     id    = packet.ReadInt();

            ServerSend.EnemyScale(toClient, scale, id);
        }
        public static void SyncEnemy(byte fromClient, Packet packet)
        {
            //if (!Server.clients[fromClient].player.isHost) return;
            byte toClient = packet.ReadByte();

            if ((Server.clients[fromClient].player.activeScene == Server.clients[toClient].player.activeScene))
            {
                return;
            }

            string goName = packet.ReadString();
            int    id     = packet.ReadInt();

            ServerSend.SyncEnemy(toClient, goName, id);
        }
        private void OnApplicationQuit()
        {
            for (byte id = 1; id <= MaxPlayers; id++)
            {
                if (Server.clients[id].player != null)
                {
                    Log($"Calling ServerSend.DisconnectPlayer({id})");
                    ServerSend.DisconnectPlayer(id);
                }
            }

            Log("Disconnected All Players");

            Server.Stop();
        }
        public static void EnemyAnimation(byte fromClient, Packet packet)
        {
            //if (!Server.clients[fromClient].player.isHost) return;
            byte toClient = packet.ReadByte();

            if (Server.clients[fromClient].player.activeScene != Server.clients[toClient].player.activeScene)
            {
                return;
            }

            string animation = packet.ReadString();
            int    id        = packet.ReadInt();

            ServerSend.EnemyAnimation(toClient, animation, id);
        }
Пример #6
0
        /// <summary>Disconnects the client and stops all network traffic.</summary>
        public void Disconnect()
        {
            Log($"{tcp.socket.Client.RemoteEndPoint} has disconnected from the server.");

            ThreadManager.ExecuteOnMainThread(() =>
            {
                UnityEngine.Object.Destroy(player.gameObject);
                player = null;
            });

            tcp.Disconnect();
            udp.Disconnect();

            ServerSend.PlayerDisconnected(id);
            Log("Disconnected on Server Side.");
        }
Пример #7
0
            /// <summary>Initializes the newly connected client's TCP-related info.</summary>
            /// <param name="socket">The TcpClient instance of the newly connected client.</param>
            public void Connect(TcpClient socket)
            {
                this.socket = socket;
                this.socket.ReceiveBufferSize = dataBufferSize;
                this.socket.SendBufferSize    = dataBufferSize;

                stream = this.socket.GetStream();

                receivedData  = new Packet();
                receiveBuffer = new byte[dataBufferSize];

                stream.BeginRead(receiveBuffer, 0, dataBufferSize, ReceiveCallback, null);

                string welcomeMessage = $"Welcome to the server! Your player ID is : {id}.";

                ServerSend.Welcome(id, welcomeMessage);
            }
Пример #8
0
        private void AnimationEventDelegate(tk2dSpriteAnimator anim, tk2dSpriteAnimationClip clip, int frameNum)
        {
            if (clip.wrapMode == tk2dSpriteAnimationClip.WrapMode.Loop && clip.name != _storedClip ||
                clip.wrapMode == tk2dSpriteAnimationClip.WrapMode.LoopSection && clip.name != _storedClip ||
                clip.wrapMode == tk2dSpriteAnimationClip.WrapMode.Once)
            {
                _storedClip = clip.name;
                tk2dSpriteAnimationFrame frame = clip.GetFrame(frameNum);

                string clipName = frame.eventInfo;
                //ServerSend.EnemyAnimation(clipName);
                foreach (byte player in playerIds)
                {
                    ServerSend.EnemyAnimation(player, clipName, enemyId);
                }
            }
        }
        public static void SceneChanged(byte fromClient, Packet packet)
        {
            string sceneName = packet.ReadString();
            bool   host      = Server.clients[fromClient].player.CurrentRoomSyncHost;
            string oldscene  = Server.clients[fromClient].player.activeScene;

            Server.clients[fromClient].player.activeScene = sceneName;
            bool first       = true;
            bool otherplayer = false;

            for (byte i = 1; i <= Server.MaxPlayers; i++)
            {
                if (Server.clients[i].player != null && i != fromClient)
                {
                    if (Server.clients[i].player.activeScene == sceneName)
                    {
                        Log("Same Scene, Spawning Players Subsequent Pass");
                        Player iPlayer    = Server.clients[i].player;
                        Player fromPlayer = Server.clients[fromClient].player;
                        ServerSend.SpawnPlayer(fromClient, iPlayer);
                        ServerSend.SpawnPlayer(i, fromPlayer);
                        otherplayer = true;
                    }
                    else
                    {
                        if (first && Server.clients[i].player.activeScene == oldscene && host)
                        {
                            Log("Different Scene, Destroying Players And Changing room host");
                            ServerSend.DestroyPlayer(i, fromClient, true);
                            first = false;
                        }
                        else
                        {
                            Log("Different Scene, Destroying Players");
                            ServerSend.DestroyPlayer(i, fromClient, false);
                        }
                        //ServerSend.DestroyPlayer(fromClient, i);
                    }
                }
            }
            if (!otherplayer && !host)
            {
                ServerSend.StartEnemySync(fromClient, true);
            }
            Server.clients[fromClient].player.CurrentRoomSyncHost = !otherplayer;
        }
Пример #10
0
        /// <summary>Initial scene load when joining the server for the first time.</summary>
        /// <param name="fromClient">The ID of the client who joined the server</param>
        /// <param name="sceneName">The name of the client's active scene when joining the server</param>
        public static void SceneChanged(byte fromClient, string sceneName)
        {
            Server.clients[fromClient].player.activeScene = sceneName;

            for (byte i = 1; i <= Server.MaxPlayers; i++)
            {
                if (Server.clients[i].player != null && i != fromClient)
                {
                    if (Server.clients[i].player.activeScene == sceneName)
                    {
                        Log("Same Scene, Spawning Players First Pass");
                        ServerSend.SpawnPlayer(fromClient, Server.clients[i].player);
                        ServerSend.SpawnPlayer(Server.clients[i].player.id, Server.clients[fromClient].player);
                        Player iPlayer    = Server.clients[i].player;
                        Player fromPlayer = Server.clients[fromClient].player;
                        ServerSend.SpawnPlayer(fromClient, iPlayer);
                        ServerSend.SpawnPlayer(i, fromPlayer);
                    }
                }
            }
        }
Пример #11
0
        public static void HandleTextureRequest(byte fromClient, Packet packet)
        {
            if (!ServerSettings.CustomKnightIntegration)
            {
                return;
            }

            byte[] hash = packet.ReadBytes(20);

            Player player = Server.clients[fromClient].player;

            if (!player.textureHashes.Contains(hash))
            {
                player.textureHashes.Add(hash);
            }

            if (MultiplayerServer.textureCache.ContainsKey(hash))
            {
                byte[] texture = File.ReadAllBytes(MultiplayerServer.textureCache[hash]);
                ServerSend.SendTexture(fromClient, hash, texture);
            }
        }
Пример #12
0
        public static void PlayerDataChange(byte fromClient, Packet packet)
        {
            PlayerDataTypes pdtype   = (PlayerDataTypes)packet.ReadInt();
            string          variable = packet.ReadString();

            Log(Server.clients[fromClient].player.username + " " + pdtype.ToString() + " " + variable);
            object obj = null;

            switch (pdtype)
            {
            case PlayerDataTypes.Bool:
                obj = packet.ReadBool();
                break;

            case PlayerDataTypes.Float:
                obj = packet.ReadFloat();
                break;

            case PlayerDataTypes.Int:
                obj = packet.ReadInt();
                break;

            case PlayerDataTypes.Other:
                obj = packet.ReadString();
                break;

            case PlayerDataTypes.String:
                obj = packet.ReadString();
                break;

            case PlayerDataTypes.Vector3:
                obj = packet.ReadVector3();
                break;
            }

            ServerSend.UpdatePlayerData(fromClient, pdtype, variable, obj);
        }
Пример #13
0
        private void FixedUpdate()
        {
            Vector3 pos = transform.position;

            if (pos != _storedPosition)
            {
                foreach (byte player in playerIds)
                {
                    ServerSend.EnemyPosition(player, pos, enemyId);
                }
                _storedPosition = pos;
            }

            Vector3 scale = transform.localScale;

            if (scale != _storedScale)
            {
                foreach (byte player in playerIds)
                {
                    ServerSend.EnemyScale(player, scale, enemyId);
                }
                _storedScale = scale;
            }
        }
Пример #14
0
        public static void WelcomeReceived(byte fromClient, Packet packet)
        {
            byte    clientIdCheck = packet.ReadByte();
            string  username      = packet.ReadString();
            bool    isHost        = packet.ReadBool();
            string  currentClip   = packet.ReadString();
            string  activeScene   = packet.ReadString();
            Vector3 position      = packet.ReadVector3();
            Vector3 scale         = packet.ReadVector3();
            int     health        = packet.ReadInt();
            int     maxHealth     = packet.ReadInt();
            int     healthBlue    = packet.ReadInt();

            List <bool> charmsData = new List <bool>();

            for (int charmNum = 1; charmNum <= 40; charmNum++)
            {
                charmsData.Add(packet.ReadBool());
            }
            int team = packet.ReadInt();

            if (isHost)
            {
                foreach (Client client in Server.clients.Values)
                {
                    if (client.player == null)
                    {
                        continue;
                    }
                    if (client.player.isHost)
                    {
                        Log("Another player tried to connect with a server DLL active! Disconnecting that player.");
                        ServerSend.DisconnectPlayer(fromClient);

                        return;
                    }
                }
            }

            Server.clients[fromClient].SendIntoGame(username, position, scale, currentClip, health, maxHealth, healthBlue, charmsData, isHost, team);

            /*for (int i = 0; i < Enum.GetNames(typeof(TextureType)).Length; i++)
             * {
             *  byte[] hash = packet.ReadBytes(20);
             *
             *  Player player = Server.clients[fromClient].player;
             *  if (!player.textureHashes.Contains(hash))
             *  {
             *      player.textureHashes.Add(hash);
             *  }
             *
             *  if (!MultiplayerServer.textureCache.ContainsKey(hash))
             *  {
             *      ServerSend.RequestTexture(fromClient, hash);
             *  }
             * }*/

            SceneChanged(fromClient, activeScene);

            Log($"{username} connected successfully and is now player {fromClient}.");
            if (fromClient != clientIdCheck)
            {
                Log($"Player \"{username}\" (ID: {fromClient}) has assumed the wrong client ID ({clientIdCheck}.");
            }
        }
Пример #15
0
        public static void PinPosition(byte fromclient, Packet packet)
        {
            Vector3 position = packet.ReadVector3();

            ServerSend.PinPosition(fromclient, position);
        }
Пример #16
0
        public static void StartPinSync(byte fromClient, Packet packet)
        {
            bool enabled = packet.ReadBool();

            ServerSend.StartPinSync(fromClient, enabled);
        }
Пример #17
0
        public void SetScale(Vector3 scale)
        {
            this.scale = scale;

            ServerSend.PlayerScale(this);
        }
Пример #18
0
        public static void WelcomeReceived(byte fromClient, Packet packet)
        {
            byte    clientIdCheck = packet.ReadByte();
            string  username      = packet.ReadString();
            bool    isHost        = packet.ReadBool();
            string  currentClip   = packet.ReadString();
            string  activeScene   = packet.ReadString();
            Vector3 position      = packet.ReadVector3();
            Vector3 scale         = packet.ReadVector3();
            int     health        = packet.ReadInt();
            int     maxHealth     = packet.ReadInt();
            int     healthBlue    = packet.ReadInt();

            List <bool> charmsData = new List <bool>();

            for (int charmNum = 1; charmNum <= 40; charmNum++)
            {
                charmsData.Add(packet.ReadBool());
            }
            int modamount = packet.ReadInt();

            for (int modNum = 1; modNum <= modamount; modNum++)
            {
                Log(username + " Has mod " + packet.ReadString() + "  " + modNum + "/" + modamount);
            }
            Log(username + " Has modding api ver " + packet.ReadString());
            int     team        = packet.ReadInt();
            string  chat        = packet.ReadString();
            bool    pinenabled  = packet.ReadBool();
            Vector3 pinposition = packet.ReadVector3();

            if (isHost)
            {
                foreach (Client client in Server.clients.Values)
                {
                    if (client.player == null)
                    {
                        continue;
                    }
                    if (client.player.isHost)
                    {
                        Log("Another player tried to connect with a server DLL active! Disconnecting that player.");
                        ServerSend.DisconnectPlayer(fromClient);

                        return;
                    }
                }
            }

            Server.clients[fromClient].SendIntoGame(username, position, scale, currentClip, health, maxHealth, healthBlue, charmsData, isHost, team, chat, pinenabled, pinposition);
            try{
                foreach (Client client in Server.clients.Values)
                {
                    ServerSend.CreatePin(client.id, fromClient);
                }
                ServerSend.CreatePin(fromClient);
            }
            catch (Exception e) {
                Log("Server could not create pin :" + e.Message + " Object: " + e.Source);
            }

            /*for (int i = 0; i < Enum.GetNames(typeof(TextureType)).Length; i++)
             * {
             *  byte[] hash = packet.ReadBytes(20);
             *
             *  Player player = Server.clients[fromClient].player;
             *  if (!player.textureHashes.Contains(hash))
             *  {
             *      player.textureHashes.Add(hash);
             *  }
             *
             *  if (!MultiplayerServer.textureCache.ContainsKey(hash))
             *  {
             *      ServerSend.RequestTexture(fromClient, hash);
             *  }
             * }*/

            bool otherplayer = false;

            if (Server.clients.Count > 1)
            {
                foreach (Client c in Server.clients.Values)
                {
                    if (c.player == null)
                    {
                        continue;
                    }
                    if (c.player.activeScene == activeScene)
                    {
                        otherplayer = true;
                    }
                }
            }
            SceneChanged(fromClient, activeScene, otherplayer);

            Log($"{username} connected successfully and is now player {fromClient}.");
            if (fromClient != clientIdCheck)
            {
                Log($"Player \"{username}\" (ID: {fromClient}) has assumed the wrong client ID ({clientIdCheck}.");
            }
        }
Пример #19
0
        public static void LoadServerScene(byte fromClient, Packet packet)
        {
            string sceneName = packet.ReadString();

            GameManager.instance.StartCoroutine(LoadSceneRoutine());

            IEnumerator LoadSceneRoutine()
            {
                Scene scene = USceneManager.GetSceneByName(sceneName);

                if (!scene.isLoaded)
                {
                    AsyncOperation operation = USceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);

                    yield return(new WaitWhile(() => !operation.isDone));
                }

                Scene loadedScene = USceneManager.GetSceneByName(sceneName);

                GameObject[] rootGOs = loadedScene.GetRootGameObjects();
                if (rootGOs != null)
                {
                    List <GameObject> enemies = new List <GameObject>();
                    foreach (GameObject rootGO in rootGOs)
                    {
                        List <GameObject> childEnemies = rootGO.FindChildEnemies();

                        foreach (GameObject enemy in childEnemies)
                        {
                            enemies.Add(enemy);
                        }
                    }

                    foreach (GameObject enemy in enemies)
                    {
                        var tracker = enemy.GetComponent <EnemyTracker>();
                        if (tracker)
                        {
                            tracker.playerIds.Add(fromClient);
                        }
                        else
                        {
                            tracker = enemy.AddComponent <EnemyTracker>();
                            ServerSend.SyncEnemy(fromClient, enemy.name, tracker.enemyId);
                            tracker.playerIds.Add(fromClient);
                        }

                        bool foundUnusedKey = false;
                        for (int i = 0; i <= Server.Enemies.Count; i++)
                        {
                            if (!Server.Enemies.Keys.Contains(i))
                            {
                                tracker.enemyId = i;
                                Server.Enemies.Add(i, tracker);
                                foundUnusedKey = true;
                                break;
                            }
                        }

                        if (!foundUnusedKey)
                        {
                            for (int i = Server.Enemies.Count + 1; i <= 99999; i++)
                            {
                                if (!Server.Enemies.Keys.Contains(i))
                                {
                                    tracker.enemyId = i;
                                    Server.Enemies.Add(i, tracker);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #20
0
        public void SetPosition(Vector3 position)
        {
            this.position = position;

            ServerSend.PlayerPosition(this);
        }
Пример #21
0
 public static void SendPD(byte fromClient, Packet packet)
 {
     Log("Got PD Request From " + Server.clients[fromClient].player.username);
     ServerSend.SendPlayerData(fromClient);
 }
Пример #22
0
        public void SetAnimation(string animation)
        {
            this.animation = animation;

            ServerSend.PlayerAnimation(this);
        }
Пример #23
0
 private static void HandleTextureUpToDate(byte fromClient, Packet packet, int serverPacketId, string texName)
 {
     byte[] texBytes = Server.clients[fromClient].player.texBytes[texName];
     ServerSend.SendTexture(fromClient, texBytes, serverPacketId);
 }