Skip to content

Commit

Permalink
create new Model project and transfer over Grynt code generator
Browse files Browse the repository at this point in the history
  • Loading branch information
colegarien committed Aug 14, 2024
1 parent 8eccb0d commit adf1d1f
Show file tree
Hide file tree
Showing 57 changed files with 4,026 additions and 0 deletions.
1 change: 1 addition & 0 deletions Ozzyria.Game/Ozzyria.Game.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<ProjectReference Include="..\Ozzyria.Content\Ozzyria.Content.csproj" />
<ProjectReference Include="..\Ozzyria.Model\Ozzyria.Model.csproj" />
</ItemGroup>

</Project>
14 changes: 14 additions & 0 deletions Ozzyria.Grynt/Ozzyria.Grynt.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Ozzyria.Model\Ozzyria.Model.csproj" />
</ItemGroup>

</Project>
54 changes: 54 additions & 0 deletions Ozzyria.Grynt/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Grynt.Generators.Decorators;
using Grynt.Generators.Fields;
using Grynt.Generators;
using Grynt.Model.Packages;
using Grynt.Model.Definitions;

namespace Ozzyria.Grynt
{
internal class Program
{
public static string OzzyriaModelRoot()
{
return Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName, "Ozzyria.Model");
}

static void Main(string[] args)
{
var targetNamespace = "Ozzyria.Model";
var modelRoot = OzzyriaModelRoot();

System.Console.WriteLine("---- TYPES ----");

var typePackage = TypePackage.Load(Path.Combine(modelRoot, "CodeGen", "Files", "type_defs.json"));
var basicFieldGenerator = new FieldsGenerator(typePackage);
var typeClassGenerator = new ClassGenerator(targetNamespace, "Types", [
new FieldsDecorator(basicFieldGenerator),
new SerializableDecorator(typePackage),
new HydrateableDecorator(typePackage)
]);
var typeGenerator = new TypeGenerator(targetNamespace, typePackage, typeClassGenerator);
foreach (var type in typePackage.Definitions.Values)
{
var code = typeGenerator.Generate(type);
System.Console.WriteLine(code);
if (type.Type != TypeDefinition.TYPE_ASSUMED)
{
File.WriteAllText(Path.Combine(modelRoot, "Types", type.Name + ".cs"), code);
}
}


System.Console.WriteLine("---- COMPONENTS ----");

var componentPackage = ComponentPackage.Load(Path.Combine(modelRoot, "CodeGen", "Files", "component_defs.json"));
var componentClassGenerator = new ComponentGenerator(targetNamespace, typePackage);
foreach (var component in componentPackage.Definitions.Values)
{
var code = componentClassGenerator.Generate(component);
System.Console.WriteLine(code);
File.WriteAllText(Path.Combine(modelRoot, "Components", component.Name + ".cs"), code);
}
}
}
}
24 changes: 24 additions & 0 deletions Ozzyria.Model/CodeGen/Definitions/ComponentDefinition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;
using Ozzyria.Model.Types;

namespace Grynt.Model.Definitions
{
public class ComponentDefinition
{
[JsonPropertyName("id")]
public string Id { get; set; }

[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("is_pooled")]
public bool IsPooled { get; set; }

[JsonPropertyName("fields")]
public Dictionary<string, FieldDefinition> Fields { get; set; }

[JsonPropertyName("defaults")]
public ValuePacket Defaults { get; set; }
}
}
20 changes: 20 additions & 0 deletions Ozzyria.Model/CodeGen/Definitions/FieldDefinition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Text.Json.Serialization;

namespace Grynt.Model.Definitions
{
public class FieldDefinition
{
[JsonPropertyName("id")]
public string Id { get; set; }

[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("type")]
public string TypeId { get; set; }

[JsonPropertyName("exclude_from_serialize")]
public bool ExcludeFromSerialize { get; set; }

}
}
21 changes: 21 additions & 0 deletions Ozzyria.Model/CodeGen/Definitions/PrefabDefinition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;
using Ozzyria.Model.Types;

namespace Grynt.Model.Definitions
{
public class PrefabDefinition
{
[JsonPropertyName("id")]
public string Id { get; set; }

[JsonPropertyName("components")]
public List<string> Components { get; set; }

[JsonPropertyName("defaults")]
public ValuePacket Defaults { get; set; }

[JsonPropertyName("exposed")]
public List<string> Exposed { get; set; }
}
}
31 changes: 31 additions & 0 deletions Ozzyria.Model/CodeGen/Definitions/TypeDefinition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;
using Ozzyria.Model.Types;

namespace Grynt.Model.Definitions
{
public class TypeDefinition
{
public const string TYPE_ASSUMED = "assumed";
public const string TYPE_ENUM = "enum";
public const string TYPE_CLASS = "class";

[JsonPropertyName("id")]
public string Id { get; set; }

[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("type")]
public string Type { get; set; }

[JsonPropertyName("values")]
public List<string> EnumValues { get; set; }

[JsonPropertyName("fields")]
public Dictionary<string, FieldDefinition> ClassFields { get; set; }

[JsonPropertyName("defaults")]
public ValuePacket ClassDefaults { get; set; }
}
}
Loading

0 comments on commit adf1d1f

Please sign in to comment.