pref: auto gen import function.

This commit is contained in:
Rintim 2023-12-30 23:58:05 +08:00
parent 519abfacbd
commit be44296a87
No known key found for this signature in database
GPG Key ID: BE9E1EA615BACFCF
2 changed files with 48 additions and 57 deletions

47
noname/init/import.js Normal file
View File

@ -0,0 +1,47 @@
import { Game as game } from '../game/index.js';
/**
* @param {string} name - 卡牌包名
*/
export const importCardPack = generateImportFunction('card', (name) => `../../card/${name}.js`)
/**
* @param {string} name - 武将包名
*/
export const importCharacterPack = generateImportFunction('character', (name) => `../../character/${name}.js`)
/**
* @param {string} name - 扩展名
*/
export const importExtension = generateImportFunction('extension', (name) => `../../extension/${name}/extension.js`)
/**
* @param {string} name - 模式名
*/
export const importMode = generateImportFunction('mode', (name) => `../../mode/${name}.js`)
/**
* 生成导入
*
* @param {string} type
* @param {(name: string) => string} pathParser
* @returns {(name: string) => Promise<void>}
*/
function generateImportFunction(type, pathParser) {
return async function (name) {
try {
const modeContent = await import(pathParser(name));
if (!modeContent.type) return;
if (modeContent.type !== type) throw new Error(`Loaded Content doesnt conform to "${type}"`);
// TODO: 设想的新导入名称,用`export function entry() {...}`将内容暴露出去
if ('entry' in modeContent) await game.import(type, (lib, game, ui, get, ai, _status) => {
// TODO: 第一个参数打算提供一些信息,用于直接判断;后面依然提供六个全局变量,用于一些其他方式制作的扩展
return modeContent.entry({}, {lib, game, ui, get, ai, _status})
})
// 不好说要不要保留
else await game.import(type, modeContent.default);
} catch (e) {
console.error(e);
}
}
}

View File

@ -10,6 +10,7 @@ import { userAgent } from '../util/index.js';
import * as config from '../util/config.js';
import { gnc } from '../gnc/index.js';
import { importCardPack, importCharacterPack, importExtension, importMode } from './import.js';
import { onload } from './onload.js';
// 无名杀,启动!
@ -562,63 +563,6 @@ export async function boot() {
await onload();
}
/**
* @param {string} name - 卡牌包名
*/
async function importCardPack(name) {
try {
const cardPackContent = await import('../../card/' + name + '.js');
if (!cardPackContent.type) return;
if (cardPackContent.type !== 'card') throw new Error('Loaded Content is not a CardPack');
await game.import('card', cardPackContent.default);
} catch (e) {
console.error(e);
}
}
/**
* @param {string} name - 武将包名
*/
async function importCharacterPack(name) {
try {
const characterPackContent = await import('../../character/' + name + '.js');
if (!characterPackContent.type) return;
if (characterPackContent.type !== 'character') throw new Error('Loaded Content is not a CharacterPack');
await game.import('character', characterPackContent.default);
} catch (e) {
console.error(e);
}
}
/**
* @param {string} name - 扩展名
*/
async function importExtension(name) {
try {
const extensionContent = await import('../../extension/' + name + '/extension.js');
if (!extensionContent.type) return;
if (extensionContent.type !== 'extension') throw new Error('Loaded Content is not a Noname Extension');
await game.import('extension', extensionContent.default);
} catch (e) {
console.error(e);
game.removeExtension(name);
}
}
/**
* @param {string} name - 模式名
*/
async function importMode(name) {
try {
const modeContent = await import('../../mode/' + name + '.js');
if (!modeContent.type) return;
if (modeContent.type !== 'mode') throw new Error('Loaded Content is not a Mode');
await game.import('mode', modeContent.default);
} catch (e) {
console.error(e);
}
}
function initSheet(libConfig) {
if (libConfig.player_style && libConfig.player_style != 'default' && libConfig.player_style != 'custom') {
var str = '';