wabbajack/Wabbajack/Settings.cs

158 lines
5.6 KiB
C#
Raw Normal View History

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reactive;
using System.Reactive.Subjects;
using System.Threading.Tasks;
using Wabbajack.Common;
2020-04-23 04:02:50 +00:00
using Wabbajack.Common.Serialization.Json;
using Wabbajack.Lib;
namespace Wabbajack
{
2020-04-23 04:02:50 +00:00
[JsonName("MainSettings")]
[JsonObject(MemberSerialization.OptOut)]
public class MainSettings
{
2020-04-23 04:02:50 +00:00
public byte Version { get; set; } = Consts.SettingsVersion;
public double PosX { get; set; }
public double PosY { get; set; }
public double Height { get; set; }
public double Width { get; set; }
public InstallerSettings Installer { get; set; } = new InstallerSettings();
public FiltersSettings Filters { get; set; } = new FiltersSettings();
public CompilerSettings Compiler { get; set; } = new CompilerSettings();
public PerformanceSettings Performance { get; set; } = new PerformanceSettings();
private Subject<Unit> _saveSignal = new Subject<Unit>();
[JsonIgnore]
public IObservable<Unit> SaveSignal => _saveSignal;
public static async ValueTask<(MainSettings settings, bool loaded)> TryLoadTypicalSettings()
{
2020-03-28 20:04:22 +00:00
if (!Consts.SettingsFile.Exists)
{
return default;
}
// Version check
try
{
var settings = Consts.SettingsFile.FromJson<MainSettings>();
if (settings.Version == Consts.SettingsVersion)
return (settings, true);
}
catch (Exception ex)
{
Utils.Error(ex, "Error loading settings");
}
var backup = Consts.SettingsFile.AppendToName("-backup");
2020-05-26 11:31:11 +00:00
await backup.DeleteAsync();
await Consts.SettingsFile.CopyToAsync(backup);
2020-05-26 11:31:11 +00:00
await Consts.SettingsFile.DeleteAsync();
return default;
}
public static async ValueTask SaveSettings(MainSettings settings)
{
settings._saveSignal.OnNext(Unit.Default);
// Might add this if people are putting save work on other threads or other
// things that delay the operation.
//settings._saveSignal.OnCompleted();
//await settings._saveSignal;
await settings.ToJsonAsync(Consts.SettingsFile);
}
}
2020-04-23 04:02:50 +00:00
[JsonName("InstallerSettings")]
public class InstallerSettings
{
2020-03-28 20:04:22 +00:00
public AbsolutePath LastInstalledListLocation { get; set; }
public Dictionary<AbsolutePath, Mo2ModlistInstallationSettings> Mo2ModlistSettings { get; } = new Dictionary<AbsolutePath, Mo2ModlistInstallationSettings>();
}
2020-04-23 04:02:50 +00:00
[JsonName("Mo2ModListInstallerSettings")]
2019-12-03 02:38:33 +00:00
public class Mo2ModlistInstallationSettings
{
2020-03-28 20:04:22 +00:00
public AbsolutePath InstallationLocation { get; set; }
public AbsolutePath DownloadLocation { get; set; }
public bool AutomaticallyOverrideExistingInstall { get; set; }
}
2020-04-23 04:02:50 +00:00
[JsonName("CompilerSettings")]
public class CompilerSettings
{
public ModManager LastCompiledModManager { get; set; }
2020-03-28 20:04:22 +00:00
public AbsolutePath OutputLocation { get; set; }
public MO2CompilationSettings MO2Compilation { get; } = new MO2CompilationSettings();
}
[JsonName("FiltersSettings")]
[JsonObject(MemberSerialization.OptOut)]
public class FiltersSettings : ViewModel
{
2020-05-10 04:59:34 +00:00
public bool ShowNSFW { get; set; }
public bool OnlyInstalled { get; set; }
public string Game { get; set; }
public string Search { get; set; }
private bool _isPersistent = true;
2020-05-10 04:59:34 +00:00
public bool IsPersistent { get => _isPersistent; set => RaiseAndSetIfChanged(ref _isPersistent, value); }
}
2020-04-23 04:02:50 +00:00
[JsonName("PerformanceSettings")]
[JsonObject(MemberSerialization.OptOut)]
public class PerformanceSettings : ViewModel
{
private bool _manual;
public bool Manual { get => _manual; set => RaiseAndSetIfChanged(ref _manual, value); }
private byte _maxCores = byte.MaxValue;
public byte MaxCores { get => _maxCores; set => RaiseAndSetIfChanged(ref _maxCores, value); }
private Percent _targetUsage = Percent.One;
public Percent TargetUsage { get => _targetUsage; set => RaiseAndSetIfChanged(ref _targetUsage, value); }
public void AttachToBatchProcessor(ABatchProcessor processor)
{
processor.Add(
this.WhenAny(x => x.Manual)
.Subscribe(processor.ManualCoreLimit));
processor.Add(
this.WhenAny(x => x.MaxCores)
.Subscribe(processor.MaxCores));
processor.Add(
this.WhenAny(x => x.TargetUsage)
.Subscribe(processor.TargetUsagePercent));
}
}
2020-04-23 04:02:50 +00:00
[JsonName("CompilationModlistSettings")]
public class CompilationModlistSettings
{
public string ModListName { get; set; }
2020-04-16 15:16:49 +00:00
public string Version { get; set; }
public string Author { get; set; }
public string Description { get; set; }
public string Website { get; set; }
public string Readme { get; set; }
2020-04-27 09:58:33 +00:00
public bool IsNSFW { get; set; }
2020-03-28 20:04:22 +00:00
public AbsolutePath SplashScreen { get; set; }
}
2020-04-23 04:02:50 +00:00
[JsonName("MO2CompilationSettings")]
public class MO2CompilationSettings
{
2020-03-28 20:04:22 +00:00
public AbsolutePath DownloadLocation { get; set; }
public AbsolutePath LastCompiledProfileLocation { get; set; }
public Dictionary<AbsolutePath, CompilationModlistSettings> ModlistSettings { get; } = new Dictionary<AbsolutePath, CompilationModlistSettings>();
}
}