Пример #1
0
        /// <summary>
        /// Starts all members.
        /// </summary>
        public void Start()
        {
            databaseManager.Start();
            scheduler.Start();
            packetProcessor.Start();

            MySqlHandle mySqlHandle = databaseManager.GetMySqlHandle();

            try
            {
                mySqlHandle.Open();

                List <long> takenObjectIds = new List <long>();

                mySqlHandle.CharactersGetCharacterIds(takenObjectIds);

                objectIdGenerator = new ObjectIdGenerator(takenObjectIds);

                mySqlHandle.Close();
            }
            catch (Exception exc)
            {
                objectIdGenerator = new ObjectIdGenerator();

                LogDatabaseError(exc);
            }

            mySqlHandle.Free();

            eventManager.Publish(new ServerStartEvent());
        }
Пример #2
0
        public static void SaveCharacter(Session session)
        {
            if (session.ActiveCharacterInfo != null)
            {
                if (session.ActiveCharacterReplica != null)
                {
                    ControllablePhysicsComponent controllablePhysicsComponent = (ControllablePhysicsComponent)session.ActiveCharacterReplica.GetComponent(ReplicaComponentId.ControllablePhysics);

                    session.ActiveCharacterInfo.Position = controllablePhysicsComponent.Position;
                    session.ActiveCharacterInfo.Rotation = controllablePhysicsComponent.Rotation;

                    Server.Instance.GetReplicaManager(session.ActiveCharacterInfo.ZoneId).RemovePlayer(session);
                }

                CharacterInfo characterInfo = session.ActiveCharacterInfo;

                Server.Instance.Scheduler.RunTaskAsync(

                    () =>
                {
                    MySqlHandle mySqlHandle = Server.Instance.DatabaseManager.GetMySqlHandle();

                    try
                    {
                        mySqlHandle.Open();
                        mySqlHandle.CharactersSetCharacterInfo(characterInfo);
                        mySqlHandle.Close();
                    }
                    catch (Exception exc)
                    {
                        Server.Instance.LogDatabaseError(exc);
                    }

                    mySqlHandle.Free();
                }

                    );
            }
        }
