From 48785e93f0c7165d31f8ca3af762297018ff6d26 Mon Sep 17 00:00:00 2001 From: Glavo Date: Tue, 8 Feb 2022 01:46:09 +0800 Subject: [PATCH] Import error messages --- HMCLauncher/HMCL/java-download.ps1 | 17 ++++++++++++++--- HMCLauncher/HMCL/lang.h | 8 ++++++-- HMCLauncher/HMCL/main.cpp | 22 ++++++++++++++++++++-- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/HMCLauncher/HMCL/java-download.ps1 b/HMCLauncher/HMCL/java-download.ps1 index 57f2abb80..8660feeed 100644 --- a/HMCLauncher/HMCL/java-download.ps1 +++ b/HMCLauncher/HMCL/java-download.ps1 @@ -8,9 +8,9 @@ $chinese = [System.Globalization.CultureInfo]::CurrentCulture.Name -eq 'zh-CN' [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") $result = if ($chinese) { - [System.Windows.Forms.MessageBox]::Show('HMCL 需要 Java 运行时环境才能正常运行,是否自动下载安装 Java?', 'HMCL', [System.Windows.Forms.MessageBoxButtons]::YesNo) + [System.Windows.Forms.MessageBox]::Show("HMCL 需要 Java 运行时环境才能正常运行,是否自动下载安装 Java?", '未能在这台电脑上找到 Java', [System.Windows.Forms.MessageBoxButtons]::YesNo) } else { - [System.Windows.Forms.MessageBox]::Show('Running HMCL requires a Java runtime environment. Do you want to download and install Java automatically?', 'HMCL', [System.Windows.Forms.MessageBoxButtons]::YesNo) + [System.Windows.Forms.MessageBox]::Show("Running HMCL requires a Java runtime environment. `nDo you want to download and install Java automatically?", 'Java not found', [System.Windows.Forms.MessageBoxButtons]::YesNo) } if ($result -ne [System.Windows.Forms.DialogResult]::Yes) { @@ -92,7 +92,18 @@ $form.Controls.Add($layout) [System.ComponentModel.AsyncCompletedEventHandler]$downloadFileCompletedEventHandler = { param($sender, [System.ComponentModel.AsyncCompletedEventArgs]$CompletedEventArgs) if (!$form.IsDisposed) { - $form.DialogResult = [System.Windows.Forms.DialogResult]::OK + if ($CompletedEventArgs.Cancelled) { + $form.DialogResult = [System.Windows.Forms.DialogResult]::Cancel + } elseif ($CompletedEventArgs.Error -ne $null) { + if ($chinese) { + [System.Windows.Forms.MessageBox]::Show($CompletedEventArgs.Error.Message, '下载失败', [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Exclamation) + } else { + [System.Windows.Forms.MessageBox]::Show($CompletedEventArgs.Error.Message, 'Download failed', [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Exclamation) + } + $form.DialogResult = [System.Windows.Forms.DialogResult]::Cancel + } else { + $form.DialogResult = [System.Windows.Forms.DialogResult]::OK + } } } diff --git a/HMCLauncher/HMCL/lang.h b/HMCLauncher/HMCL/lang.h index ac2e08b4d..8f0722dfa 100644 --- a/HMCLauncher/HMCL/lang.h +++ b/HMCLauncher/HMCL/lang.h @@ -1,4 +1,8 @@ #pragma once -#define ERROR_PROMPT L"Java installation cannot be found in this computer, please download it from https://www.microsoft.com/openjdk \n" \ -L"δ̨ҵJava https://www.microsoft.com/openjdk ذװJava" \ No newline at end of file +#define ERROR_PROMPT L"Java installation cannot be found in this computer,\n"\ + L"please download it from %s.\n"\ + L"Click 'OK' to go to the download page." + +#define ERROR_PROMPT_ZH L"δ̨ҵ Java %s ذװ Java\n"\ + L"ȷǰҳ档" \ No newline at end of file diff --git a/HMCLauncher/HMCL/main.cpp b/HMCLauncher/HMCL/main.cpp index cd4cefc28..458b8fda9 100644 --- a/HMCLauncher/HMCL/main.cpp +++ b/HMCLauncher/HMCL/main.cpp @@ -67,6 +67,15 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, exeName = exeName.substr(last_slash + 1); } + bool useChinese; + { + LCID lcid = GetUserDefaultUILanguage(); + WCHAR nameBuffer[LOCALE_NAME_MAX_LENGTH]; + useChinese = (LCIDToLocaleName(lcid, nameBuffer, LOCALE_NAME_MAX_LENGTH, 0) != 0) + && (lstrcmp(nameBuffer, L"zh-CN") == 0); + } + + OSVERSIONINFOEX osvi; DWORDLONG dwlConditionMask = 0; int op = VER_GREATER_EQUAL; @@ -191,7 +200,16 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, } error: - MessageBox(NULL, ERROR_PROMPT, L"Error", MB_ICONERROR | MB_OK); - ShellExecute(0, 0, L"https://www.microsoft.com/openjdk", 0, 0, SW_SHOW); + LPCWSTR downloadLink = isWin7OrLater && (isX64 || isARM64) ? L"https://www.microsoft.com/openjdk" : L"https://java.com"; + + WCHAR errorPrompt[180]; + { + LPCWSTR errorPromptFormat = useChinese ? ERROR_PROMPT_ZH : ERROR_PROMPT; + wsprintf(errorPrompt, errorPromptFormat, downloadLink); + } + + if (IDOK == MessageBox(NULL, errorPrompt, L"Error", MB_ICONERROR | MB_OKCANCEL)) { + ShellExecute(0, 0, downloadLink, 0, 0, SW_SHOW); + } return 1; }