From 122c3cbddd5f1b99e3de1008b641fddb2db97b53 Mon Sep 17 00:00:00 2001 From: nonameShijian <2954700422@qq.com> Date: Mon, 29 Jan 2024 18:43:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8D=87=E7=BA=A7=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E7=9B=AE=E5=89=8D=E5=9C=A8v1.7=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E7=9A=84=E8=AF=97=E7=AC=BA=E7=89=88=E7=94=B5=E8=84=91?= =?UTF-8?q?=E5=8D=87=E7=BA=A7=E6=88=90=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game/entry.js | 135 +++++++++++++++++++++++++++++++++++----- noname/game/index.js | 2 +- noname/game/promises.js | 8 +++ noname/init/index.js | 38 +++++++++++ 4 files changed, 167 insertions(+), 16 deletions(-) diff --git a/game/entry.js b/game/entry.js index 1c5249e6c..beeeb229c 100644 --- a/game/entry.js +++ b/game/entry.js @@ -1,5 +1,5 @@ import { game, get, lib, boot } from "../noname.js"; -import { canUseHttpProtocol } from "../noname/init/index.js"; +import { canUseHttpProtocol, sendUpdate } from "../noname/init/index.js"; import { userAgent } from "../noname/util/index.js"; const coreAndVersion = get.coreInfo(); @@ -14,18 +14,123 @@ if (core === 'chrome' && !isNaN(version) && version < 77) { } } -// 判断是否从file协议切换到http/s协议 - if (canUseHttpProtocol()) { - /* - 升级方法: - 1. 导出数据,然后以http/s协议重启 - 2. 以http/s协议导入数据 - 3. 保存http/s协议的状态,以后不再以file协议启动 - */ - // 导出数据到根目录的noname.config.txt - // 成功导入后应删除noname.config.txt - boot().then(lib.other.ignore); - } else { - boot().then(lib.other.ignore); - } +boot().then(() => { + // 判断是否从file协议切换到http/s协议 + if (canUseHttpProtocol()) { + /* + 升级方法: + 1. 游戏启动后导出数据,然后以http/s协议重启 + 2. 以http/s协议导入数据 + 3. 保存http/s协议的状态,以后不再以file协议启动 + */ + // 导出数据到根目录的noname.config.txt + let data; + let export_data = function (data) { + game.promises.writeFile(lib.init.encode(JSON.stringify(data)), './', 'noname.config.txt') + .then(saveProtocol) + .catch(e => { + console.error('升级失败:', e); + }); + }; + // @ts-ignore + if (!lib.db) { + data = {}; + for (let i in localStorage) { + if (i.startsWith(lib.configprefix)) { + data[i] = localStorage[i]; + } + } + export_data(data); + } + else { + game.getDB('config', null, function (data1) { + game.getDB('data', null, function (data2) { + export_data({ + config: data1, + data: data2 + }); + }); + }); + } + // 保存协议的切换状态 + function saveProtocol() { + const url = sendUpdate(); + if (typeof url == 'string') { + if (typeof window.require == 'function' && typeof window.process == 'object') { + // @ts-ignore + const remote = require('@electron/remote'); + const thisWindow = remote.getCurrentWindow(); + thisWindow.loadURL(url); + } else { + location.href = url; + } + } + } + } else { + // 成功导入后删除noname.config.txt + let searchParams = new URLSearchParams(location.search); + for (let [key, value] of searchParams) { + if (key === 'sendUpdate' && value === 'true') { + game.promises.readFileAsText('noname.config.txt').then(data => { + return /** @type {Promise} */(new Promise(async (resolve, reject) => { + if (!data) return reject('!data'); + try { + data = JSON.parse(lib.init.decode(data)); + if (!data || typeof data != 'object') { + throw ('err'); + } + // @ts-ignore + if (lib.db && (!data.config || !data.data)) { + throw ('err'); + } + } + catch (e) { + console.log(e); + if (e == 'err') { + alert('导入文件格式不正确'); + reject('导入文件格式不正确'); + } else { + alert('导入失败: ' + e.message); + reject('导入失败: ' + e.message); + } + return; + } + alert('导入成功, 即将自动重启'); + // @ts-ignore + if (!lib.db) { + const noname_inited = localStorage.getItem('noname_inited'); + const onlineKey = localStorage.getItem(lib.configprefix + 'key'); + localStorage.clear(); + if (noname_inited) { + localStorage.setItem('noname_inited', noname_inited); + } + if (onlineKey) { + localStorage.setItem(lib.configprefix + 'key', onlineKey); + } + for (let i in data) { + localStorage.setItem(i, data[i]); + } + } + else { + for (let i in data.config) { + await game.putDB('config', i, data.config[i]); + lib.config[i] = data.config[i]; + } + for (let i in data.data) { + await game.putDB('data', i, data.data[i]); + } + } + lib.init.background(); + resolve(); + })); + }).then(() => { + return game.promises.removeFile('noname.config.txt'); + }).then(() => { + const url = new URL(location.href); + location.href = url.origin + url.pathname; + }); + } + } + } +}); diff --git a/noname/game/index.js b/noname/game/index.js index aafac3e5b..0148f631e 100644 --- a/noname/game/index.js +++ b/noname/game/index.js @@ -7860,7 +7860,7 @@ export class Game extends Uninstantable { /** * * @param { string } storeName - * @param { string } [query] + * @param { string | null } [query] * @param { Function } [onSuccess] * @param { Function } [onError] */ diff --git a/noname/game/promises.js b/noname/game/promises.js index 61a452fa4..c5f6d6988 100644 --- a/noname/game/promises.js +++ b/noname/game/promises.js @@ -92,4 +92,12 @@ export class GamePromises extends Uninstantable { game.createDir(directory, resolve, reject); }); } + static removeFile(filename) { + return /** @type {Promise} */(new Promise((resolve, reject) => { + game.removeFile(filename, err => { + if (err) reject(err); + else resolve(); + }); + })); + } } diff --git a/noname/init/index.js b/noname/init/index.js index 5de328619..68d07cb7a 100644 --- a/noname/init/index.js +++ b/noname/init/index.js @@ -48,6 +48,44 @@ export function canUseHttpProtocol() { return false; } +/** + * 传递升级完成的信息 + * @returns { string | void } 返回一个网址 + */ +export function sendUpdate() { + // 手机端 + if (window.cordova) { + // 直接确定包名 + if (nonameInitialized && nonameInitialized.includes('com.noname.shijian')) { + // 给诗笺版apk的java层传递升级完成的信息 + // @ts-ignore + return window.noname_shijianInterfaces.xxx() + '?sendUpdate=true'; + } + } + // 电脑端 + else if (typeof window.require == 'function' && typeof window.process == 'object') { + // 从json判断版本号 + const fs = require('fs'); + const path = require('path'); + if (fs.existsSync(path.join(__dirname, 'package.json'))) { + // @ts-ignore + const json = require('./package.json'); + // 诗笺电脑版的判断 + if (json && Number(json.installerVersion) >= 1.7) { + fs.writeFileSync(path.join(__dirname, 'Home', 'saveProtocol.txt'), ''); + // 启动http + const cp = require('child_process'); + cp.exec(`start /min ${__dirname}\\noname-server.exe -platform=electron`, (err, stdout, stderr) => { }); + return `http://localhost:8089/app.html?sendUpdate=true`; + } + } + } + // 浏览器端 + else { + return location.href; + } +} + // 无名杀,启动! export async function boot() { // 不想看,反正别动