Merge pull request #605 from erri120/issue-587

PR: Store settings.json in local appdata
This commit is contained in:
Timothy Baldridge 2020-03-04 05:12:30 -07:00 committed by GitHub
commit 64cad0752f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 14 deletions

View File

@ -106,5 +106,8 @@ namespace Wabbajack.Common
public static string LogsFolder = "logs"; public static string LogsFolder = "logs";
public static int MaxOldLogs = 50; public static int MaxOldLogs = 50;
public static string SettingsFile => Path.Combine(LocalAppDataPath, "settings.json");
public static byte SettingsVersion => 1;
} }
} }

View File

@ -1,10 +1,8 @@
using Newtonsoft.Json; using Newtonsoft.Json;
using ReactiveUI.Fody.Helpers;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Reactive; using System.Reactive;
using System.Reactive.Disposables;
using System.Reactive.Subjects; using System.Reactive.Subjects;
using Wabbajack.Common; using Wabbajack.Common;
using Wabbajack.Lib; using Wabbajack.Lib;
@ -14,7 +12,7 @@ namespace Wabbajack
[JsonObject(MemberSerialization.OptOut)] [JsonObject(MemberSerialization.OptOut)]
public class MainSettings public class MainSettings
{ {
private static string _filename = "settings.json"; public byte Version { get; set; }
public double PosX { get; set; } public double PosX { get; set; }
public double PosY { get; set; } public double PosY { get; set; }
@ -30,13 +28,26 @@ namespace Wabbajack
public static bool TryLoadTypicalSettings(out MainSettings settings) public static bool TryLoadTypicalSettings(out MainSettings settings)
{ {
if (!File.Exists(_filename)) if (!File.Exists(Consts.SettingsFile))
{ {
settings = default; settings = default;
return false; return false;
} }
settings = JsonConvert.DeserializeObject<MainSettings>(File.ReadAllText(_filename));
return true; // Version check
settings = JsonConvert.DeserializeObject<MainSettings>(File.ReadAllText(Consts.SettingsFile));
if (settings.Version == Consts.SettingsVersion)
return true;
var backup = Consts.SettingsFile + "-backup.json";
if(File.Exists(backup))
File.Delete(backup);
File.Copy(Consts.SettingsFile, backup);
File.Delete(Consts.SettingsFile);
settings = default;
return false;
} }
public static void SaveSettings(MainSettings settings) public static void SaveSettings(MainSettings settings)
@ -48,7 +59,7 @@ namespace Wabbajack
//settings._saveSignal.OnCompleted(); //settings._saveSignal.OnCompleted();
//await settings._saveSignal; //await settings._saveSignal;
File.WriteAllText(_filename, JsonConvert.SerializeObject(settings, Formatting.Indented)); File.WriteAllText(Consts.SettingsFile, JsonConvert.SerializeObject(settings, Formatting.Indented));
} }
} }
@ -76,14 +87,14 @@ namespace Wabbajack
[JsonObject(MemberSerialization.OptOut)] [JsonObject(MemberSerialization.OptOut)]
public class PerformanceSettings : ViewModel public class PerformanceSettings : ViewModel
{ {
private bool _Manual = false; private bool _manual;
public bool Manual { get => _Manual; set => this.RaiseAndSetIfChanged(ref _Manual, value); } public bool Manual { get => _manual; set => RaiseAndSetIfChanged(ref _manual, value); }
private byte _MaxCores = byte.MaxValue; private byte _maxCores = byte.MaxValue;
public byte MaxCores { get => _MaxCores; set => this.RaiseAndSetIfChanged(ref _MaxCores, value); } public byte MaxCores { get => _maxCores; set => RaiseAndSetIfChanged(ref _maxCores, value); }
private Percent _TargetUsage = Percent.One; private Percent _targetUsage = Percent.One;
public Percent TargetUsage { get => _TargetUsage; set => this.RaiseAndSetIfChanged(ref _TargetUsage, value); } public Percent TargetUsage { get => _targetUsage; set => RaiseAndSetIfChanged(ref _targetUsage, value); }
public void AttachToBatchProcessor(ABatchProcessor processor) public void AttachToBatchProcessor(ABatchProcessor processor)
{ {

View File

@ -59,7 +59,10 @@ namespace Wabbajack
}; };
if (CLIArguments.NoSettings || !MainSettings.TryLoadTypicalSettings(out var settings)) if (CLIArguments.NoSettings || !MainSettings.TryLoadTypicalSettings(out var settings))
{ {
_settings = new MainSettings(); _settings = new MainSettings
{
Version = Consts.SettingsVersion
};
RunWhenLoaded(DefaultSettings); RunWhenLoaded(DefaultSettings);
} }
else else