Skip to content

Instantly share code, notes, and snippets.

@TheAlan404
Created January 6, 2021 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheAlan404/2ad41d62ecba5236283c5ad2e50fcb8f to your computer and use it in GitHub Desktop.
Save TheAlan404/2ad41d62ecba5236283c5ad2e50fcb8f to your computer and use it in GitHub Desktop.
const { ProtoDefCompiler } = require('protodef').Compiler
const fs = require("fs");
function simplify (data) {
function transform (value, type) {
if (type === 'compound') {
return Object.keys(value).reduce(function (acc, key) {
acc[key] = simplify(value[key])
return acc
}, {})
}
if (type === 'list') {
return value.value.map(function (v) { return transform(v, value.type) })
}
return value
}
return transform(data.value, data.type)
}
function noteblockKeyMappings(){
let lastKey = "A";
function nextKey(){
let keys = {
A: "B",
B: "C",
C: "D",
D: "E",
E: "F",
F: "G",
G: "A",
};
lastKey = keys[lastKey];
return lastKey;
};
let lastOctave = 0;
function nextOctave(){
lastOctave += 1;
return lastOctave;
};
let mappings = {};
for(let i = 0 ; i <= 87 ; i += 1){
if(i < 35 || i > 59) {
mappings[i] = { compatible: false };
continue;
};
let pitch = ( 59 - 35 ) / 59;
mappings[i] = { compatible: true, pitch: pitch, };
};
return mappings;
};
const SHORT = "li16";
const BYTE = "li8";
const STR = "string";
const INT = "li32";
const NBT_StructureTypes = {
"li16": "native", // short LE;
"li32": "native", // integer LE;
"li8": "native", // byte LE;
"pstring": "native",
"string": [
"pstring",
{
countType: INT,
}
], //
};
const NBT_Protocol = {
...NBT_StructureTypes,
nbs_header: [
"container",
[
{ name: "none", type: SHORT, },
{ name: "version", type: BYTE },
{ name: "vanillaInstruments", type: [
"count",
{
type: BYTE,
countFor: "nbs_noteblocks"
}
]
},
{ name: "lengthTicks", type: SHORT },
{ name: "layers", type: SHORT },
{ name: "name", type: STR },
{ name: "author", type: STR },
{ name: "original_author", type: STR },
{ name: "description", type: STR },
{ name: "tempo", type: SHORT },
{ name: "autoSave", type: BYTE },
{ name: "autoSaveDuration", type: BYTE },
{ name: "timeSignature", type: BYTE },
{ name: "stat_minsSpent", type: INT },
{ name: "stat_leftClicks", type: INT },
{ name: "stat_rightClicks", type: INT },
{ name: "stat_noteblocksAdded", type: INT },
{ name: "stat_noteblocksRemoved", type: INT },
{ name: "imported_name", type: STR },
{ name: "loop", type: BYTE },
{ name: "maxLoop", type: BYTE },
{ name: "loopStart", type: SHORT },
]
],
nbs_noteblocks: [
"array",
{
type: "noteblock",
count: "header.vanillaInstruments",
}
],
noteblock: [
"container",
[
{ name: "jumpsToNextTick", type: SHORT },
{ name: "jumpsToNextLayer", type: SHORT },
{ name: "instrument", type: "noteblock_instrument" },
{ name: "key", type: "noteblock_key" },
{ name: "volume", type: BYTE },
{ name: "panning", type: BYTE },
{ name: "pitch", type: SHORT },
]
],
noteblock_instrument: [
"mapper",
{
type: BYTE,
mappings: {
0: "harp",
1: "bass",
2: "basedrum",
3: "snare",
4: "click",
5: "guitar",
6: "flute",
7: "bell",
8: "chime",
9: "xylophone",
10: "iron_xylophone",
11: "cow_bell",
12: "didgeridoo",
13: "bit",
14: "banjo",
15: "pling",
}
}
],
noteblock_key: [
"mapper",
{
type: BYTE,
mappings: noteblockKeyMappings(),
}
],
nbs: [
"container",
[
{ name: "header", type: "nbs_header" },
{ name: "noteblocks", type: "nbs_noteblocks" },
]
]
};
const compiler = new ProtoDefCompiler();
compiler.addTypesToCompile(NBT_Protocol);
const proto = compiler.compileProtoDefSync();
class Song {
constructor(file){
};
};
function loadSong(filename){
let f = fs.readFileSync(filename);
return proto.parsePacketBuffer("nbs", f);
};
console.log(loadSong("./bob.nbs").data);
@TheAlan404
Copy link
Author

When count is INT:
`\compiler.js:102
throw e
^

ReferenceError: Read error for undefined : li32 is not defined`

other stuff - sort of same

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment