Skip to content

Commit

Permalink
Initialize complex structures
Browse files Browse the repository at this point in the history
Fixes crash bug when attempting to (de)serialize a structure or packet that contains nested structures.
  • Loading branch information
ethanmoffat committed May 24, 2024
1 parent 09ca2ad commit d5969f8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
7 changes: 7 additions & 0 deletions ProtocolGenerator/Model/Protocol/BaseInstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@ protected virtual void GenerateProperty(GeneratorState state, string defaultValu
}
state.Text(" ", indented: false);
state.EndBlock(newLine: false, indented: false);

var needsInitialization = TypeInfo.EoType.HasFlag(EoType.Struct) && !TypeInfo.IsArray && !TypeInfo.IsInterface && !TypeInfo.IsEnum;
if (string.IsNullOrWhiteSpace(defaultValue) && needsInitialization)
{
defaultValue = $"new {TypeInfo.PropertyType}()";
}

if (!string.IsNullOrWhiteSpace(defaultValue))
{
state.Text($" = {defaultValue};", indented: false);
Expand Down
2 changes: 1 addition & 1 deletion ProtocolGenerator/Model/Protocol/SwitchInstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public SwitchInstruction(Xml.ProtocolSwitchInstruction xmlSwitchInstruction)
{
_xmlSwitchInstruction = xmlSwitchInstruction;

TypeInfo = new TypeInfo(GetSwitchInterfaceType(_xmlSwitchInstruction.Field));
TypeInfo = new TypeInfo(GetSwitchInterfaceType(_xmlSwitchInstruction.Field), isInterface: true);
Name = GetSwitchInterfaceMemberName(_xmlSwitchInstruction.Field);
_fieldName = IdentifierConverter.SnakeCaseToPascalCase(_xmlSwitchInstruction.Field);
}
Expand Down
5 changes: 4 additions & 1 deletion ProtocolGenerator/Types/TypeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ public class TypeInfo

public bool IsArray { get; }

public bool IsInterface { get; }

public bool Optional { get; }

public bool IsEnum { get; }

public bool IsNullable { get; }

public TypeInfo(string rawType, bool isArray = false, bool optional = false, bool padded = false, bool @fixed = false)
public TypeInfo(string rawType, bool isArray = false, bool isInterface = false, bool optional = false, bool padded = false, bool @fixed = false)
{
ProtocolTypeName = GetTypeName(rawType);
ProtocolTypeSize = GetTypeSize(rawType);
Expand All @@ -31,6 +33,7 @@ public TypeInfo(string rawType, bool isArray = false, bool optional = false, boo

PropertyType = GetDotNetType(ProtocolTypeName, isArray, optional);
IsArray = isArray;
IsInterface = isInterface;
Optional = optional;
IsEnum = TypeMapper.Instance.HasEnum(ProtocolTypeName);

Expand Down

0 comments on commit d5969f8

Please sign in to comment.