jeudi 9 février 2017

Decompressing Zlib compressed Tiled map data in Nodejs

Vote count: 0

I'm making an online game using node.js as my server, and the game revolves around a Tiled map. On the client, I can unzip Zlib compressed map data easily using the below code, given a variable ZLIB_COMPRESSED_DATA:

var tiles = [], parsedTiles = [];
//decompress the tile base64 zlib compressed gibberish and put into an array, parsedTiles
tiles = new Zlib.Inflate(
    atob(ZLIB_COMPRESSED_DATA)
        .split('')
        .map(function(e){return e.charCodeAt(0)})
).decompress();

tiles.slice.call(tiles);
for(var j = 0; j <= tiles.length; j += 4){
    parsedTiles.push(tiles[j] | tiles[j + 1] << 8 | tiles[j + 2] << 16 | tiles[j + 3] << 24);
}

On the client, I use Zlib.js (the Zlib global) to decompress, and I wish to use the standard zlib module for nodejs (require('zlib'))

However, I cannot seem to get it to work with the zlib module for whatever reason. Below is the broken version of my (attempted) server code:

function tileData(d, callback){
    const buffer = Buffer.from(d, 'base64');
    buffer = buffer.toString().split('').map(e => e.charCodeAt(0));
    zlib.inflate(buffer, (err, bfr) => {
        if(!err){
            let tiles = bfr.toString();
            let parsedTiles = [];
            tiles.slice.call(tiles);
            for(let j = 0; j <= tiles.length; j += 4){
                parsedTiles.push(tiles[j] | tiles[j + 1] << 8 | tiles[j + 2] << 16 | tiles[j + 3] << 24);
            }
            callback(parsedTiles);
        }else{
            console.log(err);
        }
    });
}

If anyone has any idea how to help, I'd appreciate it very much.

asked 27 secs ago

Let's block ads! (Why?)



Decompressing Zlib compressed Tiled map data in Nodejs

Aucun commentaire:

Enregistrer un commentaire