Skip to content

Instantly share code, notes, and snippets.

@martian17
Last active February 5, 2023 10:53
Show Gist options
  • Save martian17/90771e8502fbd2b14247d8860e42fef4 to your computer and use it in GitHub Desktop.
Save martian17/90771e8502fbd2b14247d8860e42fef4 to your computer and use it in GitHub Desktop.
refactored
const TAG_End = 0;
const TAG_Byte = 1;
const TAG_Short = 2;
const TAG_Int = 3;
const TAG_Long = 4;
const TAG_Float = 5;
const TAG_Double = 6;
const TAG_Byte_Array = 7;
const TAG_String = 8;
const TAG_List = 9;
const TAG_Compound = 10;
const TAG_Int_Array = 11;
const TAG_Long_Array = 12;
const processNBT = [];
processNBT[TAG_End] = (u8,i)=>{
throw new Error("unexpected TAG_END");
};
processNBT[TAG_Byte] = (u8,i)=>{
return [u8[i],i+1];
};
processNBT[TAG_Short] = (u8,i)=>{
const val = (u8[i++]<<8)|u8[i++];
return [val,i];
};
processNBT[TAG_Int] = (u8,i)=>{
const val = (u8[i++]<<24)|(u8[i++]<<16)|(u8[i++]<<8)|u8[i++];
return [val,i];
};
processNBT[TAG_Long] = (u8,i)=>{
const v1 = (u8[i++]<<24)|(u8[i++]<<16)|(u8[i++]<<8)|u8[i++];
const v2 = (u8[i++]<<24)|(u8[i++]<<16)|(u8[i++]<<8)|u8[i++];
const val = BigInt(v1)*4294967296n+BigInt(v2);
return [val,i];
};
processNBT[TAG_Float] = (u8,i)=>{
const val = bytesToFloat(u8[i++],u8[i++],u8[i++],u8[i++]);
return [val,i];
};
processNBT[TAG_Double] = (u8,i)=>{
const val = bytesToDouble(u8[i++],u8[i++],u8[i++],u8[i++],
u8[i++],u8[i++],u8[i++],u8[i++]);
return [val,i];
};
processNBT[TAG_Byte_Array] = (u8,i)=>{
const len = (u8[i++]<<24)|(u8[i++]<<16)|(u8[i++]<<8)|u8[i++];
const val = new Int8Array(len);
for(let j = 0; j < len; j++){
val[j] = u8[i++];
}
return [val,i];
};
processNBT[TAG_String] = (u8,i)=>{
const len = (u8[i++]<<8)|u8[i++];
const val = decodeJavaUTF8(u8,i,len);
i += len;
return [val,i];
};
processNBT[TAG_List] = (u8,i)=>{
const typeid = u8[i++];
const len = (u8[i++]<<24)|(u8[i++]<<16)|(u8[i++]<<8)|u8[i++];
const val = [];
for(let j = 0; j < len; j++){
let v;
[v,i] = processNBT[typeid](u8,i);
val.push(v);
}
return [val,i];
};
processNBT[TAG_Compound] = (u8,i)=>{
const val = Object.create(null);
while(i < u8.length){
const typeid = u8[i++];
if(typeid === 0/*TAG_END*/)break;
let name,v;
[name,i] = processNBT[8/*TAG_STRING*/](u8,i);
[v,i] = processNBT[typeid](u8,i);
val[name] = v;
}
return [val,i];
};
processNBT[TAG_Int_Array] = (u8,i)=>{
const len = (u8[i++]<<24)|(u8[i++]<<16)|(u8[i++]<<8)|u8[i++];
const val = new Int32Array(len);
for(let j = 0; j < len; j++){
val[j] = (u8[i++]<<24)|(u8[i++]<<16)|(u8[i++]<<8)|u8[i++];
}
return [val,i];
};
processNBT[TAG_Long_Array] = (u8,i)=>{
const len = (u8[i++]<<24)|(u8[i++]<<16)|(u8[i++]<<8)|u8[i++];
const val = new BigInt64Array(len);
for(let j = 0; j < len; j++){
const v1 = (u8[i++]<<24)|(u8[i++]<<16)|(u8[i++]<<8)|u8[i++];
const v2 = (u8[i++]<<24)|(u8[i++]<<16)|(u8[i++]<<8)|u8[i++];
val[j] = BigInt(v1)*4294967296n+BigInt(v2);
}
return [val,i];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment