WIn_RustTools/RustTools/WindowUI/ImportModule.xaml.cs

138 lines
4.0 KiB
C#
Raw Normal View History

2024-08-02 03:00:33 +00:00
using System.Diagnostics;
using System.IO;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
2024-08-02 03:00:33 +00:00
using RustTools.muqing;
namespace RustTools.WindowUI;
public sealed partial class ImportModule : WindowEx
{
2024-08-02 03:00:33 +00:00
private readonly FileInfo? fileInfo;
2024-08-08 04:29:27 +00:00
private string ImpUrl= string.Empty;
public ImportModule(string file)
{
InitializeComponent();
ExtendsContentIntoTitleBar = true;
if (file == string.Empty) {
Close();
return;
}
2024-08-02 03:00:33 +00:00
fileInfo = new FileInfo(file);
var iniHelper = new IniHelper();
2024-08-02 03:00:33 +00:00
2024-08-08 04:29:27 +00:00
iniHelper.Load(IniHelper.FILE.Config);
if (file.EndsWith(".rwmod"))
{
var l = " | ";
var formattedSize = wj.FormatFileSize(fileInfo.Length);
MessageText.Text = "<22><><EFBFBD><EFBFBD><><C4A3>" + l + "<22><>С" + formattedSize;
ImpUrl = iniHelper.GetValue(IniHelper.CODE.Rust, IniHelper.KEY.ModFileUrl);
InfoText.Text = string.Format("ԭλ<D4AD><CEBB> {0} --> {1} \n<>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>벻Ҫ<EBB2BB>رմ˴<D5B4><CBB4><EFBFBD> <20><><EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6>ر<EFBFBD> ʧ<><CAA7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾȨ<CABE>޵ȴ<DEB5><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>",
file, ImpUrl);
}
NameText.Text =
fileInfo.Name;
GridView.Loaded += GridView_Loaded;
// <20><><EFBFBD><EFBFBD> Loaded <20>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
2024-08-02 03:00:33 +00:00
private void GridView_Loaded(object sender, RoutedEventArgs e)
{
2024-08-02 03:00:33 +00:00
Init(ImpUrl);
}
private async void Init(string v)
{
if (fileInfo == null)
{
await Dialog(<><C3BB><EFBFBD><EFBFBD><E3B9BB>Ȩ<EFBFBD><C8A8>");
return;
}
if (v == string.Empty)
{
await Dialog(<>а<EFBFBD><D0B0><EFBFBD>ģ<EFBFBD><C4A3>·<EFBFBD><C2B7>");
}
else if (fileInfo.DirectoryName == v)
{
await Dialog("ԭ·<D4AD><C2B7><EFBFBD><EFBFBD><EBB5BC>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>");
}
}
private async Task Dialog(string content)
{
if (GridView.XamlRoot == null)
{
return;
}
var dialog = new ContentDialog
{
Title = "<22><><EFBFBD><EFBFBD>",
2024-08-02 03:00:33 +00:00
Content = content,
CloseButtonText = "<22>ر<EFBFBD>",
XamlRoot = GridView.XamlRoot
};
dialog.CloseButtonClick += (a, b) =>
{
Close();
};
// <20><>ʾ<EFBFBD>Ի<EFBFBD><D4BB><EFBFBD>
await dialog.ShowAsync();
}
2024-08-02 03:00:33 +00:00
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());
// ȷ<><C8B7>Ŀ<EFBFBD><C4BF><EFBFBD>ļ<EFBFBD><C4BC>д<EFBFBD><D0B4><EFBFBD>
Directory.CreateDirectory(ImpUrl);
var destinationZipPath = Path.Combine(ImpUrl, fileInfo.Name);
await CopyFileWithProgressAsync( fileInfo.ToString() ,destinationZipPath, (percentage) =>
{
button.Content = $"{percentage:F2}%";
Debug.WriteLine($"<22><><EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD>: {percentage:F2}%");
});
button.Content = "<22><><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>";
await Task.Delay(1500);
Close();
Debug.WriteLine("<22>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɡ<EFBFBD>");
}
public static async Task CopyFileWithProgressAsync(string sourcePath, string destinationPath, Action<double> progressCallback)
{
2024-08-02 03:00:33 +00:00
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;
2024-08-02 03:00:33 +00:00
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);
}
}
}
}
}