Created
November 25, 2015 06:08
-
-
Save Tewki/4eae194ab71dd16d6932 to your computer and use it in GitHub Desktop.
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
fs = require('fs') | |
TYPES = | |
Int8: | |
SIZE: 1, U: 1 | |
Int16: | |
E: 1, SIZE: 2, U: 1 | |
Int32: | |
E: 1, SIZE: 4, U: 1 | |
Float: | |
E: 1, SIZE: 4 | |
Double: | |
E: 1, SIZE: 8 | |
_TYPES = {} | |
for key, type of TYPES | |
_types = if type.E then [key + 'LE', key + 'BE'] else [key] | |
_types.push('U' + _type) for _type in _types if type.U | |
for k in _types | |
_TYPES[k] = type.SIZE | |
class Reader | |
reader: (func, size) -> -> func.call(@buffer, @offset.read, @offset.read += size) | |
constructor: (@buffer = new Buffer(4096), @offset = {read: 0, write: 0}, @data = {}) -> | |
@['read' + type] = @reader(Buffer.prototype['read' + type], size) for type, size of _TYPES | |
readLEM128: -> | |
if(@readUInt8() == 11) | |
len = @readUInt8() | |
return @buffer.toString('utf8', @offset.read, @offset.read += len) | |
db = fs.readFileSync('collection.db') | |
re = new Reader(db) | |
data = | |
version: re.readUInt32LE() | |
amount: re.readUInt32LE() | |
collections: [] | |
for c in [0 ... data.amount] | |
collection = | |
name: re.readLEM128() | |
count: re.readUInt32LE() | |
maps: [] | |
collection.maps.push(re.readLEM128()) for b in [0 ... collection.count] | |
data.collections.push(collection) | |
console.log(JSON.stringify(data, null, ' ')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment