Skip to content

Commit

Permalink
Merge pull request #18 from Megghy/main
Browse files Browse the repository at this point in the history
fix arrayserializer
  • Loading branch information
cc004 authored Jul 19, 2022
2 parents e5e0ad3 + 55f14d6 commit 67c6ec6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
3 changes: 2 additions & 1 deletion TrProtocol/FieldSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public override void Write(BinaryWriter bw, object o)
WriteOverride(bw, (T)o);
}

public void Configure(PropertyInfo prop, string version)
public IConfigurable Configure(PropertyInfo prop, string version)
{
foreach (var bounds in prop.GetCustomAttributes<BoundsAttribute>())
{
Expand All @@ -35,6 +35,7 @@ public void Configure(PropertyInfo prop, string version)
interrupt = bounds.Interrupt;
enabled = true;
}
return this;
}
}
public abstract class FieldSerializer<T> : IFieldSerializer
Expand Down
2 changes: 1 addition & 1 deletion TrProtocol/IFieldSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public interface IFieldSerializer

public interface IConfigurable
{
void Configure(PropertyInfo prop, string version);
IConfigurable Configure(PropertyInfo prop, string version);
}
3 changes: 2 additions & 1 deletion TrProtocol/PacketSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ private void RegisterPacket(Type type)
ser = RequestFieldSerializer(t); // fallback
serFound:

if (ser is IConfigurable conf) conf.Configure(prop, Version);
if (ser is IConfigurable conf)
ser = (IFieldSerializer)conf.Configure(prop, Version);

if (shouldSerialize)
serializer += (o, bw) => { if (condition(o)) ser.Write(bw, getter(o)); };
Expand Down
10 changes: 7 additions & 3 deletions TrProtocol/Serializers/InternalTypeSerializers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private class ByteArraySerializer : FieldSerializer<byte[]>
private class ArraySerializer<T> : FieldSerializer<T[]>, IConfigurable
{
private int size;
private readonly IFieldSerializer elementSerializer;
private IFieldSerializer elementSerializer;
public ArraySerializer() : this(0)
{

Expand All @@ -77,11 +77,15 @@ protected override void WriteOverride(BinaryWriter bw, T[] t)
elementSerializer.Write(bw, x);
}

public void Configure(PropertyInfo prop, string version)
public IConfigurable Configure(PropertyInfo prop, string version)
{
if (elementSerializer is IConfigurable conf)
conf.Configure(prop, version);
size = prop.GetCustomAttribute<ArraySizeAttribute>().Size;
return new ArraySerializer<T>()
{
size = prop.GetCustomAttribute<ArraySizeAttribute>().Size,
elementSerializer = elementSerializer
};
}
}
}

0 comments on commit 67c6ec6

Please sign in to comment.