WIn_RustTools/RustTools/Views/ModulePage.xaml.cs

336 lines
10 KiB
C#
Raw Permalink Normal View History

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Media;
2024-08-24 11:50:04 +00:00
using Microsoft.VisualBasic.FileIO;
using RustTools.muqing;
2024-08-31 10:32:38 +00:00
using RustTools.Themes;
using RustTools.ViewModels;
2024-08-24 11:50:04 +00:00
using SharpCompress.Common;
namespace RustTools.Views;
/// <summary>
2024-08-08 04:29:27 +00:00
/// 模组碎片
/// </summary>
public class DataObject
{
public string Dri
{
get; set;
}
public string Name
{
get; set;
}
public string Info
{
get; set;
}
public bool IsRwmod
{
get; set;
}
2024-08-08 04:29:27 +00:00
public string Icon
{
2024-08-08 04:29:27 +00:00
get; set;
}
2024-08-09 07:05:40 +00:00
#pragma warning disable CS8765 // 参数类型的为 Null 性与重写成员不匹配(可能是由于为 Null 性特性)。
2024-08-08 04:29:27 +00:00
public override bool Equals(object obj)
{
if (obj is DataObject other)
{
return Dri == other.Dri;
}
return false;
}
2024-08-08 04:29:27 +00:00
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public DataObject(FileInfo fileInfo)
{
2024-08-08 04:29:27 +00:00
Name = Path.GetFileNameWithoutExtension(fileInfo.Name);
//Name = fileInfo.Name;
IsRwmod = true;
Info = $"大小:{wj.FormatFileSize(fileInfo.Length)}";
Dri = fileInfo.FullName;
}
public DataObject(DirectoryInfo fileInfo)
{
Name = fileInfo.Name;
IsRwmod = false;
2024-08-08 04:29:27 +00:00
Info = $"{fileInfo.CreationTime}";
Dri = fileInfo.FullName;
2024-08-08 04:29:27 +00:00
Icon = File.Exists(Path.Combine(Dri, "icon.png")) ? Path.Combine(Dri, "icon.png") : "/Assets/image/folder.svg";
var modinfo = Path.Combine(Dri, "mod-info.txt");
if (File.Exists(modinfo))
{
var dictionary = gj.IniInfo(modinfo);
if (dictionary.TryGetValue("mod", out var sectionValues))
{
gj.sc(sectionValues);
2024-08-15 01:40:33 +00:00
Name = sectionValues.TryGetValue("title", out var value) ? value : Name;
2024-08-08 04:29:27 +00:00
gj.sc(value);
Info = sectionValues.TryGetValue("description", out var description) ? description : Info;
}
}
}
}
public sealed partial class ModulePage : Page
{
2024-08-08 04:29:27 +00:00
public ModuleViewModel ViewModel
{
get; set;
}
public ModulePage()
{
ViewModel = App.GetService<ModuleViewModel>();
InitializeComponent();
}
2024-08-16 10:09:19 +00:00
/// <summary>
/// 打开模组
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
if (button != null)
{
var container = VisualTreeHelper.GetParent(button) as FrameworkElement;
if (container != null && container.DataContext is DataObject dataItem)
{
FlyoutShowOptions myOption = new FlyoutShowOptions();
myOption.ShowMode = FlyoutShowMode.Standard;
CommandBarFlyout1.ShowAt(button, myOption);
}
}
}
2024-08-16 10:09:19 +00:00
/// <summary>
/// 删除模组和文件夹
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2024-08-24 11:50:04 +00:00
private async void Button_Delete(object sender, RoutedEventArgs e)
{
2024-08-09 07:05:40 +00:00
var menuItem = sender as Control;
gj.sc(menuItem);
if (menuItem != null)
{
2024-08-08 04:29:27 +00:00
if (menuItem.DataContext is DataObject folderItem)
{
2024-08-24 11:50:04 +00:00
var task = await Dialog.DialogWarnYes("你确定要删除(回收站)" + folderItem.Dri, XamlRoot);
if (task == ContentDialogResult.Primary)
2024-08-08 04:29:27 +00:00
{
2024-08-24 11:50:04 +00:00
if (Directory.Exists(folderItem.Dri))
{
// 删除文件夹
//Directory.Delete(folderItem.Dri, true);
FileSystem.DeleteDirectory(folderItem.Dri, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
}
else if (File.Exists(folderItem.Dri))
{
//删除文件
//File.Delete(folderItem.Dri);
FileSystem.DeleteFile(folderItem.Dri, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
}
2024-08-08 04:29:27 +00:00
}
ViewModel.ListMod.Remove(folderItem);
}
}
}
2024-08-16 10:09:19 +00:00
/// <summary>
/// 解压模组
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2024-08-24 11:50:04 +00:00
private async void UnzipRwmod(DataObject a)
{
2024-08-20 13:34:07 +00:00
ContentDialog dialog = new ContentDialog
{
XamlRoot = XamlRoot,
Title = "提示",
PrimaryButtonText = "确定",
SecondaryButtonText = "取消",
Content = CreateCheckboxContent()
};
var contentDialogResult = await dialog.ShowAsync();
if (contentDialogResult == ContentDialogResult.Primary)
{
2024-08-24 11:50:04 +00:00
var checkbox = (dialog.Content as StackPanel)?.Children[1] as CheckBox;
2024-08-08 04:29:27 +00:00
2024-08-24 11:50:04 +00:00
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(a.Dri);
var GetDirectoryName = Path.GetDirectoryName(a.Dri);
if (Directory.Exists(GetDirectoryName) && fileNameWithoutExtension != null)
{
2024-08-24 11:50:04 +00:00
var targetDirectory = Path.Combine(GetDirectoryName, fileNameWithoutExtension);
var v = wj.UnzipFile(a.Dri, targetDirectory);
gj.sc("解压成功:" + v);
if (checkbox?.IsChecked == true)
{
2024-08-24 11:50:04 +00:00
File.Delete(a.Dri);
ViewModel.ListMod.Remove(a);
}
if (v)
{
ViewModel.ListMod.Remove(new DataObject(new DirectoryInfo(targetDirectory)));
ViewModel.ListMod.Insert(0, new DataObject(new DirectoryInfo(targetDirectory)));
}
}
}
}
private FrameworkElement CreateCheckboxContent()
{
var checkbox = new CheckBox
{
2024-08-08 04:29:27 +00:00
Content = "是否删除源文件",
IsChecked = false,
Margin = new Thickness(0, 16, 0, 0),
};
var text = new TextBlock
{
2024-08-08 04:29:27 +00:00
Text = "解压模组到目录",
};
var stackPanel = new StackPanel();
stackPanel.Children.Add(text);
stackPanel.Children.Add(checkbox);
return stackPanel;
}
2024-08-08 04:29:27 +00:00
2024-08-16 10:09:19 +00:00
/// <summary>
/// 打包模组
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public async void Button_Rwmod(object sender, RoutedEventArgs e)
{
var menuItem = sender as MenuFlyoutItem;
if (menuItem != null)
{
if (menuItem.DataContext is DataObject folderItem)
{
var filepath = Path.Combine(Path.GetDirectoryName(folderItem.Dri), folderItem.Name + ".rwmod");
if (File.Exists(filepath))
{
var show = await new ContentDialog()
{
XamlRoot = menuItem.XamlRoot,
Title = "警告",
Content = "已经存在必须覆盖",
PrimaryButtonText = "确定",
SecondaryButtonText = "取消"
}.ShowAsync();
if (show == ContentDialogResult.Primary)
{
}
else
{
return;
}
}
// 压缩zip
var v = wj.ZipExample(folderItem.Dri, folderItem.Name + ".rwmod");
if (v != string.Empty)
{
ViewModel.ListMod.Remove(new DataObject(new FileInfo(filepath)));
ViewModel.ListMod.Insert(0, new DataObject(new FileInfo(v)));
}
}
}
}
2024-08-24 11:50:04 +00:00
/// <summary>
/// 双击GridView事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void BasicGridView_DoubleTapped(object sender, Microsoft.UI.Xaml.Input.DoubleTappedRoutedEventArgs e)
{
var gridView = sender as GridView;
var item = (e.OriginalSource as FrameworkElement)?.DataContext;
if (item != null && gridView != null)
{
// 这里处理双击事件,例如显示一个消息框
// 替换为实际的数据类型
if (item is DataObject clickedItem)
{
// 在这里处理双击项
var IsRwmod = clickedItem.IsRwmod; // 假设你的项有一个 Name 属性
if (IsRwmod) {
UnzipRwmod(clickedItem);
}
}
}
}
2024-08-31 10:32:38 +00:00
/// <summary>
/// 发布模组
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void Button_PostRwmod(object sender, RoutedEventArgs e)
{
if (sender is MenuFlyoutItem item)
{
if (item.DataContext is not DataObject dataObject)
{
return;
}
//folderItem.IsRwmod = false;
var contentDialog = new ContentDialog()
{
XamlRoot = XamlRoot,
Title = "发布",
};
var postRwmod = new PostRwmod(contentDialog)
{
XamlRoot = XamlRoot
};
var h= ((Panel) Content).ActualHeight;
postRwmod.Height =h-50;
contentDialog.Content = postRwmod;
// 订阅 Closing 事件
contentDialog.Closing += Dialog.DialogNotEsc;
postRwmod.Init(dataObject.Dri);
await contentDialog.ShowAsync();
}
}
}
public class MyItemTemplateSelector : DataTemplateSelector
{
public DataTemplate Template1
{
get; set;
} = new();
public DataTemplate Template2
{
get; set;
} = new();
protected override DataTemplate SelectTemplateCore(object item)
{
if (item is not DataObject dataObject)
{
return base.SelectTemplateCore(item);
}
if (dataObject.IsRwmod)
return Template1;
else
return Template2;
}
}