不处理注释,将gameinfo分隔的功能放到parser中,提升了些许性能

This commit is contained in:
a92126 2024-10-09 14:50:12 +08:00
parent 16f369b956
commit 6562fe4fab
3 changed files with 30 additions and 34 deletions

View File

@ -1,5 +1,3 @@
const readCht = require("./cht");
const fs = require("fs");
const { readFile, writeFile } = require("fs/promises");
const transform = require("./worker");

View File

@ -34,15 +34,30 @@ const findIndex = (t, tr, tlen) => {
}
const read = (cht, info, order) => {
cht = cht.replace(/[-]{4,}/g, "");
const size = cht.length;
let state = WAIT_LOCK, token = [], line = 1;
let locks = [], currlock = null;
const token_str = () => { let r = String.fromCharCode.apply(String, token); return (token.length = 0, r); }
const merge_str = String.fromCharCode.bind(String);
const token_str = () => { const r = merge_str.apply(null, token); return (token.length = 0, r); }
const error = msg => { throw new SyntaxError(`${msg} at line: ${line} in lock ${currlock?.name}`) }
const next_cht_begin = pos => {
for(let i=pos; i < size; ++i ){
const ch = cht.charAt(i);
if (ch == "\n") {
++line;
if(cht.charAt(i + 1) == "[")
return i;
}
}
return size;
}
const retval = [];
for (let i = 0; i <= size; ++i) {
const ch = i == size ? "" : cht.charAt(i);
const ch = cht.charAt(i);
switch (ch) {
case "[":
if (state == WAIT_LOCK || state == READ_HOLE) {
@ -57,8 +72,16 @@ const read = (cht, info, order) => {
state = WAIT_HOLE;
currlock.name = token_str();
currlock.holes = [];
if (currlock.name.toLowerCase() == "gameinfo")
i = size; // break;
if (currlock.name.toLowerCase() == "gameinfo"){
retval.push( lade(locks, info, order) );
i = next_cht_begin(i);
if( i < size ){
state = WAIT_LOCK;
token = [];
locks = [];
currlock = null;
}
}
}
else error(`error occur ] on ${STATE[state]}`)
break;
@ -166,7 +189,7 @@ const read = (cht, info, order) => {
break;
}
}
return lade(locks, info, order);
return retval;
}
const assembleCheat = (list, dup, hole) => {

View File

@ -3,29 +3,6 @@ const readCht = require("./cht");
const { readFile, access } = require("fs/promises");
const ncpu = require("os").availableParallelism();
const trimcheat = (dat, info, order) => {
let parts = [], start = 0, done = false;
while (!done) {
let pos = dat.indexOf("[GameInfo]", start);
if (pos > 0) {
pos = dat.indexOf("[", pos + "[GameInfo]".length);
if (pos > 0) {
parts.push(dat.slice(start, pos));
start = pos;
}
else {
parts.push(dat.slice(start, dat.length));
done = true;
}
}
else {
parts.push(dat.slice(start, dat.length));
done = true;
}
}
return parts.map(tr => readCht(tr, info, order));
}
const loadcheat = async (order, serial, title, file) => {
if( false == await access(file).catch( e => false ) ) {
return Promise.resolve();
@ -35,9 +12,7 @@ const loadcheat = async (order, serial, title, file) => {
const chtfile = await readFile(file);
const cheats = chtfile.toString("utf-8");
let cheat = null;
if (cheats.indexOf("[GameInfo]") != cheats.lastIndexOf("[GameInfo]"))
cheat = trimcheat(cheats, { serial }, order);
else cheat = [readCht(cheats, { serial }, order)];
cheat = readCht(cheats, { serial }, order);
return { serial, cheat }
}
catch (e) {