From e021be061601485c486380bb1d64dc35c47359c1 Mon Sep 17 00:00:00 2001 From: Junior Date: Fri, 6 Dec 2024 19:07:47 -0600 Subject: [PATCH] Hotfix/chunk deserializing (#460) --- .../Registry/BlocksGenerator.BlockStates.cs | 11 +--- .../Registry/BlocksGenerator.Blocks.cs | 9 +-- .../Registry/NameHelper.cs | 9 ++- Obsidian/ChunkData/BiomeContainer.cs | 3 +- Obsidian/ChunkData/BlockStateContainer.cs | 9 +-- Obsidian/ChunkData/DataContainer.cs | 8 +++ Obsidian/WorldData/Region.cs | 27 +++++---- Obsidian/WorldData/World.cs | 59 ------------------- 8 files changed, 34 insertions(+), 101 deletions(-) diff --git a/Obsidian.SourceGenerators/Registry/BlocksGenerator.BlockStates.cs b/Obsidian.SourceGenerators/Registry/BlocksGenerator.BlockStates.cs index fcb81b9c2..718552b05 100644 --- a/Obsidian.SourceGenerators/Registry/BlocksGenerator.BlockStates.cs +++ b/Obsidian.SourceGenerators/Registry/BlocksGenerator.BlockStates.cs @@ -1,8 +1,10 @@ using Obsidian.SourceGenerators.Registry.Models; +using System.Text.Json; namespace Obsidian.SourceGenerators.Registry; public partial class BlocksGenerator { + private static void CreateBlockStates(Block[] blocks, SourceProductionContext ctx) { foreach (var block in blocks) @@ -10,13 +12,7 @@ private static void CreateBlockStates(Block[] blocks, SourceProductionContext ct if (block.Properties.Length == 0) continue; - var blockName = block.Name; - - //TODO THIS NEEDS TO BE MOVED SOMEWHERE - if (blockName == "TrialSpawner") - blockName = "TrialSpawnerBlock"; - if (blockName == "Vault") - blockName = "VaultBlock"; + var blockName = block.Name.SanitizeBlockName(); var builder = new CodeBuilder() .Namespace("Obsidian.API.BlockStates") @@ -34,7 +30,6 @@ private static void CreateBlockStates(Block[] blocks, SourceProductionContext ct builder.Line().Line().Method($"public {blockName}State()").EndScope(); - //00010 builder.EndScope(); ctx.AddSource($"{blockName}State.g.cs", builder.ToString()); diff --git a/Obsidian.SourceGenerators/Registry/BlocksGenerator.Blocks.cs b/Obsidian.SourceGenerators/Registry/BlocksGenerator.Blocks.cs index 8aa2e9692..220b416b0 100644 --- a/Obsidian.SourceGenerators/Registry/BlocksGenerator.Blocks.cs +++ b/Obsidian.SourceGenerators/Registry/BlocksGenerator.Blocks.cs @@ -15,14 +15,7 @@ private static void GenerateBlocks(Block[] blocks, SourceProductionContext ctx) foreach (var block in blocks) { - var blockName = block.Name; - - if (blockName == "Obsidian") - blockName = "ObsidianBlock"; - if (blockName == "TrialSpawner") - blockName = "TrialSpawnerBlock"; - if (blockName == "Vault") - blockName = "VaultBlock"; + var blockName = block.Name.SanitizeBlockName(); var builder = new CodeBuilder() .Using("Obsidian.API") diff --git a/Obsidian.SourceGenerators/Registry/NameHelper.cs b/Obsidian.SourceGenerators/Registry/NameHelper.cs index b74e04f47..7fe66a99f 100644 --- a/Obsidian.SourceGenerators/Registry/NameHelper.cs +++ b/Obsidian.SourceGenerators/Registry/NameHelper.cs @@ -1,9 +1,12 @@ -using System.Text.Json; - -namespace Obsidian.SourceGenerators.Registry; +namespace Obsidian.SourceGenerators.Registry; internal static class NameHelper { + private static readonly string[] invalidBlockNames = ["TrialSpawner", "Vault", "Obsidian"]; + + public static string SanitizeBlockName(this string blockName) => + invalidBlockNames.Contains(blockName) ? $"{blockName}Block" : blockName; + public static string RemoveNamespace(this string namespacedName) { return namespacedName.Substring(namespacedName.IndexOf(":") + 1); diff --git a/Obsidian/ChunkData/BiomeContainer.cs b/Obsidian/ChunkData/BiomeContainer.cs index 82a9e865b..26f9c1e2e 100644 --- a/Obsidian/ChunkData/BiomeContainer.cs +++ b/Obsidian/ChunkData/BiomeContainer.cs @@ -27,8 +27,7 @@ public void Set(int x, int y, int z, Biome biome) var paletteIndex = this.Palette.GetOrAddId(biome); - if (Palette.BitCount > DataArray.BitsPerEntry) - DataArray = DataArray.Grow(Palette.BitCount); + this.GrowDataArray(); this.DataArray[index] = paletteIndex; } diff --git a/Obsidian/ChunkData/BlockStateContainer.cs b/Obsidian/ChunkData/BlockStateContainer.cs index e3281d1e3..bf8031178 100644 --- a/Obsidian/ChunkData/BlockStateContainer.cs +++ b/Obsidian/ChunkData/BlockStateContainer.cs @@ -46,18 +46,11 @@ public void Set(int x, int y, int z, IBlock blockState) int paletteId = Palette.GetOrAddId(blockState); - if (Palette.BitCount > DataArray.BitsPerEntry) - DataArray = DataArray.Grow(Palette.BitCount); + this.GrowDataArray(); DataArray[blockIndex] = paletteId; } - public void GrowDataArray() - { - if (Palette.BitCount > DataArray.BitsPerEntry) - DataArray = DataArray.Grow(Palette.BitCount); - } - public IBlock Get(int x, int y, int z) { int storageId = DataArray[GetIndex(x, y, z)]; diff --git a/Obsidian/ChunkData/DataContainer.cs b/Obsidian/ChunkData/DataContainer.cs index 85e90648d..b3fad5c7d 100644 --- a/Obsidian/ChunkData/DataContainer.cs +++ b/Obsidian/ChunkData/DataContainer.cs @@ -11,5 +11,13 @@ public abstract class DataContainer public virtual int GetIndex(int x, int y, int z) => (y << this.BitsPerEntry | z) << this.BitsPerEntry | x; + public void GrowDataArray() + { + if (Palette.BitCount <= DataArray.BitsPerEntry) + return; + + DataArray = DataArray.Grow(Palette.BitCount); + } + public abstract void WriteTo(INetStreamWriter writer); } diff --git a/Obsidian/WorldData/Region.cs b/Obsidian/WorldData/Region.cs index 73bbd4748..2473d938c 100644 --- a/Obsidian/WorldData/Region.cs +++ b/Obsidian/WorldData/Region.cs @@ -195,7 +195,6 @@ private static Chunk DeserializeChunk(NbtCompound chunkCompound) section.BlockStateContainer.GrowDataArray(); } - //TODO find a way around this (We shouldn't be storing the states data array in nbt anyway.) if (statesCompound.TryGetTag("data", out var dataArrayTag)) { var data = dataArrayTag as NbtArray; @@ -203,23 +202,25 @@ private static Chunk DeserializeChunk(NbtCompound chunkCompound) } var biomesCompound = sectionCompound["biomes"] as NbtCompound; - var biomesPalette = biomesCompound!["palette"] as NbtList; - - var biomePalette = section.BiomeContainer.Palette; - foreach (NbtTag biome in biomesPalette!) - { - if (Enum.TryParse(biome.Value.TrimResourceTag(), true, out var value)) - biomePalette.GetOrAddId(value); - } - if (biomesPalette.Count > 1) + if(biomesCompound!.TryGetTag("palette", out var biomesPalette)) { - if (biomesCompound.TryGetTag("data", out var biomeDataArrayTag)) + var biomePalette = section.BiomeContainer.Palette; + foreach (NbtTag biome in biomesPalette!) { - var data = biomeDataArrayTag as NbtArray; - section.BiomeContainer.DataArray.storage = data!.GetArray(); + if (Enum.TryParse(biome.Value.TrimResourceTag(), true, out var value)) + biomePalette.GetOrAddId(value); } + + section.BiomeContainer.GrowDataArray(); } + if (biomesCompound.TryGetTag("data", out var biomeDataArrayTag)) + { + var data = biomeDataArrayTag as NbtArray; + section.BiomeContainer.DataArray.storage = data!.GetArray(); + } + + if (sectionCompound.TryGetTag("SkyLight", out var skyLightTag)) { var array = (NbtArray)skyLightTag; diff --git a/Obsidian/WorldData/World.cs b/Obsidian/WorldData/World.cs index 0465d0137..bb9e14db8 100644 --- a/Obsidian/WorldData/World.cs +++ b/Obsidian/WorldData/World.cs @@ -936,67 +936,8 @@ internal bool TryAddEntity(Entity entity) return region.Entities.TryAdd(entity.EntityId, entity); } - //TODO - private void LoadWorldGenSettings(NbtCompound levelCompound) - { - if (!levelCompound.TryGetTag("WorldGenSettings", out var genTag)) - return; - - var worldGenSettings = genTag as NbtCompound; - - // bonus_chest - // seed - // generate_features - - if (worldGenSettings.TryGetTag("dimensions", out var dimensionsTag)) - { - var dimensions = dimensionsTag as NbtCompound; - - foreach (var (_, childDimensionTag) in dimensions) - { - var childDimensionCompound = childDimensionTag as NbtCompound; - } - - var dimensionType = dimensions.GetString("type"); - } - } - private void BroadcastTime() => this.PacketBroadcaster.QueuePacketToWorld(this, new SetTimePacket(LevelData.Time, LevelData.Time % 24000, true)); - private void WriteWorldGenSettings(NbtWriter writer) - { - if (!CodecRegistry.TryGetDimension(DimensionName, out var codec)) - return; - - var dimensionsCompound = new NbtCompound("dimensions") - { - new NbtCompound(codec.Name) - { - new NbtTag("type", codec.Name), - } - }; - - foreach (var (id, _) in dimensions) - { - CodecRegistry.TryGetDimension(id, out var childDimensionCodec); - - dimensionsCompound.Add(new NbtCompound(childDimensionCodec.Name) - { - new NbtTag("type", childDimensionCodec.Name), - }); - } - - var worldGenSettingsCompound = new NbtCompound("WorldGenSettings") - { - //THE SEED SHOULD BE NUMERICAL - new NbtTag("seed", Seed), - - dimensionsCompound - }; - - writer.WriteTag(worldGenSettingsCompound); - } - public async ValueTask DisposeAsync() { foreach ((_, Region region) in Regions)