维护一下功能,累了,喝一杯咖啡

This commit is contained in:
muqing 2024-08-01 22:28:39 +08:00
parent e182fa3649
commit ddf886b20a
14 changed files with 296 additions and 55 deletions

View File

@ -4,7 +4,6 @@ using RustTools.Contracts.Services;
using RustTools.ViewModels;
namespace RustTools.Activation;
public class DefaultActivationHandler : ActivationHandler<LaunchActivatedEventArgs>
{
private readonly INavigationService _navigationService;

View File

@ -3,20 +3,24 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.UI.Xaml;
using RustTools.Activation;
using RustTools.Contracts.Services;
using RustTools.Helpers;
using RustTools.Models;
using RustTools.Services;
using RustTools.ViewModels;
using RustTools.Views;
using System.Threading;
using RustTools.muqing;
using RustTools.Activation;
using System.Diagnostics;
using RustTools.WindowUI;
using Windows.ApplicationModel.Activation;
using Windows.Storage;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Windows.AppLifecycle;
namespace RustTools;
// To learn more about WinUI 3, see https://docs.microsoft.com/windows/apps/winui/winui3/.
public partial class App : Application
public partial class App : Microsoft.UI.Xaml.Application
{
// The .NET Generic Host provides dependency injection, configuration, logging, and other services.
// https://docs.microsoft.com/dotnet/core/extensions/generic-host
@ -46,18 +50,15 @@ public partial class App : Application
public App()
{
InitializeComponent();
System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcessesByName("RustTools");//获取指定的进程名
if (myProcesses.Length > 1) //如果可以获取到知道的进程名则说明已经启动
{
Exit(); //关闭系统
}
// 注册激活事件处理程序
Host = Microsoft.Extensions.Hosting.Host.
CreateDefaultBuilder().
UseContentRoot(AppContext.BaseDirectory).
ConfigureServices((context, services) =>
{
// Default Activation Handler
services.AddTransient<ActivationHandler<LaunchActivatedEventArgs>, DefaultActivationHandler>();
services.AddTransient<ActivationHandler<Microsoft.UI.Xaml.LaunchActivatedEventArgs>, DefaultActivationHandler>();
// Other Activation Handlers
@ -103,10 +104,53 @@ public partial class App : Application
// https://docs.microsoft.com/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.application.unhandledexception.
}
protected async override void OnLaunched(LaunchActivatedEventArgs args)
{
base.OnLaunched(args);
await App.GetService<IActivationService>().ActivateAsync(args);
protected async override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
var activatedEventArgs = Microsoft.Windows.AppLifecycle.AppInstance.GetCurrent().GetActivatedEventArgs();
if (activatedEventArgs.Kind == Microsoft.Windows.AppLifecycle.ExtendedActivationKind.File)
{
gj.sc("打开文件");
if (activatedEventArgs.Kind == ExtendedActivationKind.File)
{
if (activatedEventArgs.Data is IFileActivatedEventArgs fileArgs)
{
var files = fileArgs.Files;
// 处理打开的文件
foreach (var file in files)
{
var storageFile = file as Windows.Storage.StorageFile;
if (storageFile != null)
{
gj.sc(storageFile.Path);
var importModule=new ImportModule(storageFile.Path);
importModule.Activate();
// 读取或处理文件内容
}
}
}
}
return;
}
// If this is the first instance launched, then register it as the "main" instance.
// If this isn't the first instance launched, then "main" will already be registered,
// so retrieve it.
var mainInstance = Microsoft.Windows.AppLifecycle.AppInstance.FindOrRegisterForKey("main");
// If the instance that's executing the OnLaunched handler right now
// isn't the "main" instance.
if (!mainInstance.IsCurrent)
{
// Redirect the activation (and args) to the "main" instance, and exit.
await mainInstance.RedirectActivationToAsync(activatedEventArgs);
System.Diagnostics.Process.GetCurrentProcess().Kill();
return;
}
else
{
base.OnLaunched(args);
await App.GetService<IActivationService>().ActivateAsync(args);
}
}
}

View File

@ -7,13 +7,11 @@ namespace RustTools;
public sealed partial class MainWindow : WindowEx
{
private readonly Microsoft.UI.Dispatching.DispatcherQueue dispatcherQueue;
public static MainWindow Main;
private readonly UISettings settings;
public MainWindow()
{
Main = this;
gj.SetBackTheme(this);
InitializeComponent();
//AppWindow.SetIcon("Assets/tool.png");

View File

@ -9,7 +9,6 @@
Version="{Version}"
Publisher="{Publisher}"
Uri="{MainPackageUri}"/>
<UpdateSettings>
<OnLaunch HoursBetweenUpdateChecks="0" />
</UpdateSettings>

View File

@ -58,6 +58,16 @@
</com:ExeServer>
</com:ComServer>
</com:Extension>
<uap:Extension Category="windows.fileTypeAssociation">
<uap:FileTypeAssociation Name=".rwmod">
<uap:SupportedFileTypes>
<uap:FileType>.rwmod</uap:FileType>
</uap:SupportedFileTypes>
<uap:DisplayName>添加模组</uap:DisplayName>
<uap:Logo>Assets\tool.png</uap:Logo>
<uap:InfoTip>需要绑定铁锈路径</uap:InfoTip>
</uap:FileTypeAssociation>
</uap:Extension>
</Extensions>
</Application>
</Applications>

View File

@ -27,6 +27,7 @@
<None Remove="Views\ModulePage.xaml" />
<None Remove="Views\RankingPage.xaml" />
<None Remove="Views\UserPage.xaml" />
<None Remove="WindowUI\ImportModule.xaml" />
</ItemGroup>
<ItemGroup>
@ -56,6 +57,9 @@
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<Page Update="WindowUI\ImportModule.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Update="Views\ModulePage.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>

View File

@ -70,12 +70,21 @@
<TextBlock Text="模组路径" />
<AutoSuggestBox
x:Name="ModFileUrlText"
Name="ModFileUrlText"
Margin="{StaticResource XSmallTopMargin}"
LostFocus="ModFileUrlText_LostFocus"
PlaceholderText="输入或者搜索路径(Enter/回车保存)"
PlaceholderText="输入或者搜索路径(取消焦点保存)"
QueryIcon="Find"
QuerySubmitted="AutoSuggestBox_ModFileUrl" />
QuerySubmitted="AutoSuggestBox_Url" />
<TextBlock Margin="0,9,0,0" Text="地图路径" />
<AutoSuggestBox
Name="MapsFileUrlText"
Margin="{StaticResource XSmallTopMargin}"
LostFocus="ModFileUrlText_LostFocus"
PlaceholderText="输入或者搜索路径(取消焦点保存)"
QueryIcon="Find"
QuerySubmitted="AutoSuggestBox_Url" />
</StackPanel>
<TextBlock x:Uid="Settings_About" Style="{ThemeResource SubtitleTextBlockStyle}" />

View File

@ -1,15 +1,9 @@
using Microsoft.UI;
using Microsoft.UI.Composition.SystemBackdrops;

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Media;
using RustTools.muqing;
using RustTools.ViewModels;
using Windows.Storage.AccessCache;
using Windows.Storage.Pickers;
using Windows.Storage;
using WinUIEx;
namespace RustTools.Views;
@ -52,13 +46,14 @@ public sealed partial class SettingsPage : Page
}
BackgroundRadioButtons.SelectionChanged += BackgroundColor_SelectionChanged;
ModFileUrlText.Text = iniHelper.GetValue(IniHelper.CODE.Rust, IniHelper.KEY.ModFileUrl);
MapsFileUrlText.Text = iniHelper.GetValue(IniHelper.CODE.Rust,IniHelper.KEY.MapsFileUrl);
}
private bool Backone = false;
private void BackgroundColor_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (Backone)
{
if (MainWindow.Main != null && sender is RadioButtons rb)
if (App.MainWindow != null && sender is RadioButtons rb)
{
var colorName = rb.SelectedItem as string;
var iniHelper = new IniHelper();
@ -66,7 +61,7 @@ public sealed partial class SettingsPage : Page
#pragma warning disable CS8604 // 引用类型参数可能为 null。
iniHelper.SetValue(IniHelper.CODE.Settings, IniHelper.KEY.SystemBackdrop, colorName);
iniHelper.Save();
gj.SetBackTheme(MainWindow.Main);
gj.SetBackTheme(App.MainWindow);
}
}
else
@ -75,30 +70,32 @@ public sealed partial class SettingsPage : Page
}
}
private async void AutoSuggestBox_ModFileUrl(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
private async void AutoSuggestBox_Url(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
{
var dialog = new ContentDialog();
dialog.XamlRoot = this.XamlRoot;
//dialog.Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style;
dialog.Title = "选择搜索模式";
dialog.SecondaryButtonText = "手动选择";
dialog.CloseButtonText = "自动搜索";
dialog.Content = "自动搜索利用助手专属搜索引擎进行全盘搜索游戏文件夹,直到找到 Rusted Warfare 文件夹为止 途中可选择中断。";
var dialog = new ContentDialog
{
XamlRoot = XamlRoot,
//dialog.Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style;
Title = "选择搜索模式",
SecondaryButtonText = "手动选择",
PrimaryButtonText = "自动搜索",
Content = "手动选择请找到Rusted Warfare文件目录软件会自动给你填写mod和maps路径" +
"自动搜索直到找到 Rusted Warfare 文件夹为止 途中可选择中断。"
};
//dialog.DefaultButton = ContentDialogButton.Primary;
dialog.SecondaryButtonClick += Dialog_SecondaryButtonClick;
dialog.CloseButtonClick += Dialog_CloseButtonClick;
dialog.PrimaryButtonClick += Dialog_PrimaryButtonClick;
await dialog.ShowAsync();
}
private void Dialog_CloseButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args){
var mod = new mod();
ModFileUrlText.Text = mod.FileDir;
private void Dialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args){
var mod = new Mod();
ModFileUrlText.Text =mod.getUnitsDir();
MapsFileUrlText.Text =mod.getMaps();
SaveModAndMaps();
}
private async void Dialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args){
var openPicker = new FolderPicker();
var window = App.MainWindow;
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
@ -109,18 +106,37 @@ public sealed partial class SettingsPage : Page
if (folder != null)
{
//StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);
ModFileUrlText.Text = folder.Path;
var a = Path.Combine(folder.Path, "mods\\units");
ModFileUrlText.Text = a;
var b = Path.Combine(folder.Path, "mods\\maps");
MapsFileUrlText.Text = b;
SaveModAndMaps();
}
}
private void SaveModAndMaps()
{
var iniHelper = new IniHelper();
iniHelper.Load(IniHelper.FILE.Config);
iniHelper.SetValue(IniHelper.CODE.Rust, IniHelper.KEY.MapsFileUrl, MapsFileUrlText.Text);
iniHelper.SetValue(IniHelper.CODE.Rust, IniHelper.KEY.ModFileUrl, ModFileUrlText.Text);
iniHelper.Save();
}
private void ModFileUrlText_LostFocus(object sender, RoutedEventArgs e)
{
// 获取 AutoSuggestBox 的当前文本
var text = ModFileUrlText.Text;
var a=(AutoSuggestBox) sender;
var iniHelper = new IniHelper();
iniHelper.Load(IniHelper.FILE.Config);
iniHelper.SetValue(IniHelper.CODE.Rust, IniHelper.KEY.ModFileUrl, text);
if (a.Name.Equals("MapsFileUrlText"))
{
iniHelper.SetValue(IniHelper.CODE.Rust, IniHelper.KEY.MapsFileUrl, MapsFileUrlText.Text);
}
else if(a.Name.Equals("ModFileUrlText"))
{
iniHelper.SetValue(IniHelper.CODE.Rust, IniHelper.KEY.ModFileUrl, ModFileUrlText.Text);
}
// 获取 AutoSuggestBox 的当前文本
iniHelper.Save();
}
}

