Skip to content

Commit

Permalink
非必要不询问用户
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Mar 28, 2024
1 parent adc76f7 commit 3ab473f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
28 changes: 19 additions & 9 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class Env {
return false;
}

private async searchEditorUri(): Promise<vscode.Uri | undefined> {
private async searchEditorUri(askUser = false): Promise<vscode.Uri | undefined> {
// 先看看设置里有没有
let editorPath: string|undefined = vscode.workspace.getConfiguration('Y3-Helper').get('EditorPath');
if (editorPath && await this.isValidEditorPath(editorPath)) {
Expand All @@ -66,7 +66,7 @@ export class Env {
}

// 如果没有,则询问用户
while (true) {
while (askUser) {
let selectedFiles = await vscode.window.showOpenDialog({
canSelectFiles: true,
openLabel: '选择Y3编辑器路径',
Expand Down Expand Up @@ -132,7 +132,7 @@ export class Env {
return mapFolder;
}

private async searchProjectPath(): Promise<vscode.Uri | undefined> {
private async searchProjectPath(askUser = false): Promise<vscode.Uri | undefined> {
// 先直接搜索打开的工作目录
if (vscode.workspace.workspaceFolders) {
for (const folder of vscode.workspace.workspaceFolders) {
Expand All @@ -143,6 +143,10 @@ export class Env {
}
}

if (!askUser) {
return undefined;
}

// 如果没有,则询问用户
let selectedFolders = await vscode.window.showOpenDialog({
canSelectFiles: true,
Expand Down Expand Up @@ -300,15 +304,15 @@ export class Env {

}

private async init() {
private async init(askUser = false) {
await Promise.allSettled([
(async () => {
this.editorUri = await this.searchEditorUri();
this.editorUri = await this.searchEditorUri(askUser);
this.editorVersion = await this.getEditorVersion();
this.editorExeUri = this.getEditorExeUri();
})(),
(async () => {
this.mapUri = await this.searchProjectPath();
this.mapUri = await this.searchProjectPath(askUser);
if (this.mapUri) {
this.projectUri = vscode.Uri.joinPath(this.mapUri, '../..');
this.scriptUri = vscode.Uri.joinPath(this.mapUri, 'script');
Expand All @@ -329,9 +333,15 @@ export class Env {
tools.log.info(`editorTableUri: ${this.editorTableUri?.fsPath}`);
}

public async waitReady() {
// 如果找不到路径,是否弹框询问用户?
public async waitReady(askUser = false) {
if (this.status === 'ready') {
return;
if (!askUser) {
return;
}
if (this.mapUri) {
return;
}
}
if (this.status === 'initing') {
// 自旋
Expand All @@ -341,7 +351,7 @@ export class Env {
return;
}
this.status = 'initing';
await this.init();
await this.init(askUser);
this.status = 'ready';
}

Expand Down
22 changes: 10 additions & 12 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ class Helper {
this.mainMenu = new MainMenu(this.env);
}

private async reload(waitReady = false) {
private async reload() {
this.env = new Env();
if (waitReady) {
await this.env.waitReady();
}
await this.env.waitReady();
this.mainMenu.reload(this.env);
}

Expand All @@ -44,7 +42,7 @@ class Helper {

private registerCommonCommands() {
vscode.commands.registerCommand('y3-helper.reloadEnv', async () => {
await this.reload(true);
await this.reload();
});
vscode.commands.registerCommand('y3-helper.shell', async (...args: any[]) => {
runShell("执行命令", args[0], args.slice(1));
Expand All @@ -62,7 +60,7 @@ class Helper {
location: vscode.ProgressLocation.Notification,
title: '正在初始化Y3项目...',
}, async (progress, token) => {
await this.env.waitReady();
await this.env.waitReady(true);
if (!this.env.scriptUri) {
vscode.window.showErrorMessage('未找到Y3地图路径,请先用编辑器创建地图或重新指定!');
return;
Expand Down Expand Up @@ -200,7 +198,7 @@ class Helper {
return;
}

await this.env.waitReady();
await this.env.waitReady(true);
await vscode.debug.startDebugging(vscode.workspace.workspaceFolders?.[0], "💡附加");
});
});
Expand All @@ -213,7 +211,7 @@ class Helper {
*/
private registerCommandOfImportObjectDataFromAllCSVbyConfig() {
vscode.commands.registerCommand('y3-helper.importObjectDataFromAllCSV', async () => {
await this.env.waitReady();
await this.env.waitReady(true);
let projectUri = this.env.projectUri;
let editorExeUri = this.env.editorExeUri;
let scriptUri= this.env.scriptUri;
Expand Down Expand Up @@ -247,7 +245,7 @@ class Helper {

// 在CSV表格中添加物编项目的命令
let addNewDataInCSVcommand = vscode.commands.registerCommand('y3-helper.addNewDataInCSV', async () => {
await this.env.waitReady();
await this.env.waitReady(true);
const editorTableTypes: vscode.QuickPickItem[] = [
{ label: '单位', description: 'unit' },
{ label: '装饰物', description: 'decoration' },
Expand Down Expand Up @@ -290,7 +288,7 @@ class Helper {

// 把Y3工程项目中已有的物编数据的UID和名称添加到CSV表格以便填写和导入的命令
let addUIDandNameToCSVfromProjectCommand = vscode.commands.registerCommand("y3-helper.addUIDandNameToCSVfromProject", async () => {
await this.env.waitReady();
await this.env.waitReady(true);
const inputOptions: vscode.InputBoxOptions = {
prompt: 'UID或名称',
placeHolder: 'UID或名称',
Expand Down Expand Up @@ -327,7 +325,7 @@ class Helper {
private registerCommandOfGenerateAllTemplateCSV() {
vscode.commands.registerCommand('y3-helper.generateAllTemplateCSV', async () => {
console.log("y3-helper.generateTemplateCSV");
await this.env.waitReady();
await this.env.waitReady(true);
let projectUri = this.env.projectUri;
let editorExeUri = this.env.editorExeUri;
if (!projectUri) {
Expand All @@ -352,7 +350,7 @@ class Helper {

private registerCommandOfDownloadPresetUI() {
vscode.commands.registerCommand('y3-helper.downloadPresetUI', async () => {
await this.env.waitReady();
await this.env.waitReady(true);
if (!this.env.mapUri) {
vscode.window.showErrorMessage("未找到地图路径!");
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/launchGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class GameLauncher {
}

public async launch(luaArgs?: {[key: string]: string|number|boolean}): Promise<boolean> {
await this.env.waitReady();
await this.env.waitReady(true);
let projectUri = this.env.projectUri;
let editorExeUri = this.env.editorExeUri;
if (!projectUri) {
Expand Down

0 comments on commit 3ab473f

Please sign in to comment.