From e44a649c577f73b18739497080795b72bc912383 Mon Sep 17 00:00:00 2001 From: Hakoyu Date: Sun, 4 Feb 2024 00:08:31 +0800 Subject: [PATCH] =?UTF-8?q?#=20VPet.Solution=20##=20=E6=9B=B4=E6=96=B0=20-?= =?UTF-8?q?=20=E4=BF=AE=E6=94=B9=E4=B8=BB=E7=95=8C=E9=9D=A2=E6=96=87?= =?UTF-8?q?=E5=8C=96=E6=97=B6=E4=BC=9A=E5=90=8C=E6=AD=A5=E4=BF=AE=E6=94=B9?= =?UTF-8?q?`Setting.lps`=E7=9A=84=E6=96=87=E5=8C=96=E8=AE=BE=E7=BD=AE=20-?= =?UTF-8?q?=20=E4=BF=AE=E6=94=B9=E8=AE=BE=E7=BD=AE=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E7=9A=84=E8=AE=BE=E7=BD=AE=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=20##=20=E4=BF=AE=E5=A4=8D=20-=20=E5=AD=98?= =?UTF-8?q?=E5=9C=A8=E4=B8=A4=E4=B8=AA=E7=9B=B8=E5=90=8C=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=E7=9A=84mod=E6=97=B6=E5=90=AF=E5=8A=A8=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=20-=20`=E8=AE=BE=E7=BD=AE=E7=BC=96?= =?UTF-8?q?=E8=BE=91`-`=E5=9B=BE=E5=83=8F`=E9=A1=B5=E9=9D=A2=E8=AF=AD?= =?UTF-8?q?=E8=A8=80=E5=88=97=E8=A1=A8=E6=98=BE=E7=A4=BA=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SettingEditor/GraphicsSettingModel.cs | 6 +-- .../Models/SettingEditor/ModSettingModel.cs | 1 + .../Models/SettingEditor/SettingModel.cs | 33 +++++++++++++ VPet.Solution/Utils/CompareUtils.cs | 48 +++++++++++++++++++ VPet.Solution/VPet.Solution.csproj | 1 + VPet.Solution/ViewModels/MainWindowVM.cs | 19 +++++++- .../SettingEditor/SettingWindowVM.cs | 19 ++++---- VPet.Solution/Views/MainWindow.xaml.cs | 2 + .../SettingEditor/GraphicsSettingPage.xaml | 1 + 9 files changed, 117 insertions(+), 13 deletions(-) create mode 100644 VPet.Solution/Utils/CompareUtils.cs diff --git a/VPet.Solution/Models/SettingEditor/GraphicsSettingModel.cs b/VPet.Solution/Models/SettingEditor/GraphicsSettingModel.cs index ea6047a..a43e136 100644 --- a/VPet.Solution/Models/SettingEditor/GraphicsSettingModel.cs +++ b/VPet.Solution/Models/SettingEditor/GraphicsSettingModel.cs @@ -106,7 +106,7 @@ public class GraphicsSettingModel : ObservableClass #endregion #region Language - private string _language = Languages.First(); + private string _language; /// /// 语言 @@ -118,9 +118,7 @@ public class GraphicsSettingModel : ObservableClass set => SetProperty(ref _language, value); } - public static ObservableCollection Languages { get; } = - new() { "zh-Hans", "zh-Hant", "en" }; - // NOTE: 实际上这里面并没有文化 new(LocalizeCore.AvailableCultures); + public static IEnumerable Languages => LocalizeCore.AvailableCultures; #endregion diff --git a/VPet.Solution/Models/SettingEditor/ModSettingModel.cs b/VPet.Solution/Models/SettingEditor/ModSettingModel.cs index 7619f7b..94ec1eb 100644 --- a/VPet.Solution/Models/SettingEditor/ModSettingModel.cs +++ b/VPet.Solution/Models/SettingEditor/ModSettingModel.cs @@ -27,6 +27,7 @@ public class ModSettingModel : ObservableClass Directory .EnumerateDirectories(ModDirectory) .Select(d => new ModLoader(d)) + .Distinct(CompareUtils.Create(m => m.Name)) .ToDictionary(m => m.Name, m => m), StringComparer.OrdinalIgnoreCase ); diff --git a/VPet.Solution/Models/SettingEditor/SettingModel.cs b/VPet.Solution/Models/SettingEditor/SettingModel.cs index a3be916..b6add45 100644 --- a/VPet.Solution/Models/SettingEditor/SettingModel.cs +++ b/VPet.Solution/Models/SettingEditor/SettingModel.cs @@ -1,6 +1,7 @@ using FastMember; using HKW.HKWUtils.Observable; using LinePutScript; +using LinePutScript.Localization.WPF; using System.ComponentModel; using System.Reflection; using System.Runtime.CompilerServices; @@ -22,6 +23,19 @@ public class SettingModel : ObservableClass /// public string FilePath { get; set; } + //#region IsChanged + //private bool _isChanged; + + ///// + ///// 已更改 + ///// + //public bool IsChanged + //{ + // get => _isChanged; + // set => SetProperty(ref _isChanged, value); + //} + //#endregion + #region GraphicsSetting private GraphicsSettingModel _graphicsSetting; public GraphicsSettingModel GraphicsSetting @@ -89,14 +103,33 @@ public class SettingModel : ObservableClass { _setting = setting; GraphicsSetting = LoadSetting(); + if (string.IsNullOrWhiteSpace(GraphicsSetting.Language)) + GraphicsSetting.Language = LocalizeCore.CurrentCulture; InteractiveSetting = LoadSetting(); SystemSetting = LoadSetting(); CustomizedSetting = LoadCustomizedSetting(setting); DiagnosticSetting = LoadSetting(); DiagnosticSetting.SetAutoCalToSetting(setting); ModSetting = LoadModSetting(setting); + //MergeNotify(); } + //private void MergeNotify() + //{ + // var accessor = ObjectAccessor.Create(this); + // foreach (var property in typeof(SettingModel).GetProperties()) + // { + // var value = accessor[property.Name]; + // if (value is INotifyPropertyChanged model) + // model.PropertyChanged += Notify_PropertyChanged; + // } + //} + + //private void Notify_PropertyChanged(object sender, PropertyChangedEventArgs e) + //{ + // IsChanged = true; + //} + private ModSettingModel LoadModSetting(Setting setting) { var settingModel = new ModSettingModel(setting); diff --git a/VPet.Solution/Utils/CompareUtils.cs b/VPet.Solution/Utils/CompareUtils.cs new file mode 100644 index 0000000..abd6b79 --- /dev/null +++ b/VPet.Solution/Utils/CompareUtils.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HKW.HKWUtils; + +/// +/// 比较工具 +/// +/// 比较类型 +public class CompareUtils : IEqualityComparer, IEquatable> +{ + /// + /// (Comparer, CompareUtils) + /// + public static Dictionary, CompareUtils> Comparers = new(); + + public static CompareUtils Create(Func comparer) + { + if (Comparers.TryGetValue(comparer, out var value) is false) + value = Comparers[comparer] = new CompareUtils(comparer); + return value; + } + + public Func Comparer { get; set; } + + public CompareUtils(Func comparer) + { + Comparer = comparer; + } + + public bool Equals(T x, T y) + { + return Comparer(x).Equals(Comparer(y)); + } + + public int GetHashCode(T obj) + { + return Comparer(obj).GetHashCode(); + } + + public bool Equals(CompareUtils other) + { + return Comparer.Equals(other.Comparer); + } +} diff --git a/VPet.Solution/VPet.Solution.csproj b/VPet.Solution/VPet.Solution.csproj index 1db3f59..f22d1b0 100644 --- a/VPet.Solution/VPet.Solution.csproj +++ b/VPet.Solution/VPet.Solution.csproj @@ -105,6 +105,7 @@ + diff --git a/VPet.Solution/ViewModels/MainWindowVM.cs b/VPet.Solution/ViewModels/MainWindowVM.cs index 923b148..20f71a9 100644 --- a/VPet.Solution/ViewModels/MainWindowVM.cs +++ b/VPet.Solution/ViewModels/MainWindowVM.cs @@ -6,16 +6,28 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using VPet.Solution.Models.SettingEditor; +using VPet.Solution.ViewModels.SettingEditor; +using VPet_Simulator.Windows.Interface; namespace VPet.Solution.ViewModels; public class MainWindowVM : ObservableClass { + private readonly SettingModel _mainSetting; + public MainWindowVM() { LocalizeCore.StoreTranslation = true; LocalizeCore.LoadDefaultCulture(); - CurrentCulture = LocalizeCore.CurrentCulture; + _mainSetting = SettingWindowVM.Current.ShowSettings.FirstOrDefault( + m => m.Name == nameof(Setting) + ); + if (string.IsNullOrWhiteSpace(_mainSetting.GraphicsSetting.Language)) + CurrentCulture = LocalizeCore.CurrentCulture; + else + CurrentCulture = _mainSetting.GraphicsSetting.Language; + FirstStartFailedCommand.ExecuteCommand += FirstStartFailedCommand_ExecuteCommand; OpenLocalTextCommand.ExecuteCommand += OpenLocalTextCommand_ExecuteCommand; } @@ -46,6 +58,11 @@ public class MainWindowVM : ObservableClass { SetProperty(ref _currentCulture, value); LocalizeCore.LoadCulture(_currentCulture); + if (_mainSetting is not null) + { + _mainSetting.GraphicsSetting.Language = _currentCulture; + _mainSetting.Save(); + } } } #endregion diff --git a/VPet.Solution/ViewModels/SettingEditor/SettingWindowVM.cs b/VPet.Solution/ViewModels/SettingEditor/SettingWindowVM.cs index 14265f6..114cee7 100644 --- a/VPet.Solution/ViewModels/SettingEditor/SettingWindowVM.cs +++ b/VPet.Solution/ViewModels/SettingEditor/SettingWindowVM.cs @@ -20,13 +20,12 @@ namespace VPet.Solution.ViewModels.SettingEditor; public class SettingWindowVM : ObservableClass { public static SettingWindowVM Current { get; private set; } - #region Properties - private SettingModel _currentSettings; + private SettingModel _currentSetting; public SettingModel CurrentSetting { - get => _currentSettings; - set => SetProperty(ref _currentSettings, value); + get => _currentSetting; + set { SetProperty(ref _currentSetting, value); } } private readonly ObservableCollection _settings = new(); @@ -81,8 +80,8 @@ public class SettingWindowVM : ObservableClass public SettingWindowVM() { Current = this; - ShowSettings = _settings; LoadSettings(); + ShowSettings = _settings = new(_settings.OrderBy(m => m.Name)); PropertyChanged += MainWindowVM_PropertyChanged; OpenFileCommand.ExecuteCommand += OpenFileCommand_ExecuteCommand; @@ -204,9 +203,13 @@ public class SettingWindowVM : ObservableClass "载入设置出错".Translate(), MessageBoxButton.YesNo, MessageBoxImage.Warning - ) is MessageBoxResult.Yes + ) + is not MessageBoxResult.Yes ) - _settings.Add(new SettingModel() { Name = fileName, FilePath = file }); + return; + var setting = new SettingModel() { Name = fileName, FilePath = file }; + _settings.Add(setting); + setting.Save(); } } } @@ -220,7 +223,7 @@ public class SettingWindowVM : ObservableClass { if (s.EndsWith(".lps") is false) return false; - return Path.GetFileName(s).StartsWith("Setting"); + return Path.GetFileName(s).StartsWith(nameof(Setting)); } ); } diff --git a/VPet.Solution/Views/MainWindow.xaml.cs b/VPet.Solution/Views/MainWindow.xaml.cs index 28e700b..df87ac5 100644 --- a/VPet.Solution/Views/MainWindow.xaml.cs +++ b/VPet.Solution/Views/MainWindow.xaml.cs @@ -5,9 +5,11 @@ using System.ComponentModel; using System.Text; using System.Windows; using System.Windows.Controls; +using VPet.Solution.Models.SettingEditor; using VPet.Solution.ViewModels; using VPet.Solution.Views.SaveViewer; using VPet.Solution.Views.SettingEditor; +using VPet_Simulator.Windows.Interface; namespace VPet.Solution.Views; diff --git a/VPet.Solution/Views/SettingEditor/GraphicsSettingPage.xaml b/VPet.Solution/Views/SettingEditor/GraphicsSettingPage.xaml index f249265..0830bf9 100644 --- a/VPet.Solution/Views/SettingEditor/GraphicsSettingPage.xaml +++ b/VPet.Solution/Views/SettingEditor/GraphicsSettingPage.xaml @@ -70,6 +70,7 @@