Skip to content

Commit

Permalink
Merge pull request libnoname#1708 from Rintim/Dev-Enhancement-IHatePr…
Browse files Browse the repository at this point in the history
…oceed2

拆了`proceed2`
  • Loading branch information
nonameShijian authored Aug 14, 2024
2 parents 9a47fb8 + 4412837 commit 41ec4f4
Show file tree
Hide file tree
Showing 6 changed files with 674 additions and 499 deletions.
2 changes: 2 additions & 0 deletions .mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
node = "20"
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:18
FROM node:20

WORKDIR /app

Expand Down
4 changes: 2 additions & 2 deletions noname/game/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1822,7 +1822,7 @@ export class Game extends GameCompatible {
const promise = Promise.resolve((gnc.is.generator(content) ? gnc.of(content) : content)(lib, game, ui, get, ai, _status)).then(content2 => {
if (content2.name) {
lib.imported[type][content2.name] = content2;
delete content2.name;
// delete content2.name;
}
});
if (typeof _status.importing == "undefined") _status.importing = {};
Expand Down Expand Up @@ -1971,7 +1971,7 @@ export class Game extends GameCompatible {
if (!lib.config.extension_alert) alert(`加载《${name}》扩展的precontent时出现错误。\n该错误本身可能并不影响扩展运行。您可以在“设置→通用→无视扩展报错”中关闭此弹窗。\n${decodeURI(e1.stack)}`);
}

if (content) lib.extensions.push([name, content, config, _status.evaluatingExtension, objectPackage || {}]);
if (content) lib.extensions.push([name, content, config, _status.evaluatingExtension, objectPackage || {}, object.connect]);
} catch (e) {
console.log(e);
}
Expand Down
57 changes: 24 additions & 33 deletions noname/init/import.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
// noinspection ES6PreferShortImport

import { game } from "../game/index.js";
import { lib } from "../library/index.js";
import { lib, Library } from "../library/index.js";

/**
* @param {string} name - 卡牌包名
* @returns {Promise<void>}
*/
export const importCardPack = generateImportFunction("card", (name) => `../../card/${name}.js`);
export const importCardPack = generateImportFunction("card", name => `../../card/${name}.js`);

/**
* @param {string} name - 武将包名
* @returns {Promise<void>}
*/
export const importCharacterPack = generateImportFunction("character", (name) => {
export const importCharacterPack = generateImportFunction("character", name => {
const alreadyModernCharacterPack = lib.config.moderned_chracters || [];

return alreadyModernCharacterPack.includes(name)
? `../../character/${name}/index.js`
: `../../character/${name}.js`;
return alreadyModernCharacterPack.includes(name) ? `../../character/${name}/index.js` : `../../character/${name}.js`;
});

/**
* @param {string} name - 扩展名
* @returns {Promise<void>}
*/
export const importExtension = generateImportFunction(
"extension",
(name) => `../../extension/${name}/extension.js`
);
export const importExtension = generateImportFunction("extension", name => `../../extension/${name}/extension.js`);

/**
* @param {string} name - 模式名
* @returns {Promise<void>}
*/
export const importMode = generateImportFunction("mode", (name) => `../../mode/${name}.js`);
export const importMode = generateImportFunction("mode", name => `../../mode/${name}.js`);

/**
* 生成导入
Expand All @@ -42,12 +39,8 @@ export const importMode = generateImportFunction("mode", (name) => `../../mode/$
* @returns {(name: string) => Promise<void>}
*/
function generateImportFunction(type, pathParser) {
return async (name) => {
if (
type == "extension" &&
!game.hasExtension(name) &&
!lib.config.all.stockextension.includes(name)
) {
return async name => {
if (type == "extension" && !game.hasExtension(name) && !lib.config.all.stockextension.includes(name)) {
// @ts-ignore
await game.import(type, await createEmptyExtension(name));
return;
Expand All @@ -62,7 +55,7 @@ function generateImportFunction(type, pathParser) {
let path = pathParser(name);
// 通过浏览器自带的script标签导入可直接获取报错信息,且不会影响JS运行
// 此时代码内容也将缓存在浏览器中,故再次import后将不会重新执行代码内容(测试下来如此)
const [status, script] = await new Promise((resolve) => {
const [status, script] = await new Promise(resolve => {
const createScript = () => {
const script = document.createElement("script");
script.type = "module";
Expand All @@ -71,22 +64,23 @@ function generateImportFunction(type, pathParser) {
return script;
};
let script = createScript();
script.onerror = (e) => {
script.onerror = e => {
if (path.endsWith(".js") && window.isSecureContext) {
path = path.slice(0, -3) + ".ts";
script.remove();
let ts = createScript();
ts.onerror = (e2) => {
if (
lib.path.basename(path) === "extension.js" &&
lib.path.dirname(path).endsWith("/extension")
) {
ts.onerror = e2 => {
if (lib.path.basename(path) === "extension.js" && lib.path.dirname(path).endsWith("/extension")) {
console.error(`扩展《${name}》加载失败`, e, e2);
let remove = confirm(
`扩展《${name}》加载失败,是否移除此扩展?此操作不会移除目录下的文件。`
);
let remove = confirm(`扩展《${name}》加载失败,是否移除此扩展?此操作不会移除目录下的文件。`);
if (remove) {
lib.config.extensions.remove(name);
if (lib.config[`@Experimental.extension.${name}.character`]) {
game.saveConfig(`@Experimental.extension.${name}.character`);
}
if (lib.config[`@Experimental.extension.${name}.card`]) {
game.saveConfig(`@Experimental.extension.${name}.card`);
}
game.saveConfig("extensions", lib.config.extensions);
}
}
Expand All @@ -102,16 +96,13 @@ function generateImportFunction(type, pathParser) {
script.remove();
if (status === "error") {
if (type === "character") {
console.warn(
"如果您在扩展中使用了game.import创建武将包,可将以下代码删除: lib.config.all.characters.push('武将包名');"
);
console.warn("如果您在扩展中使用了game.import创建武将包,可将以下代码删除: lib.config.all.characters.push('武将包名');");
}
return;
}
const modeContent = await import(path);
if (!modeContent.type) return;
if (modeContent.type !== type)
throw new Error(`Loaded Content doesn't conform to "${type}" but "${modeContent.type}".`);
if (modeContent.type !== type) throw new Error(`Loaded Content doesn't conform to "${type}" but "${modeContent.type}".`);
// @ts-ignore
await game.import(type, modeContent.default);
};
Expand All @@ -121,7 +112,7 @@ async function createEmptyExtension(name) {
const extensionInfo = await lib.init.promises
.json(`${lib.assetURL}extension/${name}/info.json`) //await import(`../../extension/${name}/info.json`,{assert:{type:'json'}})
.then(
(info) => info,
info => info,
() => {
return {
name: name,
Expand Down
Loading

0 comments on commit 41ec4f4

Please sign in to comment.