diff --git a/BetterGenshinImpact/App.xaml.cs b/BetterGenshinImpact/App.xaml.cs index 47bd69bd..06fd9719 100644 --- a/BetterGenshinImpact/App.xaml.cs +++ b/BetterGenshinImpact/App.xaml.cs @@ -9,6 +9,7 @@ using Serilog; using System; using System.IO; using System.Windows; +using BetterGenshinImpact.Service.Interface; using BetterGenshinImpact.ViewModel.Pages; using Microsoft.Extensions.Configuration; using Serilog.Events; @@ -65,6 +66,10 @@ namespace BetterGenshinImpact services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); + + // My Services + services.AddSingleton(); // Configuration //services.Configure(context.Configuration.GetSection(nameof(AppConfig))); diff --git a/BetterGenshinImpact/BetterGenshinImpact.csproj b/BetterGenshinImpact/BetterGenshinImpact.csproj index e839424f..130ef2b4 100644 --- a/BetterGenshinImpact/BetterGenshinImpact.csproj +++ b/BetterGenshinImpact/BetterGenshinImpact.csproj @@ -39,10 +39,6 @@ - - - - diff --git a/BetterGenshinImpact/Core/Config/ApplicationConfiguration.cs b/BetterGenshinImpact/Core/Config/AllConfig.cs similarity index 65% rename from BetterGenshinImpact/Core/Config/ApplicationConfiguration.cs rename to BetterGenshinImpact/Core/Config/AllConfig.cs index 384e443d..3c52d8ee 100644 --- a/BetterGenshinImpact/Core/Config/ApplicationConfiguration.cs +++ b/BetterGenshinImpact/Core/Config/AllConfig.cs @@ -1,6 +1,8 @@ using BetterGenshinImpact.GameTask.AutoFishing; using BetterGenshinImpact.GameTask.AutoSkip; using System; +using BetterGenshinImpact.GameTask.AutoPick; +using CommunityToolkit.Mvvm.ComponentModel; using Fischless.WindowCapture; namespace BetterGenshinImpact.Core.Config @@ -9,28 +11,33 @@ namespace BetterGenshinImpact.Core.Config /// 更好的原神配置 /// [Serializable] - public class ApplicationConfiguration + public partial class AllConfig : ObservableObject { /// /// 窗口捕获的方式 /// - public string CaptureMode { get; set; } = CaptureModes.BitBlt.ToString(); + [ObservableProperty] private string _captureMode = CaptureModes.BitBlt.ToString(); /// /// 窗口捕获帧数/触发器触发频率 /// - public int FrameRate { get; set; } = 30; + [ObservableProperty] private int _frameRate = 30; /// /// 触发器触发频率(ms) /// - public int TriggerInterval { get; set; } = 100; + [ObservableProperty] private int _triggerInterval = 100; /// /// 遮罩窗口配置 /// public MaskWindowConfig MaskWindowConfig { get; set; } = new(); + /// + /// 自动拾取配置 + /// + public AutoPickConfig AutoPickConfig { get; set; } = new(); + /// /// 自动剧情配置 /// @@ -44,6 +51,6 @@ namespace BetterGenshinImpact.Core.Config /// /// 脚本类配置 /// - public MacroConfig ScriptConfig { get; set; } = new(); + public MacroConfig MacroConfig { get; set; } = new(); } } \ No newline at end of file diff --git a/BetterGenshinImpact/Core/Config/MacroConfig.cs b/BetterGenshinImpact/Core/Config/MacroConfig.cs index f920bf52..7b8af78d 100644 --- a/BetterGenshinImpact/Core/Config/MacroConfig.cs +++ b/BetterGenshinImpact/Core/Config/MacroConfig.cs @@ -1,4 +1,5 @@ -using System; +using CommunityToolkit.Mvvm.ComponentModel; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,24 +7,27 @@ using System.Threading.Tasks; namespace BetterGenshinImpact.Core.Config { - public class MacroConfig + [Serializable] + public partial class MacroConfig : ObservableObject { /// /// 长按空格变空格连发 /// - public bool SpacePressHoldToContinuation { get; set; } = true; + [ObservableProperty] private bool _spacePressHoldToContinuationEnabled = true; + /// /// 空格连发时间间隔 /// - public int SpaceFireInterval { get; set; } = 50; + [ObservableProperty] private int _spaceFireInterval = 50; /// /// 长按F变F连发 /// - public bool FPressHoldToContinuation { get; set; } = true; + [ObservableProperty] private bool _fPressHoldToContinuationEnabled = true; + /// /// F连发时间间隔 /// - public int FFireInterval { get; set; } = 50; + [ObservableProperty] private int _fFireInterval = 50; } -} +} \ No newline at end of file diff --git a/BetterGenshinImpact/Core/Config/MaskWindowConfig.cs b/BetterGenshinImpact/Core/Config/MaskWindowConfig.cs index 417c4c0d..5d79db54 100644 --- a/BetterGenshinImpact/Core/Config/MaskWindowConfig.cs +++ b/BetterGenshinImpact/Core/Config/MaskWindowConfig.cs @@ -1,5 +1,6 @@ using System; -using System.Windows; +using CommunityToolkit.Mvvm.ComponentModel; +using OpenCvSharp; namespace BetterGenshinImpact.Core.Config { @@ -7,37 +8,36 @@ namespace BetterGenshinImpact.Core.Config /// 遮罩窗口配置 /// [Serializable] - public class MaskWindowConfig + public partial class MaskWindowConfig : ObservableObject { /// /// 是否启用遮罩窗口 /// - public bool EnableMaskWindow { get; set; } = true; + [ObservableProperty] private bool _enableMaskWindow = true; /// /// 是否在遮罩窗口上显示识别结果 /// - public bool DisplayRecognitionResultsOnMaskWindow { get; set; } = true; + [ObservableProperty] private bool _displayRecognitionResultsOnMaskWindow = true; /// /// 显示遮罩窗口边框 /// - public bool ShowMaskWindowBorder { get; set; } = false; + [ObservableProperty] private bool _showMaskWindowBorder = false; /// /// 显示日志窗口 /// - public bool ShowLogBox { get; set; } = true; + [ObservableProperty] private bool _showLogBox = true; /// /// 日志窗口位置与大小 /// - public Rect LogBoxLocation { get; set; } = Rect.Empty; + [ObservableProperty] private Rect _logBoxLocation = Rect.Empty; /// /// 控件是否锁定(拖拽移动等) /// - public bool ControlLocked { get; set; } = false; - + [ObservableProperty] private bool _controlLocked = false; } -} +} \ No newline at end of file diff --git a/BetterGenshinImpact/GameTask/AutoFishing/AutoFishingConfig.cs b/BetterGenshinImpact/GameTask/AutoFishing/AutoFishingConfig.cs index 0cf3b784..3c747abc 100644 --- a/BetterGenshinImpact/GameTask/AutoFishing/AutoFishingConfig.cs +++ b/BetterGenshinImpact/GameTask/AutoFishing/AutoFishingConfig.cs @@ -1,5 +1,6 @@ using OpenCvSharp; using System; +using CommunityToolkit.Mvvm.ComponentModel; namespace BetterGenshinImpact.GameTask.AutoFishing { @@ -7,7 +8,7 @@ namespace BetterGenshinImpact.GameTask.AutoFishing /// 自动钓鱼配置 /// [Serializable] - public class AutoFishingConfig + public partial class AutoFishingConfig : ObservableObject, ITaskConfig { /// /// 触发器是否启用 @@ -16,18 +17,23 @@ namespace BetterGenshinImpact.GameTask.AutoFishing /// 2. 自动提杆 /// 3. 自动拉条 /// - public bool Enable { get; set; } = true; + [ObservableProperty] private bool _enabled = true; /// /// 鱼儿上钩文字识别区域 /// 暂时无用 /// - public Rect FishHookedRecognitionArea { get; set; } = Rect.Empty; + [ObservableProperty] private Rect _fishHookedRecognitionArea = Rect.Empty; + + /// + /// 自动抛竿是否启用 + /// + [ObservableProperty] private bool _autoThrowRodEnabled = true; /// /// 自动抛竿未上钩超时时间(秒) /// - public int AutoThrowRodTimeOut { get; set; } = 30; + [ObservableProperty] private int _autoThrowRodTimeOut = 30; } } diff --git a/BetterGenshinImpact/GameTask/AutoPick/AutoPickConfig.cs b/BetterGenshinImpact/GameTask/AutoPick/AutoPickConfig.cs new file mode 100644 index 00000000..7b52b5e1 --- /dev/null +++ b/BetterGenshinImpact/GameTask/AutoPick/AutoPickConfig.cs @@ -0,0 +1,18 @@ +using CommunityToolkit.Mvvm.ComponentModel; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BetterGenshinImpact.GameTask.AutoPick +{ + [Serializable] + public partial class AutoPickConfig : ObservableObject, ITaskConfig + { + /// + /// 触发器是否启用 + /// + [ObservableProperty] private bool _enabled = false; + } +} diff --git a/BetterGenshinImpact/GameTask/AutoSkip/AutoSkipConfig.cs b/BetterGenshinImpact/GameTask/AutoSkip/AutoSkipConfig.cs index 69db2f05..0eae044a 100644 --- a/BetterGenshinImpact/GameTask/AutoSkip/AutoSkipConfig.cs +++ b/BetterGenshinImpact/GameTask/AutoSkip/AutoSkipConfig.cs @@ -1,4 +1,5 @@ -using System; +using CommunityToolkit.Mvvm.ComponentModel; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -10,7 +11,7 @@ namespace BetterGenshinImpact.GameTask.AutoSkip /// 自动跳过剧情配置 /// [Serializable] - public class AutoSkipConfig + public partial class AutoSkipConfig : ObservableObject, ITaskConfig { /// /// 触发器是否启用 @@ -19,11 +20,11 @@ namespace BetterGenshinImpact.GameTask.AutoSkip /// 2. 自动点击一个识别到的选项 /// 3. 黑屏过长自动点击跳过 /// - public bool Enable { get; set; } = true; + [ObservableProperty] private bool _enabled = true; /// /// 快速跳过对话 /// - public bool QuicklySkipConversations { get; set; } = true; + [ObservableProperty] private bool _quicklySkipConversationsEnabled = true; } } diff --git a/BetterGenshinImpact/GameTask/ITaskConfig.cs b/BetterGenshinImpact/GameTask/ITaskConfig.cs new file mode 100644 index 00000000..3d33269e --- /dev/null +++ b/BetterGenshinImpact/GameTask/ITaskConfig.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BetterGenshinImpact.GameTask +{ + public class TaskConfig + { + } +} diff --git a/BetterGenshinImpact/Service/ConfigService.cs b/BetterGenshinImpact/Service/ConfigService.cs new file mode 100644 index 00000000..b31dfb9f --- /dev/null +++ b/BetterGenshinImpact/Service/ConfigService.cs @@ -0,0 +1,94 @@ +using BetterGenshinImpact.Core.Config; +using BetterGenshinImpact.Service.Interface; +using System; +using System.IO; +using System.Text.Json; +using System.Threading; + +namespace BetterGenshinImpact.Service; + +public class ConfigService : IConfigService +{ + private readonly ReaderWriterLockSlim _rwLock = new(); + private readonly JsonSerializerOptions _options = new() + { + NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowNamedFloatingPointLiterals, + WriteIndented = true + }; + + /// + /// 写入只有UI线程会调用 + /// 多线程只会读,放心用static,不会丢失数据 + /// + public static AllConfig? DefaultConfig { get; private set; } + + public AllConfig Get() + { + if (DefaultConfig == null) + { + DefaultConfig = Read(); + } + + return DefaultConfig; + } + + public void Save() + { + if (DefaultConfig != null) + { + Write(DefaultConfig); + } + } + + public AllConfig Read() + { + _rwLock.EnterReadLock(); + try + { + var filePath = Global.Absolute(@"Config/config.json"); + if (!File.Exists(filePath)) + { + return new AllConfig(); + } + + var json = File.ReadAllText(filePath); + var config = JsonSerializer.Deserialize(json); + if (config == null) + { + return new AllConfig(); + } + + DefaultConfig = config; + return config; + } + finally + { + _rwLock.ExitReadLock(); + } + } + + public void Write(AllConfig config) + { + _rwLock.EnterWriteLock(); + try + { + var path = Global.Absolute("Config"); + if (!Directory.Exists(path)) + { + Directory.CreateDirectory(path); + } + + var file = Path.Combine(path, "config.json"); + File.WriteAllText(file, JsonSerializer.Serialize(config, _options)); + } + catch (Exception e) + { + Console.WriteLine(e.Message); + Console.WriteLine(e.StackTrace); + } + finally + { + _rwLock.ExitWriteLock(); + } + } +} \ No newline at end of file diff --git a/BetterGenshinImpact/Service/Interface/IConfigService.cs b/BetterGenshinImpact/Service/Interface/IConfigService.cs new file mode 100644 index 00000000..e5a27926 --- /dev/null +++ b/BetterGenshinImpact/Service/Interface/IConfigService.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using BetterGenshinImpact.Core.Config; + +namespace BetterGenshinImpact.Service.Interface +{ + public interface IConfigService + { + AllConfig Get(); + + void Save(); + + AllConfig Read(); + + void Write(AllConfig config); + } +} diff --git a/BetterGenshinImpact/View/Pages/HomePage.xaml b/BetterGenshinImpact/View/Pages/HomePage.xaml index 341de404..155efd91 100644 --- a/BetterGenshinImpact/View/Pages/HomePage.xaml +++ b/BetterGenshinImpact/View/Pages/HomePage.xaml @@ -27,7 +27,7 @@