WIn_RustTools/RustTools/WindowUI/ImportModule.xaml.cs
2024-08-08 12:29:27 +08:00

138 lines
4.0 KiB
C#

using System.Diagnostics;
using System.IO;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using RustTools.muqing;
namespace RustTools.WindowUI;
public sealed partial class ImportModule : WindowEx
{
private readonly FileInfo? fileInfo;
private string ImpUrl= string.Empty;
public ImportModule(string file)
{
InitializeComponent();
ExtendsContentIntoTitleBar = true;
if (file == string.Empty) {
Close();
return;
}
fileInfo = new FileInfo(file);
var iniHelper = new IniHelper();
iniHelper.Load(IniHelper.FILE.Config);
if (file.EndsWith(".rwmod"))
{
var l = " | ";
var formattedSize = wj.FormatFileSize(fileInfo.Length);
MessageText.Text = "类型:模组" + l + "大小" + formattedSize;
ImpUrl = iniHelper.GetValue(IniHelper.CODE.Rust, IniHelper.KEY.ModFileUrl);
InfoText.Text = string.Format("原位置 {0} --> {1} \n在处理的时候请不要关闭此窗口 导成功后会自动关闭 失败则会提示权限等错误问题",
file, ImpUrl);
}
NameText.Text =
fileInfo.Name;
GridView.Loaded += GridView_Loaded;
// 添加 Loaded 事件处理程序
}
private void GridView_Loaded(object sender, RoutedEventArgs e)
{
Init(ImpUrl);
}
private async void Init(string v)
{
if (fileInfo == null)
{
await Dialog("没有足够的权限");
return;
}
if (v == string.Empty)
{
await Dialog("没有绑定模组路径");
}
else if (fileInfo.DirectoryName == v)
{
await Dialog("原路径与导入路径不能一致");
}
}
private async Task Dialog(string content)
{
if (GridView.XamlRoot == null)
{
return;
}
var dialog = new ContentDialog
{
Title = "警告",
Content = content,
CloseButtonText = "关闭",
XamlRoot = GridView.XamlRoot
};
dialog.CloseButtonClick += (a, b) =>
{
Close();
};
// 显示对话框
await dialog.ShowAsync();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
if (fileInfo == null)
{
return;
}
var button=sender as Button;
if (button == null) { return; }
button.IsEnabled=false;
gj.sc(fileInfo.ToString());
// 确保目标文件夹存在
Directory.CreateDirectory(ImpUrl);
var destinationZipPath = Path.Combine(ImpUrl, fileInfo.Name);
await CopyFileWithProgressAsync( fileInfo.ToString() ,destinationZipPath, (percentage) =>
{
button.Content = $"{percentage:F2}%";
Debug.WriteLine($"复制进度: {percentage:F2}%");
});
button.Content = "导入成功";
await Task.Delay(1500);
Close();
Debug.WriteLine("文件复制完成。");
}
public static async Task CopyFileWithProgressAsync(string sourcePath, string destinationPath, Action<double> progressCallback)
{
const int bufferSize = 81920; // 80KB
byte[] buffer = new byte[bufferSize];
using (FileStream sourceStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read))
{
long totalBytes = sourceStream.Length;
long totalBytesRead = 0;
using (FileStream destinationStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write))
{
int bytesRead;
while ((bytesRead = await sourceStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await destinationStream.WriteAsync(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
double percentage = (double)totalBytesRead / totalBytes * 100;
progressCallback(percentage);
}
}
}
}
}