Пример #1
0
        /// <summary>
        /// get list of user's characters
        /// </summary>
        /// <param name="pack">user's credentials</param>
        /// <returns> byte array represents list of user's characters</returns>
        private byte[] GetCharacterList(LoginPacket pack)
        {
            var characters = getCharacterList(pack.login);

            var chars = new byte[characters.Count * 8 + 9];

            using (var mem = new MemoryStream(chars)) {
                var bw = new BinaryWriter(mem);

                bw.Write((byte)0xCE);
                bw.Write((characters.Count * 8) + 4);
                bw.Write(Consts.HatIdentifier);
                foreach (HatCharacter user in characters)
                {
                    bw.Write(user.CharacterID);
                }
            }

            return(chars);
        }
Пример #2
0
        /// <summary>
        /// Processing packet
        /// </summary>
        /// <param name="packet"> return response to recived packet</param>
        /// <returns></returns>
        private byte[] ProcessPacket(object packet)
        {
            if (packet is LoginPacket)
            {
                LoginPacket pack = (LoginPacket)packet;
                if (LoginUser(pack.login, pack.password))
                {
                    lock (loggedInUsers) {
                        LoginPacket gt;

                        if (loggedInUsers.TryGetValue(pack.login, out gt))
                        {
                            return(processClientMessage(Client_operation.M_SEND_MESSAGE, Client_message.M_LOGIN_LOCKED, null));
                        }

                        loggedInUsers.Add(pack.login, pack);
                    }

                    return(GetCharacterList(pack));
                }

                return(processClientMessage(Client_operation.M_SEND_MESSAGE, Client_message.M_INVALID_LOGIN_PASSWORD, null));
            }

            if (packet is Client_message)
            {
                Client_message mess = (Client_message)packet;
                return(processClientMessage(Client_operation.M_CHECK_NICKNAME, mess, null));
            }

            if (packet is Client_operation)
            {
                return(GetServersList());
            }

            return(new byte[0]);
        }
Пример #3
0
        private object ClientPacketParse(byte[] decoded, Socket handler)
        {
            object ret = null;

            using (MemoryStream mem = new MemoryStream(decoded)) {
                BinaryReader br       = new BinaryReader(mem);
                byte         packetID = br.ReadByte();

                switch (packetID)
                {
                case 0xc9:     //login
                {
                    LoginPacket lp = new LoginPacket();

                    byte loginlen = br.ReadByte();
                    lp.GameType = (GameType)br.ReadInt16();
                    byte loginOffset       = br.ReadByte();
                    byte credentialsLength = br.ReadByte();

                    mem.Seek(loginOffset, SeekOrigin.Begin);

                    lp.login    = Encoding.Default.GetString(br.ReadBytes(loginlen));
                    lp.password = Encoding.Default.GetString(br.ReadBytes(credentialsLength - loginlen));

                    Console.WriteLine("user logging in");
                    Console.WriteLine("GameType: {0}", lp.GameType);
                    Console.WriteLine("Login: {0}", lp.login);
                    Console.WriteLine("Password: {0}\n", lp.password);
                    return(lp);
                }

                case 0x4e:     //check nickname
                {
                    br.BaseStream.Seek(5, SeekOrigin.Begin);
                    byte length = br.ReadByte();
                    br.BaseStream.Seek(1, SeekOrigin.Current);
                    string nickname = Encoding.Default.GetString(br.ReadBytes(length));
                    return(check_nickname(nickname));
                }

                case 0xC8: {
                    return(Client_operation.M_GET_SERVER_LIST);
                }

                case 0xD1: {
                    return(ParseState.SERVER_CONNECTED);
                }

                case 0xCB: {
                    // new character to server

                    //                BODY  MIND  SKILL COLOR
                    //                 |     |      |     |
                    // ?? SIZE ID1 ID2 ?? ?? ?? ?? ?? ?? ?? ?? NICK SRV_IP
                    //  |                 |     |     |      |
                    // SIG              REACT SPIRIT PIC  NICK_SIZE
                    // 1   +4   +4  +4 +1 +1 +1 +1 +1 +1 +1 +1

                    Int32  size     = br.ReadInt32();
                    Int32  id1      = br.ReadInt32();
                    Int32  id2      = br.ReadInt32();
                    byte   body     = br.ReadByte();
                    byte   react    = br.ReadByte();
                    byte   mind     = br.ReadByte();
                    byte   spirit   = br.ReadByte();
                    byte   skill    = br.ReadByte();
                    byte   picture  = br.ReadByte();
                    byte   color    = br.ReadByte();
                    byte   nickSize = br.ReadByte();
                    string nickName = Encoding.Default.GetString(br.ReadBytes(nickSize));
                    string clanName = "";
                    string srvIP    = Encoding.Default.GetString(br.ReadBytes(size - (int)br.BaseStream.Position));

                    string[] nick = nickName.Split('|');
                    nickName = nick[0];

                    var chr = new HatCharacter();
                    chr.IDs[0]     = id1;
                    chr.IDs[1]     = id2;
                    chr.Clan       = clanName;
                    chr.Color      = color;
                    chr.Mind       = mind;
                    chr.Nickname   = nickName;
                    chr.ParentUser = (HatUser)Thread.GetData(Thread.GetNamedDataSlot("currentUser"));

                    bool hasPic = Array.Exists(Consts.Pics, pic => pic == picture);

                    if (hasPic)
                    {
                        chr.Pic = picture;
                    }
                    else
                    {
                        chr.Pic = Consts.Pics[0];
                    }

                    chr.React  = react;
                    chr.Skill  = skill;
                    chr.Spirit = spirit;

                    chr.SrvIP = srvIP;

                    //save character
                    throw new NotImplementedException();
                    break;
                }

                case 0xCA: {
                    //get character
                    Int32 id1, id2;

                    id1 = br.ReadInt32();
                    id2 = br.ReadInt32();


                    break;
                }

                default:
                    ret = ParseState.UNKNOWN_PACKET;

                    break;
                }
            }

            return(ret);
        }