using System; using System.IO; using Newtonsoft.Json.Linq; namespace Luneraria.EditorTools { /// /// The world bake of tools/godot-golden/world_export.gd: g1_world.json (structure, transforms, /// materials) and g1_world.bytes (little-endian arrays referenced as [offset, count]). /// public sealed class LuneWorldBake { public const string Folder = "Assets/Luneraria/World/Bake"; public readonly JObject Data; readonly byte[] _blob; LuneWorldBake(JObject data, byte[] blob) { Data = data; _blob = blob; } public static LuneWorldBake Load(string folder = Folder) { return new LuneWorldBake(JObject.Parse(File.ReadAllText(Path.Combine(folder, "g1_world.json"))), File.ReadAllBytes(Path.Combine(folder, "g1_world.bytes"))); } public float[] Floats(JToken reference, int components = 1) { int offset = (int)reference[0]; int count = (int)reference[1] * components; var values = new float[count]; Buffer.BlockCopy(_blob, offset, values, 0, count * 4); return values; } public int[] Ints(JToken reference) { int offset = (int)reference[0]; int count = (int)reference[1]; var values = new int[count]; Buffer.BlockCopy(_blob, offset, values, 0, count * 4); return values; } public static float[] Transform(JToken values) { var result = new float[12]; for (int i = 0; i < 12; i++) result[i] = (float)values[i]; return result; } } }