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

Tiled Editor - Decoding Base 64 data does not seem to work

我正在使用 Tiled 1.2.x。导出的地图是 40x40 的图块,以 Base 64 编码数据表示。

这是数据(单行数据抱歉):

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==

目标是在 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

有人对发生这种情况的原因有什么建议吗?谢谢!

经过一番研究,我找到了解决办法。我正确地解码和解压缩了数据,但我错误地读取了数据。

根据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;
}