移除 rust 多线程下载器

This commit is contained in:
2024-02-22 20:38:35 +08:00
parent 02101db99d
commit 46808e96c5
15 changed files with 78 additions and 1015 deletions

View File

@ -1,68 +0,0 @@
import 'package:file_sizes/file_sizes.dart';
import 'package:starcitizen_doctor/base/ui.dart';
import 'downloader_dialog_ui_model.dart';
class DownloaderDialogUI extends BaseUI<DownloaderDialogUIModel> {
@override
Widget? buildBody(BuildContext context, DownloaderDialogUIModel model) {
return ContentDialog(
constraints:
BoxConstraints(maxWidth: MediaQuery.of(context).size.width * .54),
title: const Text("文件下载..."),
content: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text("文件名:${model.fileName}"),
const SizedBox(height: 6),
Text("保存位置:${model.savePath}"),
const SizedBox(height: 6),
Text("线程数:${model.threadCount}"),
const SizedBox(height: 6),
Text(
"文件大小: ${FileSize.getSize(model.count ?? 0)} / ${FileSize.getSize(model.total ?? 0)}"),
const SizedBox(height: 6),
Text("下载速度: ${FileSize.getSize(model.speed?.toInt() ?? 0)}/s"),
const SizedBox(height: 12),
Row(
children: [
Text(getStatus(model)),
const SizedBox(width: 24),
Expanded(
child: ProgressBar(
value: model.progress == 100 ? null : model.progress,
)),
const SizedBox(width: 24),
],
),
if (model.isP4kDownload) ...[
const SizedBox(height: 24),
Text(
"提示因网络波动若下载进度长时间卡住或速度变慢可尝试点击暂停下载之后重新点击P4K分流下载。",
style:
TextStyle(fontSize: 13, color: Colors.white.withOpacity(.7)),
),
],
],
),
actions: [
FilledButton(
child: const Padding(
padding: EdgeInsets.only(left: 8, right: 8, top: 2, bottom: 2),
child: Text("暂停下载"),
),
onPressed: () => model.doCancel()),
],
);
}
@override
String getUITitle(BuildContext context, DownloaderDialogUIModel model) => "";
String getStatus(DownloaderDialogUIModel model) {
if (model.progress == null && !model.isInMerging) return "准备中...";
if (model.isInMerging) return "正在处理文件...";
return "${model.progress?.toStringAsFixed(2) ?? "0"}% ";
}
}

View File

@ -1,127 +0,0 @@
import 'dart:io';
import 'package:file_picker/file_picker.dart';
import 'package:hive/hive.dart';
import 'package:starcitizen_doctor/base/ui_model.dart';
import 'package:starcitizen_doctor/common/rust/api/downloader_api.dart'
as rust_downloader;
import 'package:starcitizen_doctor/common/rust/downloader.dart';
class DownloaderDialogUIModel extends BaseUIModel {
final String fileName;
String savePath;
final String downloadUrl;
final bool showChangeSavePathDialog;
final int threadCount;
final bool isP4kDownload;
DownloaderDialogUIModel(this.fileName, this.savePath, this.downloadUrl,
{this.showChangeSavePathDialog = false,
this.threadCount = 1,
this.isP4kDownload = false});
bool isInMerging = false;
String? downloadTaskId;
double? progress;
int? speed;
int? count;
int? total;
@override
void initModel() {
super.initModel();
_initDownload();
}
_initDownload() async {
if (showChangeSavePathDialog) {
final userSelect = await FilePicker.platform.saveFile(
initialDirectory: savePath,
fileName: fileName,
lockParentWindow: true);
if (userSelect == null) {
Navigator.pop(context!);
return;
}
final f = File(userSelect);
if (await f.exists()) {
await f.delete();
}
savePath = userSelect;
dPrint(savePath);
notifyListeners();
}
if (savePath.endsWith("\\$fileName")) {
savePath = savePath.substring(0, savePath.length - fileName.length - 1);
}
if (isP4kDownload) {
final box = await Hive.openBox("p4k_cache");
await box.put(
"last_save_dir",
{"save_path": savePath, "file_name": fileName},
);
}
final downloaderSavePath = "$savePath//$fileName.downloading";
try {
rust_downloader
.startDownload(
url: downloadUrl,
savePath: savePath,
fileName: "$fileName.downloading",
connectionCount: 10)
.listen((event) async {
dPrint(
"id == ${event.id} p ==${event.progress} t==${event.total} s==${event.speed} st==${event.status}");
downloadTaskId = event.id;
count = event.progress;
if (event.total != 0) {
total = event.total;
}
speed = event.speed;
if (total != null && total != 0 && event.progress != 0) {
progress = (event.progress / total!) * 100;
}
notifyListeners();
if (progress != null &&
progress != 0 &&
event.status == const MyDownloaderStatus.noStart()) {
Navigator.pop(context!, "cancel");
return;
}
if (event.status == const MyDownloaderStatus.finished()) {
count = total;
isInMerging = true;
notifyListeners();
await File(downloaderSavePath)
.rename(downloaderSavePath.replaceAll(".downloading", ""));
final bsonFile = File("$downloaderSavePath.bson");
if (await bsonFile.exists()) {
bsonFile.delete();
}
Navigator.pop(context!, "$savePath\\$fileName");
}
});
} catch (e) {
Navigator.pop(context!, e);
}
}
doCancel() async {
try {
if (downloadTaskId != null) {
await rust_downloader.cancelDownload(id: downloadTaskId!);
downloadTaskId = null;
} else {
Navigator.pop(context!, "cancel");
}
} catch (_) {}
}
}

