Merge pull request #938 from nonameShijian/PR-Branch

修复打开主代码报错的问题,修复所有js加载失败均弹窗的问题,修复代码编辑器访问caller等属性报错问题,对扩展自行创建武将包使用了lib.config.all.characters.push进行提示
This commit is contained in:
Spmario233 2024-02-13 15:57:40 +08:00 committed by GitHub
commit 7b04bb7817
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 74 additions and 58 deletions

View File

@ -1753,8 +1753,7 @@ export class Game extends Uninstantable {
static import(type, content, url) { static import(type, content, url) {
if (type == 'extension') { if (type == 'extension') {
const promise = game.loadExtension(content).then((name) => { const promise = game.loadExtension(content).then((name) => {
if (typeof _status.extensionLoaded == "undefined") if (typeof _status.extensionLoaded == "undefined") _status.extensionLoaded = [];
_status.extensionLoaded = [];
_status.extensionLoaded.add(name); _status.extensionLoaded.add(name);
return name; return name;
}); });

View File

@ -28,13 +28,14 @@ export const importMode = generateImportFunction('mode', (name) => `../../mode/$
/** /**
* 生成导入 * 生成导入
* *
* @param {string} type * @param { 'card' | 'character' | 'extension' | 'mode' } type
* @param {(name: string) => string} pathParser * @param {(name: string) => string} pathParser
* @returns {(name: string) => Promise<void>} * @returns {(name: string) => Promise<void>}
*/ */
function generateImportFunction(type, pathParser) { function generateImportFunction(type, pathParser) {
return async (name) => { return async (name) => {
if (type == 'extension' && !game.hasExtension(name) && !lib.config.all.stockextension.includes(name)) { if (type == 'extension' && !game.hasExtension(name) && !lib.config.all.stockextension.includes(name)) {
// @ts-ignore
await game.import(type, await createEmptyExtension(name)); await game.import(type, await createEmptyExtension(name));
return; return;
} }
@ -51,17 +52,19 @@ function generateImportFunction(type, pathParser) {
}; };
let script = createScript(); let script = createScript();
script.onerror = (e) => { script.onerror = (e) => {
if (path.endsWith('.js')) { if (path.endsWith('.js') && window.isSecureContext) {
path = path.slice(0, -3) + '.ts'; path = path.slice(0, -3) + '.ts';
script.remove(); script.remove();
let ts = createScript(); let ts = createScript();
ts.onerror = (e2) => { ts.onerror = (e2) => {
if (lib.path.basename(path) === 'extension.js' && lib.path.dirname(path).endsWith('/extension')) {
console.error(`扩展《${name}》加载失败`, e, e2); console.error(`扩展《${name}》加载失败`, e, e2);
let remove = confirm(`扩展《${name}》加载失败,是否移除此扩展?此操作不会移除目录下的文件。`); let remove = confirm(`扩展《${name}》加载失败,是否移除此扩展?此操作不会移除目录下的文件。`);
if (remove) { if (remove) {
lib.config.extensions.remove(name); lib.config.extensions.remove(name);
game.saveConfig('extensions', lib.config.extensions); game.saveConfig('extensions', lib.config.extensions);
} }
}
resolve(['error', ts]); resolve(['error', ts]);
} }
document.head.appendChild(ts); document.head.appendChild(ts);
@ -72,19 +75,23 @@ function generateImportFunction(type, pathParser) {
document.head.appendChild(script); document.head.appendChild(script);
}); });
script.remove(); script.remove();
if (status === 'error') return; if (status === 'error') {
if (type === 'character') {
console.error('如果您在扩展中使用了game.import创建武将包请将以下代码删除: lib.config.all.characters.push(\'武将包名\');');
}
return;
}
const modeContent = await import(path); const modeContent = await import(path);
if (!modeContent.type) return; 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); await game.import(type, modeContent.default);
} }
} }
async function createEmptyExtension(name) { 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'}}) const extensionInfo = await lib.init.promises.json(`${lib.assetURL}extension/${name}/info.json`)//await import(`../../extension/${name}/info.json`,{assert:{type:'json'}})
.then(info=>{ .then(info => info, () => {
return info;
},()=>{
return { return {
name: name, name: name,
intro: `扩展<b>《${name}》</b>尚未开启请开启后查看信息。建议扩展添加info.json以在关闭时查看信息`, intro: `扩展<b>《${name}》</b>尚未开启请开启后查看信息。建议扩展添加info.json以在关闭时查看信息`,
@ -94,7 +101,13 @@ async function createEmptyExtension(name){
version: "1.0", version: "1.0",
}; };
}); });
return {name:extensionInfo.name,content:function(config,pack){},precontent:function(){},config:{},help:{},package:{ return {
name: extensionInfo.name,
content: function (config, pack) { },
precontent: function () { },
config: {},
help: {},
package: {
character: { character: {
character: { character: {
}, },
@ -119,5 +132,12 @@ async function createEmptyExtension(name){
diskURL: extensionInfo.diskURL ? extensionInfo.diskURL : "", diskURL: extensionInfo.diskURL ? extensionInfo.diskURL : "",
forumURL: extensionInfo.forumURL ? extensionInfo.forumURL : "", forumURL: extensionInfo.forumURL ? extensionInfo.forumURL : "",
version: extensionInfo.version ? extensionInfo.version : "1.0.0", version: extensionInfo.version ? extensionInfo.version : "1.0.0",
},files:{"character":[],"card":[],"skill":[],"audio":[]}} },
files: {
"character": [],
"card": [],
"skill": [],
"audio": []
}
}
} }

View File

@ -301,8 +301,7 @@ export async function boot() {
} }
} }
if (!config.get('gameRecord')) if (!config.get('gameRecord')) config.set('gameRecord', {});
config.set('gameRecord', {});
for (const name in pack.mode) { for (const name in pack.mode) {
if (config.get('hiddenModePack').indexOf(name) == -1) { if (config.get('hiddenModePack').indexOf(name) == -1) {
config.get('all').mode.push(name); config.get('all').mode.push(name);
@ -435,11 +434,8 @@ export async function boot() {
extensionlist.push(config.get('plays')[name]); extensionlist.push(config.get('plays')[name]);
} }
} }
var alerted = false;
for (var name = 0; name < config.get('extensions').length; name++) { for (var name = 0; name < config.get('extensions').length; name++) {
if (Reflect.get(window, 'bannedExtensions').includes(config.get('extensions')[name])) { if (Reflect.get(window, 'bannedExtensions').includes(config.get('extensions')[name])) {
//if(!alerted) alert('读取某些扩展时出现问题。');
alerted = true;
continue; continue;
} }
var extcontent = localStorage.getItem(lib.configprefix + 'extension_' + config.get('extensions')[name]); var extcontent = localStorage.getItem(lib.configprefix + 'extension_' + config.get('extensions')[name]);
@ -462,11 +458,8 @@ export async function boot() {
} }
else { else {
if (config.get('mode') != 'connect' || (!localStorage.getItem(lib.configprefix + 'directstart') && show_splash)) { if (config.get('mode') != 'connect' || (!localStorage.getItem(lib.configprefix + 'directstart') && show_splash)) {
var alerted = false;
for (var name = 0; name < config.get('extensions').length; name++) { for (var name = 0; name < config.get('extensions').length; name++) {
if (Reflect.get(window, 'bannedExtensions').includes(config.get('extensions')[name])) { if (Reflect.get(window, 'bannedExtensions').includes(config.get('extensions')[name])) {
//if(!alerted) alert('读取某些扩展时出现问题。');
alerted = true;
continue; continue;
} }
// @ts-ignore // @ts-ignore

View File

@ -7883,7 +7883,11 @@ export class Library extends Uninstantable {
var icon = document.createElement("span"); var icon = document.createElement("span");
var className = "cm-completionIcon cm-completionIcon-"; var className = "cm-completionIcon cm-completionIcon-";
if (obj) { if (obj) {
const type = typeof obj[text]; // 解决访问caller报错等问题
let type;
try {
type = typeof obj[text];
} catch {}
if (type == 'function') { if (type == 'function') {
className += 'function'; className += 'function';
} }

View File

@ -5906,7 +5906,7 @@ class Create extends Uninstantable {
else { else {
if (!window.CodeMirror) { if (!window.CodeMirror) {
import('../../game/codemirror.js').then(() => { import('../../game/codemirror.js').then(() => {
lib.codeMirrorReady(node, editor); lib.codeMirrorReady(node, this.editor);
}); });
lib.init.css(lib.assetURL + 'layout/default', 'codemirror'); lib.init.css(lib.assetURL + 'layout/default', 'codemirror');
} }