添加升级逻辑,目前在v1.7版本的诗笺版电脑升级成功

This commit is contained in:
nonameShijian 2024-01-29 18:43:35 +08:00
parent 195967b2a1
commit 122c3cbddd
4 changed files with 167 additions and 16 deletions

View File

@ -1,5 +1,5 @@
import { game, get, lib, boot } from "../noname.js"; 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"; import { userAgent } from "../noname/util/index.js";
const coreAndVersion = get.coreInfo(); const coreAndVersion = get.coreInfo();
@ -14,18 +14,123 @@ if (core === 'chrome' && !isNaN(version) && version < 77) {
} }
} }
boot().then(() => {
// 判断是否从file协议切换到http/s协议 // 判断是否从file协议切换到http/s协议
if (canUseHttpProtocol()) { if (canUseHttpProtocol()) {
/* /*
升级方法: 升级方法:
1. 导出数据然后以http/s协议重启 1. 游戏启动后导出数据然后以http/s协议重启
2. 以http/s协议导入数据 2. 以http/s协议导入数据
3. 保存http/s协议的状态以后不再以file协议启动 3. 保存http/s协议的状态以后不再以file协议启动
*/ */
// 导出数据到根目录的noname.config.txt // 导出数据到根目录的noname.config.txt
// 成功导入后应删除noname.config.txt let data;
boot().then(lib.other.ignore); let export_data = function (data) {
} else { game.promises.writeFile(lib.init.encode(JSON.stringify(data)), './', 'noname.config.txt')
boot().then(lib.other.ignore); .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<void>} */(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;
});
}
}
}
});

View File

@ -7860,7 +7860,7 @@ export class Game extends Uninstantable {
/** /**
* *
* @param { string } storeName * @param { string } storeName
* @param { string } [query] * @param { string | null } [query]
* @param { Function } [onSuccess] * @param { Function } [onSuccess]
* @param { Function } [onError] * @param { Function } [onError]
*/ */

View File

@ -92,4 +92,12 @@ export class GamePromises extends Uninstantable {
game.createDir(directory, resolve, reject); game.createDir(directory, resolve, reject);
}); });
} }
static removeFile(filename) {
return /** @type {Promise<void>} */(new Promise((resolve, reject) => {
game.removeFile(filename, err => {
if (err) reject(err);
else resolve();
});
}));
}
} }

View File

@ -48,6 +48,44 @@ export function canUseHttpProtocol() {
return false; 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() { export async function boot() {
// 不想看,反正别动 // 不想看,反正别动