平铺的编辑器-解码Base 64数据似乎不起作用

时间:2019-04-26 22:07:41

标签: node.js base64 tiled

我正在使用Tiled1.2.x。导出的地图是以Base 64编码数据表示的40x40瓦片。

以下是数据(很抱歉单行数据):

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==

目标是在 Node.js 中将Base 64编码的字符串转换为2D Uint8Array。该数组将为我的地图存储每个图块的唯一ID。

下面是我编写的一些代码,试图将编码后的数据转换为更易于理解的内容。

let data = null;
let rawData = layer.data;

// ==================================
// == If applicable, decode Base64 ==
// ==================================
if(layer.encoding === 'base64') {
    console.log('Decoding data!');
    data = Buffer.from(rawData, 'base64');
}

// ============================================
// == If applicable, extract compressed data ==
// ============================================
if(layer.compression === 'gzip') {
    console.log('Extracting data!');
    zlib.gunzipSync(data);
}

// =============================================================
// == Use raw data if no Base64 encoding or GZIP compression. ==
// =============================================================
if(data === null) {
    data = rawData;
}

console.log(data.toString('ascii'));

但是,输出继续为我提供垃圾数据,我不确定是什么导致了此问题。也许我不太了解解码过程。

G!"""""#1222223123G!"""""""""#!"""""""""#13!"""""""""#12222222223!""""""""#1222222223%&&'566756675667122222222222222223

有人为什么会这样吗?谢谢!

1 个答案:

答案 0 :(得分:0)

经过研究,我找到了解决方案。我正确地解码和解压缩了数据,但是我错误地读取了数据。

根据TMX Map Format docs,解码后的数据以小尾数顺序存储为无符号32位整数

Node.js具有读取此确切数据的功能Buffer.readUint32LE(offset)。通过循环,我可以遍历缓冲区并使用递增的offset读取每个整数。对于每次迭代,我将整数添加到单独的数组中。

我的解决方案:

/**
 * @author         AdamInTheOculus
 * @purpose        Returns an array of Tiled layer data from a given layer.
 * @param  layer   Layer object. Contains metadata and map data.
 **/
function getTiles(layer) {

    let data = null;
    let rawData = layer.data;

    // ==================================
    // == If applicable, decode Base64 ==
    // ==================================
    if(layer.encoding === 'base64') {
        data = Buffer.from(rawData, 'base64');
    }

    // ============================================
    // == If applicable, extract compressed data ==
    // ============================================
    if(layer.compression === 'gzip') {
        data = zlib.gunzipSync(data);
    }

    // =============================================================
    // == Use raw data if no Base64 encoding or GZIP compression. ==
    // =============================================================
    if(data === null) {
        data = rawData;
        return new Uint8Array(data);
    }

    // ====================================
    // == Read buffer data every 4 bytes ==
    // ====================================

    // Each 32-bit integer is placed in an 8-bit integer array.
    // There will never be a tile ID greater than 255, so only 1 byte is required.
    let array = new Uint8Array(layer.width * layer.height);
    for(let i=0, index=0; i<data.length; i += 4, index++) {
        array[index] = data.readUInt32LE(i);
        index++;
    }

    return array;
}