Skip to content

Commit

Permalink
Fix json source gen
Browse files Browse the repository at this point in the history
  • Loading branch information
IrishBruse committed Sep 9, 2024
1 parent 073fd47 commit 56a95fb
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
3 changes: 2 additions & 1 deletion LDtk.Codegen/LDtkJsonFull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace LDtk.Codegen;

using System;
using System.Text.Json.Serialization;
using System.Text.Json;

using Microsoft.Xna.Framework;

Expand Down Expand Up @@ -839,7 +840,7 @@ public partial class FieldInstance

/// <summary> Actual value of the field instance. The value type varies, depending on <c>__type</c>:<br/> - For <b>classic types</b> (ie. Integer, Float, Boolean, String, Text and FilePath), you just get the actual value with the expected type.<br/> - For <b>Color</b>, the value is an hexadecimal string using "#rrggbb" format.<br/> - For <b>Enum</b>, the value is a String representing the selected enum value.<br/> - For <b>Point</b>, the value is a [GridPoint](#ldtk-GridPoint) object.<br/> - For <b>Tile</b>, the value is a [TilesetRect](#ldtk-TilesetRect) object.<br/> - For <b>EntityRef</b>, the value is an [EntityReferenceInfos](#ldtk-EntityReferenceInfos) object.<br/><br/> If the field is an array, then this <c>__value</c> will also be a JSON array. </summary>
[JsonPropertyName("__value")]
public object _Value { get; set; }
public JsonElement _Value { get; set; }
}

/// <summary> This object is just a grid-based coordinate used in Field values. </summary>
Expand Down
2 changes: 2 additions & 0 deletions LDtk.JsonSchema/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static partial class Program
static readonly string[] Namespaces = {
"using System;",
"using System.Text.Json.Serialization;",
"using System.Text.Json;",
"",
"using Microsoft.Xna.Framework;",
};
Expand Down Expand Up @@ -50,6 +51,7 @@ public static partial class Program
}},
{"FieldInstance", new() {
{"RealEditorValues","object[]"},
{"_Value", "JsonElement"},
}},
{"EntityDefinition", new() {
{"Color", "Color"},
Expand Down
14 changes: 7 additions & 7 deletions LDtk/LDtkFieldParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,26 @@ static void ParseCustomFields<T>(T classFields, FieldInstance[] fields, LDtkLeve
throw new LDtkException($"Field {variableName} does not exist on {typeof(T).Name}");
}

if (field._Value == null)
if (field._Value.ValueKind == JsonValueKind.Null)
{
continue;
}

JsonElement element = (JsonElement)field._Value;
JsonElement value = field._Value;

if (level != null && field._Type.Contains(Field.PointType))
{
HandlePoints(classFields, level, field, variableDef, element);
HandlePoints(classFields, level, field, variableDef, value);
}
else
{
switch (element.ValueKind)
switch (value.ValueKind)
{
case JsonValueKind.Object:
case JsonValueKind.Array:
case JsonValueKind.Number:
Type returnType = Nullable.GetUnderlyingType(variableDef.PropertyType) ?? variableDef.PropertyType;
variableDef.SetValue(classFields, JsonSerializer.Deserialize(element.ToString(), returnType, Constants.SerializeOptions));
variableDef.SetValue(classFields, JsonSerializer.Deserialize(value.ToString(), returnType, Constants.SerializeOptions));
break;

case JsonValueKind.String:
Expand All @@ -96,15 +96,15 @@ static void ParseCustomFields<T>(T classFields, FieldInstance[] fields, LDtkLeve

if (isEnum)
{
variableDef.SetValue(classFields, Enum.Parse(t, element.ToString()));
variableDef.SetValue(classFields, Enum.Parse(t, value.ToString()));
}
else if (isColor)
{
variableDef.SetValue(classFields, ParseStringToColor(field._Value.ToString()!, 255));
}
else
{
variableDef.SetValue(classFields, element.ToString());
variableDef.SetValue(classFields, value.ToString());
}

break;
Expand Down
3 changes: 2 additions & 1 deletion LDtk/LDtkJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace LDtk;

using System;
using System.Text.Json.Serialization;
using System.Text.Json;

using Microsoft.Xna.Framework;

Expand Down Expand Up @@ -347,7 +348,7 @@ public partial class FieldInstance

/// <summary> Actual value of the field instance. The value type varies, depending on <c>__type</c>:<br/> - For <b>classic types</b> (ie. Integer, Float, Boolean, String, Text and FilePath), you just get the actual value with the expected type.<br/> - For <b>Color</b>, the value is an hexadecimal string using "#rrggbb" format.<br/> - For <b>Enum</b>, the value is a String representing the selected enum value.<br/> - For <b>Point</b>, the value is a [GridPoint](#ldtk-GridPoint) object.<br/> - For <b>Tile</b>, the value is a [TilesetRect](#ldtk-TilesetRect) object.<br/> - For <b>EntityRef</b>, the value is an [EntityReferenceInfos](#ldtk-EntityReferenceInfos) object.<br/><br/> If the field is an array, then this <c>__value</c> will also be a JSON array. </summary>
[JsonPropertyName("__value")]
public object _Value { get; set; }
public JsonElement _Value { get; set; }
}

/// <summary> This object is just a grid-based coordinate used in Field values. </summary>
Expand Down
3 changes: 2 additions & 1 deletion LDtk/LDtkJsonSourceGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
namespace LDtk;

using System.Text.Json;
using System.Text.Json.Serialization;

using Microsoft.Xna.Framework;

/// <summary> The json source generator for LDtk files. </summary>
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(object))]
[JsonSerializable(typeof(JsonElement))]
[JsonSerializable(typeof(bool))]
[JsonSerializable(typeof(bool[]))]
[JsonSerializable(typeof(float))]
Expand Down

0 comments on commit 56a95fb

Please sign in to comment.