View File

@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8" ?>
<windowex:WindowEx
x:Class="RustTools.WindowUI.ImportModule"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:RustTools.WindowUI"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:windowex="using:WinUIEx"
Title="导入界面"
Width="500"
MinWidth="500"
MinHeight="500"
mc:Ignorable="d">
<Grid Name="Grid" Margin="16,16,16,16">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions />
<StackPanel Grid.Row="0">
<TextBlock Style="{StaticResource TitleTextBlockStyle}" Text="导入界面" />
</StackPanel>
<StackPanel Grid.Row="1" Margin="16,26,16,0">
<TextBlock
Name="NameText"
Style="{StaticResource SubtitleTextBlockStyle}"
Text="名称" />
<TextBlock
Name="MessageText"
Style="{StaticResource BodyStrongTextBlockStyle}"
Text="文件详情" />
<TextBlock
Name="InfoText"
Style="{StaticResource BodyStrongTextBlockStyle}"
Text="处理信息" />
</StackPanel>
<Button
Grid.Row="3"
HorizontalAlignment="Stretch"
Click="Button_Click"
Content="导入" />
</Grid>
</windowex:WindowEx>

View File

@ -0,0 +1,72 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace RustTools.WindowUI;
public sealed partial class ImportModule : WindowEx
{
public ImportModule(string file)
{
InitializeComponent();
ExtendsContentIntoTitleBar = true;
if (file == string.Empty) {
Close();
return;
}
var fileInfo = new FileInfo(file);
var iniHelper = new IniHelper();
iniHelper.Load(IniHelper.FILE.Config);
if (file.EndsWith(".rwmod"))
{
var l = " | ";
var formattedSize = FormatFileSize(fileInfo.Length);
MessageText.Text="类型:模组"+ l+ "大小"+ formattedSize;
var v = iniHelper.GetValue(IniHelper.CODE.Rust, IniHelper.KEY.ModFileUrl);
if (v == string.Empty) {
return;
}
InfoText.Text = string.Format("原位置 {0} --> {1} \n在处理的时候请不要关闭此窗口 导成功后会自动关闭 失败则会提示权限等错误问题",
file,v);
}
NameText.Text =
fileInfo.Name;
}
private async void Dialog()
{
ContentDialog dialog = new ContentDialog
{
Title = "警告",
Content = "这是一个示例内容",
PrimaryButtonText = "确定",
SecondaryButtonText = "取消",
CloseButtonText = "关闭"
};
// 设置 XamlRoot确保对话框显示在正确的窗口中
dialog.XamlRoot = Grid.XamlRoot;
// 显示对话框
await dialog.ShowAsync();
}
private static string FormatFileSize(long bytes)
{
return bytes switch
{
< 1024 => $"{bytes} B",
< 1024 * 1024 => $"{bytes / 1024.0:F2} KB",
< 1024 * 1024 * 1024 => $"{bytes / (1024.0 * 1024.0):F2} MB",
_ => $"{bytes / (1024.0 * 1024.0 * 1024.0):F2} GB"
};
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Dialog();
}
}

View File

@ -6,7 +6,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:windowex="using:WinUIEx"
Title="模组信息"
Title="信息"
MinWidth="650"
MinHeight="500"
Closed="WindowEx_Closed"
@ -18,7 +18,8 @@
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions />
<Grid.ColumnDefinitions>
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Border
Width="106"

View File

@ -26,6 +26,7 @@ public class IniHelper
{
public const string SystemBackdrop = "SystemBackdrop";
public const string ModFileUrl = "ModFileUrl";//模组路径
public const string MapsFileUrl = "MapsFileUrl";
}
private string filePath;
@ -62,7 +63,9 @@ public class IniHelper
this.filePath = filePath;
if (!File.Exists(filePath))
{
throw new FileNotFoundException("The specified file does not exist.", filePath);
File.Create(filePath);
return;
//throw new FileNotFoundException("The specified file does not exist.", filePath);
}
var lines = File.ReadAllLines(filePath);

View File

@ -68,6 +68,14 @@ class gj
Kind = MicaKind.Base
};
break;
default:
iniHelper.SetValue(IniHelper.CODE.Settings, IniHelper.KEY.SystemBackdrop, "Mica");
iniHelper.Save();
windowEx.SystemBackdrop = new MicaBackdrop()
{
Kind = MicaKind.Base
};
break;
}
}

View File

@ -2,7 +2,7 @@
using Microsoft.Win32;
public class mod
public class Mod
{
//开始计时
@ -27,7 +27,7 @@ public class mod
};
//游戏位置
public string FileDir = "";
public mod()
public Mod()
{
StartTime = DateTime.Now;
Console.WriteLine("开始执行文件搜索引擎");
@ -57,6 +57,37 @@ public class mod
}
}
public string getUnitsDir()
{
if (FileDir==string.Empty) {
return string.Empty;
}
else
{
var v = Path.Combine(FileDir, "mods\\units");
if (Directory.Exists(v))
{
Directory.CreateDirectory(v);
}
return v;
}
}
public string getMaps()
{
if (FileDir == string.Empty)
{
return string.Empty;
}
else
{
var v = Path.Combine(FileDir, "mods\\maps");
if (!Directory.Exists(v))
{
Directory.CreateDirectory(v);
}
return v;
}
}
private void init()
{