UNPKG

@thestarweb/trove-lang-tool

Version:

Just a tool for language files of trove.

61 lines (60 loc) 2.03 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.write = exports.read = void 0; var buff_stream_1 = __importDefault(require("@thestarweb/buff-stream")); var STRT_ARR = [0x3E, 0xAE]; var END_ARR = [0xBE, 0x03, 0x08, 0x00, 0x08, 0x1E]; var INTER_START = 0x08; var LOCA_START = 0x18; function read(data) { var fd = new buff_stream_1.default(data); if (fd.readChar() != STRT_ARR[0] || fd.readChar() != STRT_ARR[1]) { throw new Error("无效文件头"); } var len = fd.readVarUInt(); //词条数量 fd.readChar(); //一般是00,暂时不懂啥意思 var list = []; var read_count = 0; while (read_count < len) { fd.readVarUInt(); //读取国际化部分 if (fd.readChar() != INTER_START) { throw new Error("无效国际化"); } var str_int = fd.readStr(); //读取本地化部分 if (fd.readChar() != LOCA_START) { throw new Error("无效本地化"); } var str_loca = fd.readStr(); if (fd.check(END_ARR) != END_ARR.length) { throw new Error("无效词条结尾"); } var item = { key: str_int, value: str_loca }; list.push(item); read_count++; } return list; } exports.read = read; function write(data) { var fd = new buff_stream_1.default(); fd.writeArr(STRT_ARR); fd.writeVarUInt(data.length); fd.writeUInt8(0); data.forEach(function (d, i) { fd.writeVarUInt((i << 4) | 4); fd.writeUInt8(INTER_START); fd.writeStr(d.key); fd.writeUInt8(LOCA_START); fd.writeStr(d.value); fd.writeArr(END_ARR); }); fd.writeArr([0x08, 0x1E]); return fd.getData(); } exports.write = write; exports.default = { read: read, write: write };