View File

@ -3,18 +3,11 @@ import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:hive/hive.dart';
import 'package:starcitizen_doctor/api/analytics.dart';
import 'package:starcitizen_doctor/base/ui_model.dart';
import 'package:starcitizen_doctor/common/conf/app_conf.dart';
import 'package:starcitizen_doctor/common/helper/log_helper.dart';
import 'package:starcitizen_doctor/common/helper/system_helper.dart';
import 'package:starcitizen_doctor/ui/tools/downloader/downloader_dialog_ui_model.dart';
import 'package:url_launcher/url_launcher_string.dart';
import 'package:xml/xml.dart';
import 'downloader/downloader_dialog_ui.dart';
class ToolsUIModel extends BaseUIModel {
bool _working = false;
@ -341,76 +334,76 @@ class ToolsUIModel extends BaseUIModel {
}
Future<void> _downloadP4k() async {
String savePath = scInstalledPath;
String fileName = "Data.p4k";
bool isResumeDownload = false;
final box = await Hive.openBox("p4k_cache");
var downloadUrl = AppConf.networkVersionData?.p4kDownloadUrl;
if (downloadUrl == null || downloadUrl.isEmpty) {
showToast(context!, "该功能维护中,请稍后再试!");
return;
}
if ((await SystemHelper.getPID("\"RSI Launcher\"")).isNotEmpty) {
showToast(context!, "RSI启动器正在运行请先关闭启动器再使用此功能",
constraints: BoxConstraints(
maxWidth: MediaQuery.of(context!).size.width * .35));
return;
}
final lastSavePath = (box.get("last_save_dir", defaultValue: {}) as Map);
dPrint("lastSavePath === $lastSavePath");
if (lastSavePath.isNotEmpty) {
final s = lastSavePath["save_path"] ?? "";
final f = lastSavePath["file_name"] ?? "";
if ((await File("$s/$f.downloading").exists()) &&
(await File("$s/$f.downloading.bson").exists())) {
final ok = await showConfirmDialogs(context!, "是否恢复下载?",
const Text("检测到未完成的下载,点击确认即可恢复下载,点击取消将会删除之前的临时文件,并开始一个新的下载。"));
if (ok) {
savePath = s;
fileName = f;
isResumeDownload = true;
} else {
// del last cache and del file
await box.delete("last_save_dir");
await File("$s/$f.downloading").delete();
await File("$s/$f.downloading.bson").delete();
}
} else {
// del last cache
await box.delete("last_save_dir");
}
} else {
await showToast(
context!,
"P4k 是星际公民的核心游戏文件,高达近 100GB盒子提供的离线下载是为了帮助一些p4k文件下载超级慢的用户。"
"\n\n接下来会弹窗询问您保存位置(可以选择星际公民文件夹也可以选择别处),下载完成后请确保 P4K 文件夹位于 LIVE 文件夹内,之后使用星际公民启动器校验更新即可。");
AnalyticsApi.touch("p4k_download");
}
final r = await showDialog(
context: context!,
dismissWithEsc: false,
builder: (context) {
return BaseUIContainer(
uiCreate: () => DownloaderDialogUI(),
modelCreate: () => DownloaderDialogUIModel(
fileName, savePath, downloadUrl,
showChangeSavePathDialog: !isResumeDownload,
threadCount: 10,
isP4kDownload: true));
});
if (r != null) {
if (r == "cancel") {
return showToast(context!, "下载进度已保留,您可以再次点击此功能恢复下载。");
} else {
final ok = await showConfirmDialogs(
context!, "下载完毕!", Text("文件已保存到:$r\n\n是否查看P4K操作教程"));
if (ok == true) {
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");
}
}
}
// String savePath = scInstalledPath;
// String fileName = "Data.p4k";
// bool isResumeDownload = false;
// final box = await Hive.openBox("p4k_cache");
// var downloadUrl = AppConf.networkVersionData?.p4kDownloadUrl;
// if (downloadUrl == null || downloadUrl.isEmpty) {
// showToast(context!, "该功能维护中,请稍后再试!");
// return;
// }
// if ((await SystemHelper.getPID("\"RSI Launcher\"")).isNotEmpty) {
// showToast(context!, "RSI启动器正在运行请先关闭启动器再使用此功能",
// constraints: BoxConstraints(
// maxWidth: MediaQuery.of(context!).size.width * .35));
// return;
// }
// final lastSavePath = (box.get("last_save_dir", defaultValue: {}) as Map);
// dPrint("lastSavePath === $lastSavePath");
// if (lastSavePath.isNotEmpty) {
// final s = lastSavePath["save_path"] ?? "";
// final f = lastSavePath["file_name"] ?? "";
// if ((await File("$s/$f.downloading").exists()) &&
// (await File("$s/$f.downloading.bson").exists())) {
// final ok = await showConfirmDialogs(context!, "是否恢复下载?",
// const Text("检测到未完成的下载,点击确认即可恢复下载,点击取消将会删除之前的临时文件,并开始一个新的下载。"));
// if (ok) {
// savePath = s;
// fileName = f;
// isResumeDownload = true;
// } else {
// // del last cache and del file
// await box.delete("last_save_dir");
// await File("$s/$f.downloading").delete();
// await File("$s/$f.downloading.bson").delete();
// }
// } else {
// // del last cache
// await box.delete("last_save_dir");
// }
// } else {
// await showToast(
// context!,
// "P4k 是星际公民的核心游戏文件,高达近 100GB盒子提供的离线下载是为了帮助一些p4k文件下载超级慢的用户。"
// "\n\n接下来会弹窗询问您保存位置(可以选择星际公民文件夹也可以选择别处),下载完成后请确保 P4K 文件夹位于 LIVE 文件夹内,之后使用星际公民启动器校验更新即可。");
// AnalyticsApi.touch("p4k_download");
// }
// final r = await showDialog(
// context: context!,
// dismissWithEsc: false,
// builder: (context) {
// return BaseUIContainer(
// uiCreate: () => DownloaderDialogUI(),
// modelCreate: () => DownloaderDialogUIModel(
// fileName, savePath, downloadUrl,
// showChangeSavePathDialog: !isResumeDownload,
// threadCount: 10,
// isP4kDownload: true));
// });
//
// if (r != null) {
// if (r == "cancel") {
// return showToast(context!, "下载进度已保留,您可以再次点击此功能恢复下载。");
// } else {
// final ok = await showConfirmDialogs(
// context!, "下载完毕!", Text("文件已保存到:$r\n\n是否查看P4K操作教程"));
// if (ok == true) {
// 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({bool? setMode}) async {