Пример #3
0
        public void OnCharacterListRequest(CharacterListRequestEvent e)
        {
            Server.Instance.Scheduler.RunTaskAsync(

                () =>
            {
                ClientCharacterListResponsePacket response = new ClientCharacterListResponsePacket();

                MySqlHandle mySqlHandle = Server.Instance.DatabaseManager.GetMySqlHandle();

                try
                {
                    mySqlHandle.Open();

                    List <CharacterInfo> characterInfos = mySqlHandle.CharactersGetCharacterInfos(e.Session.ActiveAccountInfo.AccountId);

                    foreach (CharacterInfo characterInfo in characterInfos)
                    {
                        ClientCharacterListResponsePacket.CharacterData characterData = new ClientCharacterListResponsePacket.CharacterData();

                        characterData.CharacterInfo = characterInfo;

                        response.CharacterDataList.Add(characterData);
                    }

                    mySqlHandle.Close();
                }
                catch (Exception exc)
                {
                    Server.Instance.LogDatabaseError(exc);
                }

                mySqlHandle.Free();

                Server.Instance.SendGamePacket(response, ClientPacketId.MSG_CLIENT_CHARACTER_LIST_RESPONSE, e.Session.Address, e.Session.Port);
            }

                );
        }
        public void OnWorldCharacterDeleteRequestReceive(GamePacketReceiveEvent e)
        {
            if (e.Packet is WorldCharacterDeleteRequestPacket)
            {
                WorldCharacterDeleteRequestPacket request = (WorldCharacterDeleteRequestPacket)e.Packet;

                Session session = Server.Instance.SessionManager.GetSession(e.SourceAddress, e.SourcePort);

                if (session != null)
                {
                    Server.Instance.Scheduler.RunTaskAsync(

                        () =>
                    {
                        long characterId        = request.CharacterId;
                        string name             = String.Empty;
                        bool isOwnedByRequester = false;

                        MySqlHandle mySqlHandle = Server.Instance.DatabaseManager.GetMySqlHandle();

                        try
                        {
                            mySqlHandle.Open();

                            List <CharacterInfo> characterInfos = mySqlHandle.CharactersGetCharacterInfos(session.ActiveAccountInfo.AccountId);

                            for (int i = 0; i < characterInfos.Count && !isOwnedByRequester; i++)
                            {
                                if (characterInfos[i].CharacterId == characterId)
                                {
                                    isOwnedByRequester = true;
                                    name = characterInfos[i].Name;

                                    mySqlHandle.CharactersDeleteCharacter(characterId);
                                }
                            }

                            mySqlHandle.Close();
                        }
                        catch (Exception exc)
                        {
                            Server.Instance.LogDatabaseError(exc);
                        }

                        mySqlHandle.Free();

                        Server.Instance.Logger.Log("A client requested to delete a character. address=" + e.SourceAddress + " port=" + e.SourcePort + " name=" + name + " isOwnedByRequester=" + isOwnedByRequester);

                        if (!isOwnedByRequester)
                        {
                            GeneralDisconnectNotifyPacket dcPacket = new GeneralDisconnectNotifyPacket();

                            dcPacket.DisconnectReason = DisconnectReason.CharacterNotFound;

                            Server.Instance.SendGamePacket(dcPacket, GeneralPacketId.MSG_SERVER_DISCONNECT_NOTIFY, e.SourceAddress, e.SourcePort);
                        }
                    }

                        );
                }
            }
        }
        public void OnWorldCharacterRenameRequestPacketReceive(GamePacketReceiveEvent e)
        {
            if (e.Packet is WorldCharacterRenameRequestPacket)
            {
                WorldCharacterRenameRequestPacket request = (WorldCharacterRenameRequestPacket)e.Packet;

                Session session = Server.Instance.SessionManager.GetSession(e.SourceAddress, e.SourcePort);

                if (session != null)
                {
                    Server.Instance.Scheduler.RunTaskAsync(

                        () =>
                    {
                        ClientCharacterRenameResponsePacket response = new ClientCharacterRenameResponsePacket();

                        response.CharacterRenameResult = CharacterRenameResult.DenyInvalidCustomName;

                        string oldName              = String.Empty;
                        string newName              = request.NewName;
                        long characterId            = request.CharacterId;
                        CharacterInfo characterInfo = null;

                        MySqlHandle mySqlHandle = Server.Instance.DatabaseManager.GetMySqlHandle();

                        try
                        {
                            mySqlHandle.Open();

                            List <CharacterInfo> characterInfos = mySqlHandle.CharactersGetCharacterInfos(session.ActiveAccountInfo.AccountId);

                            for (int i = 0; i < characterInfos.Count && characterInfo == null; i++)
                            {
                                if (characterInfos[i].CharacterId == characterId)
                                {
                                    characterInfo = characterInfos[i];
                                }
                            }

                            if (characterInfo != null)
                            {
                                oldName = characterInfo.Name;

                                bool success = mySqlHandle.CharactersSetPendingName(characterId, newName);

                                if (success)
                                {
                                    response.CharacterRenameResult = CharacterRenameResult.Allow;
                                }
                                else
                                {
                                    response.CharacterRenameResult = CharacterRenameResult.DenyCustomNameTaken;

                                    mySqlHandle.CharactersUpdateLastLogout(characterId);
                                }
                            }

                            mySqlHandle.Close();
                        }
                        catch (Exception exc)
                        {
                            Server.Instance.LogDatabaseError(exc);
                        }

                        mySqlHandle.Free();

                        Server.Instance.SendGamePacket(response, ClientPacketId.MSG_CLIENT_CHARACTER_RENAME_RESPONSE, e.SourceAddress, e.SourcePort);

                        Server.Instance.Scheduler.RunTask(() => Server.Instance.EventManager.Publish(new CharacterListRequestEvent(session)));

                        Server.Instance.Logger.Log("A client requested to rename a character. address=" + e.SourceAddress + " port=" + e.SourcePort + " oldName=" + oldName + " newName=" + newName + " isOwnedByRequester=" + (characterInfo != null));

                        if (characterInfo != null)
                        {
                            GeneralDisconnectNotifyPacket dcPacket = new GeneralDisconnectNotifyPacket();

                            dcPacket.DisconnectReason = DisconnectReason.CharacterNotFound;

                            Server.Instance.SendGamePacket(dcPacket, GeneralPacketId.MSG_SERVER_DISCONNECT_NOTIFY, e.SourceAddress, e.SourcePort);
                        }
                    }

                        );
                }
            }
        }
        public void OnAuthLoginRequestPacketReceive(GamePacketReceiveEvent e)
        {
            if (e.Packet is AuthLoginRequestPacket)
            {
                AuthLoginRequestPacket request = (AuthLoginRequestPacket)e.Packet;

                string username        = request.Username;
                string password        = request.Password;
                string maintenanceText = null;

                if (File.Exists("./maintenance.txt"))
                {
                    string[] lines = File.ReadAllLines("./maintenance.txt");

                    maintenanceText = String.Empty;

                    for (int i = 0; i < lines.Length; i++)
                    {
                        if (i > 0)
                        {
                            maintenanceText += "\n";
                        }

                        maintenanceText += lines[i];
                    }
                }

                Server.Instance.Scheduler.RunTaskAsync(

                    () =>
                {
                    ClientLoginResponsePacket response = new ClientLoginResponsePacket();
                    AccountInfo accountInfo            = null;

                    MySqlHandle mySqlHandle = Server.Instance.DatabaseManager.GetMySqlHandle();

                    try
                    {
                        mySqlHandle.Open();

                        accountInfo = mySqlHandle.AccountsGetAccountInfo(username);

                        if (accountInfo != null)
                        {
                            Rfc2898DeriveBytes pbkdf2 = new Rfc2898DeriveBytes(password, accountInfo.PasswordSalt, accountInfo.PasswordIterations, HashAlgorithmName.SHA256);

                            byte[] passwordHash = pbkdf2.GetBytes(32);

                            if (passwordHash.SequenceEqual(accountInfo.PasswordHash))
                            {
                                response.LoginResult = LoginResult.Allow;
                            }
                            else
                            {
                                response.LoginResult = LoginResult.DenyInvalidCredentials;
                            }
                        }
                        else
                        {
                            response.LoginResult = LoginResult.DenyInvalidCredentials;
                        }

                        mySqlHandle.Close();
                    }
                    catch (Exception exc)
                    {
                        Server.Instance.LogDatabaseError(exc);
                    }

                    mySqlHandle.Free();

                    if (maintenanceText != null)
                    {
                        response.LoginResult = LoginResult.DenyCustomError;

                        response.CustomErrorMessage = maintenanceText;
                    }

                    if (response.LoginResult == LoginResult.Allow)
                    {
                        Session session = Server.Instance.SessionManager.CreateSession(e.SourceAddress, e.SourcePort, accountInfo);

                        response.SessionSecret = session.Secret;
                    }
                    else
                    {
                        response.SessionSecret = "AUTHENTICATION FAILED";
                    }

                    response.CharacterInstanceIp   = e.DestinationAddress;
                    response.ChatInstanceIp        = e.DestinationAddress;
                    response.CharacterInstancePort = Server.Instance.WorldPort;
                    response.ChatInstancePort      = Server.Instance.WorldPort;

                    Server.Instance.SendGamePacket(response, ClientPacketId.MSG_CLIENT_LOGIN_RESPONSE, e.SourceAddress, e.SourcePort);

                    Server.Instance.Logger.Log("A client requested to authenticate. address=" + e.SourceAddress + " port=" + e.SourcePort + " username="******" result=" + response.LoginResult);
                }

                    );
            }
        }
