Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CoolQ Pack C# Reader
class BinaryReaderBigEndian : BinaryReader
{
private byte[] a16 = new byte[2];
private byte[] a32 = new byte[4];
private byte[] a64 = new byte[8];
public BinaryReaderBigEndian(Stream stream) : base(stream) { }
public override short ReadInt16()
{
a16 = base.ReadBytes(2);
if (BitConverter.IsLittleEndian)
{
Array.Reverse(a16);
}
return BitConverter.ToInt16(a16, 0);
}
public override int ReadInt32()
{
a32 = base.ReadBytes(4);
Array.Reverse(a32);
return BitConverter.ToInt32(a32, 0);
}
public override long ReadInt64()
{
a64 = base.ReadBytes(8);
if (BitConverter.IsLittleEndian)
{
Array.Reverse(a64);
}
return BitConverter.ToInt64(a64, 0);
}
public override ushort ReadUInt16()
{
a16 = base.ReadBytes(2);
if (BitConverter.IsLittleEndian)
{
Array.Reverse(a16);
}
return BitConverter.ToUInt16(a16, 0);
}
public override uint ReadUInt32()
{
a32 = base.ReadBytes(4);
if (BitConverter.IsLittleEndian)
{
Array.Reverse(a32);
}
return BitConverter.ToUInt32(a32, 0);
}
public override ulong ReadUInt64()
{
a64 = base.ReadBytes(8);
if (BitConverter.IsLittleEndian)
{
Array.Reverse(a64);
}
return BitConverter.ToUInt64(a64, 0);
}
}
class PackReader : BinaryReaderBigEndian
{
public PackReader(Stream input) : base(input) { }
public static PackReader GetReader(string base64String)
{
var bytes = Convert.FromBase64String(base64String);
return new PackReader(new MemoryStream(bytes));
}
private byte[] ReadBytes()
{
var length = ReadUInt16();
return ReadBytes(length);
}
public string ReadStringAnsi()
{
var bytes = ReadBytes();
return Encoding.Default.GetString(bytes);
}
public string ReadStringUtf8()
{
var bytes = ReadBytes();
return Encoding.UTF8.GetString(bytes);
}
}
class PackConverter
{
public struct StrangerInfo
{
public ulong id;
public string nickname;
public uint gender;
public uint age;
}
public struct GroupMemberInfo
{
public ulong group;
public ulong id;
public string nickname;
public string nicknameInGroup;
public uint gender;
public uint age;
public string location;
public uint joinedAt;
public uint lastSentAt;
public string levelName;
public uint adminRole;
public bool isOffender;
public string title;
public uint titleExpiresAt;
public bool nicknameInGroupModifiable;
}
public static StrangerInfo ToStrangerInfo(string base64Pack)
{
var info = new StrangerInfo();
var reader = PackReader.GetReader(base64Pack);
info.id = reader.ReadUInt64();
info.nickname = reader.ReadStringUtf8();
info.gender = reader.ReadUInt32();
info.age = reader.ReadUInt32();
return info;
}
public static GroupMemberInfo ToGroupMemberInfo(string base64Pack)
{
var info = new GroupMemberInfo();
var reader = PackReader.GetReader(base64Pack);
info.group = reader.ReadUInt64();
info.id = reader.ReadUInt64();
info.nickname = reader.ReadStringUtf8();
info.nicknameInGroup = reader.ReadStringUtf8();
info.gender = reader.ReadUInt32();
info.age = reader.ReadUInt32();
info.location = reader.ReadStringUtf8();
info.joinedAt = reader.ReadUInt32();
info.lastSentAt = reader.ReadUInt32();
info.levelName = reader.ReadStringUtf8();
info.adminRole = reader.ReadUInt32();
info.isOffender = reader.ReadUInt32() == 1;
info.title = reader.ReadStringUtf8();
info.titleExpiresAt = reader.ReadUInt32();
info.nicknameInGroupModifiable = reader.ReadUInt32() == 1;
return info;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment