using FastMember; using HKW.HKWUtils.Observable; using LinePutScript; using System.ComponentModel; using System.Reflection; using System.Runtime.CompilerServices; using System.Windows; using VPet.Solution.Properties; using VPet_Simulator.Windows.Interface; namespace VPet.Solution.Models.SettingEditor; public class SettingModel : ObservableClass { /// /// 名称 /// public string Name { get; set; } /// /// 文件路径 /// public string FilePath { get; set; } #region GraphicsSetting private GraphicsSettingModel _graphicsSetting; public GraphicsSettingModel GraphicsSetting { get => _graphicsSetting; set => SetProperty(ref _graphicsSetting, value); } #endregion #region SystemSetting private SystemSettingModel _systemSetting; public SystemSettingModel SystemSetting { get => _systemSetting; set => SetProperty(ref _systemSetting, value); } #endregion #region InteractiveSetting private InteractiveSettingModel _interactiveSetting; public InteractiveSettingModel InteractiveSetting { get => _interactiveSetting; set => SetProperty(ref _interactiveSetting, value); } #endregion #region CustomizedSetting private CustomizedSettingModel _CustomizedSetting; public CustomizedSettingModel CustomizedSetting { get => _CustomizedSetting; set => SetProperty(ref _CustomizedSetting, value); } #endregion #region DiagnosticSetting private DiagnosticSettingModel _diagnosticSetting; public DiagnosticSettingModel DiagnosticSetting { get => _diagnosticSetting; set => SetProperty(ref _diagnosticSetting, value); } #endregion #region ModSetting private ModSettingModel _modSetting; public ModSettingModel ModSetting { get => _modSetting; set => SetProperty(ref _modSetting, value); } #endregion private readonly Setting _setting; private readonly ReflectionOptions _saveReflectionOptions = new() { CheckValueEquals = true }; public SettingModel() : this(new("")) { } public SettingModel(Setting setting) { _setting = setting; GraphicsSetting = LoadSetting(); InteractiveSetting = LoadSetting(); SystemSetting = LoadSetting(); CustomizedSetting = LoadCustomizedSetting(setting); DiagnosticSetting = LoadSetting(); DiagnosticSetting.SetAutoCalToSetting(setting); ModSetting = LoadModSetting(setting); } private ModSettingModel LoadModSetting(Setting setting) { var settingModel = new ModSettingModel(setting); return settingModel; } private CustomizedSettingModel LoadCustomizedSetting(Setting setting) { var model = new CustomizedSettingModel(); if (setting[CustomizedSettingModel.TargetName] is ILine line && line.Count > 0) { foreach (var sub in line) model.Links.Add(new(sub.Name, sub.Info)); } else { setting.Remove(CustomizedSettingModel.TargetName); } return model; } private T LoadSetting() where T : new() { var settingModel = new T(); ReflectionUtils.SetValue(_setting, settingModel); return settingModel; } public void Save() { SaveSetting(GraphicsSetting); SaveSetting(InteractiveSetting); SaveSetting(SystemSetting); SaveSetting(DiagnosticSetting); DiagnosticSetting.SetAutoCalToSetting(_setting); foreach (var link in CustomizedSetting.Links) _setting[CustomizedSettingModel.TargetName].Add(new Sub(link.Name, link.Link)); ModSetting.Save(_setting); File.WriteAllText(FilePath, _setting.ToString()); } private void SaveSetting(object settingModel) { ReflectionUtils.SetValue(settingModel, _setting, _saveReflectionOptions); } }