mirror of
https://ghfast.top/https://github.com/StarCitizenToolBox/app.git
synced 2025-06-29 14:50:25 +08:00
feat:riverpod 迁移 BinaryModuleConf
This commit is contained in:
44
lib/common/conf/binary_conf.dart
Normal file
44
lib/common/conf/binary_conf.dart
Normal file
@ -0,0 +1,44 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:archive/archive.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:starcitizen_doctor/common/utils/log.dart';
|
||||
|
||||
class BinaryModuleConf {
|
||||
static const _modules = {
|
||||
"aria2c": "0",
|
||||
};
|
||||
|
||||
static Future extractModule(List<String> modules, String workingDir) async {
|
||||
for (var m in _modules.entries) {
|
||||
if (!modules.contains(m.key)) continue;
|
||||
final name = m.key;
|
||||
final version = m.value;
|
||||
final dir = "$workingDir\\$name";
|
||||
final versionFile = File("$dir\\version");
|
||||
if (await versionFile.exists() &&
|
||||
(await versionFile.readAsString()).trim() == version) {
|
||||
dPrint(
|
||||
"BinaryModuleConf.extractModule skip $name version == $version");
|
||||
continue;
|
||||
}
|
||||
// write model file
|
||||
final zipBuffer = await rootBundle.load("assets/binary/$name.zip");
|
||||
final decoder = ZipDecoder().decodeBytes(zipBuffer.buffer.asUint8List());
|
||||
for (var value in decoder.files) {
|
||||
final filename = value.name;
|
||||
if (value.isFile) {
|
||||
final data = value.content as List<int>;
|
||||
final file = File('$dir\\$filename');
|
||||
await file.create(recursive: true);
|
||||
await file.writeAsBytes(data);
|
||||
} else {
|
||||
await Directory('$dir\\$filename').create(recursive: true);
|
||||
}
|
||||
}
|
||||
// write version file
|
||||
await versionFile.writeAsString(version);
|
||||
dPrint("BinaryModuleConf.extractModule $name $dir");
|
||||
}
|
||||
}
|
||||
}
|
@ -7,157 +7,9 @@ import 'package:hive/hive.dart';
|
||||
import 'package:starcitizen_doctor/api/api.dart';
|
||||
import 'package:starcitizen_doctor/common/helper/system_helper.dart';
|
||||
|
||||
import 'package:starcitizen_doctor/common/rust/api/process_api.dart'
|
||||
as rs_process;
|
||||
|
||||
import 'package:starcitizen_doctor/common/utils/log.dart';
|
||||
|
||||
class Aria2cManager {
|
||||
static bool _isDaemonRunning = false;
|
||||
|
||||
// static final String _aria2cDir = "${AppConf.applicationBinaryModuleDir}\\aria2c";
|
||||
static final String _aria2cDir = "\\aria2c";
|
||||
|
||||
static Aria2c? _aria2c;
|
||||
|
||||
static Aria2c getClient() {
|
||||
if (_aria2c != null) return _aria2c!;
|
||||
throw "not connect!";
|
||||
}
|
||||
|
||||
static bool get isAvailable => _isDaemonRunning && _aria2c != null;
|
||||
|
||||
static Future checkLazyLoad() async {
|
||||
try {
|
||||
final sessionFile = File("$_aria2cDir\\aria2.session");
|
||||
// 有下载任务则第一时间初始化
|
||||
if (await sessionFile.exists() &&
|
||||
(await sessionFile.readAsString()).trim().isNotEmpty) {
|
||||
await launchDaemon();
|
||||
}
|
||||
} catch (e) {
|
||||
dPrint("Aria2cManager.checkLazyLoad Error:$e");
|
||||
}
|
||||
}
|
||||
|
||||
static Future launchDaemon() async {
|
||||
if (_isDaemonRunning) return;
|
||||
// await BinaryModuleConf.extractModule(["aria2c"]);
|
||||
|
||||
/// skip for debug hot reload
|
||||
if (kDebugMode) {
|
||||
if ((await SystemHelper.getPID("aria2c")).isNotEmpty) {
|
||||
dPrint("[Aria2cManager] debug skip for hot reload");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
final sessionFile = File("$_aria2cDir\\aria2.session");
|
||||
if (!await sessionFile.exists()) {
|
||||
await sessionFile.create(recursive: true);
|
||||
}
|
||||
|
||||
final exePath = "$_aria2cDir\\aria2c.exe";
|
||||
final port = await getFreePort();
|
||||
final pwd = generateRandomPassword(16);
|
||||
dPrint("pwd === $pwd");
|
||||
final trackerList = await Api.getTorrentTrackerList();
|
||||
dPrint("trackerList === $trackerList");
|
||||
dPrint("Aria2cManager .----- aria2c start $port------");
|
||||
|
||||
final stream = rs_process.startProcess(
|
||||
executable: exePath,
|
||||
arguments: [
|
||||
"-V",
|
||||
"-c",
|
||||
"-x 10",
|
||||
"--dir=$_aria2cDir\\downloads",
|
||||
"--disable-ipv6",
|
||||
"--enable-rpc",
|
||||
"--pause",
|
||||
"--rpc-listen-port=$port",
|
||||
"--rpc-secret=$pwd",
|
||||
"--input-file=${sessionFile.absolute.path.trim()}",
|
||||
"--save-session=${sessionFile.absolute.path.trim()}",
|
||||
"--save-session-interval=60",
|
||||
"--file-allocation=trunc",
|
||||
"--seed-time=0",
|
||||
],
|
||||
workingDirectory: _aria2cDir);
|
||||
|
||||
String launchError = "";
|
||||
|
||||
stream.listen((event) {
|
||||
dPrint("Aria2cManager.rs_process event === $event");
|
||||
if (event.startsWith("output:")) {
|
||||
if (event.contains("IPv4 RPC: listening on TCP port")) {
|
||||
_onLaunch(port, pwd, trackerList);
|
||||
}
|
||||
} else if (event.startsWith("error:")) {
|
||||
_isDaemonRunning = false;
|
||||
_aria2c = null;
|
||||
launchError = event;
|
||||
} else if (event.startsWith("exit:")) {
|
||||
_isDaemonRunning = false;
|
||||
_aria2c = null;
|
||||
launchError = event;
|
||||
}
|
||||
});
|
||||
|
||||
while (true) {
|
||||
if (_isDaemonRunning) return;
|
||||
if (launchError.isNotEmpty) throw launchError;
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
}
|
||||
}
|
||||
|
||||
static Future<int> getFreePort() async {
|
||||
final serverSocket = await ServerSocket.bind("127.0.0.1", 0);
|
||||
final port = serverSocket.port;
|
||||
await serverSocket.close();
|
||||
return port;
|
||||
}
|
||||
|
||||
static String generateRandomPassword(int length) {
|
||||
const String charset =
|
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
Random random = Random();
|
||||
StringBuffer buffer = StringBuffer();
|
||||
for (int i = 0; i < length; i++) {
|
||||
int randomIndex = random.nextInt(charset.length);
|
||||
buffer.write(charset[randomIndex]);
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
static int textToByte(String text) {
|
||||
if (text.length == 1) {
|
||||
return 0;
|
||||
}
|
||||
if (int.tryParse(text) != null) {
|
||||
return int.parse(text);
|
||||
}
|
||||
if (text.endsWith("k")) {
|
||||
return int.parse(text.substring(0, text.length - 1)) * 1024;
|
||||
}
|
||||
if (text.endsWith("m")) {
|
||||
return int.parse(text.substring(0, text.length - 1)) * 1024 * 1024;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static Future<void> _onLaunch(
|
||||
int port, String pwd, String trackerList) async {
|
||||
_isDaemonRunning = true;
|
||||
_aria2c = Aria2c("ws://127.0.0.1:$port/jsonrpc", "websocket", pwd);
|
||||
_aria2c!.getVersion().then((value) {
|
||||
dPrint("Aria2cManager.connected! version == ${value.version}");
|
||||
});
|
||||
final box = await Hive.openBox("app_conf");
|
||||
_aria2c!.changeGlobalOption(Aria2Option()
|
||||
..maxOverallUploadLimit =
|
||||
textToByte(box.get("downloader_up_limit", defaultValue: "0"))
|
||||
..maxOverallDownloadLimit =
|
||||
textToByte(box.get("downloader_down_limit", defaultValue: "0"))
|
||||
..btTracker = trackerList);
|
||||
}
|
||||
}
|
||||
|
9
lib/common/utils/provider.dart
Normal file
9
lib/common/utils/provider.dart
Normal file
@ -0,0 +1,9 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:starcitizen_doctor/app.dart';
|
||||
|
||||
extension ProviderExtension on AutoDisposeNotifier {
|
||||
AppGlobalModel get appGlobalModel =>
|
||||
ref.read(appGlobalModelProvider.notifier);
|
||||
|
||||
AppGlobalState get appGlobalState => ref.read(appGlobalModelProvider);
|
||||
}
|
Reference in New Issue
Block a user