Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PowerSystem syncing #503

Closed
wants to merge 10 commits into from
135 changes: 113 additions & 22 deletions NebulaModel/Networking/Serialization/NetSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,84 @@ private class StringSerializer<T> : FastCallSpecific<T, string>
public override void WriteArray(T inf, NetDataWriter w) { w.PutArray(GetterArr(inf), _maxLength); }
}

private class Long2ArraySerializer<T> : FastCallSpecific<T, long[][]>
{
public override void Read(T inf, NetDataReader r)
{
ushort size = r.GetUShort();
long[][] arr = new long[size][];

for(int i = 0; i < size; i++)
{
arr[i] = r.GetLongArray();
}

Setter(inf, arr);
}
public override void Write(T inf, NetDataWriter w)
{
ushort len = Getter(inf) == null ? (ushort)0 : (ushort)Getter(inf).Length;

w.Put(len);
for(int i = 0; i < len; i++)
{
w.PutArray(Getter(inf)[i]);
}
}
}

private class Double2ArraySerializer<T> : FastCallSpecific<T, double[][]>
{
public override void Read(T inf, NetDataReader r)
{
ushort size = r.GetUShort();
double[][] arr = new double[size][];

for (int i = 0; i < size; i++)
{
arr[i] = r.GetDoubleArray();
}

Setter(inf, arr);
}
public override void Write(T inf, NetDataWriter w)
{
ushort len = Getter(inf) == null ? (ushort)0 : (ushort)Getter(inf).Length;

w.Put(len);
for (int i = 0; i < len; i++)
{
w.PutArray(Getter(inf)[i]);
}
}
}

private class Bool2ArraySerializer<T> : FastCallSpecific<T, bool[][]>
{
public override void Read(T inf, NetDataReader r)
{
ushort size = r.GetUShort();
bool[][] arr = new bool[size][];

for (int i = 0; i < size; i++)
{
arr[i] = r.GetBoolArray();
}

Setter(inf, arr);
}
public override void Write(T inf, NetDataWriter w)
{
ushort len = Getter(inf) == null ? (ushort)0 : (ushort)Getter(inf).Length;

w.Put(len);
for (int i = 0; i < len; i++)
{
w.PutArray(Getter(inf)[i]);
}
}
}

private class EnumByteSerializer<T> : FastCall<T>
{
protected readonly PropertyInfo Property;
Expand Down Expand Up @@ -637,7 +715,8 @@ private ClassInfo<T> RegisterInternal<T>()
Type propertyType = property.PropertyType;

Type elementType = propertyType.IsArray ? propertyType.GetElementType() : propertyType;
CallType callType = propertyType.IsArray ? CallType.Array : CallType.Basic;
// todo: find a better way to handle 2d arrays in the Init() method
CallType callType = (propertyType.IsArray && elementType != typeof(long[]) && elementType != typeof(double[]) && elementType != typeof(bool[])) ? CallType.Array : CallType.Basic;

if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(List<>))
{
Expand All @@ -657,17 +736,17 @@ private ClassInfo<T> RegisterInternal<T>()
continue;
}

