Skip to content

Commit

Permalink
Hotfix/chunk deserializing (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tides authored Dec 7, 2024
1 parent 75a8a71 commit e021be0
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
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)
{
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")
Expand All @@ -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());
Expand Down
9 changes: 1 addition & 8 deletions Obsidian.SourceGenerators/Registry/BlocksGenerator.Blocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
9 changes: 6 additions & 3 deletions Obsidian.SourceGenerators/Registry/NameHelper.cs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
3 changes: 1 addition & 2 deletions Obsidian/ChunkData/BiomeContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
9 changes: 1 addition & 8 deletions Obsidian/ChunkData/BlockStateContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
Expand Down
8 changes: 8 additions & 0 deletions Obsidian/ChunkData/DataContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,13 @@ public abstract class DataContainer<T>

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);
}
27 changes: 14 additions & 13 deletions Obsidian/WorldData/Region.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,31 +195,32 @@ 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<long>;
section.BlockStateContainer.DataArray.storage = data!.GetArray();
}

var biomesCompound = sectionCompound["biomes"] as NbtCompound;
var biomesPalette = biomesCompound!["palette"] as NbtList;

var biomePalette = section.BiomeContainer.Palette;
foreach (NbtTag<string> biome in biomesPalette!)
{
if (Enum.TryParse<Biome>(biome.Value.TrimResourceTag(), true, out var value))
biomePalette.GetOrAddId(value);
}
if (biomesPalette.Count > 1)
if(biomesCompound!.TryGetTag<NbtList>("palette", out var biomesPalette))
{
if (biomesCompound.TryGetTag("data", out var biomeDataArrayTag))
var biomePalette = section.BiomeContainer.Palette;
foreach (NbtTag<string> biome in biomesPalette!)
{
var data = biomeDataArrayTag as NbtArray<long>;
section.BiomeContainer.DataArray.storage = data!.GetArray();
if (Enum.TryParse<Biome>(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<long>;
section.BiomeContainer.DataArray.storage = data!.GetArray();
}


if (sectionCompound.TryGetTag("SkyLight", out var skyLightTag))
{
var array = (NbtArray<byte>)skyLightTag;
Expand Down
59 changes: 0 additions & 59 deletions Obsidian/WorldData/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>("type", codec.Name),
}
};

foreach (var (id, _) in dimensions)
{
CodecRegistry.TryGetDimension(id, out var childDimensionCodec);

dimensionsCompound.Add(new NbtCompound(childDimensionCodec.Name)
{
new NbtTag<string>("type", childDimensionCodec.Name),
});
}

var worldGenSettingsCompound = new NbtCompound("WorldGenSettings")
{
//THE SEED SHOULD BE NUMERICAL
new NbtTag<string>("seed", Seed),

dimensionsCompound
};

writer.WriteTag(worldGenSettingsCompound);
}

public async ValueTask DisposeAsync()
{
foreach ((_, Region region) in Regions)
Expand Down

0 comments on commit e021be0

Please sign in to comment.