Пример #7
0
        public void OnWorldLoginRequestPacketReceive(GamePacketReceiveEvent e)
        {
            if (e.Packet is WorldLoginRequestPacket)
            {
                WorldLoginRequestPacket request = (WorldLoginRequestPacket)e.Packet;

                Session session = Server.Instance.SessionManager.GetSession(e.SourceAddress, e.SourcePort);

                if (session != null)
                {
                    Server.Instance.Scheduler.RunTaskAsync(

                        () =>
                    {
                        long characterId            = request.CharacterId;
                        CharacterInfo characterInfo = null;

                        MySqlHandle mySqlHandle = Server.Instance.DatabaseManager.GetMySqlHandle();

                        try
                        {
                            mySqlHandle.Open();

                            List <CharacterInfo> characterInfos = mySqlHandle.CharactersGetCharacterInfos(session.ActiveAccountInfo.AccountId);

                            for (int i = 0; i < characterInfos.Count && characterInfo == null; i++)
                            {
                                if (characterInfos[i].CharacterId == characterId)
                                {
                                    characterInfo = characterInfos[i];
                                }
                            }

                            mySqlHandle.Close();
                        }
                        catch (Exception exc)
                        {
                            Server.Instance.LogDatabaseError(exc);
                        }

                        mySqlHandle.Free();

                        if (characterInfo != null)
                        {
                            if (session.ActiveCharacterInfo == null || session.ActiveCharacterInfo.CharacterId == characterInfo.CharacterId)
                            {
                                if (characterInfo.ZoneId == 0)
                                {
                                    characterInfo.ZoneId = 1000;
                                }

                                session.ActiveCharacterInfo = characterInfo;
                            }

                            ClientLoadStaticZonePacket response = new ClientLoadStaticZonePacket();

                            response.ZoneId        = characterInfo.ZoneId;
                            response.CloneId       = characterInfo.CloneId;
                            response.ZoneChecksum  = Server.Instance.ZoneChecksumCache.GetZoneChecksum(characterInfo.ZoneId);
                            response.EditorEnabled = (characterInfo.EditorLevel) > 0 ? (byte)1 : (byte)0;
                            response.EditorLevel   = characterInfo.EditorLevel;
                            response.Position      = characterInfo.Position;

                            Server.Instance.SendGamePacket(response, ClientPacketId.MSG_CLIENT_LOAD_STATIC_ZONE, e.SourceAddress, e.SourcePort);
                        }
                        else
                        {
                            GeneralDisconnectNotifyPacket dcPacket = new GeneralDisconnectNotifyPacket();

                            dcPacket.DisconnectReason = DisconnectReason.CharacterNotFound;

                            Server.Instance.SendGamePacket(dcPacket, GeneralPacketId.MSG_SERVER_DISCONNECT_NOTIFY, e.SourceAddress, e.SourcePort);
                        }
                    }

                        );
                }
            }
        }
