app/lib/ui/tools/tools_ui_model.dart

569 lines
20 KiB
Dart
Raw Normal View History

2024-03-10 18:00:46 +08:00
// ignore_for_file: avoid_build_context_in_providers
import 'dart:convert';
import 'dart:io';
import 'package:file_picker/file_picker.dart';
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter/foundation.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:go_router/go_router.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:starcitizen_doctor/api/analytics.dart';
import 'package:starcitizen_doctor/api/api.dart';
import 'package:starcitizen_doctor/common/helper/log_helper.dart';
import 'package:starcitizen_doctor/common/helper/system_helper.dart';
import 'package:starcitizen_doctor/common/io/rs_http.dart';
import 'package:starcitizen_doctor/common/utils/log.dart';
import 'package:starcitizen_doctor/common/utils/provider.dart';
import 'package:starcitizen_doctor/provider/aria2c.dart';
import 'package:starcitizen_doctor/ui/home/downloader/home_downloader_ui_model.dart';
import 'package:starcitizen_doctor/widgets/widgets.dart';
import 'package:url_launcher/url_launcher_string.dart';
import 'package:xml/xml.dart';
import 'dialogs/hosts_booster_dialog_ui.dart';
2024-05-01 13:48:37 +08:00
import 'dialogs/rsi_launcher_enhance_dialog_ui.dart';
2024-03-10 18:00:46 +08:00
part 'tools_ui_model.g.dart';
part 'tools_ui_model.freezed.dart';
class ToolsItemData {
String key;
ToolsItemData(this.key, this.name, this.infoString, this.icon, {this.onTap});
String name;
String infoString;
Widget icon;
AsyncCallback? onTap;
}
@freezed
class ToolsUIState with _$ToolsUIState {
2024-03-15 00:01:06 +08:00
factory ToolsUIState({
2024-03-10 18:00:46 +08:00
@Default(false) bool working,
@Default("") String scInstalledPath,
@Default("") String rsiLauncherInstalledPath,
@Default([]) List<String> scInstallPaths,
@Default([]) List<String> rsiLauncherInstallPaths,
@Default([]) List<ToolsItemData> items,
@Default(false) bool isItemLoading,
}) = _ToolsUIState;
}
@riverpod
class ToolsUIModel extends _$ToolsUIModel {
@override
ToolsUIState build() {
2024-03-15 00:01:06 +08:00
state = ToolsUIState();
2024-03-10 18:00:46 +08:00
return state;
}
loadToolsCard(BuildContext context, {bool skipPathScan = false}) async {
if (state.isItemLoading) return;
var items = <ToolsItemData>[];
state = state.copyWith(items: items, isItemLoading: true);
if (!skipPathScan) {
await reScanPath(context);
}
try {
items = [
ToolsItemData(
"systemnfo",
2024-03-15 00:01:06 +08:00
S.current.tools_action_view_system_info,
2024-03-16 19:13:49 +08:00
S.current.tools_action_info_view_critical_system_info,
2024-03-13 22:40:28 +08:00
const Icon(FluentIcons.system, size: 24),
2024-03-10 18:00:46 +08:00
onTap: () => _showSystemInfo(context),
),
ToolsItemData(
"p4k_downloader",
2024-03-15 00:01:06 +08:00
S.current.tools_action_p4k_download_repair,
2024-03-16 19:13:49 +08:00
S.current.tools_action_info_p4k_download_repair_tip,
2024-03-13 22:40:28 +08:00
const Icon(FontAwesomeIcons.download, size: 24),
2024-03-10 18:00:46 +08:00
onTap: () => _downloadP4k(context),
),
ToolsItemData(
"hosts_booster",
2024-03-15 00:01:06 +08:00
S.current.tools_action_hosts_acceleration_experimental,
2024-03-16 19:13:49 +08:00
S.current.tools_action_info_hosts_acceleration_experimental_tip,
2024-03-13 22:40:28 +08:00
const Icon(FluentIcons.virtual_network, size: 24),
onTap: () => _doHostsBooster(context),
),
2024-05-01 13:48:37 +08:00
ToolsItemData(
"rsilauncher_enhance_mod",
2024-05-05 16:34:38 +08:00
S.current.tools_rsi_launcher_enhance_title,
S.current.tools_action_rsi_launcher_enhance_info,
2024-05-01 13:48:37 +08:00
const Icon(FluentIcons.c_plus_plus, size: 24),
2024-05-03 14:39:46 +08:00
onTap: () => rsiEnhance(context),
2024-05-01 13:48:37 +08:00
),
2024-03-10 18:00:46 +08:00
ToolsItemData(
"reinstall_eac",
2024-03-15 00:01:06 +08:00
S.current.tools_action_reinstall_easyanticheat,
S.current.tools_action_info_reinstall_eac,
2024-03-13 22:40:28 +08:00
const Icon(FluentIcons.game, size: 24),
2024-03-10 18:00:46 +08:00
onTap: () => _reinstallEAC(context),
),
ToolsItemData(
"rsilauncher_admin_mode",
2024-03-15 00:01:06 +08:00
S.current.tools_action_rsi_launcher_admin_mode,
2024-03-16 19:13:49 +08:00
S.current.tools_action_info_run_rsi_as_admin,
2024-03-13 22:40:28 +08:00
const Icon(FluentIcons.admin, size: 24),
2024-03-10 18:00:46 +08:00
onTap: () => _adminRSILauncher(context),
),
2024-04-14 19:52:42 +08:00
ToolsItemData(
"unp4kc",
2024-05-05 18:52:04 +08:00
S.current.tools_action_unp4k,
S.current.tools_action_unp4k_info,
2024-04-14 19:52:42 +08:00
const Icon(FontAwesomeIcons.fileZipper, size: 24),
onTap: () => _unp4kc(context),
),
2024-03-10 18:00:46 +08:00
];
state = state.copyWith(items: items);
if (!context.mounted) return;
items.add(await _addShaderCard(context));
state = state.copyWith(items: items);
if (!context.mounted) return;
items.add(await _addPhotographyCard(context));
state = state.copyWith(items: items);
if (!context.mounted) return;
items.addAll(await _addNvmePatchCard(context));
state = state.copyWith(items: items, isItemLoading: false);
} catch (e) {
if (!context.mounted) return;
2024-03-16 19:13:49 +08:00
showToast(context, S.current.tools_action_info_init_failed(e));
2024-03-10 18:00:46 +08:00
}
}
Future<List<ToolsItemData>> _addNvmePatchCard(BuildContext context) async {
final nvmePatchStatus = await SystemHelper.checkNvmePatchStatus();
return [
if (nvmePatchStatus)
ToolsItemData(
"remove_nvme_settings",
2024-03-15 00:01:06 +08:00
S.current.tools_action_remove_nvme_registry_patch,
2024-03-16 19:13:49 +08:00
S.current.tools_action_info_nvme_patch_issue(nvmePatchStatus
? S.current.localization_info_installed
: S.current.tools_action_info_not_installed),
2024-03-13 22:40:28 +08:00
const Icon(FluentIcons.hard_drive, size: 24),
2024-03-10 18:00:46 +08:00
onTap: nvmePatchStatus
? () async {
state = state.copyWith(working: true);
await SystemHelper.doRemoveNvmePath();
state = state.copyWith(working: false);
if (!context.mounted) return;
2024-03-16 19:13:49 +08:00
showToast(context,
S.current.tools_action_info_removed_restart_effective);
2024-03-10 18:00:46 +08:00
loadToolsCard(context, skipPathScan: true);
}
: null,
),
if (!nvmePatchStatus)
ToolsItemData(
"add_nvme_settings",
2024-03-15 00:01:06 +08:00
S.current.tools_action_write_nvme_registry_patch,
S.current.tools_action_info_manual_nvme_patch,
2024-03-13 22:40:28 +08:00
const Icon(FontAwesomeIcons.cashRegister, size: 24),
2024-03-10 18:00:46 +08:00
onTap: () async {
state = state.copyWith(working: true);
final r = await SystemHelper.addNvmePatch();
if (r == "") {
if (!context.mounted) return;
2024-03-16 19:13:49 +08:00
showToast(
context, S.current.tools_action_info_fix_success_restart);
2024-03-10 18:00:46 +08:00
} else {
if (!context.mounted) return;
2024-03-16 19:13:49 +08:00
showToast(context, S.current.doctor_action_result_fix_fail(r));
2024-03-10 18:00:46 +08:00
}
state = state.copyWith(working: false);
loadToolsCard(context, skipPathScan: true);
},
)
];
}
Future<ToolsItemData> _addShaderCard(BuildContext context) async {
final gameShaderCachePath = await SCLoggerHelper.getShaderCachePath();
2024-03-16 19:13:49 +08:00
final shaderSize = ((await SystemHelper.getDirLen(gameShaderCachePath ?? "",
skipPath: ["$gameShaderCachePath\\Crashes"])) /
1024 /
1024)
.toStringAsFixed(4);
2024-03-10 18:00:46 +08:00
return ToolsItemData(
"clean_shaders",
2024-03-15 00:01:06 +08:00
S.current.tools_action_clear_shader_cache,
2024-03-16 19:13:49 +08:00
S.current.tools_action_info_shader_cache_issue(shaderSize),
2024-03-13 22:40:28 +08:00
const Icon(FontAwesomeIcons.shapes, size: 24),
2024-03-10 18:00:46 +08:00
onTap: () => _cleanShaderCache(context),
);
}
Future<ToolsItemData> _addPhotographyCard(BuildContext context) async {
// 获取配置文件状态
final isEnable = await _checkPhotographyStatus(context);
return ToolsItemData(
"photography_mode",
isEnable
2024-03-16 19:13:49 +08:00
? S.current.tools_action_close_photography_mode
: S.current.tools_action_open_photography_mode,
isEnable
? S.current.tools_action_info_restore_lens_shake
: S.current.tools_action_info_one_key_close_lens_shake,
2024-03-13 22:40:28 +08:00
const Icon(FontAwesomeIcons.camera, size: 24),
2024-03-10 18:00:46 +08:00
onTap: () => _onChangePhotographyMode(context, isEnable),
);
}
/// ---------------------------- func -------------------------------------------------------
/// -----------------------------------------------------------------------------------------
/// -----------------------------------------------------------------------------------------
/// -----------------------------------------------------------------------------------------
Future<void> reScanPath(BuildContext context) async {
var scInstallPaths = <String>[];
var rsiLauncherInstallPaths = <String>[];
var scInstalledPath = "";
var rsiLauncherInstalledPath = "";
state = state.copyWith(
scInstalledPath: scInstalledPath,
rsiLauncherInstalledPath: rsiLauncherInstalledPath,
scInstallPaths: scInstallPaths,
rsiLauncherInstallPaths: rsiLauncherInstallPaths,
);
try {
rsiLauncherInstalledPath = await SystemHelper.getRSILauncherPath();
rsiLauncherInstallPaths.add(rsiLauncherInstalledPath);
final listData = await SCLoggerHelper.getLauncherLogList();
if (listData == null) {
return;
}
scInstallPaths = await SCLoggerHelper.getGameInstallPath(listData,
checkExists: false, withVersion: ["LIVE", "PTU", "EPTU"]);
if (scInstallPaths.isNotEmpty) {
scInstalledPath = scInstallPaths.first;
}
state = state.copyWith(
scInstalledPath: scInstalledPath,
rsiLauncherInstalledPath: rsiLauncherInstalledPath,
scInstallPaths: scInstallPaths,
rsiLauncherInstallPaths: rsiLauncherInstallPaths,
);
} catch (e) {
dPrint(e);
if (!context.mounted) return;
2024-03-16 19:13:49 +08:00
showToast(context, S.current.tools_action_info_log_file_parse_failed);
2024-03-10 18:00:46 +08:00
}
if (rsiLauncherInstalledPath == "") {
if (!context.mounted) return;
2024-03-15 00:01:06 +08:00
showToast(context, S.current.tools_action_info_rsi_launcher_not_found);
2024-03-10 18:00:46 +08:00
}
if (scInstalledPath == "") {
if (!context.mounted) return;
2024-03-15 00:01:06 +08:00
showToast(context, S.current.tools_action_info_star_citizen_not_found);
2024-03-10 18:00:46 +08:00
}
}
/// 重装EAC
Future<void> _reinstallEAC(BuildContext context) async {
if (state.scInstalledPath.isEmpty) {
2024-03-16 19:13:49 +08:00
showToast(
context, S.current.tools_action_info_valid_game_directory_needed);
2024-03-10 18:00:46 +08:00
return;
}
state = state.copyWith(working: true);
try {
final eacPath = "${state.scInstalledPath}\\EasyAntiCheat";
final eacJsonPath = "$eacPath\\Settings.json";
if (await File(eacJsonPath).exists()) {
Map<String, String> envVars = Platform.environment;
final eacJsonData = await File(eacJsonPath).readAsString();
final Map eacJson = json.decode(eacJsonData);
final eacID = eacJson["productid"];
if (eacID != null) {
final eacCacheDir =
Directory("${envVars["appdata"]}\\EasyAntiCheat\\$eacID");
if (await eacCacheDir.exists()) {
await eacCacheDir.delete(recursive: true);
}
}
}
final dir = Directory(eacPath);
if (await dir.exists()) {
await dir.delete(recursive: true);
}
final eacLauncher =
File("${state.scInstalledPath}\\StarCitizen_Launcher.exe");
if (await eacLauncher.exists()) {
await eacLauncher.delete(recursive: true);
}
if (!context.mounted) return;
2024-03-16 19:13:49 +08:00
showToast(context, S.current.tools_action_info_eac_file_removed);
2024-03-10 18:00:46 +08:00
_adminRSILauncher(context);
} catch (e) {
2024-03-16 19:13:49 +08:00
showToast(context, S.current.tools_action_info_error_occurred(e));
2024-03-10 18:00:46 +08:00
}
state = state.copyWith(working: false);
loadToolsCard(context, skipPathScan: true);
}
Future<String> getSystemInfo() async {
2024-03-16 19:13:49 +08:00
return S.current.tools_action_info_system_info_content(
await SystemHelper.getSystemName(),
await SystemHelper.getCpuName(),
await SystemHelper.getSystemMemorySizeGB(),
await SystemHelper.getGpuInfo(),
await SystemHelper.getDiskInfo());
2024-03-10 18:00:46 +08:00
}
/// 管理员模式运行 RSI 启动器
Future _adminRSILauncher(BuildContext context) async {
if (state.rsiLauncherInstalledPath == "") {
2024-03-16 19:13:49 +08:00
showToast(context,
S.current.tools_action_info_rsi_launcher_directory_not_found);
2024-03-10 18:00:46 +08:00
}
SystemHelper.checkAndLaunchRSILauncher(state.rsiLauncherInstalledPath);
}
openDir(path) async {
2024-04-12 20:02:00 +08:00
SystemHelper.openDir(path);
2024-03-10 18:00:46 +08:00
}
Future _showSystemInfo(BuildContext context) async {
state = state.copyWith(working: true);
final systemInfo = await getSystemInfo();
if (!context.mounted) return;
showDialog<String>(
context: context,
builder: (context) => ContentDialog(
2024-03-15 00:01:06 +08:00
title: Text(S.current.tools_action_info_system_info_title),
2024-03-10 18:00:46 +08:00
content: Text(systemInfo),
constraints: BoxConstraints(
maxWidth: MediaQuery.of(context).size.width * .65,
),
actions: [
FilledButton(
2024-03-15 00:01:06 +08:00
child: Padding(
2024-03-16 19:13:49 +08:00
padding:
const EdgeInsets.only(top: 2, bottom: 2, left: 8, right: 8),
2024-03-15 00:01:06 +08:00
child: Text(S.current.action_close),
2024-03-10 18:00:46 +08:00
),
onPressed: () => Navigator.pop(context),
),
],
),
);
state = state.copyWith(working: false);
}
Future<void> _cleanShaderCache(BuildContext context) async {
state = state.copyWith(working: true);
final gameShaderCachePath = await SCLoggerHelper.getShaderCachePath();
final l =
await Directory(gameShaderCachePath!).list(recursive: false).toList();
for (var value in l) {
if (value is Directory) {
if (!value.absolute.path.contains("Crashes")) {
await value.delete(recursive: true);
}
}
}
if (!context.mounted) return;
loadToolsCard(context, skipPathScan: true);
state = state.copyWith(working: false);
}
Future<void> _downloadP4k(BuildContext context) async {
String savePath = state.scInstalledPath;
String fileName = "Data.p4k";
if ((await SystemHelper.getPID("\"RSI Launcher\"")).isNotEmpty) {
if (!context.mounted) return;
2024-03-16 19:13:49 +08:00
showToast(
context, S.current.tools_action_info_rsi_launcher_running_warning,
2024-03-10 18:00:46 +08:00
constraints: BoxConstraints(
maxWidth: MediaQuery.of(context).size.width * .35));
return;
}
if (!context.mounted) return;
2024-03-16 19:13:49 +08:00
await showToast(context, S.current.tools_action_info_p4k_file_description);
2024-03-10 18:00:46 +08:00
try {
state = state.copyWith(working: true);
final aria2cManager = ref.read(aria2cModelProvider.notifier);
await aria2cManager
.launchDaemon(appGlobalState.applicationBinaryModuleDir!);
final aria2c = ref.read(aria2cModelProvider).aria2c!;
// check download task list
for (var value in [
...await aria2c.tellActive(),
...await aria2c.tellWaiting(0, 100000)
]) {
final t = HomeDownloaderUIModel.getTaskTypeAndName(value);
if (t.key == "torrent" && t.value.contains("Data.p4k")) {
if (!context.mounted) return;
2024-03-16 19:13:49 +08:00
showToast(
context, S.current.tools_action_info_p4k_download_in_progress);
2024-03-10 18:00:46 +08:00
state = state.copyWith(working: false);
return;
}
}
var torrentUrl = "";
final l = await Api.getAppTorrentDataList();
for (var torrent in l) {
if (torrent.name == "Data.p4k") {
torrentUrl = torrent.url!;
}
}
if (torrentUrl == "") {
state = state.copyWith(working: false);
if (!context.mounted) return;
2024-03-16 19:13:49 +08:00
showToast(
context, S.current.tools_action_info_function_under_maintenance);
2024-03-10 18:00:46 +08:00
return;
}
final userSelect = await FilePicker.platform.saveFile(
initialDirectory: savePath,
fileName: fileName,
lockParentWindow: true);
if (userSelect == null) {
state = state.copyWith(working: false);
return;
}
savePath = userSelect;
dPrint(savePath);
if (savePath.endsWith("\\$fileName")) {
savePath = savePath.substring(0, savePath.length - fileName.length - 1);
}
if (!context.mounted) return;
final btData = await RSHttp.get(torrentUrl).unwrap(context: context);
if (btData == null || btData.data == null) {
state = state.copyWith(working: false);
return;
}
final b64Str = base64Encode(btData.data!);
final gid =
await aria2c.addTorrent(b64Str, extraParams: {"dir": savePath});
state = state.copyWith(working: false);
dPrint("Aria2cManager.aria2c.addUri resp === $gid");
await aria2c.saveSession();
AnalyticsApi.touch("p4k_download");
if (!context.mounted) return;
context.push("/index/downloader");
} catch (e) {
state = state.copyWith(working: false);
if (!context.mounted) return;
2024-03-16 19:13:49 +08:00
showToast(context, S.current.app_init_failed_with_reason(e));
2024-03-10 18:00:46 +08:00
}
await Future.delayed(const Duration(seconds: 3));
launchUrlString(
"https://citizenwiki.cn/SC%E6%B1%89%E5%8C%96%E7%9B%92%E5%AD%90#%E5%88%86%E6%B5%81%E4%B8%8B%E8%BD%BD%E6%95%99%E7%A8%8B");
}
Future<bool> _checkPhotographyStatus(BuildContext context,
{bool? setMode}) async {
final scInstalledPath = state.scInstalledPath;
2024-03-16 19:13:49 +08:00
final keys = ["AudioShakeStrength", "CameraSpringMovement", "ShakeScale"];
2024-03-10 18:00:46 +08:00
final attributesFile = File(
"$scInstalledPath\\USER\\Client\\0\\Profiles\\default\\attributes.xml");
if (setMode == null) {
bool isEnable = false;
if (scInstalledPath.isNotEmpty) {
if (await attributesFile.exists()) {
final xmlFile =
XmlDocument.parse(await attributesFile.readAsString());
isEnable = true;
for (var k in keys) {
if (!isEnable) break;
final e = xmlFile.rootElement.children
.where((element) => element.getAttribute("name") == k)
.firstOrNull;
if (e != null && e.getAttribute("value") == "0") {
} else {
isEnable = false;
}
}
}
}
return isEnable;
} else {
if (!await attributesFile.exists()) {
if (!context.mounted) return false;
2024-03-15 00:01:06 +08:00
showToast(context, S.current.tools_action_info_config_file_not_exist);
2024-03-10 18:00:46 +08:00
return false;
}
final xmlFile = XmlDocument.parse(await attributesFile.readAsString());
// clear all
xmlFile.rootElement.children.removeWhere(
(element) => keys.contains(element.getAttribute("name")));
if (setMode) {
for (var element in keys) {
XmlElement newNode = XmlElement(XmlName('Attr'), [
XmlAttribute(XmlName('name'), element),
XmlAttribute(XmlName('value'), '0'),
]);
xmlFile.rootElement.children.add(newNode);
}
}
dPrint(xmlFile);
await attributesFile.delete();
await attributesFile.writeAsString(xmlFile.toXmlString(pretty: true));
}
return true;
}
_onChangePhotographyMode(BuildContext context, bool isEnable) async {
_checkPhotographyStatus(context, setMode: !isEnable)
.unwrap(context: context);
loadToolsCard(context, skipPathScan: true);
}
void onChangeGamePath(String v) {
state = state.copyWith(scInstalledPath: v);
}
void onChangeLauncherPath(String s) {
state = state.copyWith(rsiLauncherInstalledPath: s);
}
_doHostsBooster(BuildContext context) async {
showDialog(
context: context,
builder: (BuildContext context) => const HostsBoosterDialogUI());
}
2024-04-14 19:52:42 +08:00
_unp4kc(BuildContext context) async {
context.push("/tools/unp4kc");
}
2024-05-01 13:48:37 +08:00
2024-07-07 17:49:15 +08:00
static rsiEnhance(BuildContext context,
{bool showNotGameInstallMsg = false}) async {
2024-05-01 14:30:47 +08:00
if ((await SystemHelper.getPID("\"RSI Launcher\"")).isNotEmpty) {
if (!context.mounted) return;
showToast(
context, S.current.tools_action_info_rsi_launcher_running_warning,
constraints: BoxConstraints(
maxWidth: MediaQuery.of(context).size.width * .35));
return;
}
if (!context.mounted) return;
2024-05-01 13:48:37 +08:00
showDialog(
context: context,
2024-07-07 17:49:15 +08:00
builder: (BuildContext context) => RsiLauncherEnhanceDialogUI(
showNotGameInstallMsg: showNotGameInstallMsg,
));
2024-05-01 13:48:37 +08:00
}
2024-05-05 18:52:04 +08:00
}