Skip to content

Commit

Permalink
Move codecs and item registry to api (#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tides authored Dec 10, 2024
1 parent 2b99b43 commit d08ae37
Show file tree
Hide file tree
Showing 24 changed files with 64 additions and 71 deletions.
3 changes: 3 additions & 0 deletions Obsidian.API/Obsidian.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
<AdditionalFiles Include="..\Obsidian\Assets\items.json" Link="Assets\items.json" />
<AdditionalFiles Include="..\Obsidian\Assets\fluids.json" Link="Assets\fluids.json" />
<AdditionalFiles Include="..\Obsidian\Assets\sounds.json" Link="Assets\sounds.json" />
<AdditionalFiles Include="..\Obsidian\Assets\tags.json" Link="Assets\tags.json" />

<AdditionalFiles Include="..\Obsidian\Assets\Codecs\**\*.json" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
using Obsidian.API.Registry.Codecs.Dimensions;
using System.Diagnostics.CodeAnalysis;

namespace Obsidian.Registries;
namespace Obsidian.API.Registries;
public static partial class CodecRegistry
{
public static bool TryGetChatType(string resourceId, [MaybeNullWhen(false)] out ChatTypeCodec codec) =>
public static bool TryGetChatType(string resourceId, [MaybeNullWhen(false)] out ChatTypeCodec? codec) =>
ChatType.All.TryGetValue(resourceId, out codec);

public static bool TryGetBiome(string resourceId, [MaybeNullWhen(false)] out BiomeCodec codec) =>
public static bool TryGetBiome(string resourceId, [MaybeNullWhen(false)] out BiomeCodec? codec) =>
Biomes.All.TryGetValue(resourceId, out codec);

public static bool TryGetDimension(string resourceId, [MaybeNullWhen(false)] out DimensionCodec codec) =>
public static bool TryGetDimension(string resourceId, [MaybeNullWhen(false)] out DimensionCodec? codec) =>
Dimensions.All.TryGetValue(resourceId, out codec);
}

Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using Obsidian.API.Crafting;
using Obsidian.API.Utilities;

namespace Obsidian.Registries;
namespace Obsidian.API.Registries;
public static partial class ItemsRegistry
{
public static Item Get(int id) => Items.Values.SingleOrDefault(x => x.Id == id);
public static Item Get(Material mat) => Items.GetValueOrDefault(mat);
public static Item Get(string unlocalizedName) =>
Items.Values.SingleOrDefault(x => x.UnlocalizedName.EqualsIgnoreCase(unlocalizedName));

public static bool TryGet(Material mat, out Item item) => Items.TryGetValue(mat, out item);

public static ItemStack Get(string unlocalizedName, short count, ItemMeta? meta = null) => new(Get(unlocalizedName).Type, count, meta);

public static ItemStack GetSingleItem(Material mat, ItemMeta? meta = null) => new(mat, 1, meta);
Expand All @@ -30,8 +32,8 @@ public static Ingredient GetIngredientFromTag(string tag, short count)
return ingredient;
}

public static Ingredient GetIngredientFromName(string name, short count) => new()
{
public static Ingredient GetIngredientFromName(string name, short count) =>
[
Get(name, count)
};
];
}
14 changes: 9 additions & 5 deletions Obsidian.API/_Types/Inventory/Item.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
namespace Obsidian.API;
using System.Diagnostics.CodeAnalysis;

public struct Item
namespace Obsidian.API;

