/* runtime-plugin-backup.js —— 插件进出备份 * * 备份里带上插件清单与源码(体积很小),换机器恢复后不必重装。 * v3.25.41:兼容目录插件(//{manifest.json,main.js,styles.css}) * collect() → [{ id, version, kind:'file', file, integrity, code }] * | [{ id, version, kind:'folder', dir, main, style, files:{name: text}, hashes }] * restore(v) → 写回 <资料目录>/plugins/ 并合并清单(备份里没有的插件保留) * * 注意:只备份**代码**,不备份插件数据(plugin-data 单独进备份,见 M2)。 */ ;(function (global) { 'use strict' function store () { const F = global.IWBStorageFile || null return (F && F.plugins) ? F.plugins : null } async function collect () { const s = store() if (!s) return [] let list = [] try { list = await s.manifest() } catch (e) { return [] } const out = [] for (const e of (Array.isArray(list) ? list : [])) { if (!e || !e.id) continue // 目录插件:main + 可选 style 一起收(manifest 里的 files 只存 hash,文本要读出来) if (e.dir) { try { const main = String(e.main || 'main.js') const files = {} files[main] = String(await s.readFile(String(e.dir) + '/' + main) || '') if (e.style) files[String(e.style)] = String(await s.readFile(String(e.dir) + '/' + e.style) || '') out.push({ id: e.id, version: e.version || '', kind: 'folder', dir: String(e.dir), main: main, style: e.style ? String(e.style) : '', files: files, hashes: e.files || null }) } catch (err) { /* 单个插件读失败不影响其它 */ } continue } if (!e.file) continue try { const text = await s.read(e.file) out.push({ id: e.id, version: e.version || '', kind: 'file', file: e.file, integrity: e.integrity || '', code: String(text || '') }) } catch (err) { /* 同上 */ } } return out } async function restore (list) { const s = store() if (!s || !Array.isArray(list) || !list.length) return 0 let cur = [] try { cur = await s.manifest() } catch (e) { cur = [] } const byId = new Map((Array.isArray(cur) ? cur : []).map(function (e) { return [String(e && e.id), e] })) let n = 0 for (const p of list) { if (!p || !p.id) continue if (p.kind === 'folder' && p.dir && p.files && typeof p.files === 'object') { if (!/^[A-Za-z0-9._-]+$/.test(String(p.dir))) continue try { const names = Object.keys(p.files) for (const name of names) { if (!/^[A-Za-z0-9._-]+$/.test(name)) continue await s.writeFile(String(p.dir) + '/' + name, String(p.files[name] || '')) } byId.set(String(p.id), { id: String(p.id), dir: String(p.dir), main: String(p.main || 'main.js'), style: String(p.style || ''), version: String(p.version || ''), files: p.hashes || null }) n++ } catch (err) { /* 单个失败忽略 */ } continue } if (!p.file) continue if (!/^[A-Za-z0-9._-]+$/.test(String(p.file))) continue try { await s.write(String(p.file), String(p.code || '')) byId.set(String(p.id), { id: String(p.id), file: String(p.file), version: String(p.version || ''), integrity: String(p.integrity || '') }) n++ } catch (err) { /* 单个失败忽略 */ } } try { await s.write('index.json', JSON.stringify(Array.from(byId.values()))) } catch (e) { /* ignore */ } return n } global.IWBRuntimePluginBackup = { collect: collect, restore: restore } })(typeof window !== 'undefined' ? window : globalThis)