using System.Collections.ObjectModel; using System.Diagnostics; using System.Reflection.Metadata.Ecma335; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Controls.Primitives; using Microsoft.UI.Xaml.Media; using RustTools.muqing; using RustTools.ViewModels; using static IniHelper; using static System.Collections.Specialized.BitVector32; namespace RustTools.Views; /// /// 模组碎片 /// public class DataObject { public string Dri { get; set; } public string Name { get; set; } public string Info { get; set; } public bool IsRwmod { get; set; } public string Icon { get; set; } public override bool Equals(object obj) { if (obj is DataObject other) { return Dri == other.Dri; } return false; } #pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。 public DataObject(FileInfo fileInfo) { 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; Info = $"{fileInfo.CreationTime}"; Dri = fileInfo.FullName; 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); Name =sectionValues.TryGetValue("title", out var value) ? value : Name; gj.sc(value); Info = sectionValues.TryGetValue("description", out var description) ? description : Info; } } } } public sealed partial class ModulePage : Page { public ModuleViewModel ViewModel { get; set; } public ModulePage() { ViewModel = App.GetService(); InitializeComponent(); } //private void ShowMenu(bool isTransient, GridViewItem dataTemplate) //{ // FlyoutShowOptions myOption = new FlyoutShowOptions(); // myOption.ShowMode = isTransient ? FlyoutShowMode.Transient : FlyoutShowMode.Standard; // CommandBarFlyout1.ShowAt(dataTemplate, myOption); //} 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); } } } private void Button_Delete(object sender, RoutedEventArgs e) { var menuItem = sender as ButtonBase; if (menuItem != null) { if (menuItem.DataContext is DataObject folderItem) { if (Directory.Exists(folderItem.Dri)) { // 删除文件夹 Directory.Delete(folderItem.Dri, true); } else if (File.Exists(folderItem.Dri)) { //删除文件 File.Delete(folderItem.Dri); } ViewModel.ListMod.Remove(folderItem); } } } private async void Button_Click_RwmodItem(object sender, RoutedEventArgs e) { ContentDialog dialog = new ContentDialog(); dialog.XamlRoot = XamlRoot; dialog.Title = "提示"; dialog.PrimaryButtonText = "确定"; dialog.SecondaryButtonText = "取消"; dialog.Content = CreateCheckboxContent(); var contentDialogResult = await dialog.ShowAsync(); if (contentDialogResult == ContentDialogResult.Primary) { var button = sender as Button; if (button != null) { var container = VisualTreeHelper.GetParent(button) as FrameworkElement; if (container != null && container.DataContext is DataObject dataItem) { CheckBox checkbox = (dialog.Content as StackPanel)?.Children[1] as CheckBox; if (checkbox.IsChecked == true) { File.Delete(dataItem.Dri); ViewModel.ListMod.Remove(dataItem); } var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(dataItem.Dri); var GetDirectoryName = Path.GetDirectoryName(dataItem.Dri); if (Directory.Exists(GetDirectoryName) && fileNameWithoutExtension != null) { var targetDirectory = Path.Combine(GetDirectoryName, fileNameWithoutExtension); var v = wj.UnzipFile(dataItem.Dri, targetDirectory); gj.sc(v); if (v) { ViewModel.ListMod.Add(new DataObject(new DirectoryInfo(targetDirectory))); } } } } } } private FrameworkElement CreateCheckboxContent() { var checkbox = new CheckBox { Content = "是否删除源文件", IsChecked = false, Margin = new Thickness(0, 16, 0, 0), }; var text = new TextBlock { Text = "解压模组到目录", }; var stackPanel = new StackPanel(); stackPanel.Children.Add(text); stackPanel.Children.Add(checkbox); return stackPanel; } } 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; } }