public readonly struct Item
{
public string UnlocalizedName { get; }
public required string UnlocalizedName { get; init; }

public Material Type { get; }
public required Material Type { get; init; }

public short Id { get; internal set; }
public required short Id { get; init; }

[SetsRequiredMembers]
public Item(int id, string unlocalizedName, Material type)
{
this.Id = (short)id;
this.UnlocalizedName = unlocalizedName;
this.Type = type;
}

[SetsRequiredMembers]
public Item(Item item)
{
this.Id = item.Id;
Expand Down
9 changes: 9 additions & 0 deletions Obsidian.API/_Types/Tag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Obsidian.API;
public sealed record class Tag
{
public required string Name { get; init; }
public required string Type { get; init; }
public bool Replace { get; init; }
public required int[] Entries { get; init; }
public int Count => Entries.Length;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private static void GenerateCodecs(Assets assets, SourceProductionContext ctx)
.Using("Obsidian.API.Registry.Codecs.PaintingVariant")
.Using("System.Collections.Frozen")
.Line()
.Namespace("Obsidian.Registries")
.Namespace("Obsidian.API.Registries")
.Line()
.Type("public static partial class CodecRegistry");

Expand Down Expand Up @@ -59,6 +59,11 @@ private static void GenerateCodecs(Assets assets, SourceProductionContext ctx)
builder.ParseProperty(value, ctx);
return;
}
else if (value.ValueKind == JsonValueKind.Array)
{
builder.ParseArray(value, ctx);
return;
}

builder.AppendValueType(value, ctx);
}, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,31 @@ private static void GenerateItems(Assets assets, SourceProductionContext context
{
var builder = new CodeBuilder();
builder.Using("System.Collections.Generic");
builder.Using("System.Collections.Frozen");
builder.Using("Obsidian.API");
builder.Using("Obsidian.API.Inventory");
builder.Line();
builder.Namespace("Obsidian.Registries");
builder.Namespace("Obsidian.API.Registries");
builder.Line();
builder.Type("public static partial class ItemsRegistry");

builder.Statement("internal static Dictionary<Material, Item> Items = new()");
foreach (Item item in assets.Items)
{
var name = item.Name;

builder.Line($"public static Item {name} {{ get; }} = new Item({item.Id}, \"{item.Tag}\", Material.{name});");
}

builder.Statement("internal static FrozenDictionary<Material, Item> Items = new Dictionary<Material, Item>()");

foreach (Item item in assets.Items)
{
var name = item.Name;

builder.Line($"{{ Material.{name}, new Item({item.Id}, \"{item.Tag}\", Material.{name}) }},");
builder.Line($"{{ Material.{name}, {name} }},");
}

builder.EndScope(semicolon: true);
builder.EndScope(".ToFrozenDictionary()", semicolon: true);

builder.EndScope();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ public partial class RegistryAssetsGenerator
private static void GenerateTags(Assets assets, SourceProductionContext context)
{
var builder = new CodeBuilder();
builder.Using("Obsidian.Net.Packets.Play.Clientbound");
builder.Line();
builder.Namespace("Obsidian.Registries");
builder.Namespace("Obsidian.API.Registries");
builder.Line();
builder.Type("internal static class TagsRegistry");
builder.Type("public static class TagsRegistry");

var tags = assets.Tags.GroupBy(tag => tag.Parent).ToDictionary(x => x.Key, x => x.ToImmutableList());
var skip = new List<string>();
Expand Down
6 changes: 3 additions & 3 deletions Obsidian.SourceGenerators/Registry/RegistryAssetsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ private void Generate(SourceProductionContext context, (Compilation compilation,

if (assembly == "Obsidian")
{
GenerateTags(assets, context);
GenerateItems(assets, context);
GenerateBlockIds(assets, context);
GenerateCodecs(assets, context);
}
else if (assembly == "Obsidian.API")
{
GenerateTags(assets, context);
GenerateItems(assets, context);
GenerateCodecs(assets, context);
GenerateMaterials(assets, context);
GenerateSounds(assets, context);
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 1 addition & 2 deletions Obsidian/BlockUpdate.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Obsidian.Registries;
using Obsidian.WorldData;
using Obsidian.WorldData;

namespace Obsidian;

Expand Down
1 change: 1 addition & 0 deletions Obsidian/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
global using Obsidian.API;
global using Obsidian.API.Registries;
global using Obsidian.Utilities;
global using System;
global using System.Collections.Concurrent;
Expand Down
20 changes: 1 addition & 19 deletions Obsidian/Obsidian.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@
</None>
</ItemGroup>

<ItemGroup>
<Compile Remove="Serializer\Enums\FieldType.cs" />
</ItemGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
Expand All @@ -62,27 +58,13 @@
<PackageReference Include="SharpNoise" Version="0.12.1.1" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Assets/*.*" CopyToOutputDirectory="PreserveNewest" />
<EmbeddedResource Include="Assets/Structures/*.*" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<ItemGroup>
<Compile Remove="$(CompilerGeneratedFilesOutputPath)/**/*.cs" />
<Compile Remove="Commands\Framework\ArgumentParsers\**" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="Assets\**\*.json" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Remove="Commands\Framework\ArgumentParsers\**" />
<None Remove="Commands\Framework\ArgumentParsers\**" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Remove="Assets\sounds.json" />
<EmbeddedResource Include="Assets\**\*.*" />
</ItemGroup>

<ItemGroup>
Expand Down
18 changes: 5 additions & 13 deletions Obsidian/Registries/BlocksRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace Obsidian.Registries;

internal static partial class BlocksRegistry
{
private static string[] illegalBlockNames = ["Obsidian", "TrialSpawner", "Vault"];

public static int GlobalBitsPerBlocks { get; private set; }
static BlocksRegistry()
{
Expand Down Expand Up @@ -83,7 +85,7 @@ public static IBlock Get(string resourceId, IBlockState? state = null)
blockTypeCache.TryAdd(blockName, type!);
}

var ctor = type!.GetConstructor(Type.EmptyTypes);
var ctor = type!.GetConstructor(Type.EmptyTypes)!;

var expression = Expression.New(ctor);

Expand Down Expand Up @@ -136,16 +138,6 @@ public static IBlock Get(Material material, IBlockState? state = null)
return block;
}

private static string GetSanitizedName(string value)
{
var sanitizedBlockName = value;
if (sanitizedBlockName == "Obsidian")
sanitizedBlockName = "ObsidianBlock";
if (sanitizedBlockName == "TrialSpawner")
sanitizedBlockName = "TrialSpawnerBlock";
if (sanitizedBlockName == "Vault")
sanitizedBlockName = "VaultBlock";

return sanitizedBlockName;
}
private static string GetSanitizedName(string value) =>
illegalBlockNames.Contains(value) ? $"{value}Block" : string.Empty;
}
3 changes: 0 additions & 3 deletions Obsidian/Registries/CommandsRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
using Obsidian.Commands.Parsers;
using Obsidian.Net.Packets.Play.Clientbound;
using Obsidian.Utilities.Interfaces;
using System.Xml;
using System;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Obsidian.Registries;
public static class CommandsRegistry
Expand Down
2 changes: 1 addition & 1 deletion Obsidian/Registries/RecipesRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace Obsidian.Registries;
public static partial class RecipesRegistry
{
public static readonly Dictionary<string, IRecipe> Recipes = new();
public static readonly Dictionary<string, IRecipe> Recipes = [];

public static async Task InitializeAsync()
{
Expand Down
9 changes: 0 additions & 9 deletions Obsidian/Tag.cs

This file was deleted.

0 comments on commit d08ae37

Please sign in to comment.