Skip to content

Commit

Permalink
Reviver Debug
Browse files Browse the repository at this point in the history
Checking that the order properties are logged is in the same order that JSON.parse() logs them. Stringifying should be BFS, while parsing should be DFS. It's the order that they are traversed as the file is read/written through.

In theory this means that you would be able to write a replacer/reviver pair and it would work both with JSON and NBT, if you did things in a certain way. This seems like a very neat thing that could make converting between the formats much more safe. It seems like a natural progression for adding custom primitives to either format, by way of essentially transpiling them in.

Come to think of it, this would actually work very well for converting to and from NBTify primitives to Prismarine-NBT objects as well, that might be a nice way to add that to NBTify itself without needing to implement custom functions or options for that outright.

#33
#22
  • Loading branch information
Offroaders123 committed Nov 30, 2024
1 parent 5dbae0b commit e773b20
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@ const options: ReadAdjacentOptions = {
// }

const nbts: NBTData[] = await Array.fromAsync(readAdjacent(data, options));
console.log(nbts);
// const nbts: NBTData = (await readAdjacent(data, options).next()).value!;
console.log(nbts.pop());

interface ReadAdjacentOptions extends Omit<ReadOptions, "strict" | "rootCheck"> {}

async function* readAdjacent<T extends RootTagLike = RootTag>(data: Uint8Array, options: ReadAdjacentOptions): AsyncGenerator<NBTData<T>, void, void> {
let byteOffset: number = 0;

while (byteOffset < data.byteLength) {
const nbt: NBTData<T> = await read(data.subarray(byteOffset), { ...options, strict: false });
// console.log(byteOffset);
const nbt: NBTData<T> = await read(data.subarray(byteOffset), { ...options, strict: false },
(key, value) => {
if (byteOffset > 850) console.log(key, value);
return value;
});
byteOffset += nbt.byteOffset!;
yield nbt;
}
Expand Down

0 comments on commit e773b20

Please sign in to comment.