Skip to content

Commit

Permalink
Add armor trims to registry
Browse files Browse the repository at this point in the history
  • Loading branch information
Tides committed Nov 14, 2023
1 parent cc2a6c4 commit b09be32
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 10 deletions.
7 changes: 7 additions & 0 deletions Obsidian.API/Registry/Codecs/ArmorTrims/TrimDescription.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Obsidian.API.Registry.Codecs.ArmorTrims;
public sealed class TrimDescription
{
public required string Translate { get; init; }

public string? Color { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Obsidian.API.Registry.Codecs.ArmorTrims.TrimMaterial;
public sealed class TrimMaterialCodec : ICodec
{
public required string Name { get; init; }

public required int Id { get; init; }

public required TrimMaterialElement Element { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Obsidian.API.Registry.Codecs.ArmorTrims.TrimMaterial;
public sealed class TrimMaterialElement
{
public required string Ingredient { get; init; }

public required string AssetName { get; init; }

public required double ItemModelIndex { get; init; }

public required TrimDescription Description { get; init; }

public Dictionary<string, string>? OverrideArmorMaterials { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Obsidian.API.Registry.Codecs.ArmorTrims.TrimPattern;
public sealed class TrimPatternCodec : ICodec
{
public required string Name { get; init; }

public required int Id { get; init; }

public required TrimPatternElement Element { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Obsidian.API.Registry.Codecs.ArmorTrims.TrimPattern;
public sealed class TrimPatternElement
{
public required string TemplateItem { get; init; }

public required TrimDescription Description { get; init; }

public required string AssetId { get; init; }

public required bool Decal { get; init; }
}
4 changes: 3 additions & 1 deletion Obsidian.SourceGenerators/Registry/Models/Assets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public static Dictionary<string, Codec[]> GetCodecs(ImmutableArray<(string name,
{ "dimensions", ParseCodec(files.GetJsonFromArray("dimensions")) },
{ "biomes", ParseCodec(files.GetJsonFromArray("biomes")) },
{ "chat_type", ParseCodec(files.GetJsonFromArray("chat_type")) },
{ "damage_type", ParseCodec(files.GetJsonFromArray("damage_type")) }
{ "damage_type", ParseCodec(files.GetJsonFromArray("damage_type")) },
{ "trim_pattern", ParseCodec(files.GetJsonFromArray("trim_pattern")) },
{ "trim_material", ParseCodec(files.GetJsonFromArray("trim_material")) }
};
}

Expand Down
4 changes: 3 additions & 1 deletion Obsidian.SourceGenerators/Registry/NameHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Obsidian.SourceGenerators.Registry;
using System.Text.Json;

namespace Obsidian.SourceGenerators.Registry;

internal static class NameHelper
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using Obsidian.SourceGenerators.Registry.Models;
using System.Text.Json;

namespace Obsidian.SourceGenerators.Registry;
public partial class RegistryAssetsGenerator
{
private static void GenerateTrimMaterial(Codec[] trimMaterials, CodeBuilder builder, SourceProductionContext ctx)
{
builder.Type($"public static class TrimMaterials");

builder.Indent().Append("public const string CodecKey = \"minecraft:trim_material\";").Line().Line();
builder.Indent().Append($"public const int GlobalBitsPerEntry = {(int)Math.Ceiling(Math.Log(trimMaterials.Length, 2))};").Line().Line();

foreach (var trimMaterial in trimMaterials)
{
var propertyName = trimMaterial.Name.RemoveNamespace().ToPascalCase();

builder.Indent().Append($"public static TrimMaterialCodec {propertyName} {{ get; }} = new() {{ Id = {trimMaterial.RegistryId}, Name = \"{trimMaterial.Name}\", Element = new() {{ ");

foreach (var property in trimMaterial.Properties)
{
var name = property.Key;
var value = property.Value;

builder.Append($"{name} = ");

if (value.ValueKind == JsonValueKind.Object)
{
ParseProperty(builder, value, ctx, name == "OverrideArmorMaterials");
continue;
}

AppendValueType(builder, value, ctx, name == "OverrideArmorMaterials");
}

builder.Append("} };").Line();
}

builder.Line().Statement("public static IReadOnlyDictionary<string, TrimMaterialCodec> All { get; } = new Dictionary<string, TrimMaterialCodec>");

foreach (var name in trimMaterials.Select(x => x.Name))
{
var propertyName = name.RemoveNamespace().ToPascalCase();
builder.Line($"{{ \"{name}\", {propertyName} }},");
}

builder.EndScope(".AsReadOnly()", true).Line();

builder.EndScope();
}

private static void GenerateTrimPattern(Codec[] trimPatterns, CodeBuilder builder, SourceProductionContext ctx)
{
builder.Type($"public static class TrimPatterns");

builder.Indent().Append("public const string CodecKey = \"minecraft:trim_pattern\";").Line().Line();
builder.Indent().Append($"public const int GlobalBitsPerEntry = {(int)Math.Ceiling(Math.Log(trimPatterns.Length, 2))};").Line().Line();

foreach (var trimPattern in trimPatterns)
{
var propertyName = trimPattern.Name.RemoveNamespace().ToPascalCase();

builder.Indent().Append($"public static TrimPatternCodec {propertyName} {{ get; }} = new() {{ Id = {trimPattern.RegistryId}, Name = \"{trimPattern.Name}\", Element = new() {{ ");

foreach (var property in trimPattern.Properties)
{
var name = property.Key;
var value = property.Value;

builder.Append($"{name} = ");

if (value.ValueKind == JsonValueKind.Object)
{
ParseProperty(builder, value, ctx);
continue;
}

AppendValueType(builder, value, ctx);
}

builder.Append("} };").Line();
}

builder.Line().Statement("public static IReadOnlyDictionary<string, TrimPatternCodec> All { get; } = new Dictionary<string, TrimPatternCodec>");

foreach (var name in trimPatterns.Select(x => x.Name))
{
var propertyName = name.RemoveNamespace().ToPascalCase();
builder.Line($"{{ \"{name}\", {propertyName} }},");
}

builder.EndScope(".AsReadOnly()", true).Line();

builder.EndScope();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ private static void GenerateCodecs(Assets assets, SourceProductionContext ctx)
.Using("Obsidian.API.Registry.Codecs.Chat")
.Using("Obsidian.API.Registry.Codecs.Dimensions")
.Using("Obsidian.API.Registry.Codecs.DamageTypes")
.Using("Obsidian.API.Registry.Codecs.ArmorTrims")
.Using("Obsidian.API.Registry.Codecs.ArmorTrims.TrimPattern")
.Using("Obsidian.API.Registry.Codecs.ArmorTrims.TrimMaterial")
.Line()
.Namespace("Obsidian.Registries")
.Line()
Expand All @@ -22,6 +25,8 @@ private static void GenerateCodecs(Assets assets, SourceProductionContext ctx)
GenerateBiomes(codecs["biomes"].ToArray(), builder, ctx);
GenerateChatType(codecs["chat_type"].ToArray(), builder, ctx);
GenerateDamageTypes(codecs["damage_type"].ToArray(), builder, ctx);
GenerateTrimMaterial(codecs["trim_material"].ToArray(), builder, ctx);
GenerateTrimPattern(codecs["trim_pattern"].ToArray(), builder, ctx);

builder.EndScope();

Expand Down
56 changes: 48 additions & 8 deletions Obsidian.SourceGenerators/Registry/RegistryAssetsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private void Generate(SourceProductionContext context, (Compilation compilation,
}
}

private static void ParseProperty(CodeBuilder builder, JsonElement element, SourceProductionContext ctx)
private static void ParseProperty(CodeBuilder builder, JsonElement element, SourceProductionContext ctx, bool isDictionary = false)
{
builder.Append("new() { ");

Expand All @@ -53,11 +53,11 @@ private static void ParseProperty(CodeBuilder builder, JsonElement element, Sour
{
if (value.ValueKind is JsonValueKind.Object or JsonValueKind.Array)
{
ParseProperty(builder, value, ctx);
ParseProperty(builder, value, ctx, isDictionary);
continue;
}

AppendValueType(builder, value, ctx);
AppendValueType(builder, value, ctx, isDictionary);
}
}
else
Expand All @@ -68,25 +68,65 @@ private static void ParseProperty(CodeBuilder builder, JsonElement element, Sour

if (!isArray)
{
var name = property.Name.ToPascalCase();
builder.Append($"{name} = ");
if (isDictionary)
{
builder.Append($"{{ \"{property.Name}\", ");
}
else
{
var name = property.Name.ToPascalCase();
builder.Append($"{name} = ");
}
}

if (value.ValueKind is JsonValueKind.Object or JsonValueKind.Array)
{
ParseProperty(builder, value, ctx);
ParseProperty(builder, value, ctx, isDictionary);
continue;
}

AppendValueType(builder, value, ctx);
AppendValueType(builder, value, ctx, isDictionary);
}
}

builder.Append("}, ");
}

private static void AppendValueType(CodeBuilder builder, JsonElement element, SourceProductionContext ctx)
private static void AppendValueType(CodeBuilder builder, JsonElement element, SourceProductionContext ctx, bool isDictionary = false)
{
if (isDictionary)
{
switch (element.ValueKind)
{
case JsonValueKind.String:
builder.Append($"\"{element.GetString()}\" }},");
break;
case JsonValueKind.Number:
{
if (element.TryGetInt32(out var intValue))
builder.Append($"{intValue} }},");
else if (element.TryGetInt64(out var longValue))
builder.Append($"{longValue} }},");
else if (element.TryGetSingle(out var floatValue))
builder.Append($"{floatValue}f }},");
else if (element.TryGetDouble(out var doubleValue))
builder.Append($"{doubleValue}d }},");
break;
}
case JsonValueKind.True:
case JsonValueKind.False:
builder.Append($"{element.GetBoolean().ToString().ToLower()} }},");
break;
case JsonValueKind.Null:
break;
default:
ctx.ReportDiagnostic(DiagnosticSeverity.Error, $"Found an invalid property type: {element.ValueKind} in json.");
break;
}

return;
}

switch (element.ValueKind)
{
case JsonValueKind.String:
Expand Down

0 comments on commit b09be32

Please sign in to comment.