-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from sondirn/main
- Loading branch information
Showing
13 changed files
with
300 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,222 @@ | ||
namespace LDtk.Codegen.Generators; | ||
|
||
using LDtk.Codegen; | ||
using LDtk.Full; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Text.Json; | ||
|
||
using Full; | ||
|
||
public class ClassGenerator(LDtkFileFull ldtkFile, Options options) : BaseGenerator(ldtkFile, options) | ||
{ | ||
public void Generate() | ||
{ | ||
// Level Classes | ||
GenClass(Options.LevelClassName, LDtkFile.Defs.LevelFields, string.Empty, false); | ||
GenClass(Options.LevelClassName, string.Empty, fieldDefinitions: LDtkFile.Defs.LevelFields); | ||
|
||
// Entity Classes | ||
foreach (EntityDefinition e in LDtkFile.Defs.Entities) | ||
{ | ||
GenClass(e.Identifier, e.FieldDefs, "Entities", true); | ||
GenClass(e.Identifier, "Entities", e); | ||
} | ||
} | ||
|
||
void GenClass(string identifier, FieldDefinition[] fields, string folder, bool isEntity) | ||
void GenClass(string identifier, string folder, EntityDefinition entityDefinition = null, | ||
FieldDefinition[] fieldDefinitions = null) | ||
{ | ||
Line($"namespace {Options.Namespace};"); | ||
Blank(); | ||
Line("// This file was automatically generated, any modifications will be lost!"); | ||
Line("#pragma warning disable"); | ||
Blank(); | ||
Line("using LDtk;"); | ||
Line("using Microsoft.Xna.Framework;"); | ||
Blank(); | ||
GenHeaders(); | ||
|
||
string classDef = $"public partial class {identifier}"; | ||
|
||
if (isEntity && Options.EntityInterface) | ||
if (entityDefinition != null && Options.EntityInterface) | ||
{ | ||
classDef += " : ILDtkEntity"; | ||
} | ||
|
||
Line(classDef); | ||
|
||
StartBlock(); | ||
{ | ||
if (entityDefinition != null) | ||
{ | ||
GenEntityFields(identifier, entityDefinition); | ||
} | ||
else if (fieldDefinitions != null) | ||
{ | ||
GenFieldDefs(fieldDefinitions); | ||
} | ||
} | ||
EndBlock(); | ||
|
||
Line("#pragma warning restore"); | ||
Output(folder, identifier); | ||
} | ||
|
||
void GenEntityFields(string identifier, EntityDefinition entityDefinition) | ||
{ | ||
//generate the default data for fields. | ||
Line($"public static {identifier} Default() => new()"); | ||
StartBlock(); | ||
{ | ||
if (isEntity) | ||
Line($"Identifier = \"{identifier}\","); | ||
Line($"Uid = {entityDefinition.Uid},"); | ||
Line($"Size = new Vector2({entityDefinition.Width}f, {entityDefinition.Height}f),"); | ||
Line($"Pivot = new Vector2({entityDefinition.PivotX}f, {entityDefinition.PivotY}f),"); | ||
if (entityDefinition.TileRect != null) | ||
{ | ||
GenTilesetRectangle("Tile", entityDefinition.TileRect); | ||
} | ||
|
||
byte r = entityDefinition.Color.R; | ||
byte g = entityDefinition.Color.G; | ||
byte b = entityDefinition.Color.B; | ||
byte a = entityDefinition.Color.A; | ||
|
||
Line($"SmartColor = new Color({r}, {g}, {b}, {a}),"); | ||
|
||
//generate default data for custom fields | ||
GenCustomFieldDefData(entityDefinition.FieldDefs); | ||
} | ||
EndCodeBlock(); | ||
Blank(); | ||
|
||
//generate the rest of the class | ||
Line("public string Identifier { get; set; }"); | ||
Line("public System.Guid Iid { get; set; }"); | ||
Line("public int Uid { get; set; }"); | ||
Line("public Vector2 Position { get; set; }"); | ||
Line("public Vector2 Size { get; set; }"); | ||
Line("public Vector2 Pivot { get; set; }"); | ||
Line("public Rectangle Tile { get; set; }"); | ||
Blank(); | ||
Line("public Color SmartColor { get; set; }"); | ||
|
||
FieldDefinition[] fields = entityDefinition.FieldDefs; | ||
GenFieldDefs(fields); | ||
} | ||
|
||
void GenFieldDefs(FieldDefinition[] fields) | ||
{ | ||
if (fields.Length > 0) | ||
{ | ||
Blank(); | ||
} | ||
|
||
foreach (FieldDefinition value in fields) | ||
{ | ||
string type = Converter.ConvertFieldDefinitionTypes(value._Type, Options.PointAsVector2); | ||
|
||
if (value.CanBeNull) | ||
{ | ||
type += "?"; | ||
} | ||
|
||
Line($"public {type} {value.Identifier} {{ get; set; }}"); | ||
} | ||
} | ||
|
||
void GenCustomFieldDefData(FieldDefinition[] fieldDefs) | ||
{ | ||
if (fieldDefs.Length > 0) | ||
{ | ||
Blank(); | ||
} | ||
|
||
foreach (FieldDefinition field in fieldDefs) | ||
{ | ||
if (field.DefaultOverride == null) | ||
{ | ||
Line("public string Identifier { get; set; }"); | ||
Line("public System.Guid Iid { get; set; }"); | ||
Line("public int Uid { get; set; }"); | ||
Line("public Vector2 Position { get; set; }"); | ||
Line("public Vector2 Size { get; set; }"); | ||
Line("public Vector2 Pivot { get; set; }"); | ||
Line("public Rectangle Tile { get; set; }"); | ||
Blank(); | ||
Line("public Color SmartColor { get; set; }"); | ||
Blank(); | ||
continue; | ||
} | ||
|
||
foreach (FieldDefinition value in fields) | ||
if (field.DefaultOverride.Params.ValueKind != JsonValueKind.Array) | ||
{ | ||
string type = Converter.ConvertFieldDefinitionTypes(value._Type, Options.PointAsVector2); | ||
continue; | ||
} | ||
|
||
JsonElement defaultValue = field.DefaultOverride.Params.EnumerateArray().First(); | ||
|
||
if (field._Type.Contains("Enum")) | ||
{ | ||
string enumName = field._Type.Replace("LocalEnum.", ""); | ||
string value = defaultValue.GetString(); | ||
string enumValue = $"{enumName}.{value}"; | ||
|
||
Line($"entity.{field.Identifier} = {enumValue};"); | ||
} | ||
|
||
switch (field._Type) | ||
{ | ||
case Field.IntType: | ||
Line($"{field.Identifier} = {defaultValue.GetInt32()},"); | ||
break; | ||
|
||
case Field.FloatType: | ||
Line($"{field.Identifier} = {defaultValue.GetSingle().ToString(CultureInfo.InvariantCulture)}f,"); | ||
break; | ||
|
||
case Field.BoolType: | ||
Line($"{field.Identifier} = {defaultValue.GetBoolean().ToString().ToLower()},"); | ||
break; | ||
|
||
if (value.CanBeNull) | ||
case Field.StringType: | ||
Line($"{field.Identifier} = {defaultValue.GetRawText()},"); | ||
break; | ||
|
||
case Field.FilePathType: | ||
Line($"{field.Identifier} = {defaultValue.GetString()},"); | ||
break; | ||
|
||
case Field.TileType: | ||
string[] rectValues = defaultValue.GetString()!.Split(','); | ||
int x = int.Parse(rectValues[0]); | ||
int y = int.Parse(rectValues[1]); | ||
int width = int.Parse(rectValues[2]); | ||
int height = int.Parse(rectValues[3]); | ||
int tilesetUid = (int)field.TilesetUid!; | ||
TilesetRectangle finalRect = new() | ||
{ | ||
type += "?"; | ||
} | ||
X = x, | ||
Y = y, | ||
W = width, | ||
H = height, | ||
TilesetUid = tilesetUid | ||
}; | ||
GenTilesetRectangle(field.Identifier, finalRect); | ||
break; | ||
|
||
Line($"public {type} {value.Identifier} {{ get; set; }}"); | ||
case Field.ColorType: | ||
int colorValue = defaultValue.GetInt32(); | ||
int r = (colorValue >> 16) & 0xFF; | ||
int g = (colorValue >> 8) & 0xFF; | ||
int b = colorValue & 0xFF; | ||
Line($"{field.Identifier} = new Color({r}, {g}, {b}, {1}),"); | ||
break; | ||
} | ||
} | ||
EndBlock(); | ||
Line("#pragma warning restore"); | ||
} | ||
|
||
Output(folder, identifier); | ||
void GenHeaders() | ||
{ | ||
Line($"namespace {Options.Namespace};"); | ||
Blank(); | ||
Line("// This file was automatically generated, any modifications will be lost!"); | ||
Line("#pragma warning disable"); | ||
Blank(); | ||
Line("using LDtk;"); | ||
Line("using Microsoft.Xna.Framework;"); | ||
Blank(); | ||
} | ||
|
||
void GenTilesetRectangle(string identifier, TilesetRectangle rect) | ||
{ | ||
Line($"{identifier} = new TilesetRectangle()"); | ||
StartBlock(); | ||
{ | ||
Line($"X = {rect.X},"); | ||
Line($"Y = {rect.Y},"); | ||
Line($"W = {rect.W},"); | ||
Line($"H = {rect.H}"); | ||
} | ||
EndBlockSeparator(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.