Skip to content

Commit

Permalink
Merge pull request #10 from Megghy/main
Browse files Browse the repository at this point in the history
add TileSquare packet, fix liquid update
  • Loading branch information
cc004 authored Oct 20, 2021
2 parents 446a080 + 35f66ae commit df12242
Show file tree
Hide file tree
Showing 11 changed files with 273 additions and 421 deletions.
22 changes: 22 additions & 0 deletions TrProtocol/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TrProtocol
{
public static class Constants
{
public static readonly bool[] tileFrameImportant = Create(624, true, 3, 4, 5, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 24, 26, 27, 28, 29, 31, 33, 34, 35, 36, 42, 49, 50, 55, 61, 71, 72, 73, 74, 77, 78, 79, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 110, 113, 114, 125, 126, 128, 129, 132, 133, 134, 135, 136, 137, 138, 139, 141, 142, 143, 144, 149, 165, 171, 172, 173, 174, 178, 184, 185, 186, 187, 201, 207, 209, 210, 212, 215, 216, 217, 218, 219, 220, 227, 228, 231, 233, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 254, 269, 270, 271, 275, 276, 277, 278, 279, 280, 281, 282, 283, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 314, 316, 317, 318, 319, 320, 323, 324, 334, 335, 337, 338, 339, 349, 354, 355, 356, 358, 359, 360, 361, 362, 363, 364, 372, 373, 374, 375, 376, 377, 378, 380, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 405, 406, 410, 411, 412, 413, 414, 419, 420, 423, 424, 425, 427, 428, 429, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 452, 453, 454, 455, 456, 457, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 475, 476, 480, 484, 485, 486, 487, 488, 489, 490, 491, 493, 494, 497, 499, 505, 506, 509, 510, 511, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 529, 530, 531, 532, 533, 538, 542, 543, 544, 545, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 558, 559, 560, 564, 565, 567, 568, 569, 570, 571, 572, 573, 579, 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599, 600, 601, 602, 603, 604, 605, 606, 607, 608, 609, 610, 611, 612, 613, 614, 615, 616, 617, 619, 620, 621, 622, 623);
private static T[] Create<T>(int count, T nonDefaultValue, params int[] indexes)
{
var result = new T[count];
foreach (var item in indexes)
{
result[item] = nonDefaultValue;
}
return result;
}
}
}
9 changes: 9 additions & 0 deletions TrProtocol/Models/Position.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
{
public partial struct Position
{
public Position(int x, int y)
{
X = x;
Y = y;
}
public int X, Y;
public override string ToString()
{
return $"[{X}, {Y}]";
}
}
}
5 changes: 5 additions & 0 deletions TrProtocol/Models/ShortPosition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
{
public partial struct ShortPosition
{
public ShortPosition(short x, short y)
{
X = x;
Y = y;
}
public short X, Y;
public override string ToString()
{
Expand Down
16 changes: 16 additions & 0 deletions TrProtocol/Models/SimpleTileData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace TrProtocol.Models
{
public struct SimpleTileData
{
public BitsByte Flags1;
public BitsByte Flags2;
public byte TileColor { get; set; }
public byte WallColor { get; set; }
public ushort TileType { get; set; }
public short FrameX { get; set; }
public short FrameY { get; set; }
public ushort WallType { get; set; }
public byte Liquid { get; set; }
public byte LiquidType { get; set; }
}
}
12 changes: 12 additions & 0 deletions TrProtocol/Models/SquareData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace TrProtocol.Models
{
public partial class SquareData
{
public short TilePosX { get; set; }
public short TilePosY { get; set; }
public byte Width { get; set; }
public byte Height { get; set; }
public TileChangeType ChangeType { get; set; }
public SimpleTileData[,] Tiles { get; set; }
}
}
13 changes: 13 additions & 0 deletions TrProtocol/Models/TileChangeType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using TrProtocol.Serializers;

namespace TrProtocol.Models
{
[Serializer(typeof(ByteEnumSerializer<TileChangeType>))]
public enum TileChangeType : byte
{
None,
LavaWater,
HoneyWater,
HoneyLava
}
}
133 changes: 70 additions & 63 deletions TrProtocol/PacketSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,88 +16,95 @@ public partial class PacketSerializer
private Dictionary<MessageID, Func<BinaryReader, Packet>> deserializers = new();
private Dictionary<NetModuleType, Func<BinaryReader, NetModulesPacket>> moduledeserializers = new();

public void LoadPackets(Assembly asm)
private void LoadPackets(Assembly asm)
{
foreach (var type in asm.GetTypes())
{
if (type.IsAbstract || !type.IsSubclassOf(typeof(Packet))) continue;
Serializer serializer = null;
Deserializer deserializer = null;
RegisterPacket(type);
}
}
public void RegisterPacket<T>() where T : Packet
{
RegisterPacket(typeof(T));
}
private void RegisterPacket(Type type)
{
if (type.IsAbstract || !type.IsSubclassOf(typeof(Packet))) return;
Serializer serializer = null;
Deserializer deserializer = null;

var dict = new Dictionary<string, PropertyInfo>();
var empty = Array.Empty<object>();
var dict = new Dictionary<string, PropertyInfo>();
var empty = Array.Empty<object>();

foreach (var prop in type.GetProperties())
{
dict.Add(prop.Name, prop);
foreach (var prop in type.GetProperties())
{
dict.Add(prop.Name, prop);

if (prop.IsDefined(typeof(IgnoreAttribute))) continue;
if (prop.IsDefined(typeof(IgnoreAttribute))) continue;

var get = prop.GetMethod;
var set = prop.SetMethod;
var t = prop.PropertyType;
var get = prop.GetMethod;
var set = prop.SetMethod;
var t = prop.PropertyType;

Func<object, bool> condition = _ => true;
Func<object, bool> condition = _ => true;

var cond = prop.GetCustomAttribute<ConditionAttribute>();
var cond = prop.GetCustomAttribute<ConditionAttribute>();

var shouldSerialize = (client
? (object)prop.GetCustomAttribute<S2COnlyAttribute>()
: prop.GetCustomAttribute<C2SOnlyAttribute>()) == null;
var shouldDeserialize = (!client
? (object)prop.GetCustomAttribute<S2COnlyAttribute>()
: prop.GetCustomAttribute<C2SOnlyAttribute>()) == null && set != null;
var shouldSerialize = (client
? (object)prop.GetCustomAttribute<S2COnlyAttribute>()
: prop.GetCustomAttribute<C2SOnlyAttribute>()) == null;
var shouldDeserialize = (!client
? (object)prop.GetCustomAttribute<S2COnlyAttribute>()
: prop.GetCustomAttribute<C2SOnlyAttribute>()) == null && set != null;

if (cond != null)
{
var get2 = dict[cond.field].GetMethod;
if (cond.bit == -1)
condition = o => ((bool)get2.Invoke(o, empty));
else
condition = o => ((BitsByte)get2.Invoke(o, empty))[cond.bit] == cond.pred;
}


var attr = t.GetCustomAttribute<SerializerAttribute>();
IFieldSerializer ser;
if (attr != null) ser = attr.serializer;
else if (!fieldSerializers.TryGetValue(t, out ser))
throw new Exception("No valid serializer for type: " + t.FullName);
if (ser is IConfigurable conf) ser = conf.Configure(prop);

if (shouldSerialize)
serializer += (o, bw) => { if (condition(o)) ser.Write(bw, get.Invoke(o, empty)); };
if (shouldDeserialize)
deserializer += (o, br) => { if (condition(o)) set.Invoke(o, new[] { ser.Read(br) }); };
if (cond != null)
{
var get2 = dict[cond.field].GetMethod;
if (cond.bit == -1)
condition = o => ((bool)get2.Invoke(o, empty));
else
condition = o => ((BitsByte)get2.Invoke(o, empty))[cond.bit] == cond.pred;
}

var inst = Activator.CreateInstance(type);

if (client ? (type.GetCustomAttribute<S2COnlyAttribute>() == null) : (type.GetCustomAttribute<C2SOnlyAttribute>()) == null)
serializers[type] = (bw, o) => serializer?.Invoke(o, bw);
var attr = t.GetCustomAttribute<SerializerAttribute>();
IFieldSerializer ser;
if (attr != null) ser = attr.serializer;
else if (!fieldSerializers.TryGetValue(t, out ser))
throw new Exception("No valid serializer for type: " + t.FullName);
if (ser is IConfigurable conf) ser = conf.Configure(prop);

if ((!client) ? (type.GetCustomAttribute<S2COnlyAttribute>() == null) : (type.GetCustomAttribute<C2SOnlyAttribute>()) == null)
if (shouldSerialize)
serializer += (o, bw) => { if (condition(o)) ser.Write(bw, get.Invoke(o, empty)); };
if (shouldDeserialize)
deserializer += (o, br) => { if (condition(o)) set.Invoke(o, new[] { ser.Read(br) }); };
}

var inst = Activator.CreateInstance(type);

if (client ? (type.GetCustomAttribute<S2COnlyAttribute>() == null) : (type.GetCustomAttribute<C2SOnlyAttribute>()) == null)
serializers[type] = (bw, o) => serializer?.Invoke(o, bw);

if ((!client) ? (type.GetCustomAttribute<S2COnlyAttribute>() == null) : (type.GetCustomAttribute<C2SOnlyAttribute>()) == null)
{
if (inst is NetModulesPacket p)
{
if (inst is NetModulesPacket p)
moduledeserializers.Add(p.ModuleType, br =>
{
moduledeserializers.Add(p.ModuleType, br =>
{
var result = Activator.CreateInstance(type) as NetModulesPacket;
deserializer?.Invoke(result, br);
return result;
});
}
else if (inst is Packet p2)
var result = Activator.CreateInstance(type) as NetModulesPacket;
deserializer?.Invoke(result, br);
return result;
});
}
else if (inst is Packet p2)
{
deserializers.Add(p2.Type, br =>
{
deserializers.Add(p2.Type, br =>
{
var result = Activator.CreateInstance(type) as Packet;
deserializer?.Invoke(result, br);
return result;
});
}
var result = Activator.CreateInstance(type) as Packet;
deserializer?.Invoke(result, br);
return result;
});
}

}
}

Expand Down
11 changes: 11 additions & 0 deletions TrProtocol/Packets/LiquidUpdate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace TrProtocol.Packets
{
public class LiquidUpdate : Packet
{
public override MessageID Type => MessageID.LiquidUpdate;
public short TileX { get; set; }
public short TileY { get; set; }
public byte Liquid { get; set; }
public byte LiquidType { get; set; }
}
}
6 changes: 4 additions & 2 deletions TrProtocol/Packets/TileSquare.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace TrProtocol.Packets
using TrProtocol.Models;

namespace TrProtocol.Packets
{
public class TileSquare : Packet
{
public override MessageID Type => MessageID.TileSquare;
public byte[] Data { get; set; }
public SquareData Data { get; set; }
}
}
Loading

0 comments on commit df12242

Please sign in to comment.