Пример #8
0
        public void OnWorldCharacterCreateRequestPacketReceive(GamePacketReceiveEvent e)
        {
            if (e.Packet is WorldCharacterCreateRequestPacket)
            {
                WorldCharacterCreateRequestPacket request = (WorldCharacterCreateRequestPacket)e.Packet;

                Session session = Server.Instance.SessionManager.GetSession(e.SourceAddress, e.SourcePort);

                if (session != null)
                {
                    long characterId = Server.Instance.ObjectIdGenerator.GenerateGlobalId();

                    CharacterInfo characterInfo = new CharacterInfo(characterId, session.ActiveAccountInfo.AccountId);

                    string name;

                    if (Server.Instance.PredefinedNameCache != null)
                    {
                        name  = Server.Instance.PredefinedNameCache.GetFirstName(request.PredefinedNameFirst);
                        name += Server.Instance.PredefinedNameCache.GetMiddleName(request.PredefinedNameMiddle);
                        name += Server.Instance.PredefinedNameCache.GetLastName(request.PredefinedNameLast);
                    }
                    else
                    {
                        name = characterId.ToString();
                    }

                    characterInfo.Name         = name;
                    characterInfo.HeadColor    = request.HeadColor;
                    characterInfo.Head         = request.Head;
                    characterInfo.ChestColor   = request.ChestColor;
                    characterInfo.Chest        = request.Chest;
                    characterInfo.Legs         = request.Legs;
                    characterInfo.HairStyle    = request.HairStyle;
                    characterInfo.HairColor    = request.HairColor;
                    characterInfo.LeftHand     = request.LeftHand;
                    characterInfo.RightHand    = request.RightHand;
                    characterInfo.EyebrowStyle = request.EyebrowStyle;
                    characterInfo.EyesStyle    = request.EyesStyle;
                    characterInfo.MouthStyle   = request.MouthStyle;
                    characterInfo.ZoneId       = 0;
                    characterInfo.CloneId      = 0;
                    characterInfo.LastLogout   = (ulong)DateTime.Now.Subtract(Time.UnixEpoch).TotalSeconds;

                    if (Server.Instance.GetZone(1000) != null)
                    {
                        characterInfo.Position = Server.Instance.GetZone(1000).SpawnPosition;
                        characterInfo.Rotation = Server.Instance.GetZone(1000).SpawnRotation;
                    }
                    else
                    {
                        characterInfo.Position = JVector.Zero;
                        characterInfo.Rotation = new JQuaternion(0.0f, 0.0f, 0.0f, 0.0f);
                    }

                    Server.Instance.Scheduler.RunTaskAsync(

                        () =>
                    {
                        ClientCharacterCreateResponsePacket response = new ClientCharacterCreateResponsePacket();

                        response.CharacterCreationResult = CharacterCreateResult.DenyInvalidCustomName;

                        MySqlHandle mySqlHandle = Server.Instance.DatabaseManager.GetMySqlHandle();

                        try
                        {
                            mySqlHandle.Open();

                            bool success = mySqlHandle.CharactersCreateCharacter(characterInfo);

                            if (success)
                            {
                                if (!String.IsNullOrEmpty(request.CustomName))
                                {
                                    success = mySqlHandle.CharactersSetPendingName(characterId, request.CustomName);
                                }

                                if (success)
                                {
                                    response.CharacterCreationResult = CharacterCreateResult.Allow;
                                }
                                else
                                {
                                    response.CharacterCreationResult = CharacterCreateResult.DenyCustomNameTaken;

                                    mySqlHandle.CharactersDeleteCharacter(characterId);
                                }
                            }
                            else
                            {
                                response.CharacterCreationResult = CharacterCreateResult.DenyPredefinedNameTaken;
                            }

                            mySqlHandle.Close();
                        }
                        catch (Exception exc)
                        {
                            Server.Instance.LogDatabaseError(exc);
                        }

                        mySqlHandle.Free();

                        Server.Instance.SendGamePacket(response, ClientPacketId.MSG_CLIENT_CHARACTER_CREATE_RESPONSE, e.SourceAddress, e.SourcePort);

                        Server.Instance.Logger.Log("A client requested to create a character. address=" + e.SourceAddress + " port=" + e.SourcePort + " predefinedName=" + name + " customName=" + request.CustomName + " result=" + response.CharacterCreationResult);

                        if (response.CharacterCreationResult == CharacterCreateResult.Allow)
                        {
                            Server.Instance.Scheduler.RunTask(() => Server.Instance.EventManager.Publish(new CharacterListRequestEvent(session)));
                        }
                        else
                        {
                            Server.Instance.ObjectIdGenerator.FreeObjectId(characterId);
                        }
                    }

                        );
                }
            }
        }