Skip to content

Commit

Permalink
MUTF-8 Test, Fix!
Browse files Browse the repository at this point in the history
This was an interesting one! Thankfully I found an issue page about MUTF-8 handling on the repo for Twoolie/NBT, the Python project. It gave me some insight and a file to test against. I wrote my own script to slim it down a bunch, and dedupe the tags that are used multiple times. It's crazy how big just book text can get!

I used this actual version of NBTify in this commit, to write the new content to the file. That's also why I diffed it out, I wanted to make sure when I slimmed it down that the content coming out of it was actually what it was supposed to be as well. When using older NBTify, it didn't work correctly, because MUTF-8 handles things different than standard UTF-8.

```js
// @ts-check

import { readFile, writeFile } from "node:fs/promises";
import * as NBT from "./NBTify/src/index.ts";

const data = await readFile("./hotbar.nbt");

const trimmed = data.subarray(0x000BAE96, 0x000CA7C2);
console.log(trimmed);

/** @type {NBT.NBTData<any>} */
const hotbar = await NBT.read(data);

const book = hotbar.data[0][1].tag.BlockEntityTag.Items[12];
console.log(book);

const mutf8Demo = await NBT.write(book);
console.log(mutf8Demo);

const demoDiff = mutf8Demo.subarray(1, -2);
console.log(Buffer.compare(trimmed, demoDiff));

await writeFile("./alien-book.nbt", mutf8Demo);
```

#42
#44
twoolie/NBT#144 (comment)
twoolie/NBT#144

I'm still not sure I'm going to use the dependency itself or if I should just emded that into NBTify on it's own. I think I may just use it as a dependency, as I've been trying to get more used to not reinventing the wheel for everything, unless that has benefits. The MUTF-8 library already does everything I need it to, and it's ESM TypeScript, so I'm not sure what other reason I have to not just use it, it's great! Eventually I want to move my compression handling into a separate module too, so I will have to use module resolution for that down the road either way. I say heck to it! Let's do it :) Gonna look into if there's anything I'm forgetting, before doing that though. I really like having the ability to use projects like these (NBTify) without needing a transpilation or build step. Modern CDNs seem to handle this nicely, so we'll see.
  • Loading branch information
Offroaders123 committed May 14, 2024
1 parent 8bed4c7 commit f132db1
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 2 deletions.
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
"url": "https://github.com/Offroaders123/NBTify/issues"
},
"homepage": "https://github.com/Offroaders123/NBTify#readme",
"dependencies": {
"mutf-8": "^1.1.3"
},
"devDependencies": {
"@types/node": "^20.12.7",
"tsx": "^4.7.2",
Expand Down
3 changes: 2 additions & 1 deletion src/read.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MUtf8Decoder } from "mutf-8";
import { NBTData } from "./format.js";
import { Int8, Int16, Int32, Float32 } from "./primitive.js";
import { TAG, TAG_TYPE } from "./tag.js";
Expand Down Expand Up @@ -120,7 +121,7 @@ class NBTReader {
#data: Uint8Array;
#view: DataView;
#littleEndian: boolean;
#decoder: TextDecoder = new TextDecoder();
#decoder: MUtf8Decoder = new MUtf8Decoder();

constructor(data: Uint8Array, littleEndian: boolean) {
this.#data = data;
Expand Down
3 changes: 2 additions & 1 deletion src/write.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MUtf8Encoder } from "mutf-8";
import { NBTData } from "./format.js";
import { TAG, TAG_TYPE, isTag, getTagType } from "./tag.js";
import { Int32 } from "./primitive.js";
Expand Down Expand Up @@ -46,7 +47,7 @@ class NBTWriter {
#data: Uint8Array = new Uint8Array(1024);
#view: DataView = new DataView(this.#data.buffer);
#littleEndian: boolean;
#encoder: TextEncoder = new TextEncoder();
#encoder: MUtf8Encoder = new MUtf8Encoder();

constructor(littleEndian: boolean) {
this.#littleEndian = littleEndian;
Expand Down
Binary file added test/nbt/alien-book.nbt
Binary file not shown.

0 comments on commit f132db1

Please sign in to comment.