const {Worker, isMainThread, parentPort, workerData} = require("worker_threads"); const readCht = require("./cht"); const { readFileSync } = require("fs"); const ncpu = require("os").availableParallelism(); const loadcheat = (order, serial, title, file) => { try { const chtfile = readFileSync(file, {encoding: "utf-8"}); const cheat = readCht(chtfile, { serial }, order); return { serial, cheat } } catch (e) { //console.log("bad cheat: %s[order=%d]\n%s", title, order, e.stack) return { serial } } } if( isMainThread ) { module.exports = ncpu > 3 ? function(list) { const nPerThread = Math.ceil(list.length / ncpu); const results = new Array(ncpu); let n = 0; return new Promise( ok => { const workers = new Array(ncpu).fill(0).map( (_,i) => { const sublist = list.slice(i * nPerThread, (i+1) * nPerThread); const worker = new Worker(__filename, { workerData: sublist }); worker.once("message", msg => { results[i] = msg; worker.unref(); if( ++n == ncpu ) ok(results.flat()); }); } ); }); } : list => Promise.resolve( list.map( game => { const [order, serial, title] = game; const file = `./gba/${order}.u8`; return loadcheat(order, serial, title ?? order, file); } ) ); } else { const list = workerData; /*Promise.all( list.map( game => { const [order, serial, title] = game; const file = `./gba/${order}.u8`; return loadcheat(order, serial, title ?? order, file); } ) ).then( res => parentPort.postMessage(res) );*/ parentPort.postMessage( list.map( game => { const [order, serial, title] = game; const file = `./gba/${order}.u8`; return loadcheat(order, serial, title ?? order, file); }) ); }