-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e771947
Showing
4 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules/ | ||
package-lock.json | ||
*.nbt | ||
*.dat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# nbt-dump | ||
|
||
A simple command line Node.js tool to read and write NBT files to JSON and back. Supports big, little and little-varint encoding. | ||
|
||
Uses `prismarine-nbt` for serialization and deserialization, see https://github.com/PrismarineJS/prismarine-nbt for more info on schema. | ||
|
||
```sh | ||
usage, feel free to use natural language: | ||
Parse an NBT file to JSON: | ||
nbt-dump <path-to-nbt-file> [out-json-file] [little|big|varint] | ||
|
||
nbt-dump level.dat | ||
nbt-dump level.dat to level.json | ||
nbt-dump level.dat as little to level.json | ||
|
||
Write an JSON file to uncompressed NBT (defaults to big endian): | ||
nbt-dump write <path-to-json> [out-nbt-file] [little|big|varint] | ||
|
||
nbt-dump write level.json to level.dat | ||
nbt-dump write level.json to level.dat as little | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
const fs = require('fs') | ||
const nbt = require('prismarine-nbt') | ||
|
||
const usage = `usage, feel free to use natural language: | ||
Parse an NBT file to JSON: | ||
nbt-dump <path-to-nbt-file> [out-json-file] [little|big|varint] | ||
nbt-dump level.dat | ||
nbt-dump level.dat to level.json | ||
nbt-dump level.dat as little to level.json | ||
Write an JSON file to uncompressed NBT (defaults to big endian): | ||
nbt-dump write <path-to-json> [out-nbt-file] [little|big|varint] | ||
nbt-dump write level.json to level.dat | ||
nbt-dump write level.json to level.dat as little | ||
` | ||
|
||
function main(args, argsStr) { | ||
if (args.length == 0 || argsStr.includes('help')) { | ||
console.warn(usage) | ||
process.exit(1) | ||
} | ||
if (args[0] == 'read') args.shift() | ||
let files = [] | ||
for (var arg of args) { | ||
if (arg.includes('.')) files.push(arg) | ||
} | ||
const getFmt = () => { | ||
if (args.includes('big')) return 'big' | ||
if (args.includes('varint')) return 'littleVarint' | ||
if (args.includes('little')) return 'little' | ||
return '' | ||
} | ||
if (args[0] == 'write') { | ||
if (!files.length && args[1]) files.push(args[1]) | ||
if (files.length == 1) files.push(files[0] + '.nbt') | ||
if (files.length == 2) return write(...files, getFmt()) | ||
} else { | ||
if (!files.length) files = [args[0], args[0] + '.json'] | ||
// if (files.length == 1) files.push(files[0] + '.json') | ||
return read(files[0], files[1], getFmt()) | ||
} | ||
|
||
console.error('Bad arguments') | ||
console.warn(arguments) | ||
} | ||
|
||
function write(inpf, outf, fmt) { | ||
console.log(`* Writing JSON from "${inpf}" to "${outf}" as ${fmt || 'big'} endian`) | ||
|
||
const json = JSON.parse(fs.readFileSync(inpf)) | ||
const outBuffer = fs.createWriteStream(outf) | ||
|
||
try { | ||
const newBuf = nbt.writeUncompressed(result, json) | ||
outBuffer.write(newBuf) | ||
outBuffer.end(() => console.log('written!')) | ||
} catch (e) { | ||
console.warn('Failed to write. Make sure that the JSON schema matches the prismarine-nbt ProtoDef schema. See https://github.com/PrismarineJS/prismarine-nbt') | ||
throw e | ||
} | ||
return | ||
} | ||
|
||
async function read(inpf, outf, fmt) { | ||
console.log(`* Dumping NBT file "${inpf}" to "${outf || 'stdout'}" as JSON ${fmt ? 'as ' + fmt : ''}`) | ||
|
||
const buffer = await fs.promises.readFile(inpf) | ||
const { result, type } = await nbt.parse(buffer, fmt) | ||
|
||
if (outf) { | ||
const json = JSON.stringify(result, (k, v) => typeof v === 'bigint' ? v.toString() : v) | ||
fs.writeFileSync(outf, json) | ||
} else { | ||
if (!fmt) console.log(`(as ${type} endian)`) | ||
console.log(nbt.simplify(result)) | ||
} | ||
|
||
return true | ||
} | ||
|
||
const args = process.argv.slice(2) | ||
main(args, args.join(' ')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "nbt-dump", | ||
"version": "1.0.0", | ||
"description": "Read and write Minecraft Named Binary Tag files in big, little and little-varint encoding", | ||
"main": "index.js", | ||
"bin": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/extremeheat/nbt-dump.git" | ||
}, | ||
"keywords": [ | ||
"minecraft", | ||
"nbt", | ||
"named", | ||
"binary", | ||
"tag", | ||
"cli", | ||
"bedrock" | ||
], | ||
"author": "extremeheat", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/extremeheat/nbt-dump/issues" | ||
}, | ||
"homepage": "https://github.com/extremeheat/nbt-dump#readme", | ||
"dependencies": { | ||
"prismarine-nbt": "github:extremeheat/prismarine-nbt#le", | ||
"protodef": "github:extremeheat/node-protodef#new" | ||
} | ||
} |