完善导入模块

This commit is contained in:
muqing 2024-08-02 11:00:33 +08:00
parent ddf886b20a
commit f0177e5180
2 changed files with 98 additions and 22 deletions

View File

@ -13,7 +13,7 @@
MinHeight="500" MinHeight="500"
mc:Ignorable="d"> mc:Ignorable="d">
<Grid Name="Grid" Margin="16,16,16,16"> <Grid Name="GridView" Margin="16,16,16,16">
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
<RowDefinition Height="*" /> <RowDefinition Height="*" />
@ -35,7 +35,7 @@
Text="文件详情" /> Text="文件详情" />
<TextBlock <TextBlock
Name="InfoText" Name="InfoText"
Style="{StaticResource BodyStrongTextBlockStyle}" Style="{StaticResource CaptionTextBlockStyle}"
Text="处理信息" /> Text="处理信息" />
</StackPanel> </StackPanel>
<Button <Button

View File

@ -1,11 +1,16 @@
using System.Diagnostics;
using System.IO;
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Controls;
using RustTools.muqing;
namespace RustTools.WindowUI; namespace RustTools.WindowUI;
public sealed partial class ImportModule : WindowEx public sealed partial class ImportModule : WindowEx
{ {
private readonly FileInfo? fileInfo;
private readonly string ImpUrl= string.Empty;
public ImportModule(string file) public ImportModule(string file)
{ {
InitializeComponent(); InitializeComponent();
@ -14,40 +19,65 @@ public sealed partial class ImportModule : WindowEx
Close(); Close();
return; return;
} }
var fileInfo = new FileInfo(file); fileInfo = new FileInfo(file);
var iniHelper = new IniHelper(); var iniHelper = new IniHelper();
iniHelper.Load(IniHelper.FILE.Config); iniHelper.Load(IniHelper.FILE.Config);
ImpUrl = string.Empty;
if (file.EndsWith(".rwmod")) if (file.EndsWith(".rwmod"))
{ {
var l = " | "; var l = " | ";
var formattedSize = FormatFileSize(fileInfo.Length); var formattedSize = FormatFileSize(fileInfo.Length);
MessageText.Text="类型:模组"+ l+ "大小"+ formattedSize; MessageText.Text="类型:模组"+ l+ "大小"+ formattedSize;
var v = iniHelper.GetValue(IniHelper.CODE.Rust, IniHelper.KEY.ModFileUrl); ImpUrl = iniHelper.GetValue(IniHelper.CODE.Rust, IniHelper.KEY.ModFileUrl);
if (v == string.Empty) {
return;
}
InfoText.Text = string.Format("原位置 {0} --> {1} \n在处理的时候请不要关闭此窗口 导成功后会自动关闭 失败则会提示权限等错误问题", InfoText.Text = string.Format("原位置 {0} --> {1} \n在处理的时候请不要关闭此窗口 导成功后会自动关闭 失败则会提示权限等错误问题",
file,v); file,ImpUrl);
} }
NameText.Text = NameText.Text =
fileInfo.Name; fileInfo.Name;
GridView.Loaded += GridView_Loaded;
// 添加 Loaded 事件处理程序
} }
private async void Dialog()
private void GridView_Loaded(object sender, RoutedEventArgs e)
{ {
ContentDialog dialog = new ContentDialog 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 = "警告", Title = "警告",
Content = "这是一个示例内容", Content = content,
PrimaryButtonText = "确定", CloseButtonText = "关闭",
SecondaryButtonText = "取消", XamlRoot = GridView.XamlRoot
CloseButtonText = "关闭" };
dialog.CloseButtonClick += (a, b) =>
{
Close();
}; };
// 设置 XamlRoot确保对话框显示在正确的窗口中
dialog.XamlRoot = Grid.XamlRoot;
// 显示对话框 // 显示对话框
await dialog.ShowAsync(); await dialog.ShowAsync();
} }
@ -64,9 +94,55 @@ public sealed partial class ImportModule : WindowEx
}; };
} }
private void Button_Click(object sender, RoutedEventArgs e) private async void Button_Click(object sender, RoutedEventArgs e)
{ {
Dialog(); 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);
}
}
}
} }
} }