Skip to content

Commit

Permalink
removed most warnings with generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotr7 committed Jan 26, 2025
1 parent 7f96814 commit ff00162
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 87 deletions.
2 changes: 2 additions & 0 deletions src/StarBreaker.DataCore.Generated/DataCoreBinaryGenerated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace StarBreaker.DataCore;

//TODO: come up with a way of hashing the types we generated, then verify that the datacore file we're reading matches the types we have.
// if they don't match we *WILL* fail to read it. We should throw before this.
public sealed class DataCoreBinaryGenerated : IDataCoreBinary<IDataCoreReadable>
{
public DataCoreDatabase Database { get; }
Expand Down
25 changes: 18 additions & 7 deletions src/StarBreaker.DataCore.Generated/DataCoreHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ public static Lazy<T>[] ReadWeakPointerArrayLazy<T>(DataCoreDatabase db, ref Spa
return array;
}

public static T[] ReadStrongPointerArray<T>(DataCoreDatabase db, ref SpanReader reader) where T : class, IDataCoreReadable<T>
public static T?[] ReadStrongPointerArray<T>(DataCoreDatabase db, ref SpanReader reader) where T : class, IDataCoreReadable<T>
{
var count = reader.ReadInt32();
var firstIndex = reader.ReadInt32();

var array = new T[count];
var array = new T?[count];

for (var i = firstIndex; i < firstIndex + count; i++)
{
Expand All @@ -125,7 +125,8 @@ public static T[] ReadClassArray<T>(DataCoreDatabase db, ref SpanReader reader,

for (var i = firstIndex; i < firstIndex + count; i++)
{
array[i - firstIndex] = ReadFromInstance<T>(db, structIndex, i);
array[i - firstIndex] = ReadFromInstance<T>(db, structIndex, i)
?? throw new Exception($"ReadFromInstance failed to read instance of {typeof(T)}");
}

return array;
Expand All @@ -146,18 +147,28 @@ public static T[] ReadEnumArray<T>(DataCoreDatabase db, ref SpanReader reader) w
return array;
}

public static DataCoreStringId[] ReadStringArray(DataCoreDatabase db, ref SpanReader reader)
public static string[] ReadStringArray(DataCoreDatabase db, ref SpanReader reader)
{
var count = reader.ReadInt32();
var firstIndex = reader.ReadInt32();
return db.StringIdValues.AsSpan(firstIndex, count).ToArray();
var result = new string[count];

for (var i = 0; i < count; i++)
result[i] = db.StringIdValues[firstIndex + i].ToString(db);

return result;
}

public static DataCoreStringId[] ReadLocaleArray(DataCoreDatabase db, ref SpanReader reader)
public static string[] ReadLocaleArray(DataCoreDatabase db, ref SpanReader reader)
{
var count = reader.ReadInt32();
var firstIndex = reader.ReadInt32();
return db.LocaleValues.AsSpan(firstIndex, count).ToArray();
var result = new string[count];

for (var i = 0; i < count; i++)
result[i] = db.LocaleValues[firstIndex + i].ToString(db);

return result;
}

public static sbyte[] ReadSByteArray(DataCoreDatabase db, ref SpanReader reader)
Expand Down
147 changes: 67 additions & 80 deletions src/StarBreaker.DataCore.TypeGenerator/DataCoreCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ private void GenerateTypes(string path)
}
else
{
//sb.AppendLine($"public class {structDefinition.GetName(Database)}");
sb.AppendLine($"public record {structDefinition.GetName(Database)} : IDataCoreReadable<{structDefinition.GetName(Database)}>");
}

Expand All @@ -137,7 +136,7 @@ private void GenerateTypes(string path)
var propertyType = GetPropertyType(property);
var name = property.GetName(Database);

sb.AppendLine($" public {propertyType} @{name} {{ get; init; }}");
sb.AppendLine($" public required {propertyType} @{name} {{ get; init; }}");
}

sb.AppendLine();
Expand All @@ -152,57 +151,34 @@ private void GenerateTypes(string path)
}
}

private void WriteBasicConstructor(StringBuilder sb, DataCoreStructDefinition structDefinition, int structIndex)
private string GetScalarPropertyType(DataCorePropertyDefinition property) => property.DataType switch
{
// The constructor should take as arguments the properties in the order we expect.
// Which is base type -> derived type -> derived type -> our type strictly.
// then we handle passing the properties to the following constructor.
var allprops = Database.GetProperties(structIndex).AsSpan();
sb.AppendLine($" public {structDefinition.GetName(Database)}(");
for (var i = 0; i < allprops.Length; i++)
{
var property = allprops[i];
var propertyType = GetPropertyType(property);
var name = property.GetName(Database);

sb.Append($" {propertyType} _{name}");
if (i != allprops.Length - 1)
sb.AppendLine(",");
else sb.AppendLine();
}

sb.AppendLine(" )");

if (structDefinition.ParentTypeIndex != -1)
{
sb.AppendLine(" : base(");
//we take all properties from all parent types. We will consume our own in our constructor, and pass down the rest.
var propsForConstructor = allprops[..^structDefinition.AttributeCount];
for (var i = 0; i < propsForConstructor.Length; i++)
{
var property = propsForConstructor[i];
var name = property.GetName(Database);
sb.Append($" _{name}");
if (i != propsForConstructor.Length - 1)
sb.AppendLine(",");
else sb.AppendLine();
}

sb.AppendLine(" )");
}

sb.AppendLine(" {");
var thisProps = Database.PropertyDefinitions.AsSpan(structDefinition.FirstAttributeIndex, structDefinition.AttributeCount);
foreach (var property in thisProps)
{
var name = property.GetName(Database);
sb.AppendLine($" @{name} = _{name};");
}
DataType.Boolean => "bool",
DataType.Byte => "byte",
DataType.SByte => "sbyte",
DataType.Int16 => "short",
DataType.UInt16 => "ushort",
DataType.Int32 => "int",
DataType.UInt32 => "uint",
DataType.Int64 => "long",
DataType.UInt64 => "ulong",
DataType.Single => "float",
DataType.Double => "double",
DataType.Guid => "CigGuid",
DataType.Locale => "string",
DataType.String => "string",

sb.AppendLine(" }");
}
DataType.EnumChoice => Database.EnumDefinitions[property.StructIndex].GetName(Database),
DataType.Reference => $"{Database.StructDefinitions[property.StructIndex].GetName(Database)}?",
DataType.StrongPointer => $"{Database.StructDefinitions[property.StructIndex].GetName(Database)}?",
DataType.Class => Database.StructDefinitions[property.StructIndex].GetName(Database),

private string GetSimplePropertyType(DataCorePropertyDefinition property) => property.DataType switch
DataType.WeakPointer => "DataCorePointer",
// DataType.WeakPointer => Database.StructDefinitions[property.StructIndex].GetName(Database)?,
_ => throw new ArgumentOutOfRangeException()
};

private string GetGenericPropertyType(DataCorePropertyDefinition property) => property.DataType switch
{
DataType.Boolean => "bool",
DataType.Byte => "byte",
Expand All @@ -216,42 +192,51 @@ private void WriteBasicConstructor(StringBuilder sb, DataCoreStructDefinition st
DataType.Single => "float",
DataType.Double => "double",
DataType.Guid => "CigGuid",
DataType.Locale => "DataCoreStringId",
DataType.String => "DataCoreStringId",
DataType.Locale => "string",
DataType.String => "string",

DataType.EnumChoice => Database.EnumDefinitions[property.StructIndex].GetName(Database),
DataType.Reference => Database.StructDefinitions[property.StructIndex].GetName(Database),
DataType.WeakPointer => "DataCorePointer",
DataType.StrongPointer => Database.StructDefinitions[property.StructIndex].GetName(Database),
DataType.Class => Database.StructDefinitions[property.StructIndex].GetName(Database),

//todo
// DataType.EnumChoice => Database.EnumDefinitions[property.StructIndex].GetName(Database),
// DataType.Class => Database.StructDefinitions[property.StructIndex].GetName(Database),
// DataType.Reference => Database.StructDefinitions[property.StructIndex].GetName(Database),
// DataType.WeakPointer => Database.StructDefinitions[property.StructIndex].GetName(Database),
// DataType.StrongPointer => Database.StructDefinitions[property.StructIndex].GetName(Database),
DataType.WeakPointer => "DataCorePointer",
// DataType.WeakPointer => Database.StructDefinitions[property.StructIndex].GetName(Database)?,
_ => throw new ArgumentOutOfRangeException()
};


private string GetPropertyType(DataCorePropertyDefinition property)
private string GetArrayPropertyType(DataCorePropertyDefinition property) => property.DataType switch
{
var baseProperty = GetSimplePropertyType(property);

return property.ConversionType switch
{
ConversionType.Attribute => baseProperty,
_ => $"{baseProperty}[]"
};
}
DataType.Boolean => "bool[]",
DataType.Byte => "byte[]",
DataType.SByte => "sbyte[]",
DataType.Int16 => "short[]",
DataType.UInt16 => "ushort[]",
DataType.Int32 => "int[]",
DataType.UInt32 => "uint[]",
DataType.Int64 => "long[]",
DataType.UInt64 => "ulong[]",
DataType.Single => "float[]",
DataType.Double => "double[]",
DataType.Guid => "CigGuid[]",
DataType.Locale => "string[]",
DataType.String => "string[]",

DataType.EnumChoice => $"{Database.EnumDefinitions[property.StructIndex].GetName(Database)}[]",
DataType.Reference => $"{Database.StructDefinitions[property.StructIndex].GetName(Database)}?[]",
DataType.StrongPointer => $"{Database.StructDefinitions[property.StructIndex].GetName(Database)}?[]",
DataType.Class => $"{Database.StructDefinitions[property.StructIndex].GetName(Database)}[]",

DataType.WeakPointer => "DataCorePointer[]",
// DataType.WeakPointer => $"{Database.StructDefinitions[property.StructIndex].GetName(Database)}?[]",
_ => throw new ArgumentOutOfRangeException()
};

//TODO: generate a constructor that accepts something useful like a SpanReader and a database.
// it should then, based on its properties, generate either:
// if attribute, reader.Read<t>(). For struct types, we *have* to pass down the spanreader or it gets out of sync.
// For references or pointers, we can just read that and ski the actual data for a POC impl.
// if array, we probably pass it down to a generic method that handles reading the array i and count, and reads the elements as needed.
// for a poc, realistically we read those two ints and skip. things should work fine from there on even half complete.
private string GetPropertyType(DataCorePropertyDefinition property) => property.ConversionType switch
{
ConversionType.Attribute => GetScalarPropertyType(property),
_ => GetArrayPropertyType(property)
};

private void WriteSpecialConstructor(StringBuilder sb, DataCoreStructDefinition structDefinition, int structIndex)
{
Expand Down Expand Up @@ -291,7 +276,7 @@ private void WriteSpecialConstructor(StringBuilder sb, DataCoreStructDefinition

private void WriteSingleRead(StringBuilder sb, DataCorePropertyDefinition property)
{
var propertyType = GetSimplePropertyType(property);
var propertyType = GetGenericPropertyType(property);
var name = property.GetName(Database);

switch (property.DataType)
Expand All @@ -301,7 +286,7 @@ private void WriteSingleRead(StringBuilder sb, DataCorePropertyDefinition proper
break;
case DataType.EnumChoice:
var enumName = Database.EnumDefinitions[property.StructIndex].GetName(Database);
sb.AppendLine($" var _{name} = DataCoreHelper.EnumParse<{enumName}>(reader.Read<DataCoreStringId>().ToString(db), {enumName}.__Unknown);");
sb.AppendLine($" var _{name} = DataCoreHelper.EnumParse(reader.Read<DataCoreStringId>().ToString(db), {enumName}.__Unknown);");
break;
case DataType.Reference:
sb.AppendLine($" var _{name} = DataCoreHelper.ReadFromReference<{propertyType}>(db, reader.Read<DataCoreReference>());");
Expand All @@ -313,11 +298,13 @@ private void WriteSingleRead(StringBuilder sb, DataCorePropertyDefinition proper
//do as default. we probably should handle this, it's actually feasible now :D
sb.AppendLine($" var _{name} = reader.Read<{propertyType}>();");
break;
case DataType.Guid:
case DataType.String:
case DataType.Locale:
sb.AppendLine($" var _{name} = reader.Read<DataCoreStringId>().ToString(db);");
break;
case DataType.Guid:
case DataType.Double:
case DataType.Single:
case DataType.String:
case DataType.UInt64:
case DataType.UInt32:
case DataType.UInt16:
Expand All @@ -337,7 +324,7 @@ private void WriteSingleRead(StringBuilder sb, DataCorePropertyDefinition proper

private void WriteArrayRead(StringBuilder sb, DataCorePropertyDefinition property)
{
var propertyType = GetSimplePropertyType(property);
var propertyType = GetGenericPropertyType(property);
var name = property.GetName(Database);

switch (property.DataType)
Expand Down

0 comments on commit ff00162

Please sign in to comment.