/// <summary> /// Player (admin?) broadcast a letter. /// </summary> public static void HandleBroadcastLetter(NetworkClient Client, ProcessedPacket Packet) { string Subject = Packet.ReadString(); string Msg = Packet.ReadString(); NetworkFacade.CurrentSession.SendBroadcastLetter(Client, Subject, Msg); }
/// <summary> /// Player sent a letter to another player. /// </summary> public static void HandlePlayerSentLetter(NetworkClient Client, ProcessedPacket Packet) { string GUID = Packet.ReadString(); if (GUID == string.Empty) { return; } string Subject = Packet.ReadString(); string Msg = Packet.ReadString(); NetworkClient SendTo = NetworkFacade.CurrentSession.GetPlayersClient(GUID); Character FromChar = NetworkFacade.CurrentSession.GetPlayer(Client); if (SendTo != null) { if (FromChar != null) { NetworkFacade.CurrentSession.SendPlayerReceivedLetter(SendTo, Subject, Msg, FromChar.Name); } } else { //TODO: Error handling. } }
/// <summary> /// Received a letter from another player in a session. /// </summary> public static void OnPlayerReceivedLetter(NetworkClient Client, ProcessedPacket Packet) { string From = Packet.ReadString(); string Subject = Packet.ReadString(); string Message = Packet.ReadString(); string GUID = string.Empty; lock (NetworkFacade.AvatarsInSession) { foreach (UISim Sim in NetworkFacade.AvatarsInSession) { if (Sim.Name.Equals(From, StringComparison.CurrentCultureIgnoreCase)) { GUID = Sim.GUID.ToString(); } } } UI.Panels.MessageAuthor Author = new UI.Panels.MessageAuthor(); Author.Author = From; if (GUID != string.Empty) { Author.GUID = GUID; } //Ignore this for now... /*if (!Code.GameFacade.MessageController.ConversationExisted(Author)) * Code.GameFacade.MessageController.PassEmail(Author, Subject, Message); * else*/ GameFacade.MessageController.PassMessage(Author, Message); //MessagesCache.CacheLetter(From, Subject, Message); }
public static void OnCityInfoResponse(ProcessedPacket Packet) { byte NumCities = (byte)Packet.ReadByte(); if (Packet.DecryptedLength > 1) { lock (NetworkFacade.Cities) { for (int i = 0; i < NumCities; i++) { string Name = Packet.ReadString(); string Description = Packet.ReadString(); string IP = Packet.ReadString(); int Port = Packet.ReadInt32(); byte StatusByte = (byte)Packet.ReadByte(); CityInfoStatus Status = (CityInfoStatus)StatusByte; ulong Thumbnail = Packet.ReadUInt64(); string UUID = Packet.ReadString(); ulong Map = Packet.ReadUInt64(); CityInfo Info = new CityInfo(false); Info.Name = Name; Info.Description = Description; Info.Thumbnail = Thumbnail; Info.UUID = UUID; Info.Map = Map; Info.IP = IP; Info.Port = Port; Info.Online = true; Info.Status = Status; NetworkFacade.Cities.Add(Info); } } } }
public static void HandlePlayerOnlineResponse(NetworkClient Client, ProcessedPacket P) { byte Result = (byte)P.ReadByte(); string Token = P.ReadString(); //NOTE: Might have to find another way to identify a client, since two people // can be on the same account from the same IP. string RemoteIP = P.ReadString(); int RemotePort = P.ReadInt32(); PacketStream Packet; NetworkClient FoundClient; switch(Result) { case 0x01: Packet = new PacketStream((byte)PacketType.REQUEST_CITY_TOKEN, 0); Packet.WriteString(Token); FoundClient = NetworkFacade.ClientListener.GetClient(RemoteIP, RemotePort); if(FoundClient != null) FoundClient.SendEncrypted((byte)PacketType.REQUEST_CITY_TOKEN, Packet.ToArray()); break; case 0x02: //Write player was already online packet! Packet = new PacketStream((byte)PacketType.PLAYER_ALREADY_ONLINE, 0); Packet.WriteByte(0x00); //Dummy FoundClient = NetworkFacade.ClientListener.GetClient(RemoteIP, RemotePort); if (FoundClient != null) FoundClient.SendEncrypted((byte)PacketType.PLAYER_ALREADY_ONLINE, Packet.ToArray()); break; } }
/// <summary> /// Received from the CityServer in response to a CITY_TOKEN packet. /// </summary> public static CityTransferStatus OnCityTokenResponse(NetworkClient Client, ProcessedPacket Packet) { LogThis.Log.LogThis("Received OnCityTokenResponse", LogThis.eloglevel.info); CityTransferStatus Status = (CityTransferStatus)Packet.ReadByte(); ushort NumHouses = Packet.ReadUInt16(); LotTileEntry[] TileEntries = new LotTileEntry[NumHouses]; if (NumHouses > 0) { for (int i = 0; i < NumHouses; i++) { TileEntries[i] = new LotTileEntry(Packet.ReadInt32(), Packet.ReadString(), (short)Packet.ReadUInt16(), (short)Packet.ReadUInt16(), (byte)Packet.ReadByte(), Packet.ReadInt32()); } } PlayerAccount.Money = Packet.ReadInt32(); lock (GameFacade.CDataRetriever.LotTileData) GameFacade.CDataRetriever.LotTileData = TileEntries; return(Status); }
/// <summary> /// A cityserver requested a decryptionkey for a client! /// </summary> public static void HandleKeyFetch(NetworkClient Client, ProcessedPacket P) { string AccountName = P.ReadString(); byte[] EncKey = new byte[1]; foreach (NetworkClient Cl in NetworkFacade.CServerListener.Clients) { if (Cl.ClientEncryptor.Username == AccountName) { EncKey = Cl.ClientEncryptor.GetDecryptionArgsContainer().ARC4DecryptArgs.EncryptionKey; //TODO: Figure out what to do about CurrentlyActiveSim... //if (Cl.CurrentlyActiveSim.CreatedThisSession) { //TODO: Update the DB to reflect the city that // this sim resides in. //Database.UpdateCityForCharacter(Cl.CurrentlyActiveSim.Name, Client.ServerInfo.Name); } } } PacketStream OutPacket = new PacketStream(0x01, 0x00); OutPacket.WriteByte((byte)0x01); OutPacket.WriteByte((byte)(EncKey.Length + 2)); OutPacket.WriteByte((byte)EncKey.Length); OutPacket.Write(EncKey, 0, EncKey.Length); Client.Send(OutPacket.ToArray()); //For now, assume client has already disconnected and doesn't need to be disconnected manually. NetworkFacade.CServerListener.TransferringClients.Remove(Client); }
/// <summary> /// New city server came online! /// </summary> public static void OnNewCityServer(NetworkClient Client, ProcessedPacket Packet) { lock (NetworkFacade.Cities) { CityInfo Info = new CityInfo(false); Info.Name = Packet.ReadString(); Info.Description = Packet.ReadString(); Info.IP = Packet.ReadString(); Info.Port = Packet.ReadInt32(); Info.Status = (CityInfoStatus)Packet.ReadByte(); Info.Thumbnail = Packet.ReadUInt64(); Info.UUID = Packet.ReadString(); Info.Map = Packet.ReadUInt64(); NetworkFacade.Cities.Add(Info); } }
/// <summary> /// A cityserver logged in! /// </summary> public static void HandleCityServerLogin(NetworkClient Client, ProcessedPacket P) { Logger.LogInfo("CityServer logged in!\r\n"); string Name = P.ReadString(); string Description = P.ReadString(); string IP = P.ReadString(); int Port = P.ReadInt32(); CityInfoStatus Status = (CityInfoStatus)P.ReadByte(); ulong Thumbnail = P.ReadUInt64(); string UUID = P.ReadString(); ulong Map = P.ReadUInt64(); CityInfo Info = new CityInfo(true); Info.Name = Name; Info.Description = Description; Info.IP = IP; Info.Port = Port; Info.Status = Status; Info.Thumbnail = Thumbnail; Info.UUID = UUID; Info.Map = Map; Info.Client = Client; Info.Online = true; NetworkFacade.CServerListener.CityServers.Add(Info); NetworkFacade.CServerListener.PotentialLogins.TryTake(out Client); NetworkClient[] Clients = new NetworkClient[NetworkFacade.ClientListener.Clients.Count]; NetworkFacade.ClientListener.Clients.CopyTo(Clients, 0); PacketStream ClientPacket = new PacketStream((byte)PacketType.NEW_CITY_SERVER, 0); ClientPacket.WriteString(Name); ClientPacket.WriteString(Description); ClientPacket.WriteString(IP); ClientPacket.WriteInt32(Port); ClientPacket.WriteByte((byte)Status); ClientPacket.WriteUInt64(Thumbnail); ClientPacket.WriteString(UUID); ClientPacket.WriteUInt64(Map); foreach(NetworkClient Receiver in Clients) Receiver.SendEncrypted((byte)PacketType.NEW_CITY_SERVER, ClientPacket.ToArray()); }
/// <summary> /// Received CharacterCreation packet from LoginServer. /// </summary> /// <returns>The result of the character creation.</returns> public static CharacterCreationStatus OnCharacterCreationProgress(NetworkClient Client, ProcessedPacket Packet) { CharacterCreationStatus CCStatus = (CharacterCreationStatus)Packet.ReadByte(); if (CCStatus == CharacterCreationStatus.Success) { Guid CharacterGUID = Guid.NewGuid(); CharacterGUID = new Guid(Packet.ReadString()); PlayerAccount.CityToken = Packet.ReadString(); PlayerAccount.CurrentlyActiveSim.AssignGUID(CharacterGUID.ToString()); //This previously happened when clicking the accept button in CAS, causing //all chars to be cached even if the new char wasn't successfully created. lock (NetworkFacade.Avatars) Cache.CacheSims(NetworkFacade.Avatars); } return(CCStatus); }
/// <summary> /// Received CharacterCreation packet from LoginServer. /// </summary> /// <returns>The result of the character creation.</returns> public static CharacterCreationStatus OnCharacterCreationProgress(NetworkClient Client, ProcessedPacket Packet) { CharacterCreationStatus CCStatus = (CharacterCreationStatus)Packet.ReadByte(); if (CCStatus == CharacterCreationStatus.Success) { Guid CharacterGUID = Guid.NewGuid(); CharacterGUID = new Guid(Packet.ReadString()); PlayerAccount.CityToken = Packet.ReadString(); PlayerAccount.CurrentlyActiveSim.AssignGUID(CharacterGUID.ToString()); //This previously happened when clicking the accept button in CAS, causing //all chars to be cached even if the new char wasn't successfully created. lock(NetworkFacade.Avatars) Cache.CacheSims(NetworkFacade.Avatars); } return CCStatus; }
public static LotTileEntry OnLotCostResponse(NetworkClient Client, ProcessedPacket Packet) { ushort X = Packet.ReadUInt16(); ushort Y = Packet.ReadUInt16(); int LotID = Packet.ReadInt32(); string LotName = Packet.ReadString(); //bit 0 = online, bit 1 = spotlight, bit 2 = locked, bit 3 = occupied, other bits free for whatever use byte Flags = (byte)Packet.ReadByte(); int Cost = Packet.ReadInt32(); return(new LotTileEntry(LotID, LotName, (short)X, (short)Y, Flags, Cost)); }
/// <summary> /// A cityserver logged in! /// </summary> public static void HandleCityServerLogin(NetworkClient Client, ProcessedPacket P) { CityServerClient CityClient = (CityServerClient)Client; Logger.LogInfo("CityServer logged in!\r\n"); string Name = P.ReadString(); string Description = P.ReadString(); string IP = P.ReadString(); int Port = P.ReadInt32(); CityInfoStatus Status = (CityInfoStatus)P.ReadByte(); ulong Thumbnail = P.ReadUInt64(); string UUID = P.ReadString(); ulong Map = P.ReadUInt64(); CityInfo Info = new CityInfo(Name, Description, Thumbnail, UUID, Map, IP, Port); Info.Status = Status; CityClient.ServerInfo = Info; //Client instance changed, so update it... NetworkFacade.CServerListener.UpdateClient(CityClient); }
/// <summary> /// A player left a session (game) in progress. /// </summary> public static void OnPlayerLeftSession(NetworkClient Client, ProcessedPacket Packet) { string GUID = Packet.ReadString(); lock (NetworkFacade.AvatarsInSession) { foreach (UISim Avatar in NetworkFacade.AvatarsInSession) { if (Avatar.GUID.ToString().Equals(GUID, StringComparison.CurrentCultureIgnoreCase)) { NetworkFacade.AvatarsInSession.Remove(Avatar); } } } }
/// <summary> /// A client decided to retire a character. /// </summary> public static void HandleCharacterRetirement(NetworkClient Client, ProcessedPacket P) { int AccountID = P.ReadInt32(); string GUID = P.ReadString(); using (DataAccess db = DataAccess.Get()) { IQueryable <Character> Query = db.Characters.GetForAccount(AccountID); //F**K, I hate LINQ. Guid CharGUID = new Guid(GUID); Character Char = Query.Where(x => x.GUID == CharGUID).SingleOrDefault(); db.Characters.RetireCharacter(Char); } }
/// <summary> /// A player joined a session (game) in progress. /// </summary> public static LotTileEntry OnPlayerJoinedSession(NetworkClient Client, ProcessedPacket Packet) { LotTileEntry TileEntry = new LotTileEntry(0, "", 0, 0, 0, 0); UISim Avatar = new UISim(Packet.ReadString()); Avatar.Name = Packet.ReadString(); Avatar.Sex = Packet.ReadString(); Avatar.Description = Packet.ReadString(); Avatar.HeadOutfitID = Packet.ReadUInt64(); Avatar.BodyOutfitID = Packet.ReadUInt64(); Avatar.Avatar.Appearance = (AppearanceType)Packet.ReadInt32(); byte HasHouse = (byte)Packet.ReadByte(); if (HasHouse != 0) { TileEntry = new LotTileEntry(Packet.ReadInt32(), Packet.ReadString(), (short)Packet.ReadUInt16(), (short)Packet.ReadUInt16(), (byte)Packet.ReadByte(), Packet.ReadInt32()); Avatar.LotID = TileEntry.lotid; Avatar.HouseX = TileEntry.x; Avatar.HouseY = TileEntry.y; LotTileEntry[] TileEntries = new LotTileEntry[GameFacade.CDataRetriever.LotTileData.Length + 1]; TileEntries[0] = TileEntry; GameFacade.CDataRetriever.LotTileData.CopyTo(TileEntries, 1); } lock (NetworkFacade.AvatarsInSession) { NetworkFacade.AvatarsInSession.Add(Avatar); } return(TileEntry); }
/// <summary> /// A city server went offline! /// </summary> public static void OnCityServerOffline(NetworkClient Client, ProcessedPacket Packet) { lock (NetworkFacade.Cities) { string Name = Packet.ReadString(); foreach (CityInfo City in NetworkFacade.Cities) { if (City.Name.Equals(Name, StringComparison.CurrentCultureIgnoreCase)) { NetworkFacade.Cities.Remove(City); break; } } } }
public static void HandleChallengeResponse(NetworkClient Client, ProcessedPacket Packet) { Console.WriteLine("Server receives challenge response - test 3"); byte[] CResponse = Packet.ReadBytes(Packet.ReadByte()); if (CResponse.SequenceEqual(ChallengeResponse)) { Console.WriteLine("Received correct challenge response, client was authenticated!"); } string Username = Packet.ReadString(); Console.WriteLine("Username: "******"Test 3: passed!"); }
public static void HandleCityToken(NetworkClient Client, ProcessedPacket P) { try { bool ClientAuthenticated = false; using (var db = DataAccess.Get()) { byte HashLength = (byte)P.ReadByte(); byte[] HashBuf = new byte[HashLength]; P.Read(HashBuf, 0, HashLength); byte KeyLength = (byte)P.ReadByte(); byte[] EncKey = new byte[KeyLength]; P.Read(EncKey, 0, KeyLength); Client.ClientEncryptor = new ARC4Encryptor(Convert.ToBase64String(HashBuf), EncKey); string Token = P.ReadString(); foreach (ClientToken Tok in NetworkFacade.TransferringClients.GetList()) { if (Tok.Token == Token) { ClientAuthenticated = true; PacketStream SuccessPacket = new PacketStream((byte)PacketType.CITY_TOKEN, 0); SuccessPacket.WriteByte((byte)CityTransferStatus.Success); Client.SendEncrypted((byte)PacketType.CITY_TOKEN, SuccessPacket.ToArray()); } } if (!ClientAuthenticated) { PacketStream ErrorPacket = new PacketStream((byte)PacketType.CITY_TOKEN, 0); ErrorPacket.WriteByte((byte)CityTransferStatus.GeneralError); Client.SendEncrypted((byte)PacketType.CITY_TOKEN, ErrorPacket.ToArray()); } } } catch (Exception E) { Logger.LogDebug("Exception in HandleCityToken: " + E.ToString()); } }
/// <summary> /// Received CharacterCreation packet from CityServer. /// </summary> /// <returns>The result of the character creation.</returns> public static CharacterCreationStatus OnCharacterCreationStatus(NetworkClient Client, ProcessedPacket Packet) { LogThis.Log.LogThis("Received OnCharacterCreationStatus!", LogThis.eloglevel.info); CharacterCreationStatus CCStatus = (CharacterCreationStatus)Packet.ReadByte(); ushort NumHouses = Packet.ReadUInt16(); LotTileEntry[] TileEntries = new LotTileEntry[NumHouses]; if (NumHouses > 0) { for (int i = 0; i < NumHouses; i++) { TileEntries[i] = new LotTileEntry(Packet.ReadInt32(), Packet.ReadString(), (short)Packet.ReadUInt16(), (short)Packet.ReadUInt16(), (byte)Packet.ReadByte(), Packet.ReadInt32()); } } lock (GameFacade.CDataRetriever.LotTileData) GameFacade.CDataRetriever.LotTileData = TileEntries; return(CCStatus); }
/// <summary> /// Client wanted to retire a character. /// </summary> public static void HandleCharacterRetirement(NetworkClient Client, ProcessedPacket P) { PacketStream Packet; string AccountName = P.ReadString(); string GUID = P.ReadString(); if (AccountName == string.Empty || GUID == string.Empty) return; using (var db = DataAccess.Get()) { Account Acc = db.Accounts.GetByUsername(AccountName); IQueryable<Character> Query = db.Characters.GetForAccount(Acc.AccountID); //F**K, I hate LINQ. Guid CharGUID = new Guid(GUID); Character Char = Query.Where(x => x.GUID == CharGUID).SingleOrDefault(); if (Char != null) db.Characters.RetireCharacter(Char); else return; //This actually updates the record, not sure how. Acc.NumCharacters--; if (Char != null) { CityInfo CInfo = NetworkFacade.CServerListener.GetCityServer(Char.City); //Just in case... if (CInfo != null) { Packet = new PacketStream(0x02, 0); Packet.WriteHeader(); ushort PacketLength = (ushort)(PacketHeaders.UNENCRYPTED + 4 + GUID.Length + 1); Packet.WriteUInt16(PacketLength); Packet.WriteInt32(Acc.AccountID); Packet.WriteString(GUID); CInfo.Client.Send(Packet.ToArray()); } } } Packet = new PacketStream((byte)PacketType.RETIRE_CHARACTER_STATUS, 0); Packet.WriteString(GUID); Client.SendEncrypted((byte)PacketType.RETIRE_CHARACTER_STATUS, Packet.ToArray()); }
/// <summary> /// Client requested information about its characters. /// </summary> public static void HandleCharacterInfoRequest(NetworkClient Client, ProcessedPacket P) { Logger.LogInfo("Received CharacterInfoRequest!"); string DateTimeStr = P.ReadString(); DateTime Timestamp; if (DateTimeStr != string.Empty) Timestamp = DateTime.Parse(DateTimeStr); else { //Unix epoch Timestamp = new DateTime(1970, 1, 1, 0, 0, 1); } Character[] Characters = new Character[] { }; using (var db = DataAccess.Get()) { var account = db.Accounts.GetByUsername(Client.ClientEncryptor.Username); Characters = db.Characters.GetForAccount((int)account.AccountID).ToArray(); } int NumChars = 0, NewChars = 0; if (Characters != null) { PacketStream Packet = new PacketStream((byte)PacketType.CHARACTER_LIST, 0); MemoryStream PacketData = new MemoryStream(); BinaryWriter PacketWriter = new BinaryWriter(PacketData); NumChars = Characters.Length; foreach (Character avatar in Characters) { //Zero means same, less than zero means T1 is earlier than T2, more than zero means T1 is later. if (DateTime.Compare(Timestamp, avatar.LastCached) < 0) { NewChars++; PacketWriter.Write((int)avatar.CharacterID); PacketWriter.Write(avatar.GUID.ToString()); PacketWriter.Write(avatar.LastCached.ToString("yyyy.MM.dd hh:mm:ss", CultureInfo.InvariantCulture)); PacketWriter.Write(avatar.Name); PacketWriter.Write(avatar.Sex); PacketWriter.Write(avatar.Description); PacketWriter.Write((ulong)avatar.HeadOutfitID); PacketWriter.Write((ulong)avatar.BodyOutfitID); PacketWriter.Write((byte)avatar.AppearanceType); PacketWriter.Write((string)avatar.CityName); PacketWriter.Write((ulong)avatar.CityThumb); PacketWriter.Write((string)avatar.City); PacketWriter.Write((ulong)avatar.CityMap); PacketWriter.Write((string)avatar.CityIp); PacketWriter.Write((int)avatar.CityPort); PacketWriter.Write((int)avatar.Money); } } //NOTE: If Characters != null, but no chars were new, NumChars will be however many characters, // and NewChars will be 0. Packet.WriteByte((byte)NumChars); Packet.WriteByte((byte)NewChars); Packet.Write(PacketData.ToArray(), 0, (int)PacketData.Length); PacketWriter.Close(); Client.SendEncrypted((byte)PacketType.CHARACTER_LIST, Packet.ToArray()); } else //No characters existed for the account. { PacketStream Packet = new PacketStream(0x05, 0); Packet.WriteByte((byte)NumChars); //0 characters. Packet.WriteByte((byte)NewChars); //0 new characters. Client.SendEncrypted((byte)PacketType.CHARACTER_LIST, Packet.ToArray()); } }
/// <summary> /// Received a letter from another player in a session. /// </summary> public static void OnPlayerReceivedLetter(NetworkClient Client, ProcessedPacket Packet) { string From = Packet.ReadString(); string Subject = Packet.ReadString(); string Message = Packet.ReadString(); string GUID = string.Empty; lock (NetworkFacade.AvatarsInSession) { foreach (UISim Sim in NetworkFacade.AvatarsInSession) { if (Sim.Name.Equals(From, StringComparison.CurrentCultureIgnoreCase)) GUID = Sim.GUID.ToString(); } } Code.UI.Panels.MessageAuthor Author = new TSOClient.Code.UI.Panels.MessageAuthor(); Author.Author = From; if (GUID != string.Empty) Author.GUID = GUID; //Ignore this for now... /*if (!Code.GameFacade.MessageController.ConversationExisted(Author)) Code.GameFacade.MessageController.PassEmail(Author, Subject, Message); else*/ Code.GameFacade.MessageController.PassMessage(Author, Message); //MessagesCache.CacheLetter(From, Subject, Message); }
/// <summary> /// Received CharacterCreation packet from CityServer. /// </summary> /// <returns>The result of the character creation.</returns> public static CharacterCreationStatus OnCharacterCreationStatus(NetworkClient Client, ProcessedPacket Packet) { LogThis.Log.LogThis("Received OnCharacterCreationStatus!", LogThis.eloglevel.info); CharacterCreationStatus CCStatus = (CharacterCreationStatus)Packet.ReadByte(); ushort NumHouses = Packet.ReadUInt16(); LotTileEntry[] TileEntries = new LotTileEntry[NumHouses]; if (NumHouses > 0) { for (int i = 0; i < NumHouses; i++) { TileEntries[i] = new LotTileEntry(Packet.ReadInt32(), Packet.ReadString(), (short)Packet.ReadUInt16(), (short)Packet.ReadUInt16(), (byte)Packet.ReadByte(), Packet.ReadInt32()); } } lock(TSOClient.Code.GameFacade.CDataRetriever.LotTileData) TSOClient.Code.GameFacade.CDataRetriever.LotTileData = TileEntries; return CCStatus; }
/// <summary> /// Received from the LoginServer in response to a RETIRE_CHARACTER packet. /// </summary> /// <returns>Name of character that was retired.</returns> public static string OnCharacterRetirement(NetworkClient Client, ProcessedPacket Packet) { string GUID = Packet.ReadString(); return GUID; }
/// <summary> /// Client wanted to transfer to a city server. /// </summary> public static void HandleCityTokenRequest(NetworkClient Client, ProcessedPacket P) { string AccountName = P.ReadString(); string CityGUID = P.ReadString(); string CharGUID = P.ReadString(); if (AccountName == string.Empty || CityGUID == string.Empty || CharGUID == string.Empty) return; Guid Token = Guid.NewGuid(); CityInfo CServer = NetworkFacade.CServerListener.GetCityServer(CityGUID); using (var db = DataAccess.Get()) { Account Acc = db.Accounts.GetByUsername(AccountName); PacketStream CServerPacket = new PacketStream(0x01, 0); CServerPacket.WriteHeader(); ushort PacketLength = (ushort)(PacketHeaders.UNENCRYPTED + 1 + 4 + (Client.RemoteIP.Length + 1) + 4 + (CharGUID.ToString().Length + 1) + (Token.ToString().Length + 1)); CServerPacket.WriteUInt16(PacketLength); CServerPacket.WriteByte(0); //CharacterCreate = false. CServerPacket.WriteInt32(Acc.AccountID); CServerPacket.WriteString(Client.RemoteIP); CServerPacket.WriteInt32(Client.RemotePort); CServerPacket.WriteString(CharGUID.ToString()); CServerPacket.WriteString(Token.ToString("")); CServer.Client.Send(CServerPacket.ToArray()); } }
public static void OnCityInfoResponse(ProcessedPacket Packet) { byte NumCities = (byte)Packet.ReadByte(); if (Packet.DecryptedLength > 1) { lock (NetworkFacade.Cities) { for (int i = 0; i < NumCities; i++) { string Name = Packet.ReadString(); string Description = Packet.ReadString(); string IP = Packet.ReadString(); int Port = Packet.ReadInt32(); byte StatusByte = (byte)Packet.ReadByte(); CityInfoStatus Status = (CityInfoStatus)StatusByte; ulong Thumbnail = Packet.ReadUInt64(); string UUID = Packet.ReadString(); ulong Map = Packet.ReadUInt64(); CityInfo Info = new CityInfo(false); Info.Name = Name; Info.Description = Description; Info.Thumbnail = Thumbnail; Info.UUID = UUID; Info.Map = Map; Info.IP = IP; Info.Port = Port; Info.Online = true; Info.Status = Status; NetworkFacade.Cities.Add(Info); } } } }
/// <summary> /// A client wanted to transfer to this server, so a token was generated by the login server. /// </summary> public static void HandleClientToken(NetworkClient Client, ProcessedPacket P) { try { ClientToken Token = new ClientToken(); byte CharacterCreate = (byte)P.ReadByte(); Token.AccountID = P.ReadInt32(); Token.ClientIP = P.ReadString(); int ClientPort = P.ReadInt32(); Token.CharacterGUID = P.ReadString(); Token.Token = P.ReadString(); PacketStream PlayerOnlinePacket = new PacketStream(0x67, 0); PlayerOnlinePacket.WriteHeader(); PlayerOnlinePacket.WriteUInt16((ushort)(PacketHeaders.UNENCRYPTED + 1 + Token.Token.Length + 1 + Token.ClientIP.Length + 1 + 4)); if (CharacterCreate == 0) { if (NetworkFacade.CurrentSession.GetPlayer(Token.CharacterGUID) == null) { NetworkClient WaitingClient = NetworkFacade.NetworkListener.GetClient(Token.ClientIP, ClientPort); //Uh-oh, someone's waiting for their token! if (WaitingClient != null) { PacketStream SuccessPacket = new PacketStream((byte)PacketType.CITY_TOKEN, 0); SuccessPacket.WriteByte((byte)CityTransferStatus.Success); WaitingClient.SendEncrypted((byte)PacketType.CITY_TOKEN, SuccessPacket.ToArray()); } PlayerOnlinePacket.WriteByte(0x01); PlayerOnlinePacket.WriteString(Token.Token); PlayerOnlinePacket.WriteString(Token.ClientIP); PlayerOnlinePacket.WriteInt32(ClientPort); lock (NetworkFacade.TransferringClients) { if (!NetworkFacade.TransferringClients.Contains(Token)) { NetworkFacade.TransferringClients.Add(Token); } } Client.Send(PlayerOnlinePacket.ToArray()); } else { PlayerOnlinePacket.WriteByte(0x02); PlayerOnlinePacket.WriteString(Token.Token); PlayerOnlinePacket.WriteString(Token.ClientIP); PlayerOnlinePacket.WriteInt32(ClientPort); Client.Send(PlayerOnlinePacket.ToArray()); } } else { NetworkClient WaitingClient = NetworkFacade.NetworkListener.GetClient(Token.ClientIP, ClientPort); //Uh-oh, someone's waiting for their token! if (WaitingClient != null) { PacketStream SuccessPacket = new PacketStream((byte)PacketType.CITY_TOKEN, 0); SuccessPacket.WriteByte((byte)CityTransferStatus.Success); WaitingClient.SendEncrypted((byte)PacketType.CITY_TOKEN, SuccessPacket.ToArray()); } if (!NetworkFacade.TransferringClients.Contains(Token)) { NetworkFacade.TransferringClients.Add(Token); } } } catch (Exception E) { Logger.LogDebug("Exception in HandleClientToken: " + E.ToString()); } }
/// <summary> /// LoginServer sent information about the player's characters. /// </summary> /// <param name="Packet">The packet that was received.</param> public static void OnCharacterInfoResponse(ProcessedPacket Packet, NetworkClient Client) { //If the decrypted length == 1, it means that there were 0 //characters that needed to be updated, or that the user //hasn't created any characters on his/her account yet. //Since the Packet.Length property is equal to the length //of the encrypted data, it cannot be used to get the length //of the decrypted data. if (Packet.DecryptedLength > 1) { byte NumCharacters = (byte)Packet.ReadByte(); List<Sim> FreshSims = new List<Sim>(); for (int i = 0; i < NumCharacters; i++) { int CharacterID = Packet.ReadInt32(); Sim FreshSim = new Sim(Packet.ReadString()); FreshSim.CharacterID = CharacterID; FreshSim.Timestamp = Packet.ReadString(); FreshSim.Name = Packet.ReadString(); FreshSim.Sex = Packet.ReadString(); FreshSim.Description = Packet.ReadString(); FreshSim.HeadOutfitID = Packet.ReadUInt64(); FreshSim.BodyOutfitID = Packet.ReadUInt64(); FreshSim.AppearanceType = (SimsLib.ThreeD.AppearanceType)Packet.ReadByte(); FreshSim.ResidingCity = new CityInfo(Packet.ReadString(), "", Packet.ReadUInt64(), Packet.ReadString(), Packet.ReadUInt64(), Packet.ReadString(), Packet.ReadInt32()); FreshSims.Add(FreshSim); } NetworkFacade.Avatars = FreshSims; Cache.CacheSims(FreshSims); } PacketStream CityInfoRequest = new PacketStream(0x06, 0); CityInfoRequest.WriteByte(0x00); //Dummy Client.SendEncrypted((byte)PacketType.CITY_LIST, CityInfoRequest.ToArray()); }
public static void OnCityToken(NetworkClient Client, ProcessedPacket Packet) { PlayerAccount.CityToken = Packet.ReadString(); }
public static void OnCityInfoResponse(ProcessedPacket Packet) { byte NumCities = (byte)Packet.ReadByte(); if (Packet.DecryptedLength > 1) { for (int i = 0; i < NumCities; i++) { string Name = Packet.ReadString(); string Description = Packet.ReadString(); string IP = Packet.ReadString(); int Port = Packet.ReadInt32(); byte StatusByte = (byte)Packet.ReadByte(); CityInfoStatus Status = (CityInfoStatus)StatusByte; ulong Thumbnail = Packet.ReadUInt64(); string UUID = Packet.ReadString(); ulong Map = Packet.ReadUInt64(); CityInfo Info = new CityInfo(Name, Description, Thumbnail, UUID, Map, IP, Port); Info.Online = true; Info.Status = Status; NetworkFacade.Cities.Add(Info); } } }
public static void HandleCharacterCreate(NetworkClient Client, ProcessedPacket P) { try { Logger.LogInfo("Received CharacterCreate!"); bool ClientAuthenticated = false; byte AccountStrLength = (byte)P.ReadByte(); byte[] AccountNameBuf = new byte[AccountStrLength]; P.Read(AccountNameBuf, 0, AccountStrLength); string AccountName = Encoding.ASCII.GetString(AccountNameBuf); byte HashLength = (byte)P.ReadByte(); byte[] HashBuf = new byte[HashLength]; P.Read(HashBuf, 0, HashLength); using (DataAccess db = DataAccess.Get()) { Client.ClientEncryptor = new ARC4Encryptor(Convert.ToBase64String(HashBuf)); Client.ClientEncryptor.Username = AccountName; string Token = P.ReadString(); string GUID = ""; int AccountID = 0; foreach (ClientToken CToken in NetworkFacade.TransferringClients.GetList()) { if (CToken.ClientIP == Client.RemoteIP) { if (CToken.Token == Token) { PacketStream SuccessPacket = new PacketStream((byte)PacketType.CHARACTER_CREATE_CITY, (int)(PacketHeaders.ENCRYPTED + 1)); SuccessPacket.WriteByte((byte)CityDataModel.Entities.CharacterCreationStatus.Success); Client.SendEncrypted((byte)PacketType.CHARACTER_CREATE_CITY, SuccessPacket.ToArray()); ClientAuthenticated = true; GUID = CToken.CharacterGUID; AccountID = CToken.AccountID; } break; } } Sim Char = new Sim(new Guid(GUID)); Char.Timestamp = P.ReadPascalString(); Char.Name = P.ReadPascalString(); Char.Sex = P.ReadPascalString(); Char.Description = P.ReadPascalString(); Char.HeadOutfitID = P.ReadUInt64(); Char.BodyOutfitID = P.ReadUInt64(); Char.Appearance = (AppearanceType)P.ReadByte(); Char.CreatedThisSession = true; var characterModel = new Character(); characterModel.Name = Char.Name; characterModel.Sex = Char.Sex; characterModel.Description = Char.Description; characterModel.LastCached = Char.Timestamp; characterModel.GUID = Char.GUID; characterModel.HeadOutfitID = (long)Char.HeadOutfitID; characterModel.BodyOutfitID = (long)Char.BodyOutfitID; characterModel.AccountID = AccountID; characterModel.AppearanceType = (int)Char.Appearance; var status = db.Characters.CreateCharacter(characterModel); } //Invalid token, should never occur... if (!ClientAuthenticated) { PacketStream SuccessPacket = new PacketStream(0x65, (int)(PacketHeaders.ENCRYPTED + 1)); SuccessPacket.WriteByte((byte)CityDataModel.Entities.CharacterCreationStatus.GeneralError); Client.SendEncrypted(0x64, SuccessPacket.ToArray()); Client.Disconnect(); } } catch (Exception E) { Logger.LogDebug("Exception in HandleCharacterCreate: " + E.ToString()); } }
/// <summary> /// A city server went offline! /// </summary> public static void OnCityServerOffline(NetworkClient Client, ProcessedPacket Packet) { lock (NetworkFacade.Cities) { string Name = Packet.ReadString(); foreach (CityInfo City in NetworkFacade.Cities) { if (City.Name.Equals(Name, StringComparison.CurrentCultureIgnoreCase)) { NetworkFacade.Cities.Remove(City); break; } } } }
/// <summary> /// A player left a session (game) in progress. /// </summary> public static void OnPlayerLeftSession(NetworkClient Client, ProcessedPacket Packet) { string GUID = Packet.ReadString(); lock (NetworkFacade.AvatarsInSession) { foreach (UISim Avatar in NetworkFacade.AvatarsInSession) { if (Avatar.GUID.ToString().Equals(GUID, StringComparison.CurrentCultureIgnoreCase)) NetworkFacade.AvatarsInSession.Remove(Avatar); } } }
/// <summary> /// LoginServer sent information about the player's characters. /// </summary> /// <param name="Packet">The packet that was received.</param> public static void OnCharacterInfoResponse(ProcessedPacket Packet, NetworkClient Client) { byte NumCharacters = (byte)Packet.ReadByte(); byte NewCharacters = (byte)Packet.ReadByte(); List <UISim> FreshSims = new List <UISim>(); for (int i = 0; i < NewCharacters; i++) { int CharacterID = Packet.ReadInt32(); UISim FreshSim = new UISim(Packet.ReadString(), false); FreshSim.CharacterID = CharacterID; FreshSim.Timestamp = Packet.ReadString(); FreshSim.Name = Packet.ReadString(); FreshSim.Sex = Packet.ReadString(); FreshSim.Description = Packet.ReadString(); FreshSim.HeadOutfitID = Packet.ReadUInt64(); FreshSim.BodyOutfitID = Packet.ReadUInt64(); FreshSim.Avatar.Appearance = (AppearanceType)Packet.ReadByte(); FreshSim.ResidingCity = new CityInfo(false); FreshSim.ResidingCity.Name = Packet.ReadString(); FreshSim.ResidingCity.Thumbnail = Packet.ReadUInt64(); FreshSim.ResidingCity.UUID = Packet.ReadString(); FreshSim.ResidingCity.Map = Packet.ReadUInt64(); FreshSim.ResidingCity.IP = Packet.ReadString(); FreshSim.ResidingCity.Port = Packet.ReadInt32(); FreshSims.Add(FreshSim); } lock (NetworkFacade.Avatars) { if ((NumCharacters < 3) && (NewCharacters > 0)) { FreshSims = Cache.LoadCachedSims(FreshSims); NetworkFacade.Avatars = FreshSims; Cache.CacheSims(FreshSims); } if (NewCharacters == 0 && NumCharacters > 0) { NetworkFacade.Avatars = Cache.LoadAllSims(); } else if (NewCharacters == 3 && NumCharacters == 3) { NetworkFacade.Avatars = FreshSims; Cache.CacheSims(FreshSims); } else if (NewCharacters == 0 && NumCharacters == 0) { //Make sure if sims existed in the cache, they are deleted (because they didn't exist in DB). Cache.DeleteCache(); } else if (NumCharacters == 3 && NewCharacters == 3) { NetworkFacade.Avatars = FreshSims; } } PacketStream CityInfoRequest = new PacketStream(0x06, 0); CityInfoRequest.WriteByte(0x00); //Dummy Client.SendEncrypted((byte)PacketType.CITY_LIST, CityInfoRequest.ToArray()); }
/// <summary> /// Received client token. /// </summary> public static void HandleCityToken(NetworkClient Client, ProcessedPacket P) { try { //bool ClientAuthenticated = false; ClientToken TokenToRemove = new ClientToken(); using (DataAccess db = DataAccess.Get()) { string Token = P.ReadString(); ClientToken Tok; if (Token == string.Empty) { return; } Tok = NetworkFacade.GetClientToken(new Guid(Token)); if (Tok != null) { //ClientAuthenticated = true; TokenToRemove = Tok; Character Char = db.Characters.GetForCharacterGUID(new Guid(Tok.CharacterGUID)); if (Char != null) { NetworkFacade.CurrentSession.AddPlayer(Client, Char); PacketStream SuccessPacket = new PacketStream((byte)PacketType.CITY_TOKEN, 0); SuccessPacket.WriteByte((byte)CityTransferStatus.Success); House[] Houses = NetworkFacade.CurrentSession.GetHousesInSession(); SuccessPacket.WriteUInt16((ushort)Houses.Length); //Ho, ho, ho... foreach (House Ho in Houses) { SuccessPacket.WriteInt32(Ho.HouseID); SuccessPacket.WriteString(Ho.Name); SuccessPacket.WriteUInt16((ushort)Ho.X); SuccessPacket.WriteUInt16((ushort)Ho.Y); SuccessPacket.WriteByte((byte)Ho.Flags); //Might have to save this as unsigned in DB? SuccessPacket.WriteInt32(Ho.Cost); } SuccessPacket.WriteInt32(Char.Money); Client.SendEncrypted((byte)PacketType.CITY_TOKEN, SuccessPacket.ToArray()); } /*else * { * ClientAuthenticated = false; * break; * }*/ } NetworkFacade.TransferringClients.TryRemove(out TokenToRemove); //This is not really valid anymore, because if the token doesn't exist yet, //the client will now receive it when it arrives - see LoginPacketHandlers.cs // - HandleClientToken() /*if (!ClientAuthenticated) * { * PacketStream ErrorPacket = new PacketStream((byte)PacketType.CITY_TOKEN, 0); * ErrorPacket.WriteByte((byte)CityTransferStatus.GeneralError); * Client.SendEncrypted((byte)PacketType.CITY_TOKEN, ErrorPacket.ToArray()); * }*/ } } catch (Exception E) { Logger.LogDebug("Exception in HandleCityToken: " + E.ToString()); Debug.WriteLine("Exception in HandleCityToken: " + E.ToString()); PacketStream ErrorPacket = new PacketStream((byte)PacketType.CITY_TOKEN, 0); ErrorPacket.WriteByte((byte)CityTransferStatus.GeneralError); Client.SendEncrypted((byte)PacketType.CITY_TOKEN, ErrorPacket.ToArray()); } }
/// <summary> /// LoginServer sent information about the player's characters. /// </summary> /// <param name="Packet">The packet that was received.</param> public static void OnCharacterInfoResponse(ProcessedPacket Packet, NetworkClient Client) { byte NumCharacters = (byte)Packet.ReadByte(); byte NewCharacters = (byte)Packet.ReadByte(); List<UISim> FreshSims = new List<UISim>(); for (int i = 0; i < NewCharacters; i++) { int CharacterID = Packet.ReadInt32(); UISim FreshSim = new UISim(Packet.ReadString(), false); FreshSim.CharacterID = CharacterID; FreshSim.Timestamp = Packet.ReadString(); FreshSim.Name = Packet.ReadString(); FreshSim.Sex = Packet.ReadString(); FreshSim.Description = Packet.ReadString(); FreshSim.HeadOutfitID = Packet.ReadUInt64(); FreshSim.BodyOutfitID = Packet.ReadUInt64(); FreshSim.Avatar.Appearance = (AppearanceType)Packet.ReadByte(); FreshSim.ResidingCity = new CityInfo(false); FreshSim.ResidingCity.Name = Packet.ReadString(); FreshSim.ResidingCity.Thumbnail = Packet.ReadUInt64(); FreshSim.ResidingCity.UUID = Packet.ReadString(); FreshSim.ResidingCity.Map = Packet.ReadUInt64(); FreshSim.ResidingCity.IP = Packet.ReadString(); FreshSim.ResidingCity.Port = Packet.ReadInt32(); FreshSims.Add(FreshSim); } lock (NetworkFacade.Avatars) { if ((NumCharacters < 3) && (NewCharacters > 0)) { FreshSims = Cache.LoadCachedSims(FreshSims); NetworkFacade.Avatars = FreshSims; Cache.CacheSims(FreshSims); } if (NewCharacters == 0 && NumCharacters > 0) NetworkFacade.Avatars = Cache.LoadAllSims(); else if (NewCharacters == 3 && NumCharacters == 3) { NetworkFacade.Avatars = FreshSims; Cache.CacheSims(FreshSims); } else if (NewCharacters == 0 && NumCharacters == 0) { //Make sure if sims existed in the cache, they are deleted (because they didn't exist in DB). Cache.DeleteCache(); } else if (NumCharacters == 3 && NewCharacters == 3) { NetworkFacade.Avatars = FreshSims; } } PacketStream CityInfoRequest = new PacketStream(0x06, 0); CityInfoRequest.WriteByte(0x00); //Dummy Client.SendEncrypted((byte)PacketType.CITY_LIST, CityInfoRequest.ToArray()); }
/// <summary> /// Received from the CityServer in response to a CITY_TOKEN packet. /// </summary> public static CityTransferStatus OnCityTokenResponse(NetworkClient Client, ProcessedPacket Packet) { LogThis.Log.LogThis("Received OnCityTokenResponse", LogThis.eloglevel.info); CityTransferStatus Status = (CityTransferStatus)Packet.ReadByte(); ushort NumHouses = Packet.ReadUInt16(); LotTileEntry[] TileEntries = new LotTileEntry[NumHouses]; if (NumHouses > 0) { for(int i = 0; i < NumHouses; i++) { TileEntries[i] = new LotTileEntry(Packet.ReadInt32(), Packet.ReadString(), (short)Packet.ReadUInt16(), (short)Packet.ReadUInt16(), (byte)Packet.ReadByte(), Packet.ReadInt32()); } } PlayerAccount.Money = Packet.ReadInt32(); lock(TSOClient.Code.GameFacade.CDataRetriever.LotTileData) TSOClient.Code.GameFacade.CDataRetriever.LotTileData = TileEntries; return Status; }
/// <summary> /// Received from the LoginServer in response to a CITY_TOKEN_REQUEST packet. /// </summary> public static void OnCityToken(NetworkClient Client, ProcessedPacket Packet) { PlayerAccount.CityToken = Packet.ReadString(); System.Diagnostics.Debug.WriteLine("CityToken: " + PlayerAccount.CityToken); }
/// <summary> /// Received from the LoginServer in response to a CITY_TOKEN_REQUEST packet. /// </summary> public static void OnCityToken(NetworkClient Client, ProcessedPacket Packet) { PlayerAccount.CityToken = Packet.ReadString(); Debug.WriteLine("CityToken: " + PlayerAccount.CityToken); }
/// <summary> /// Received from the LoginServer in response to a RETIRE_CHARACTER packet. /// </summary> /// <returns>Name of character that was retired.</returns> public static string OnCharacterRetirement(NetworkClient Client, ProcessedPacket Packet) { string GUID = Packet.ReadString(); return(GUID); }
/// <summary> /// A player joined a session (game) in progress. /// </summary> public static LotTileEntry OnPlayerJoinedSession(NetworkClient Client, ProcessedPacket Packet) { LotTileEntry TileEntry = new LotTileEntry(0, "", 0, 0, 0, 0); UISim Avatar = new UISim(Packet.ReadString()); Avatar.Name = Packet.ReadString(); Avatar.Sex = Packet.ReadString(); Avatar.Description = Packet.ReadString(); Avatar.HeadOutfitID = Packet.ReadUInt64(); Avatar.BodyOutfitID = Packet.ReadUInt64(); Avatar.Avatar.Appearance = (AppearanceType)Packet.ReadInt32(); byte HasHouse = (byte)Packet.ReadByte(); if (HasHouse != 0) { TileEntry = new LotTileEntry(Packet.ReadInt32(), Packet.ReadString(), (short)Packet.ReadUInt16(), (short)Packet.ReadUInt16(), (byte)Packet.ReadByte(), Packet.ReadInt32()); Avatar.LotID = TileEntry.lotid; Avatar.HouseX = TileEntry.x; Avatar.HouseY = TileEntry.y; LotTileEntry[] TileEntries = new LotTileEntry[TSOClient.Code.GameFacade.CDataRetriever.LotTileData.Length + 1]; TileEntries[0] = TileEntry; TSOClient.Code.GameFacade.CDataRetriever.LotTileData.CopyTo(TileEntries, 1); } lock (NetworkFacade.AvatarsInSession) { NetworkFacade.AvatarsInSession.Add(Avatar); } return TileEntry; }
/// <summary> /// Client created a character! /// </summary> public static void HandleCharacterCreate(NetworkClient Client, ProcessedPacket P) { Logger.LogInfo("Received CharacterCreate!"); string AccountName = SanitizeAccount(P.ReadString()); //Need to be variable length, because the success packet contains a token. PacketStream CCStatusPacket = new PacketStream((byte)PacketType.CHARACTER_CREATION_STATUS, 0); using (var db = DataAccess.Get()) { Account Acc = db.Accounts.GetByUsername(AccountName); if (Acc.NumCharacters >= 3) { CCStatusPacket.WriteByte((int)LoginDataModel.Entities.CharacterCreationStatus.ExceededCharacterLimit); Client.SendEncrypted(CCStatusPacket.PacketID, CCStatusPacket.ToArray()); return; } //TODO: Send GUID to client... var Char = new Character(); string LastCached = P.ReadString(); if (LastCached == string.Empty) { //TODO: Proper error... CCStatusPacket.WriteByte((int)LoginDataModel.Entities.CharacterCreationStatus.NameAlreadyExisted); Client.SendEncrypted(CCStatusPacket.PacketID, CCStatusPacket.ToArray()); return; } Char.LastCached = ProtoHelpers.ParseDateTime(LastCached); Char.Name = P.ReadString(); Char.Sex = P.ReadString(); Char.Description = P.ReadString(); Char.GUID = Guid.NewGuid(); Char.HeadOutfitID = (long)P.ReadUInt64(); Char.BodyOutfitID = (long)P.ReadUInt64(); Char.AccountID = Acc.AccountID; Char.AppearanceType = P.ReadByte(); Char.CityName = P.ReadString(); Char.CityThumb = (long)P.ReadUInt64(); Char.City = P.ReadString(); Char.CityMap = (long)P.ReadUInt64(); Char.CityIp = P.ReadString(); Char.CityPort = P.ReadInt32(); Char.Money = NetworkFacade.INITIAL_MONEY; //These are going into DB, so be nazi. Sieg heil! if (Char.Name == string.Empty || Char.Sex == string.Empty || Char.Description == string.Empty) { CCStatusPacket.WriteByte((int)LoginDataModel.Entities.CharacterCreationStatus.NameAlreadyExisted); Client.SendEncrypted(CCStatusPacket.PacketID, CCStatusPacket.ToArray()); return; } var status = db.Characters.CreateCharacter(Char); switch (status) { case LoginDataModel.Entities.CharacterCreationStatus.NameAlreadyExisted: CCStatusPacket.WriteByte((int)LoginDataModel.Entities.CharacterCreationStatus.NameAlreadyExisted); Client.SendEncrypted(CCStatusPacket.PacketID, CCStatusPacket.ToArray()); break; case LoginDataModel.Entities.CharacterCreationStatus.NameTooLong: CCStatusPacket.WriteByte((int)LoginDataModel.Entities.CharacterCreationStatus.NameTooLong); Client.SendEncrypted(CCStatusPacket.PacketID, CCStatusPacket.ToArray()); break; case LoginDataModel.Entities.CharacterCreationStatus.Success: Guid Token = Guid.NewGuid(); //This actually updates the record, not sure how. Acc.NumCharacters++; //THIS NEEDS TO HAPPEN FIRST FOR CITY SERVER AUTHENTICATION TO WORK! CityInfo CServer = NetworkFacade.CServerListener.GetCityServer(Char.City); //Just in case... if (CServer != null) { PacketStream CServerPacket = new PacketStream(0x01, 0); CServerPacket.WriteHeader(); ushort PacketLength = (ushort)(PacketHeaders.UNENCRYPTED + 1 + 4 + (Client.RemoteIP.Length + 1) + 4 + (Char.GUID.ToString().Length + 1) + (Token.ToString().Length + 1)); CServerPacket.WriteUInt16(PacketLength); CServerPacket.WriteByte(1); //CharacterCreate = true CServerPacket.WriteInt32(Acc.AccountID); CServerPacket.WriteString(Client.RemoteIP); CServerPacket.WriteInt32(Client.RemotePort); CServerPacket.WriteString(Char.GUID.ToString()); CServerPacket.WriteString(Token.ToString("")); CServer.Client.Send(CServerPacket.ToArray()); } CCStatusPacket.WriteByte((int)LoginDataModel.Entities.CharacterCreationStatus.Success); CCStatusPacket.WriteString(Char.GUID.ToString()); CCStatusPacket.WriteString(Token.ToString()); Client.SendEncrypted(CCStatusPacket.PacketID, CCStatusPacket.ToArray()); break; } } }
public static LotTileEntry OnLotCostResponse(NetworkClient Client, ProcessedPacket Packet) { ushort X = Packet.ReadUInt16(); ushort Y = Packet.ReadUInt16(); int LotID = Packet.ReadInt32(); string LotName = Packet.ReadString(); //bit 0 = online, bit 1 = spotlight, bit 2 = locked, bit 3 = occupied, other bits free for whatever use byte Flags = (byte)Packet.ReadByte(); int Cost = Packet.ReadInt32(); return new LotTileEntry(LotID, LotName, (short)X, (short)Y, Flags, Cost); }
/// <summary> /// Client sent a response to our challenge, as well as account name and password. /// </summary> public static void HandleChallengeResponse(NetworkClient Client, ProcessedPacket P) { PacketStream OutPacket; if (P.BufferLength <= 1) { OutPacket = new PacketStream((byte)PacketType.LOGIN_FAILURE, 0); OutPacket.WriteByte(0x03); //Bad challenge response. Client.Send(OutPacket.ToArray()); Logger.LogInfo("Bad challenge response - sent SLoginFailResponse!\r\n"); return; //How does this even happen?! } int Length = P.ReadByte(); byte[] CResponse; if (P.BufferLength >= Length) CResponse = P.ReadBytes(Length); else return; AESDecryptionArgs DecryptionArgs = Client.ClientEncryptor.GetDecryptionArgsContainer().AESDecryptArgs; if (DecryptionArgs.Challenge.SequenceEqual(CResponse)) { string AccountName = SanitizeAccount(P.ReadString()); Length = P.ReadByte(); byte[] PasswordHash; if (P.BufferLength >= Length) PasswordHash = P.ReadBytes(Length); else return; // Check whether the accountname is empty or is/contains "username" if (AccountName == string.Empty || AccountName.ToLower().Equals("username") || AccountName.ToLower().Contains("username")) { OutPacket = new PacketStream((byte)PacketType.LOGIN_FAILURE, 0); OutPacket.WriteHeader(); OutPacket.WriteUInt16((ushort)(PacketHeaders.UNENCRYPTED + 1)); OutPacket.WriteByte(0x01); Client.Send(OutPacket.ToArray()); Logger.LogInfo(@"Bad accountname (""" + AccountName + @""") - sent SLoginFailResponse!\r\n"); Client.Disconnect(); return; } using (var db = DataAccess.Get()) { var account = db.Accounts.GetByUsername(AccountName); if (!GlobalSettings.Default.CreateAccountsOnLogin) { Logger.LogInfo("Done reading LoginRequest, checking account...\r\n"); if (account == null) { OutPacket = new PacketStream((byte)PacketType.LOGIN_FAILURE, 0); OutPacket.WriteHeader(); OutPacket.WriteUInt16((ushort)(PacketHeaders.UNENCRYPTED + 1)); OutPacket.WriteByte(0x01); Client.Send(OutPacket.ToArray()); Logger.LogInfo(@"Bad accountname (""" + AccountName + @""") - sent SLoginFailResponse!\r\n"); Client.Disconnect(); return; } } else { if (account == null) { try { if (!AccountName.ToLower().Equals("username") || !AccountName.ToLower().Contains("username")) db.Accounts.Create(new Account { AccountName = AccountName.ToLower(), Password = Convert.ToBase64String(PasswordHash) }); } catch (Exception) { OutPacket = new PacketStream((byte)PacketType.LOGIN_FAILURE, 0); OutPacket.WriteHeader(); OutPacket.WriteUInt16((ushort)(PacketHeaders.UNENCRYPTED + 1)); OutPacket.WriteByte(0x01); Client.Send(OutPacket.ToArray()); Logger.LogInfo(@"Bad accountname (""" + AccountName + @""") - sent SLoginFailResponse!\r\n"); Client.Disconnect(); return; } account = db.Accounts.GetByUsername(AccountName); } } if (account.IsCorrectPassword(AccountName, PasswordHash)) { OutPacket = new PacketStream((byte)PacketType.LOGIN_SUCCESS, 0); OutPacket.WriteByte(0x01); Client.ClientEncryptor.Username = AccountName; Client.SendEncrypted((byte)PacketType.LOGIN_SUCCESS, OutPacket.ToArray()); Logger.LogInfo("Sent SLoginSuccessResponse!\r\n"); return; } else { OutPacket = new PacketStream((byte)PacketType.LOGIN_FAILURE, 0); OutPacket.WriteHeader(); OutPacket.WriteUInt16((ushort)(PacketHeaders.UNENCRYPTED + 1)); OutPacket.WriteByte(0x02); Client.Send(OutPacket.ToArray()); Logger.LogInfo("Bad password - sent SLoginFailResponse!\r\n"); Client.Disconnect(); return; } } } OutPacket = new PacketStream((byte)PacketType.LOGIN_FAILURE, 0); OutPacket.WriteHeader(); OutPacket.WriteUInt16((ushort)(PacketHeaders.UNENCRYPTED + 1)); OutPacket.WriteByte(0x03); //Bad challenge response. Client.Send(OutPacket.ToArray()); Logger.LogInfo("Bad challenge response - sent SLoginFailResponse!\r\n"); return; }
/// <summary> /// New city server came online! /// </summary> public static void OnNewCityServer(NetworkClient Client, ProcessedPacket Packet) { lock (NetworkFacade.Cities) { CityInfo Info = new CityInfo(false); Info.Name = Packet.ReadString(); Info.Description = Packet.ReadString(); Info.IP = Packet.ReadString(); Info.Port = Packet.ReadInt32(); Info.Status = (CityInfoStatus)Packet.ReadByte(); Info.Thumbnail = Packet.ReadUInt64(); Info.UUID = Packet.ReadString(); Info.Map = Packet.ReadUInt64(); NetworkFacade.Cities.Add(Info); } }
/// <summary> /// Client wanted to create a new character. /// </summary> public static void HandleCharacterCreate(NetworkClient Client, ProcessedPacket P) { try { Logger.LogInfo("Received CharacterCreate!"); bool ClientAuthenticated = false; byte AccountStrLength = (byte)P.ReadByte(); byte[] AccountNameBuf = new byte[AccountStrLength]; if (P.BufferLength >= AccountStrLength) { P.Read(AccountNameBuf, 0, AccountStrLength); string AccountName = Encoding.ASCII.GetString(AccountNameBuf); } else { return; } using (DataAccess db = DataAccess.Get()) { //No need to check for empty string here, because all it will do is have ClientAuthenticated be false string Token = P.ReadString(); string GUID = ""; int AccountID = 0; ClientToken TokenToRemove = NetworkFacade.GetClientToken(Client.RemoteIP); //Just in case... if (TokenToRemove != null) { if (TokenToRemove.Token.Equals(Token, StringComparison.CurrentCultureIgnoreCase)) { PacketStream SuccessPacket = new PacketStream((byte)PacketType.CHARACTER_CREATE_CITY, 0); SuccessPacket.WriteByte((byte)CityDataModel.Entities.CharacterCreationStatus.Success); House[] Houses = NetworkFacade.CurrentSession.GetHousesInSession(); SuccessPacket.WriteUInt16((ushort)Houses.Length); //Ho, ho, ho... foreach (House Ho in Houses) { SuccessPacket.WriteInt32(Ho.HouseID); SuccessPacket.WriteString(Ho.Name); SuccessPacket.WriteUInt16((ushort)Ho.X); SuccessPacket.WriteUInt16((ushort)Ho.Y); SuccessPacket.WriteByte((byte)Ho.Flags); //Might have to save this as unsigned in DB? SuccessPacket.WriteInt32(Ho.Cost); } Client.SendEncrypted((byte)PacketType.CHARACTER_CREATE_CITY, SuccessPacket.ToArray()); ClientAuthenticated = true; GUID = TokenToRemove.CharacterGUID; AccountID = TokenToRemove.AccountID; Sim Char = new Sim(new Guid(GUID)); Char.Timestamp = P.ReadString(); Char.Name = P.ReadString(); Char.Sex = P.ReadString(); Char.Description = P.ReadString(); Char.HeadOutfitID = P.ReadUInt64(); Char.BodyOutfitID = P.ReadUInt64(); Char.Appearance = (AppearanceType)P.ReadByte(); Char.CreatedThisSession = true; //These are going into DB, so be nazi. Sieg heil! if (Char.Timestamp == string.Empty || Char.Name == string.Empty || Char.Sex == string.Empty || Char.Description == string.Empty) { //TODO: Tell loginserver to clean up DB? ClientAuthenticated = false; } var characterModel = new Character(); characterModel.Name = Char.Name; characterModel.Sex = Char.Sex; characterModel.Description = Char.Description; characterModel.LastCached = ProtoHelpers.ParseDateTime(Char.Timestamp); characterModel.GUID = Char.GUID; characterModel.HeadOutfitID = (long)Char.HeadOutfitID; characterModel.BodyOutfitID = (long)Char.BodyOutfitID; characterModel.AccountID = AccountID; characterModel.AppearanceType = (int)Char.Appearance; characterModel.Money = NetworkFacade.INITIAL_MONEY; NetworkFacade.CurrentSession.AddPlayer(Client, characterModel); var status = db.Characters.CreateCharacter(characterModel); } } NetworkFacade.TransferringClients.TryRemove(out TokenToRemove); } //Invalid token, should never occur... if (!ClientAuthenticated) { NetworkFacade.CurrentSession.RemovePlayer(Client); PacketStream FailPacket = new PacketStream((byte)PacketType.CHARACTER_CREATE_CITY_FAILED, (int)(PacketHeaders.ENCRYPTED + 1)); FailPacket.WriteByte((byte)CityDataModel.Entities.CharacterCreationStatus.GeneralError); Client.SendEncrypted((byte)PacketType.CHARACTER_CREATE_CITY_FAILED, FailPacket.ToArray()); } } catch (Exception E) { Debug.WriteLine("Exception in HandleCharacterCreate: " + E.ToString()); Logger.LogDebug("Exception in HandleCharacterCreate: " + E.ToString()); PacketStream FailPacket = new PacketStream((byte)PacketType.CHARACTER_CREATE_CITY_FAILED, (int)(PacketHeaders.ENCRYPTED + 1)); FailPacket.WriteByte((byte)CityDataModel.Entities.CharacterCreationStatus.GeneralError); Client.SendEncrypted((byte)PacketType.CHARACTER_CREATE_CITY_FAILED, FailPacket.ToArray()); } }
public static void HandleCharacterInfoRequest(NetworkClient Client, ProcessedPacket P) { Logger.LogDebug("Received CharacterInfoRequest!"); DateTime Timestamp = DateTime.Parse(P.ReadString()); //Database.CheckCharacterTimestamp(Client.Username, Client, TimeStamp); Character[] Characters = new Character[] { }; using (var db = DataAccess.Get()) { var account = db.Accounts.GetByUsername(Client.ClientEncryptor.Username); Characters = db.Characters.GetForAccount((int)account.AccountID).ToArray(); } if (Characters != null) { PacketStream Packet = new PacketStream(0x05, 0); MemoryStream PacketData = new MemoryStream(); BinaryWriter PacketWriter = new BinaryWriter(PacketData); /** * Whats the point of checking a timestamp here? It saves a few bytes on a packet * sent once per user session. Premature optimization. */ PacketWriter.Write((byte)Characters.Length); foreach (Character avatar in Characters) { PacketWriter.Write((int)avatar.CharacterID); PacketWriter.Write(avatar.GUID.ToString()); PacketWriter.Write(avatar.LastCached); PacketWriter.Write(avatar.Name); PacketWriter.Write(avatar.Sex); PacketWriter.Write(avatar.Description); PacketWriter.Write((ulong)avatar.HeadOutfitID); PacketWriter.Write((ulong)avatar.BodyOutfitID); PacketWriter.Write((byte)avatar.AppearanceType); PacketWriter.Write((string)avatar.CityName); PacketWriter.Write((ulong)avatar.CityThumb); PacketWriter.Write((string)avatar.City); PacketWriter.Write((ulong)avatar.CityMap); PacketWriter.Write((string)avatar.CityIp); PacketWriter.Write((int)avatar.CityPort); } Packet.Write(PacketData.ToArray(), 0, (int)PacketData.Length); PacketWriter.Close(); Client.SendEncrypted(0x05, Packet.ToArray()); } else //No characters existed for the account. { PacketStream Packet = new PacketStream(0x05, 0); Packet.WriteByte(0x00); //0 characters. Client.SendEncrypted(0x05, Packet.ToArray()); } }