Skip to content

Commit

Permalink
检查版本暂存
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Dec 9, 2024
1 parent 40757eb commit 34859b6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/launchGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class GameLauncher {
return false;
}


await vscode.workspace.fs.createDirectory(vscode.Uri.joinPath(env.scriptUri!, '.log'));
let suc = await this.runPlugin();
if (!suc) {
Expand Down
16 changes: 14 additions & 2 deletions src/mainMenu/pages/environments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ export class 环境 extends TreeNode {
update: async (node) => {
let version = await y3.version.getServer();
node.description = version ? String(version.display) : '获取失败...';
node.tooltip = version ? version.display : undefined;
node.tooltip = version ? String(version.version) : undefined;
subscribeUpdate(node);
},
})
}),
] : undefined;
},
}),
Expand All @@ -71,3 +72,14 @@ export class 环境 extends TreeNode {
});
}
};

let hasSubscribed = false;
function subscribeUpdate(node: TreeNode) {
if (hasSubscribed) {
return;
}
hasSubscribed = true;
y3.version.onDidChange(() => {
node.refresh();
});
}
48 changes: 46 additions & 2 deletions src/tools/version.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as y3 from 'y3-helper';
import * as vscode from 'vscode';

interface Version {
version: number;
display: string;
}

export async function getClient(): Promise<Version | undefined> {
await y3.env.editorReady();
if (!y3.env.editorUri) {
return undefined;
}
Expand All @@ -32,8 +34,12 @@ export async function getClient(): Promise<Version | undefined> {
return undefined;
}


export async function getServer(): Promise<Version | undefined> {
let _lastServer: Version | undefined;
async function _getServer(): Promise<Version | undefined> {
await y3.env.editorReady();
if (!y3.env.editorUri) {
return undefined;
}
// 从指定网址上下载json并解析出版本号
const url = 'https://up5.update.netease.com/pl/patchmd5_windows64_produp5_release.txt';
try {
Expand All @@ -51,3 +57,41 @@ export async function getServer(): Promise<Version | undefined> {
return undefined;
}
}

let _lastServerTest: Version | undefined;
async function _getServerTest(): Promise<Version | undefined> {
if (!_lastServerTest) {
_lastServerTest = await _getServer();
} else {
_lastServerTest = {
version: _lastServerTest.version + 1,
display: _lastServerTest.display,
};
}
return _lastServerTest;
}

export async function getServer(): Promise<Version | undefined> {
return _lastServer ??= await _getServer();
}

export async function needUpdate(): Promise<boolean> {
let [client, server] = await Promise.all([getClient(), getServer()]);
if (!client || !server) {
return false;
}
return client.version < server.version;
}

const _onDidChange = new vscode.EventEmitter<Version>();
export function onDidChange(callback: (version: Version) => void) {
_onDidChange.event(callback);
}

setInterval(async () => {
let server = await _getServerTest();
if (server && server?.version !== _lastServer?.version) {
_lastServer = server;
_onDidChange.fire(server);
}
}, 1000 * 1);

0 comments on commit 34859b6

Please sign in to comment.