diff --git a/lib/common/conf.dart b/lib/common/conf.dart index 5e62401..112827f 100644 --- a/lib/common/conf.dart +++ b/lib/common/conf.dart @@ -6,6 +6,7 @@ import 'package:hive/hive.dart'; import 'package:path_provider/path_provider.dart'; import 'package:starcitizen_doctor/api/analytics.dart'; import 'package:starcitizen_doctor/api/api.dart'; +import 'package:starcitizen_doctor/common/helper/system_helper.dart'; import 'package:starcitizen_doctor/common/rust/ffi.dart'; import 'package:starcitizen_doctor/data/app_version_data.dart'; import 'package:starcitizen_doctor/global_ui_model.dart'; @@ -80,6 +81,7 @@ class AppConf { } dPrint("---- rust bridge inited -----"); isRunningAdmin = await globalUIModel.checkAdmin(); + await SystemHelper.initPowerShellPath(); /// init windows await windowManager.ensureInitialized(); diff --git a/lib/common/helper/system_helper.dart b/lib/common/helper/system_helper.dart index 8766bf3..a25c117 100644 --- a/lib/common/helper/system_helper.dart +++ b/lib/common/helper/system_helper.dart @@ -4,9 +4,27 @@ import 'package:starcitizen_doctor/common/conf.dart'; import 'package:starcitizen_doctor/common/utils/base_utils.dart'; class SystemHelper { + static String powershellPath = "powershell.exe"; + + static initPowerShellPath() async { + var result = await Process.run(powershellPath, ["echo", "ping"]); + if (!result.stdout.toString().startsWith("ping") && + powershellPath == "powershell.exe") { + Map envVars = Platform.environment; + final systemRoot = envVars["SYSTEMROOT"]; + if (systemRoot != null) { + final autoSearchPath = + "$systemRoot\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"; + dPrint("auto search powershell path === $autoSearchPath"); + powershellPath = autoSearchPath; + initPowerShellPath(); + } + } + } + static Future checkNvmePatchStatus() async { try { - var result = await Process.run('powershell.exe', [ + var result = await Process.run(SystemHelper.powershellPath, [ "Get-ItemProperty", "-Path", "\"HKLM:\\SYSTEM\\CurrentControlSet\\Services\\stornvme\\Parameters\\Device\"", @@ -26,7 +44,7 @@ class SystemHelper { } static Future addNvmePatch() async { - var result = await Process.run('powershell.exe', [ + var result = await Process.run(SystemHelper.powershellPath, [ 'New-ItemProperty', "-Path", "\"HKLM:\\SYSTEM\\CurrentControlSet\\Services\\stornvme\\Parameters\\Device\"", @@ -42,7 +60,7 @@ class SystemHelper { static doRemoveNvmePath() async { try { - var result = await Process.run('powershell.exe', [ + var result = await Process.run(SystemHelper.powershellPath, [ "Clear-ItemProperty", "-Path", "\"HKLM:\\SYSTEM\\CurrentControlSet\\Services\\stornvme\\Parameters\\Device\"", @@ -68,7 +86,7 @@ class SystemHelper { "$programDataPath\\Microsoft\\Windows\\Start Menu\\Programs\\Roberts Space Industries\\RSI Launcher.lnk"; final rsiLinkFile = File(rsiFilePath); if (await rsiLinkFile.exists()) { - final r = await Process.run("powershell.exe", [ + final r = await Process.run(SystemHelper.powershellPath, [ "(New-Object -ComObject WScript.Shell).CreateShortcut(\"$rsiFilePath\").targetpath" ]); if (r.stdout.toString().contains("RSI Launcher.exe")) { diff --git a/lib/global_ui_model.dart b/lib/global_ui_model.dart index 16cffc6..c8240c4 100644 --- a/lib/global_ui_model.dart +++ b/lib/global_ui_model.dart @@ -50,7 +50,7 @@ class AppGlobalUIModel extends BaseUIModel { Future checkAdmin() async { const checkAdmin = r"if ((New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { exit 0 } else { exit 1 }"; - final r = await Process.run("powershell.exe", [checkAdmin]); + final r = await Process.run(SystemHelper.powershellPath, [checkAdmin]); dPrint("code == ${r.exitCode} msg == ${r.stdout} err = ${r.stderr}"); if (r.exitCode == 0) { return true; @@ -71,7 +71,8 @@ class AppGlobalUIModel extends BaseUIModel { _runAsAdmin() async { await SystemHelper.initVBS(); - await Process.run("powershell.exe", [AppConf.launchHelperPath]); + await Process.run( + SystemHelper.powershellPath, [AppConf.launchHelperPath]); exit(0); } } diff --git a/lib/ui/home/home_ui_model.dart b/lib/ui/home/home_ui_model.dart index 2d6f0c0..e895e62 100644 --- a/lib/ui/home/home_ui_model.dart +++ b/lib/ui/home/home_ui_model.dart @@ -350,7 +350,7 @@ class HomeUIModel extends BaseUIModel { } openDir(rsiLauncherInstalledPath) async { - await Process.run("powershell.exe", + await Process.run(SystemHelper.powershellPath, ["explorer.exe", "/select,\"$rsiLauncherInstalledPath\""]); } @@ -489,7 +489,7 @@ class HomeUIModel extends BaseUIModel { return; } if (isCurGameRunning) { - await Process.run("powershell.exe", ["ps \"StarCitizen\" | kill"]); + await Process.run(SystemHelper.powershellPath, ["ps \"StarCitizen\" | kill"]); return; } AnalyticsApi.touch("gameLaunch"); diff --git a/lib/ui/home/localization/localization_ui_model.dart b/lib/ui/home/localization/localization_ui_model.dart index b3694c8..0c2fa11 100644 --- a/lib/ui/home/localization/localization_ui_model.dart +++ b/lib/ui/home/localization/localization_ui_model.dart @@ -8,6 +8,7 @@ import 'package:starcitizen_doctor/api/analytics.dart'; import 'package:starcitizen_doctor/api/api.dart'; import 'package:starcitizen_doctor/base/ui_model.dart'; import 'package:starcitizen_doctor/common/conf.dart'; +import 'package:starcitizen_doctor/common/helper/system_helper.dart'; import 'package:starcitizen_doctor/data/sc_localization_data.dart'; class LocalizationUIModel extends BaseUIModel { @@ -212,7 +213,7 @@ class LocalizationUIModel extends BaseUIModel { openDir() async { showToast(context!, "即将打开本地化文件夹,请将自定义的 任意名称.ini 文件放入 Customize_ini 文件夹。\n\n添加新文件后未显示请使用右上角刷新按钮。\n\n安装时请确保选择了正确的语言。"); - await Process.run("powershell.exe", + await Process.run(SystemHelper.powershellPath, ["explorer.exe", "/select,\"${customizeDir.absolute.path}\"\\"]); } diff --git a/lib/ui/index_ui_model.dart b/lib/ui/index_ui_model.dart index 87f1228..b96d3fc 100644 --- a/lib/ui/index_ui_model.dart +++ b/lib/ui/index_ui_model.dart @@ -2,6 +2,7 @@ import 'dart:io'; import 'package:starcitizen_doctor/api/analytics.dart'; import 'package:starcitizen_doctor/base/ui_model.dart'; +import 'package:starcitizen_doctor/common/helper/system_helper.dart'; import 'package:starcitizen_doctor/global_ui_model.dart'; import 'package:starcitizen_doctor/ui/about/about_ui_model.dart'; import 'package:starcitizen_doctor/ui/home/home_ui_model.dart'; @@ -64,7 +65,7 @@ class IndexUIModel extends BaseUIModel { } try { - var result = await Process.run('powershell.exe', ["echo", "ping"]); + var result = await Process.run(SystemHelper.powershellPath, ["echo", "ping"]); if (result.stdout.toString().startsWith("ping")) { dPrint("powershell check pass"); } else { diff --git a/lib/ui/settings/upgrade_dialog_ui_model.dart b/lib/ui/settings/upgrade_dialog_ui_model.dart index ecbbb1b..e14396d 100644 --- a/lib/ui/settings/upgrade_dialog_ui_model.dart +++ b/lib/ui/settings/upgrade_dialog_ui_model.dart @@ -4,6 +4,7 @@ import 'package:dio/dio.dart'; import 'package:starcitizen_doctor/api/api.dart'; import 'package:starcitizen_doctor/base/ui_model.dart'; import 'package:starcitizen_doctor/common/conf.dart'; +import 'package:starcitizen_doctor/common/helper/system_helper.dart'; class UpgradeDialogUIModel extends BaseUIModel { String? description; @@ -64,7 +65,8 @@ class UpgradeDialogUIModel extends BaseUIModel { isUpgrading = false; progress = null; showToast(context!, "运行失败,请尝试手动安装!"); - Process.run("powershell.exe", ["explorer.exe", "/select,\"$fileName\""]); + Process.run(SystemHelper.powershellPath, + ["explorer.exe", "/select,\"$fileName\""]); notifyListeners(); } } diff --git a/lib/ui/tools/tools_ui_model.dart b/lib/ui/tools/tools_ui_model.dart index 5bf0738..a2aaeed 100644 --- a/lib/ui/tools/tools_ui_model.dart +++ b/lib/ui/tools/tools_ui_model.dart @@ -289,7 +289,7 @@ class ToolsUIModel extends BaseUIModel { } openDir(path) async { - await Process.run("powershell.exe", ["explorer.exe", "/select,\"$path\""]); + await Process.run(SystemHelper.powershellPath, ["explorer.exe", "/select,\"$path\""]); } Future _showSystemInfo() async {