VPet/VPet.Solution/Models/SettingEditor/SettingModel.cs

147 lines
4.1 KiB
C#
Raw Normal View History

2023-12-30 14:54:49 +00:00
using FastMember;
using HKW.HKWUtils.Observable;
2024-01-09 15:22:25 +00:00
using LinePutScript;
2023-12-30 14:54:49 +00:00
using System.ComponentModel;
2024-01-10 13:37:17 +00:00
using System.Reflection;
2023-12-30 14:54:49 +00:00
using System.Runtime.CompilerServices;
using System.Windows;
using VPet.Solution.Properties;
using VPet_Simulator.Windows.Interface;
2024-01-07 14:57:27 +00:00
namespace VPet.Solution.Models.SettingEditor;
2023-12-30 14:54:49 +00:00
public class SettingModel : ObservableClass<SettingModel>
{
/// <summary>
/// 名称
/// </summary>
2024-01-11 16:28:22 +00:00
public string Name { get; set; }
2023-12-30 14:54:49 +00:00
/// <summary>
2024-01-09 15:22:25 +00:00
/// 文件路径
2023-12-30 14:54:49 +00:00
/// </summary>
2024-01-11 16:28:22 +00:00
public string FilePath { get; set; }
2023-12-30 14:54:49 +00:00
2024-01-11 16:28:22 +00:00
#region GraphicsSetting
2023-12-30 14:54:49 +00:00
private GraphicsSettingModel _graphicsSetting;
public GraphicsSettingModel GraphicsSetting
{
get => _graphicsSetting;
set => SetProperty(ref _graphicsSetting, value);
}
2024-01-11 16:28:22 +00:00
#endregion
2023-12-30 14:54:49 +00:00
2024-01-11 16:28:22 +00:00
#region SystemSetting
2023-12-30 14:54:49 +00:00
private SystemSettingModel _systemSetting;
public SystemSettingModel SystemSetting
{
get => _systemSetting;
set => SetProperty(ref _systemSetting, value);
}
2024-01-11 16:28:22 +00:00
#endregion
2023-12-30 14:54:49 +00:00
2024-01-11 16:28:22 +00:00
#region InteractiveSetting
2023-12-30 14:54:49 +00:00
private InteractiveSettingModel _interactiveSetting;
public InteractiveSettingModel InteractiveSetting
{
get => _interactiveSetting;
set => SetProperty(ref _interactiveSetting, value);
}
2024-01-11 16:28:22 +00:00
#endregion
2023-12-30 14:54:49 +00:00
#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
2024-01-09 15:22:25 +00:00
private readonly Setting _setting;
2024-01-09 15:22:25 +00:00
private readonly ReflectionOptions _saveReflectionOptions = new() { CheckValueEquals = true };
2024-01-10 13:37:17 +00:00
2024-01-11 16:28:22 +00:00
public SettingModel()
: this(new("")) { }
2023-12-30 14:54:49 +00:00
public SettingModel(Setting setting)
{
2024-01-09 15:22:25 +00:00
_setting = setting;
2024-01-11 16:28:22 +00:00
GraphicsSetting = LoadSetting<GraphicsSettingModel>();
InteractiveSetting = LoadSetting<InteractiveSettingModel>();
SystemSetting = LoadSetting<SystemSettingModel>();
CustomizedSetting = LoadCustomizedSetting(setting);
DiagnosticSetting = LoadSetting<DiagnosticSettingModel>();
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;
2023-12-30 14:54:49 +00:00
}
2024-01-11 16:28:22 +00:00
private T LoadSetting<T>()
where T : new()
2023-12-30 14:54:49 +00:00
{
var settingModel = new T();
ReflectionUtils.SetValue(_setting, settingModel);
return settingModel;
2023-12-30 14:54:49 +00:00
}
2024-01-09 15:22:25 +00:00
public void Save()
{
2024-01-11 16:28:22 +00:00
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);
2024-01-09 15:22:25 +00:00
File.WriteAllText(FilePath, _setting.ToString());
}
private void SaveSetting(object settingModel)
2024-01-09 15:22:25 +00:00
{
ReflectionUtils.SetValue(settingModel, _setting, _saveReflectionOptions);
2024-01-09 15:22:25 +00:00
}
2023-12-30 14:54:49 +00:00
}