47 lines
2.3 KiB
JavaScript
47 lines
2.3 KiB
JavaScript
const fs = require('node:fs');
|
|
const vm = require('node:vm');
|
|
const ts = require('typescript');
|
|
|
|
function loadMembers(file, names, globals = {}) {
|
|
const source = ts.createSourceFile(file, fs.readFileSync(file, 'utf8'), ts.ScriptTarget.Latest, true);
|
|
const declaration = source.statements.find(ts.isClassDeclaration);
|
|
const available = declaration ? declaration.members : source.statements.filter(ts.isVariableStatement)
|
|
.flatMap(statement => statement.declarationList.declarations)
|
|
.find(node => node.name.getText(source) === 'GameTool').initializer.properties;
|
|
const members = names.map(name => {
|
|
const member = available.find(node => node.name && node.name.getText(source) === name);
|
|
if (!member) throw new Error(`${file}: missing ${name}`);
|
|
return member.getText(source);
|
|
});
|
|
return vm.runInNewContext(ts.transpileModule(`class Controller { ${members.join('\n')} }\nController;`, {
|
|
compilerOptions: { target: ts.ScriptTarget.ES2017 },
|
|
}).outputText, globals);
|
|
}
|
|
|
|
const Config = loadMembers('assets/Script/module/Config/GameConfig.ts', [
|
|
'isNewPlayerB', 'getLevelConfigPath', 'getPropUnlockLevel', 'getFeatureUnlockLevel',
|
|
'isWinStreakUnlocked', 'shouldShowWinStreakGuide', 'canSettleCareer', 'defaultNewLevel', 'newPlayerNewLevel',
|
|
'defaultNewGuide', 'NEW_LEVEL', 'NEW_GUIDE',
|
|
]);
|
|
function withNewPlayerConfig(config) {
|
|
for (const name of Object.getOwnPropertyNames(Config)) {
|
|
if (['length', 'name', 'prototype'].includes(name)) continue;
|
|
Object.defineProperty(config, name, Object.getOwnPropertyDescriptor(Config, name));
|
|
}
|
|
return config;
|
|
}
|
|
|
|
const ServerUtils = loadMembers('server/laf-cloud/functions/Utils.ts', ['isNewPlayerB']);
|
|
function loadModule(file, deps = {}, globals = {}) {
|
|
const exports = {};
|
|
const code = ts.transpileModule(fs.readFileSync(file, 'utf8'), {
|
|
compilerOptions: { target: ts.ScriptTarget.ES2020, module: ts.ModuleKind.CommonJS },
|
|
}).outputText;
|
|
vm.runInNewContext(code, { exports, require: name => {
|
|
if (!(name in deps)) throw new Error(`${file}: missing dependency ${name}`);
|
|
return deps[name];
|
|
}, Date, console, ...globals });
|
|
return exports;
|
|
}
|
|
module.exports = { loadMembers, withNewPlayerConfig, ServerUtils, loadModule };
|