diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..914924f --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "windows/corrosion"] + path = windows/corrosion + url = https://github.com/corrosion-rs/corrosion.git diff --git a/lib/common/conf.dart b/lib/common/conf.dart index f9d2d99..025f7e1 100644 --- a/lib/common/conf.dart +++ b/lib/common/conf.dart @@ -77,8 +77,8 @@ class AppConf { exit(1); } dPrint("---- rust bridge inited -----"); + await SystemHelper.initPowershellPath(); 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 917e042..22db331 100644 --- a/lib/common/helper/system_helper.dart +++ b/lib/common/helper/system_helper.dart @@ -1,11 +1,12 @@ import 'dart:io'; +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 { + static initPowershellPath() async { var result = await Process.run(powershellPath, ["echo", "ping"]); if (!result.stdout.toString().startsWith("ping") && powershellPath == "powershell.exe") { @@ -16,7 +17,7 @@ class SystemHelper { "$systemRoot\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"; dPrint("auto search powershell path === $autoSearchPath"); powershellPath = autoSearchPath; - initPowerShellPath(); + initPowershellPath(); } } } @@ -43,7 +44,7 @@ class SystemHelper { } static Future addNvmePatch() async { - var result = await Process.run(SystemHelper.powershellPath, [ + var result = await powershellAdminRun([ 'New-ItemProperty', "-Path", "\"HKLM:\\SYSTEM\\CurrentControlSet\\Services\\stornvme\\Parameters\\Device\"", @@ -59,7 +60,7 @@ class SystemHelper { static doRemoveNvmePath() async { try { - var result = await Process.run(SystemHelper.powershellPath, [ + var result = await powershellAdminRun([ "Clear-ItemProperty", "-Path", "\"HKLM:\\SYSTEM\\CurrentControlSet\\Services\\stornvme\\Parameters\\Device\"", @@ -98,7 +99,7 @@ class SystemHelper { static killRSILauncher() async { var psr = await Process.run( - "powershell", ["ps", "\"RSI Launcher\"", "|select -expand id"]); + powershellPath, ["ps", "\"RSI Launcher\"", "|select -expand id"]); if (psr.stderr == "") { for (var value in (psr.stdout ?? "").toString().split("\n")) { dPrint(value); @@ -110,7 +111,7 @@ class SystemHelper { } static Future> getPID(String name) async { - final r = await Process.run("powershell", ["(ps $name).Id"]); + final r = await Process.run(powershellPath, ["(ps $name).Id"]); final str = r.stdout.toString().trim(); dPrint(str); if (str.isEmpty) return []; @@ -121,14 +122,18 @@ class SystemHelper { // check running and kill await killRSILauncher(); // launch - final r = await Process.run("powershell", ["start", "\"$path\""]); + final r = await Process.run(powershellPath, [ + 'Start-Process', + "'$path'", + '-Verb RunAs', + ]); dPrint(path); dPrint(r.stdout); dPrint(r.stderr); } static Future getSystemMemorySizeGB() async { - final r = await Process.run("powershell", [ + final r = await Process.run(powershellPath, [ "(Get-CimInstance Win32_PhysicalMemory | Measure-Object -Property capacity -Sum).sum /1gb" ]); return int.tryParse(r.stdout.toString().trim()) ?? 0; @@ -137,19 +142,19 @@ class SystemHelper { static Future getSystemCimInstance(String win32InstanceName, {pathName = "Name"}) async { final r = await Process.run( - "powershell", ["(Get-CimInstance $win32InstanceName).$pathName"]); + powershellPath, ["(Get-CimInstance $win32InstanceName).$pathName"]); return r.stdout.toString().trim(); } static Future getSystemName() async { final r = await Process.run( - "powershell", ["(Get-ComputerInfo | Select-Object -expand OsName)"]); + powershellPath, ["(Get-ComputerInfo | Select-Object -expand OsName)"]); return r.stdout.toString().trim(); } static Future getCpuName() async { final r = await Process.run( - "powershell", ["(Get-WmiObject -Class Win32_Processor).Name"]); + powershellPath, ["(Get-WmiObject -Class Win32_Processor).Name"]); return r.stdout.toString().trim(); } @@ -163,12 +168,12 @@ foreach ($adapter in $adapterMemory) { } } """; - final r = await Process.run("powershell", [cmd]); + final r = await Process.run(powershellPath, [cmd]); return r.stdout.toString().trim(); } static Future getDiskInfo() async { - return (await Process.run("powershell", + return (await Process.run(powershellPath, ["Get-PhysicalDisk | format-table BusType,FriendlyName,Size"])) .stdout .toString() @@ -197,4 +202,40 @@ foreach ($adapter in $adapterMemory) { } catch (_) {} return totalSize; } + + static Future powershellAdminRun(List args) async { + // 创建 PowerShell 脚本文件 + final scriptContent = """ + ${args.join(' ')} + """; + + // 将脚本内容写入临时文件 + final scriptFile = + File('${AppConf.applicationSupportDir}\\temp\\psh_script.ps1'); + if (await scriptFile.exists()) { + await scriptFile.delete(); + } + if (!await scriptFile.exists()) { + await scriptFile.create(recursive: true); + } + await scriptFile.writeAsString(scriptContent); + + List command = [ + 'Start-Process', + 'powershell.exe', + '-Verb RunAs', + '-ArgumentList', + "'-NoProfile','-NonInteractive','-ExecutionPolicy','Bypass','-File','${scriptFile.absolute.path}'", + '-Wait', + '-PassThru ' + ]; + + final r = await Process.run( + 'powershell.exe', + command, + runInShell: true, + ); + await scriptFile.delete(); + return r; + } } diff --git a/lib/global_ui_model.dart b/lib/global_ui_model.dart index 9811ecc..980f71a 100644 --- a/lib/global_ui_model.dart +++ b/lib/global_ui_model.dart @@ -4,7 +4,6 @@ import 'dart:io'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:hive/hive.dart'; -import 'package:starcitizen_doctor/common/helper/log_helper.dart'; import 'api/api.dart'; import 'base/ui_model.dart'; @@ -58,13 +57,6 @@ class AppGlobalUIModel extends BaseUIModel { } else { if (!AppConf.isMSE) { await _runAsAdmin(); - } else { - final logPath = await SCLoggerHelper.getLogFilePath(); - if (logPath != null) { - if (await File(logPath).exists()) { - await _runAsAdmin(); - } - } } return false; } @@ -73,11 +65,8 @@ class AppGlobalUIModel extends BaseUIModel { _runAsAdmin() async { final box = await Hive.openBox("app_conf"); await box.close(); - await Process.run(SystemHelper.powershellPath, [ - """ - Start-Process powershell -Verb RunAs -ArgumentList "Start-Process '${Platform.resolvedExecutable}' -Verb RunAs" - """ - ]); + await Process.run(SystemHelper.powershellPath, + ["Start-Process '${Platform.resolvedExecutable}' -Verb RunAs"]); await Future.delayed(const Duration(seconds: 2)); exit(0); } diff --git a/lib/ui/home/dialogs/md_content_dialog_ui.dart b/lib/ui/home/dialogs/md_content_dialog_ui.dart index 0ad9c4e..367da4f 100644 --- a/lib/ui/home/dialogs/md_content_dialog_ui.dart +++ b/lib/ui/home/dialogs/md_content_dialog_ui.dart @@ -18,6 +18,7 @@ class MDContentDialogUI extends BaseUI { padding: const EdgeInsets.only(left: 12, right: 12), child: Column( mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.start, children: makeMarkdownView(model.data ?? ""), ), ), diff --git a/lib/ui/home/home_ui_model.dart b/lib/ui/home/home_ui_model.dart index 62350ac..1ac22fe 100644 --- a/lib/ui/home/home_ui_model.dart +++ b/lib/ui/home/home_ui_model.dart @@ -296,7 +296,8 @@ class HomeUIModel extends BaseUIModel { return; case "no_live_path": try { - await Directory(item.value).create(recursive: true); + SystemHelper.powershellAdminRun( + ['New-Item -ItemType Directory -Path "${item.value}" -Force']); showToast(context!, "创建文件夹成功,请尝试继续下载游戏!"); checkResult?.remove(item); notifyListeners(); @@ -325,8 +326,8 @@ class HomeUIModel extends BaseUIModel { final Map eacJson = json.decode(utf8.decode(eacJsonData)); final eacID = eacJson["productid"]; try { - var result = await Process.run( - "${item.value}\\EasyAntiCheat_EOS_Setup.exe", ["install", eacID]); + var result = await SystemHelper.powershellAdminRun( + ["${item.value}\\EasyAntiCheat_EOS_Setup.exe", "install", eacID]); dPrint("${item.value}\\EasyAntiCheat_EOS_Setup.exe install $eacID"); if (result.stderr == "") { showToast(context!, "修复成功,请尝试启动游戏。(若问题无法解决,请使用工具箱的 《重装 EAC》)"); diff --git a/lib/ui/settings/upgrade_dialog_ui_model.dart b/lib/ui/settings/upgrade_dialog_ui_model.dart index e14396d..dc95eb0 100644 --- a/lib/ui/settings/upgrade_dialog_ui_model.dart +++ b/lib/ui/settings/upgrade_dialog_ui_model.dart @@ -55,8 +55,8 @@ class UpgradeDialogUIModel extends BaseUIModel { } try { - final r = - await (Process.run("powershell", ["start", fileName, "/SILENT"])); + final r = await (Process.run( + SystemHelper.powershellPath, ["start", fileName, "/SILENT"])); if (r.stderr.toString().isNotEmpty) { throw r.stderr; } diff --git a/windows/corrosion b/windows/corrosion new file mode 160000 index 0000000..e9aaa32 --- /dev/null +++ b/windows/corrosion @@ -0,0 +1 @@ +Subproject commit e9aaa32e233dd9c96aed45b5a006e366a0a779d3 diff --git a/windows/rust.cmake b/windows/rust.cmake index 4931b35..77316f9 100644 --- a/windows/rust.cmake +++ b/windows/rust.cmake @@ -4,15 +4,7 @@ # Once done, uncomment this line: # find_package(Corrosion REQUIRED) -include(FetchContent) - -FetchContent_Declare( - Corrosion - GIT_REPOSITORY https://github.com/AndrewGaspar/corrosion.git - GIT_TAG origin/master # Optionally specify a version tag or branch here -) - -FetchContent_MakeAvailable(Corrosion) +add_subdirectory(./corrosion) corrosion_import_crate(MANIFEST_PATH ../rust/Cargo.toml IMPORTED_CRATES imported_crates) target_link_libraries(${BINARY_NAME} PRIVATE ${imported_crates})