使用新的方法申请UAC

This commit is contained in:
xkeyC 2023-11-06 22:23:06 +08:00
parent 1f68ad8ded
commit 403840ef12
7 changed files with 65 additions and 7 deletions

View File

@ -6,8 +6,10 @@ import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart'; import 'package:path_provider/path_provider.dart';
import 'package:starcitizen_doctor/api/analytics.dart'; import 'package:starcitizen_doctor/api/analytics.dart';
import 'package:starcitizen_doctor/api/api.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/common/rust/ffi.dart';
import 'package:starcitizen_doctor/data/app_version_data.dart'; import 'package:starcitizen_doctor/data/app_version_data.dart';
import 'package:starcitizen_doctor/global_ui_model.dart';
import 'package:uuid/uuid.dart'; import 'package:uuid/uuid.dart';
import 'package:window_manager/window_manager.dart'; import 'package:window_manager/window_manager.dart';
@ -37,12 +39,18 @@ class AppConf {
static bool offlineMode = false; static bool offlineMode = false;
static bool isRunningAdmin = false;
static late final WindowsDeviceInfo windowsDeviceInfo; static late final WindowsDeviceInfo windowsDeviceInfo;
static const isMSE = static const isMSE =
String.fromEnvironment("MSE", defaultValue: "false") == "true"; String.fromEnvironment("MSE", defaultValue: "false") == "true";
static init() async { static final launchHelperPath =
"${AppConf.applicationSupportDir}\\launch_helper.vbs";
static init(List<String> args) async {
dPrint("launch args == $args");
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
/// init device info /// init device info
@ -72,6 +80,7 @@ class AppConf {
exit(1); exit(1);
} }
dPrint("---- rust bridge inited -----"); dPrint("---- rust bridge inited -----");
isRunningAdmin = await globalUIModel.checkAdmin();
/// init windows /// init windows
await windowManager.ensureInitialized(); await windowManager.ensureInitialized();

View File

@ -1,5 +1,6 @@
import 'dart:io'; import 'dart:io';
import 'package:starcitizen_doctor/common/conf.dart';
import 'package:starcitizen_doctor/common/utils/base_utils.dart'; import 'package:starcitizen_doctor/common/utils/base_utils.dart';
class SystemHelper { class SystemHelper {
@ -179,4 +180,24 @@ foreach ($adapter in $adapterMemory) {
} catch (_) {} } catch (_) {}
return totalSize; return totalSize;
} }
static initVBS() async {
final script = '''
If Not WScript.Arguments.Named.Exists("elevate") Then
CreateObject("Shell.Application").ShellExecute WScript.FullName _
, """" & WScript.ScriptFullName & """ /elevate", "", "runas", 1
WScript.Quit
End If
Set WshShell = WScript.CreateObject("WScript.Shell")
exeName = """${Platform.resolvedExecutable}"""
statusCode = WshShell.Run(exeName, 1, true)
''';
final vbsFile = File(AppConf.launchHelperPath);
if (await vbsFile.exists()) {
await vbsFile.delete();
}
await vbsFile.create();
await vbsFile.writeAsString(script);
}
} }

View File

@ -1,10 +1,14 @@
// ignore_for_file: use_build_context_synchronously // ignore_for_file: use_build_context_synchronously
import 'dart:io';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:starcitizen_doctor/common/helper/log_helper.dart';
import 'api/api.dart'; import 'api/api.dart';
import 'base/ui_model.dart'; import 'base/ui_model.dart';
import 'common/conf.dart'; import 'common/conf.dart';
import 'common/helper/system_helper.dart';
import 'ui/settings/upgrade_dialog_ui.dart'; import 'ui/settings/upgrade_dialog_ui.dart';
import 'ui/settings/upgrade_dialog_ui_model.dart'; import 'ui/settings/upgrade_dialog_ui_model.dart';
@ -42,4 +46,32 @@ class AppGlobalUIModel extends BaseUIModel {
} }
return false; return false;
} }
Future<bool> 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]);
dPrint("code == ${r.exitCode} msg == ${r.stdout} err = ${r.stderr}");
if (r.exitCode == 0) {
return true;
} else {
if (!AppConf.isMSE) {
await _runAsAdmin();
} else {
final logPath = await SCLoggerHelper.getLogFilePath();
if (logPath != null) {
if (await File(logPath).exists()) {
await _runAsAdmin();
}
}
}
return false;
}
}
_runAsAdmin() async {
await SystemHelper.initVBS();
await Process.run("powershell.exe", [AppConf.launchHelperPath]);
exit(0);
}
} }

View File

@ -14,7 +14,7 @@ void main(List<String> args) async {
builder: _defaultWebviewTitleBar)) { builder: _defaultWebviewTitleBar)) {
return; return;
} }
await AppConf.init(); await AppConf.init(args);
runApp(ProviderScope( runApp(ProviderScope(
child: BaseUIContainer( child: BaseUIContainer(
uiCreate: () => AppUI(), uiCreate: () => AppUI(),

View File

@ -172,7 +172,6 @@ class HomeUIModel extends BaseUIModel {
checkResult = []; checkResult = [];
await _checkPreInstall(); await _checkPreInstall();
await _checkEAC(); await _checkEAC();
// TODO for debug // TODO for debug
// checkResult?.add(const MapEntry("unSupport_system", "android")); // checkResult?.add(const MapEntry("unSupport_system", "android"));
// checkResult?.add(const MapEntry("nvme_PhysicalBytes", "c")); // checkResult?.add(const MapEntry("nvme_PhysicalBytes", "c"));

View File

@ -127,7 +127,7 @@ msix_config:
publisher: CN=B54C897B-C263-4680-B6AB-4913C603DF87 publisher: CN=B54C897B-C263-4680-B6AB-4913C603DF87
msix_version: 2.9.7.0 msix_version: 2.9.7.0
logo_path: ./assets/app_logo.png logo_path: ./assets/app_logo.png
capabilities: internetClient,allowElevation capabilities: internetClient
languages: zh-cn languages: zh-cn
windows_build_args: --dart-define="MSE=true" windows_build_args: --dart-define="MSE=true"
# store: true # store: true

View File

@ -16,9 +16,6 @@ add_executable(${BINARY_NAME} WIN32
"runner.exe.manifest" "runner.exe.manifest"
) )
SET_TARGET_PROPERTIES(${BINARY_NAME} PROPERTIES LINK_FLAGS "/MANIFESTUAC:\"level='requireAdministrator' uiAccess='false'\" /SUBSYSTEM:WINDOWS")
# Apply the standard set of build settings. This can be removed for applications # Apply the standard set of build settings. This can be removed for applications
# that need different build settings. # that need different build settings.
apply_standard_settings(${BINARY_NAME}) apply_standard_settings(${BINARY_NAME})