Allow serialization of all generic types in scripts

This commit is contained in:
Simon Lübeß
2026-10-24 13:36:36 +02:00
parent ed94213435
commit 2674836cca
10 changed files with 262 additions and 33 deletions
+2 -9
View File
@@ -613,16 +613,9 @@ internal class EntityEditor
{
}
else if (fieldType.IsGenericType)
else if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(List<>))
{
if (fieldType.GetGenericTypeDefinition() == typeof(List<>))
{
newValue = ShowListEditor(fieldType, reference, fieldName);
}
else
{
ImGui.TextColored(new Vector4(1, 0, 0, 1), $"{fieldName}: Type {fieldType} is not implemented.");
}
newValue = ShowListEditor(fieldType, reference, fieldName);
}
else if (fieldType.IsArray)
{
+5 -23
View File
@@ -176,6 +176,9 @@ public class SerializedObject
public void SerializeField(string fieldName, object? fieldValue, Type fieldType)
{
if (TryCustomSerializer(fieldName, fieldValue, fieldType))
return;
// Decimals are not actually primitives, but we treat them as if they were, because they are a built-in type
// and it makes more sense to send them over to the engine as a binary type than to serialize their underlying fields (which would happen when we treated them as a struct).
if (fieldType.IsPrimitive || fieldType == typeof(decimal))
@@ -202,38 +205,17 @@ public class SerializedObject
SerializeList(fieldName, fieldValue, fieldType, fieldType.GetElementType()!);
}
else if (fieldType.IsGenericType)
else if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(List<>))
{
if (TryCustomSerializer(fieldName, fieldValue, fieldType))
return;
if (fieldType.GetGenericTypeDefinition() == typeof(List<>))
{
SerializeList(fieldName, fieldValue, fieldType, fieldType.GetGenericArguments()[0]);
}
//else if (fieldType.GetGenericTypeDefinition() == typeof(Dictionary<,>))
//{
// ImGui.Text($"{fieldName} Dictionary");
//}
else
{
// TODO: what to do?
Log.Error($"Generic class serialization not yet implemented");
}
SerializeList(fieldName, fieldValue, fieldType, fieldType.GetGenericArguments()[0]);
}
else if (fieldType.IsValueType)
{
if (TryCustomSerializer(fieldName, fieldValue, fieldType))
return;
// Value Types cant be null
SerializeStruct(fieldName, fieldValue!, fieldType);
}
else if (fieldType.IsClass)
{
if (TryCustomSerializer(fieldName, fieldValue, fieldType))
return;
SerializeClass(fieldName, fieldValue, fieldType);
}
else