From a095a748a30503ded9dd4bbd5b01acc6b6efb6e3 Mon Sep 17 00:00:00 2001 From: a92126 <182859762@qq.com> Date: Wed, 16 Oct 2024 11:28:43 +0800 Subject: [PATCH] =?UTF-8?q?1=20=E6=98=A8=E5=A4=A9=E5=81=9Ano=5Fiter?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E5=80=99=EF=BC=8C=E6=BC=8F=E6=8E=89=E4=BA=86?= =?UTF-8?q?forEach=202=20addString=E5=8F=AF=E4=BB=A5=E5=A4=8D=E7=94=A8?= =?UTF-8?q?=E6=97=A7=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=B2=A1=E9=94=99=EF=BC=8C?= =?UTF-8?q?=E4=BD=86=E6=98=AF=E4=B9=9F=E6=9C=89=E5=B1=80=E9=99=90=EF=BC=8C?= =?UTF-8?q?=E5=A6=82=E6=9E=9C=E5=85=88=E6=94=BE=E5=85=A5'x'=E5=86=8D?= =?UTF-8?q?=E6=94=BE=E5=85=A5'xx'=EF=BC=8C=E8=99=BD=E7=84=B6'x'=E6=98=AF'x?= =?UTF-8?q?x'=E7=9A=84=E4=B8=80=E9=83=A8=E5=88=86=EF=BC=8C=E5=8D=B4?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E5=85=85=E5=88=86=E5=88=A9=E7=94=A8=E7=A9=BA?= =?UTF-8?q?=E9=97=B4=E3=80=82=E6=89=80=E4=BB=A5=E4=BF=AE=E6=94=B9=E4=BA=86?= =?UTF-8?q?addString=E7=9A=84=E6=96=B9=E6=A1=88=EF=BC=8C=E5=85=88=E6=94=B6?= =?UTF-8?q?=E9=9B=86=E5=AD=97=E7=AC=A6=E4=B8=B2=EF=BC=8C=E5=9C=A8=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E6=94=B6=E9=9B=86=E5=AE=8C=E6=88=90=E5=90=8E?= =?UTF-8?q?=E6=8C=89=E7=85=A7=E9=95=BF=E5=BA=A6=E8=BF=9B=E8=A1=8C=E9=99=8D?= =?UTF-8?q?=E5=BA=8F=E6=8E=92=E5=BA=8F=EF=BC=8C=E7=84=B6=E5=90=8E=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E7=BB=9F=E4=B8=80addString=EF=BC=8C=E7=84=B6=E5=90=8E?= =?UTF-8?q?=E8=AE=BE=E7=BD=AEcache=EF=BC=8C=E8=BF=99=E6=A0=B7=E5=90=8E?= =?UTF-8?q?=E7=BB=AD=E4=BD=BF=E7=94=A8=E5=B0=B1=E5=8F=AF=E4=BB=A5=E5=A4=8D?= =?UTF-8?q?=E7=94=A8=E7=A9=BA=E9=97=B4=E4=BA=86=E3=80=82=E4=B9=9F=E7=A1=AE?= =?UTF-8?q?=E5=AE=9E=E8=AE=A9acll=E6=96=87=E4=BB=B6=E5=A4=A7=E5=B0=8F?= =?UTF-8?q?=E9=99=8D=E4=BD=8E=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/cheat-builder/cht.js | 107 ++++++++++++++++++++++++------------- 1 file changed, 69 insertions(+), 38 deletions(-) diff --git a/tools/cheat-builder/cht.js b/tools/cheat-builder/cht.js index c27598c..c408912 100644 --- a/tools/cheat-builder/cht.js +++ b/tools/cheat-builder/cht.js @@ -19,7 +19,6 @@ const a = "a".charCodeAt(); const f = "f".charCodeAt(); const conv = new TextEncoder(); const noiter = process.env.OAFCHT_NO_ITER==='1'; -const noreuse = process.env.OAFCHT_NO_REUSE==='1'; /** * 这个函数做了一些数据复用处理,能省下来一点文件容量 @@ -27,9 +26,6 @@ const noreuse = process.env.OAFCHT_NO_REUSE==='1'; * 具体来说,字符串表里面,如果有两个字符串'1'和'11' * 那么在字符串表里面,只会有一个索引指向'11',而'1'会 * 复用'11'的数据,方案是索引的位置放到'11'的最后一个1前面 - * - * 当然,这样做有点吃cpu,如果不希望使用这个方式,可以配置 - * 环境变量 OAFCHT_NO_REUSE=1 将会产生一个更快的过程 */ const findIndex = (t, tr, tlen) => { const rlen = tr.length, limit = tlen - rlen; @@ -51,7 +47,7 @@ const findIndex = (t, tr, tlen) => { const read = (cht, info, order) => { let state = WAIT_LOCK, token = [], line = 1; - let locks = [], currlock = null + let locks = [], currlock = null; const retval = []; const token_str = () => { const r = token.join(""); return (token = [], r); } @@ -313,12 +309,23 @@ const pack = (entlist, strlist, idxlist, cmdlist, xvalue) => { view.setUint16(0, entlist.length, true); // entry data - entlist.forEach((entry, index) => { - let offset = 2 + index * entrysize; - view.setUint32(offset, entry[0], true); - view.setUint32(offset + 4, entry[1] + (entry[0] > 0xffff ? cmdbase : idxbase), true); - view.setUint32(offset + 8, entry[2], true); - }); + if( noiter === false ){ + entlist.forEach((entry, index) => { + let offset = 2 + index * entrysize; + view.setUint32(offset, entry[0], true); + view.setUint32(offset + 4, entry[1] + (entry[0] > 0xffff ? cmdbase : idxbase), true); + view.setUint32(offset + 8, entry[2], true); + }); + } + else { + for( let i=0; i < entlist.length; ++i ){ + const entry = entlist[i]; + let offset = 2 + i * entrysize; + view.setUint32(offset, entry[0], true); + view.setUint32(offset + 4, entry[1] + (entry[0] > 0xffff ? cmdbase : idxbase), true); + view.setUint32(offset + 8, entry[2], true); + } + } // string table let strings = new Uint8Array(result, strbase, strlist.length); @@ -337,35 +344,62 @@ const pack = (entlist, strlist, idxlist, cmdlist, xvalue) => { const lade = (list, info, order) => { let enttable = []; // 保存hole/key数据 + let rootstr = [], names = [], dummy = []; const makeID = (a, b) => a | b << 16; const makeEntry = (id, str) => ({ id, data: str }); // collect - const collectEntry = (id, keys, list) => { + const collectEntry = noiter === false ? (id, keys, list, colname) => { keys.forEach(({ name, cmd }, index) => { - list.push(name); + list[index] = name; + if( colname ) names.push( name ); enttable.push(makeEntry(makeID(id, index + 1), cmd)); }); + } : (id, keys, list, colname) => { + for( let i=0; i < keys.length; ++i ){ + const { name, cmd } = keys[i]; + list[i] = name; + if( colname ) names.push( name ); + enttable.push(makeEntry(makeID(id, i + 1), cmd)); + } + }; + + if( noiter === false ){ + list.forEach((hole, index) => { + rootstr.push(hole.name); + names.push(hole.name); + + let holestr = new Array(hole.keys.length); + collectEntry(index + 1, hole.keys, holestr, hole.keys.length > 1); + + let entry = makeEntry(makeID(index + 1, 0), holestr); + enttable.push(entry); + }); + } + else { + for( let i=0; i < list.length; ++i ){ + const hole = list[i]; + rootstr.push(hole.name); + names.push(hole.name); + + let holestr = new Array(hole.keys.length); + collectEntry(i + 1, hole.keys, holestr, hole.keys.length > 1); + + let entry = makeEntry(makeID(i + 1, 0), holestr); + enttable.push(entry); + } } - - let rootstr = []; - list.forEach((hole, index) => { - rootstr.push(hole.name); - - let holestr = []; - collectEntry(index + 1, hole.keys, holestr); - - let entry = makeEntry(makeID(index + 1, 0), holestr); - enttable.push(entry); - }); enttable.push(makeEntry(makeID(0, 0), rootstr)); enttable.sort((a, b) => a.id - b.id); + names.sort( (a,b) => b.length - a.length ); let strtable = new Uint8Array(1024); // 保存字符串常量 let idxtable = new Uint16Array(128); // 从字符串常量索引出来的字符串列表 let cmdtable = new Uint32Array(4096); // 保存选项对应指令 + + const strOffsetCache = {}; const size = {"str": 0, "cmd": 0, "idx": 0}; const addString = list => { const ut_pushtr = tr => { @@ -383,21 +417,17 @@ const lade = (list, info, order) => { size.str = len; return off; }; - const ut_addtr = noreuse ? (() => { - const cache = {}; - return tr => {// 不要直接ut_pushtr,因为有些秘籍会撑爆字符串表 - if (cache[tr] != undefined) return cache[tr]; - else { - const code = conv.encode(tr); - const off = ut_pushtr(code); - cache[tr] = off; - return off; - } + const ut_addtr = tr => { + if (strOffsetCache[tr] != undefined) { + return strOffsetCache[tr]; + } + else { + const code = conv.encode(tr); + let off = findIndex(strtable, code, size.str); + off = off < 0 ? ut_pushtr(code) : off; + strOffsetCache[tr] = off; + return off; } - })() : tr => { - const code = conv.encode(tr); - const off = findIndex(strtable, code, size.str); - return off < 0 ? ut_pushtr(code) : off; }; // 避免使用迭代器方法,减少CPU(已走过performance对比流程) // ---------------原来的代码在这里------------------ @@ -461,6 +491,7 @@ const lade = (list, info, order) => { // 避免使用迭代器方法,减少CPU(已走过performance对比流程) // ---------------原来的代码在这里------------------ let entbytelist; + addString(names); // if( noiter === false ){ entbytelist = enttable.map(entry => { let { id, data } = entry;