Created
May 23, 2015 15:48
-
-
Save Joeywp/63d8d4681dbee08755c9 to your computer and use it in GitHub Desktop.
A NBT reader for JavaScript making use of jDataViewer to read binary files. Originally made for use in an online schematic viewer that was cancelled
This file contains hidden or 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
| var NBT = function(data) | |
| { | |
| this.data = data; | |
| this.viewer = new jDataView(data); | |
| this.tags = { | |
| TAG_END: 0, | |
| TAG_BYTE: 1, | |
| TAG_SHORT: 2, | |
| TAG_INT: 3, | |
| TAG_LONG: 4, | |
| TAG_FLOAT: 5, | |
| TAG_DOUBLE: 6, | |
| TAG_BYTE_ARRAY: 7, | |
| TAG_STRING: 8, | |
| TAG_LIST: 9, | |
| TAG_COMPOUND: 10, | |
| TAG_INT_ARRAY: 11 | |
| }; | |
| this.read = function() | |
| { | |
| var data = {}; | |
| data.values = []; | |
| data.datasize = this.data.length; | |
| try | |
| { | |
| this.traverse(data); | |
| //document.write("<pre>" + JSON.stringify(data, undefined, 2) + "</pre>"); | |
| } catch (e) | |
| { | |
| console.log(e); | |
| return null; | |
| } | |
| return data.values; | |
| }; | |
| this.traverse = function(parent, sentType) | |
| { | |
| var data = {}; | |
| if(sentType === undefined) | |
| data.type = this.viewer.getUint8(); | |
| else | |
| data.type = sentType; | |
| var readName = (sentType === undefined) ? true : false; | |
| switch(data.type) | |
| { | |
| case this.tags.TAG_END: | |
| break; | |
| case this.tags.TAG_BYTE: | |
| if(readName) | |
| data.name = this._getString(); | |
| data.values = this.viewer.getUint8(); | |
| break; | |
| case this.tags.TAG_SHORT: | |
| if(readName) | |
| data.name = this._getString(); | |
| data.values = this.viewer.getUint16(); | |
| break; | |
| case this.tags.TAG_INT: | |
| if(readName) | |
| data.name = this._getString(); | |
| data.values = this.viewer.getUint32(); | |
| break; | |
| case this.tags.TAG_LONG: | |
| if(readName) | |
| data.name = this._getString(); | |
| data.values = this.viewer.getUint64(); | |
| break; | |
| case this.tags.TAG_FLOAT: | |
| if(readName) | |
| data.name = this._getString(); | |
| data.values = this.viewer.getFloat32(); | |
| break; | |
| case this.tags.TAG_DOUBLE: | |
| if(readName) | |
| data.name = this._getString(); | |
| data.values = this.viewer.getFloat64(); | |
| break; | |
| case this.tags.TAG_BYTE_ARRAY: | |
| if(readName) | |
| data.name = this._getString(); | |
| data.length = this.viewer.getUint32(); | |
| data.values = []; | |
| for(var i = 0; i < data.length; i++) | |
| { | |
| data.values.push(this.viewer.getUint8()); | |
| } | |
| break; | |
| case this.tags.TAG_STRING: | |
| if(readName) | |
| data.name = this._getString(); | |
| data.values = this._getString(); | |
| break; | |
| case this.tags.TAG_LIST: | |
| if(readName) | |
| data.name = this._getString(); | |
| data.listtype = this.viewer.getUint8(); | |
| data.length = this.viewer.getInt32(); | |
| data.values = []; | |
| for(var i = 0; i < data.length; i++) | |
| { | |
| if(data.listtype == this.tags.TAG_COMPOUND) | |
| { | |
| var type = null; | |
| while(type != this.tags.TAG_END) | |
| { | |
| type = this.traverse(data); | |
| } | |
| } else | |
| { | |
| this.traverse(data, data.listtype); | |
| } | |
| } | |
| break; | |
| case this.tags.TAG_COMPOUND: | |
| if(readName) | |
| data.name = this._getString(); | |
| data.values = []; | |
| var type = null; | |
| while(type != this.tags.TAG_END) | |
| { | |
| type = this.traverse(data); | |
| } | |
| break; | |
| case this.tags.TAG_INT_ARRAY: | |
| //console.log("TAG_INT_ARRAY"); | |
| if(readName) | |
| data.name = this._getString(); | |
| data.length = this.viewer.getUint32(); | |
| data.values = []; | |
| for(var i = 0; i < data.length; i++) | |
| { | |
| data.values.push(this.viewer.getUint32()); | |
| } | |
| //throw "Tagtype not implemented yet"; | |
| break; | |
| default: | |
| throw "Unknown datatype: " + data.type; | |
| break; | |
| } | |
| if(data.type != this.tags.TAG_END) | |
| parent.values.push(data); | |
| return data.type; | |
| }; | |
| this._getString = function() | |
| { | |
| var length = this.viewer.getUint16(); | |
| return this.viewer.getString(length); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment