Skip to content

Commit

Permalink
Merge pull request #2 from mikkelbfriis/master
Browse files Browse the repository at this point in the history
Cleaning out
  • Loading branch information
Sledmore authored Jan 6, 2018
2 parents 3b2a4a0 + 750f726 commit 352b1ef
Show file tree
Hide file tree
Showing 67 changed files with 1,104 additions and 1,179 deletions.
71 changes: 36 additions & 35 deletions Communication/GamePacketParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ namespace Plus.Communication
{
public class GamePacketParser : IDataParser
{
private static readonly ILog log = LogManager.GetLogger("Plus.Communication.GamePacketParser");
private static readonly ILog Log = LogManager.GetLogger("Plus.Communication.GamePacketParser");

public delegate void HandlePacket(ClientPacket message);

private readonly GameClient _client;

private bool _halfDataRecieved = false;
private byte[] _halfData = null;
private bool _deciphered = false;
private bool _halfDataRecieved;
private byte[] _halfData;
private bool _deciphered;

public GamePacketParser(GameClient client)
{
Expand All @@ -29,60 +29,61 @@ public void HandlePacketData(byte[] data)
{
try
{
if (this._client.Rc4Client != null && !this._deciphered)
if (_client.Rc4Client != null && !_deciphered)
{
this._client.Rc4Client.Decrypt(ref data);
this._deciphered = true;
_client.Rc4Client.Decrypt(ref data);
_deciphered = true;
}

if (this._halfDataRecieved)
if (_halfDataRecieved)
{
byte[] FullDataRcv = new byte[this._halfData.Length + data.Length];
Buffer.BlockCopy(this._halfData, 0, FullDataRcv, 0, this._halfData.Length);
Buffer.BlockCopy(data, 0, FullDataRcv, this._halfData.Length, data.Length);
byte[] fullDataRcv = new byte[_halfData.Length + data.Length];
Buffer.BlockCopy(_halfData, 0, fullDataRcv, 0, _halfData.Length);
Buffer.BlockCopy(data, 0, fullDataRcv, _halfData.Length, data.Length);

this._halfDataRecieved = false; // mark done this round
HandlePacketData(FullDataRcv); // repeat now we have the combined array
_halfDataRecieved = false; // mark done this round
HandlePacketData(fullDataRcv); // repeat now we have the combined array
return;
}

using (BinaryReader Reader = new BinaryReader(new MemoryStream(data)))
using (BinaryReader reader = new BinaryReader(new MemoryStream(data)))
{
if (data.Length < 4)
return;

int MsgLen = HabboEncoding.DecodeInt32(Reader.ReadBytes(4));
if ((Reader.BaseStream.Length - 4) < MsgLen)
int msgLen = HabboEncoding.DecodeInt32(reader.ReadBytes(4));
if ((reader.BaseStream.Length - 4) < msgLen)
{
this._halfData = data;
this._halfDataRecieved = true;
_halfData = data;
_halfDataRecieved = true;
return;
}
else if (MsgLen < 0 || MsgLen > 5120)//TODO: Const somewhere.

if (msgLen < 0 || msgLen > 5120)//TODO: Const somewhere.
return;

byte[] Packet = Reader.ReadBytes(MsgLen);
byte[] packet = reader.ReadBytes(msgLen);

using (BinaryReader R = new BinaryReader(new MemoryStream(Packet)))
using (BinaryReader r = new BinaryReader(new MemoryStream(packet)))
{
int Header = HabboEncoding.DecodeInt16(R.ReadBytes(2));
int header = HabboEncoding.DecodeInt16(r.ReadBytes(2));

byte[] Content = new byte[Packet.Length - 2];
Buffer.BlockCopy(Packet, 2, Content, 0, Packet.Length - 2);
byte[] content = new byte[packet.Length - 2];
Buffer.BlockCopy(packet, 2, content, 0, packet.Length - 2);

ClientPacket Message = new ClientPacket(Header, Content);
onNewPacket.Invoke(Message);
ClientPacket message = new ClientPacket(header, content);
OnNewPacket.Invoke(message);

this._deciphered = false;
_deciphered = false;
}

if (Reader.BaseStream.Length - 4 > MsgLen)
if (reader.BaseStream.Length - 4 > msgLen)
{
byte[] Extra = new byte[Reader.BaseStream.Length - Reader.BaseStream.Position];
Buffer.BlockCopy(data, (int)Reader.BaseStream.Position, Extra, 0, (int)(Reader.BaseStream.Length - Reader.BaseStream.Position));
byte[] extra = new byte[reader.BaseStream.Length - reader.BaseStream.Position];
Buffer.BlockCopy(data, (int)reader.BaseStream.Position, extra, 0, (int)(reader.BaseStream.Length - reader.BaseStream.Position));

this._deciphered = true;
HandlePacketData(Extra);
_deciphered = true;
HandlePacketData(extra);
}
}
}
Expand All @@ -96,7 +97,7 @@ public void HandlePacketData(byte[] data)

public void Dispose()
{
onNewPacket = null;
OnNewPacket = null;
GC.SuppressFinalize(this);
}

Expand All @@ -105,12 +106,12 @@ public object Clone()
return new GamePacketParser(_client);
}

public event HandlePacket onNewPacket;
public event HandlePacket OnNewPacket;

public void SetConnection(ConnectionInformation con)
{
// Connection information passes through, but we seemingly do nothing?
onNewPacket = null;
OnNewPacket = null;
}
}
}
4 changes: 2 additions & 2 deletions Communication/InitialPacketParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class InitialPacketParser : IDataParser
{
public delegate void NoParamDelegate();

public byte[] currentData;
public byte[] CurrentData;

public void HandlePacketData(byte[] packet)
{
Expand All @@ -17,7 +17,7 @@ public void HandlePacketData(byte[] packet)
}
else if (packet[0] != 67 && SwitchParserRequest != null)
{
currentData = packet;
CurrentData = packet;
SwitchParserRequest.Invoke();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public void Parse(HabboHotel.GameClients.GameClient Session, ClientPacket Packet
Item GiveItem = ItemFactory.CreateGiftItem(PresentData, Habbo, ED, ED, NewItemId, 0, 0);
if (GiveItem != null)
{
GameClient Receiver = PlusEnvironment.GetGame().GetClientManager().GetClientByUserID(Habbo.Id);
GameClient Receiver = PlusEnvironment.GetGame().GetClientManager().GetClientByUserId(Habbo.Id);
if (Receiver != null)
{
Receiver.GetHabbo().GetInventoryComponent().TryAddItem(GiveItem);
Expand Down
72 changes: 28 additions & 44 deletions Communication/Packets/Incoming/ClientPacket.cs
Original file line number Diff line number Diff line change
@@ -1,81 +1,70 @@
using System;
using System.Text;

using Plus.Utilities;
using Plus.Communication.Packets.Incoming;

namespace Plus.Communication.Packets.Incoming
{
public class ClientPacket
{
private byte[] Body;
private int MessageId;
private int Pointer;
private byte[] _body;
private int _pointer;

public ClientPacket(int messageID, byte[] body)
public ClientPacket(int messageId, byte[] body)
{
Init(messageID, body);
Init(messageId, body);
}

public int Id
{
get { return MessageId; }
}
public int Id { get; private set; }

public int RemainingLength
{
get { return Body.Length - Pointer; }
}

public int Header
{
get { return MessageId; }
get { return _body.Length - _pointer; }
}

public void Init(int messageID, byte[] body)
public void Init(int messageId, byte[] body)
{
if (body == null)
body = new byte[0];

MessageId = messageID;
Body = body;
Id = messageId;
_body = body;

Pointer = 0;
_pointer = 0;
}

public override string ToString()
{
return "[" + Header + "] BODY: " + (PlusEnvironment.GetDefaultEncoding().GetString(Body).Replace(Convert.ToChar(0).ToString(), "[0]"));
return "[" + Id + "] BODY: " + (PlusEnvironment.GetDefaultEncoding().GetString(_body).Replace(Convert.ToChar(0).ToString(), "[0]"));
}

public void AdvancePointer(int i)
{
Pointer += i*4;
_pointer += i*4;
}

public byte[] ReadBytes(int Bytes)
public byte[] ReadBytes(int bytes)
{
if (Bytes > RemainingLength)
Bytes = RemainingLength;
if (bytes > RemainingLength)
bytes = RemainingLength;

var data = new byte[Bytes];
var data = new byte[bytes];

for (int i = 0; i < Bytes; i++)
data[i] = Body[Pointer++];
for (int i = 0; i < bytes; i++)
data[i] = _body[_pointer++];

return data;
}

public byte[] PlainReadBytes(int Bytes)
public byte[] PlainReadBytes(int bytes)
{
if (Bytes > RemainingLength)
Bytes = RemainingLength;
if (bytes > RemainingLength)
bytes = RemainingLength;

var data = new byte[Bytes];
var data = new byte[bytes];

for (int x = 0, y = Pointer; x < Bytes; x++, y++)
for (int x = 0, y = _pointer; x < bytes; x++, y++)
{
data[x] = Body[y];
data[x] = _body[y];
}

return data;
Expand All @@ -94,12 +83,7 @@ public string PopString()

public bool PopBoolean()
{
if (RemainingLength > 0 && Body[Pointer++] == Convert.ToChar(1))
{
return true;
}

return false;
return RemainingLength > 0 && _body[_pointer++] == Convert.ToChar(1);
}

public int PopInt()
Expand All @@ -109,11 +93,11 @@ public int PopInt()
return 0;
}

byte[] Data = PlainReadBytes(4);
byte[] data = PlainReadBytes(4);

Int32 i = HabboEncoding.DecodeInt32(Data);
int i = HabboEncoding.DecodeInt32(data);

Pointer += 4;
_pointer += 4;

return i;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Parse(HabboHotel.GameClients.GameClient Session, ClientPacket Packet
return;
}

GameClient Client = PlusEnvironment.GetGame().GetClientManager().GetClientByUserID(Convert.ToInt32(UserId));
GameClient Client = PlusEnvironment.GetGame().GetClientManager().GetClientByUserId(Convert.ToInt32(UserId));
if (Client == null)
{
Session.SendPacket(new SubmitBullyReportComposer(0));//Just say it's sent, the user isn't found.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void Parse(HabboHotel.GameClients.GameClient Session, ClientPacket Packet
if (BuddyId == 0 || BuddyId == Session.GetHabbo().Id)
return;

GameClient Client = PlusEnvironment.GetGame().GetClientManager().GetClientByUserID(BuddyId);
GameClient Client = PlusEnvironment.GetGame().GetClientManager().GetClientByUserId(BuddyId);
if (Client == null || Client.GetHabbo() == null)
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void Parse(HabboHotel.GameClients.GameClient Session, ClientPacket Packet
if (Session.GetHabbo().Relationships.ContainsKey(Id))
Session.GetHabbo().Relationships.Remove(Id);

GameClient Target = PlusEnvironment.GetGame().GetClientManager().GetClientByUserID(Id);
GameClient Target = PlusEnvironment.GetGame().GetClientManager().GetClientByUserId(Id);
if (Target != null)
{
if (Target.GetHabbo().Relationships.ContainsKey(Session.GetHabbo().Id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void Parse(HabboHotel.GameClients.GameClient Session, ClientPacket Packet
if (!Session.GetHabbo().GetMessenger().FriendshipExists(UserId))
continue;

GameClient Client = PlusEnvironment.GetGame().GetClientManager().GetClientByUserID(UserId);
GameClient Client = PlusEnvironment.GetGame().GetClientManager().GetClientByUserId(UserId);
if (Client == null || Client.GetHabbo() == null || Client.GetHabbo().AllowMessengerInvites == true || Client.GetHabbo().AllowConsoleMessages == false)
continue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void Parse(HabboHotel.GameClients.GameClient session, ClientPacket packet
if (ticket.Moderator.Id != session.GetHabbo().Id)
return;

GameClient Client = PlusEnvironment.GetGame().GetClientManager().GetClientByUserID(ticket.Sender.Id);
GameClient Client = PlusEnvironment.GetGame().GetClientManager().GetClientByUserId(ticket.Sender.Id);
if (Client != null)
{
Client.SendPacket(new ModeratorSupportTicketResponseComposer(Result));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void Parse(HabboHotel.GameClients.GameClient Session, ClientPacket Packet
return;

int UserId = Packet.PopInt();
GameClient Target = PlusEnvironment.GetGame().GetClientManager().GetClientByUserID(UserId);
GameClient Target = PlusEnvironment.GetGame().GetClientManager().GetClientByUserId(UserId);
if (Target == null)
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void Parse(HabboHotel.GameClients.GameClient Session, ClientPacket Packet
int UserId = Packet.PopInt();
String Message = Packet.PopString();

GameClient Client = PlusEnvironment.GetGame().GetClientManager().GetClientByUserID(UserId);
GameClient Client = PlusEnvironment.GetGame().GetClientManager().GetClientByUserId(UserId);
if (Client == null || Client.GetHabbo() == null)
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void Parse(HabboHotel.GameClients.GameClient Session, ClientPacket Packet
int UserId = Packet.PopInt();
string Message = Packet.PopString();

GameClient Client = PlusEnvironment.GetGame().GetClientManager().GetClientByUserID(UserId);
GameClient Client = PlusEnvironment.GetGame().GetClientManager().GetClientByUserId(UserId);
if (Client == null || Client.GetHabbo() == null || Client.GetHabbo().CurrentRoomId < 1 || Client.GetHabbo().Id == Session.GetHabbo().Id)
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void Parse(GameClient Session, ClientPacket Packet)
int UserId = Packet.PopInt();
string Message = Packet.PopString();

GameClient Client = PlusEnvironment.GetGame().GetClientManager().GetClientByUserID(UserId);
GameClient Client = PlusEnvironment.GetGame().GetClientManager().GetClientByUserId(UserId);
if (Client == null)
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void Parse(HabboHotel.GameClients.GameClient Session, ClientPacket Packet

if (pet.OwnerId != Session.GetHabbo().Id)
{
GameClient Target = PlusEnvironment.GetGame().GetClientManager().GetClientByUserID(pet.OwnerId);
GameClient Target = PlusEnvironment.GetGame().GetClientManager().GetClientByUserId(pet.OwnerId);
if (Target != null)
{
Target.GetHabbo().GetInventoryComponent().TryAddPet(Pet.PetData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void Parse(GameClient session, ClientPacket packet)
}
else//Item is being ejected.
{
GameClient targetClient = PlusEnvironment.GetGame().GetClientManager().GetClientByUserID(item.UserID);
GameClient targetClient = PlusEnvironment.GetGame().GetClientManager().GetClientByUserId(item.UserID);
if (targetClient != null && targetClient.GetHabbo() != null)//Again, do we have an active client?
{
Room.GetRoomItemHandler().RemoveFurniture(targetClient, item.Id);
Expand Down
Loading

0 comments on commit 352b1ef

Please sign in to comment.