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 7a6db90..9150693 100644 --- a/VPet.Solution/VPet.Solution.csproj +++ b/VPet.Solution/VPet.Solution.csproj @@ -107,6 +107,7 @@ + diff --git a/VPet.Solution/ViewModels/MainWindowVM.cs b/VPet.Solution/ViewModels/MainWindowVM.cs index 08b817b..e5ca79c 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; } @@ -47,6 +59,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 @@