FastCall<T> serialzer = null;
FastCall<T> serializer = null;
if (propertyType.IsEnum)
{
Type underlyingType = Enum.GetUnderlyingType(propertyType);
if (underlyingType == typeof(byte))
{
serialzer = new EnumByteSerializer<T>(property, propertyType);
serializer = new EnumByteSerializer<T>(property, propertyType);
}
else if (underlyingType == typeof(int))
{
serialzer = new EnumIntSerializer<T>(property, propertyType);
serializer = new EnumIntSerializer<T>(property, propertyType);
}
else
{
Expand All @@ -676,73 +755,85 @@ private ClassInfo<T> RegisterInternal<T>()
}
else if (elementType == typeof(string))
{
serialzer = new StringSerializer<T>(_maxStringLength);
serializer = new StringSerializer<T>(_maxStringLength);
}
else if (elementType == typeof(bool))
{
serialzer = new BoolSerializer<T>();
serializer = new BoolSerializer<T>();
}
else if (elementType == typeof(byte))
{
serialzer = new ByteSerializer<T>();
serializer = new ByteSerializer<T>();
}
else if (elementType == typeof(sbyte))
{
serialzer = new SByteSerializer<T>();
serializer = new SByteSerializer<T>();
}
else if (elementType == typeof(short))
{
serialzer = new ShortSerializer<T>();
serializer = new ShortSerializer<T>();
}
else if (elementType == typeof(ushort))
{
serialzer = new UShortSerializer<T>();
serializer = new UShortSerializer<T>();
}
else if (elementType == typeof(int))
{
serialzer = new IntSerializer<T>();
serializer = new IntSerializer<T>();
}
else if (elementType == typeof(uint))
{
serialzer = new UIntSerializer<T>();
serializer = new UIntSerializer<T>();
}
else if (elementType == typeof(long))
{
serialzer = new LongSerializer<T>();
serializer = new LongSerializer<T>();
}
else if (elementType == typeof(ulong))
{
serialzer = new ULongSerializer<T>();
serializer = new ULongSerializer<T>();
}
else if (elementType == typeof(float))
{
serialzer = new FloatSerializer<T>();
serializer = new FloatSerializer<T>();
}
else if (elementType == typeof(double))
{
serialzer = new DoubleSerializer<T>();
serializer = new DoubleSerializer<T>();
}
else if (elementType == typeof(char))
{
serialzer = new CharSerializer<T>();
serializer = new CharSerializer<T>();
}
else if (elementType == typeof(IPEndPoint))
{
serialzer = new IPEndPointSerializer<T>();
serializer = new IPEndPointSerializer<T>();
}
else if(elementType == typeof(long[])) // handles long[][]
{
serializer = new Long2ArraySerializer<T>();
}
else if(elementType == typeof(double[])) // handles double[][]
{
serializer = new Double2ArraySerializer<T>();
}
else if(elementType == typeof(bool[])) // handles bool[][]
{
serializer = new Bool2ArraySerializer<T>();
}
else
{
_registeredTypes.TryGetValue(elementType, out CustomType customType);
if (customType != null)
{
serialzer = customType.Get<T>();
serializer = customType.Get<T>();
}
}

if (serialzer != null)
if (serializer != null)
{
serialzer.Init(getMethod, setMethod, callType);
serializers.Add(serialzer);
serializer.Init(getMethod, setMethod, callType);
serializers.Add(serializer);
}
else
{
Expand Down
12 changes: 12 additions & 0 deletions NebulaModel/Packets/PowerSystem/PowerSystemUpdateRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace NebulaModel.Packets.PowerSystem
{
public class PowerSystemUpdateRequest
{
public int[] PlanetIDs { get; set; }
public PowerSystemUpdateRequest() { }
public PowerSystemUpdateRequest(int[] planetIDs)
{
PlanetIDs = planetIDs;
}
}
}
55 changes: 55 additions & 0 deletions NebulaModel/Packets/PowerSystem/PowerSystemUpdateResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
namespace NebulaModel.Packets.PowerSystem
{
public class PowerSystemUpdateResponse
{
// simulate 2d long array with 1d string array separated by ;
//public long[][] EnergyCapacity { get; set; }
//public long[][] EnergyRequired { get; set; }
//public long[][] EnergyServed { get; set; }
//public long[][] EnergyAccumulated { get; set; }
//public long[][] EnergyExchanged { get; set; }
public double[][] ConsumerRatio { get; set; }
public double[][] GeneratorRatio { get; set; }
public bool[][] CopyValues { get; set; }
public long[][] GenerateCurrentTick { get; set; }
public long[][] Num35 { get; set; }
public long[] PowerGenRegister { get; set; }
public long[] PowerConRegister { get; set; }
public long[] PowerDisRegister { get; set; }
public long[] PowerChaRegister { get; set; }
public long[] EnergyConsumption { get; set; }
public PowerSystemUpdateResponse() { }
public PowerSystemUpdateResponse(//long[][] energyCapacity,
//long[][] energyRequired,
//long[][] energyServed,
//long[][] energyAccumulated,
//long[][] energyExchanged,
double[][] consumerRatio,
double[][] generatorRatio,
bool[][] copyValues,
long[][] generateCurrentTick,
long[][] num35,
long[] powerGenRegister,
long[] powerConRegister,
long[] powerDisRegister,
long[] powerChaRegister,
long[] energyConsumption)
{
//EnergyCapacity = energyCapacity;
//EnergyRequired = energyRequired;
//EnergyServed = energyServed;
//EnergyAccumulated = energyAccumulated;
//EnergyExchanged = energyExchanged;
ConsumerRatio = consumerRatio;
GeneratorRatio = generatorRatio;
CopyValues = copyValues;
GenerateCurrentTick = generateCurrentTick;
Num35 = num35;
PowerGenRegister = powerGenRegister;
PowerConRegister = powerConRegister;
PowerDisRegister = powerDisRegister;
PowerChaRegister = powerChaRegister;
EnergyConsumption = energyConsumption;
}
}
}

This file was deleted.

Loading