-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathReHUDVersion.cs
86 lines (73 loc) · 3.58 KB
/
ReHUDVersion.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using Newtonsoft.Json.Linq;
using ReHUD.Models;
namespace ReHUD;
public class ReHUDVersion : UserData
{
protected override string? DEFAULT_WHEN_EMPTY => null;
public static readonly Version DEFAULT_VERSION = Version.Parse("0.0.0");
protected override string DataFilePath => "ReHUD.version";
private Version version = DEFAULT_VERSION;
public override string Serialize() {
return version.ToString();
}
protected override void Load(string? data) {
version = TrimVersion(data);
}
public async Task Update(string version) {
var newVersion = TrimVersion(version);
var compare = this.version.CompareTo(newVersion);
if (compare == 0) return;
if (compare > 0) {
await Startup.QuitApp($"Cannot downgrade ReHUD version.\nIf you wish to downgrade, please remove your '{dataPath}' folder first.\nThis will reset all settings and saved user data, such as fuel consumption, tire wear and laptimes.");
return;
}
OnVersionChanged(this.version, newVersion);
this.version = newVersion;
Save();
}
private static void OnVersionChanged(Version previousVersion, Version version) {
Startup.logger.InfoFormat("Upgrading ReHUD from v{0} to v{1}", previousVersion, version);
var matchingActions = UpgradeActions.Where(x => x.Item1 > previousVersion && x.Item1 <= version);
foreach (var versionActions in matchingActions) {
foreach (var action in versionActions.Item2) {
Startup.logger.InfoFormat("Running upgrade action v{0}: {1}", versionActions.Item1, action.Item1);
action.Item2();
}
}
}
// Version, Array of (Action name, Action)
private static readonly Tuple<Version, Tuple<string, Action>[]>[] UpgradeActions = new Tuple<Version, Tuple<string, Action>[]>[]
{
new(Version.Parse("0.8.0"), new Tuple<string, Action>[] {
new("Move HUD layout to separate file", () => {
try {
Startup.settings.Load();
HudLayout.LoadHudLayouts();
var hudLayout = (JObject?) Startup.settings.Data.Remove("hudLayout") ?? (!HudLayout.Layouts.Any() ? new JObject() : null);
if (hudLayout != null) {
Startup.logger.Info("Found HUD layout in settings, moving to separate file");
HudLayout hudLayoutData = HudLayout.AddHudLayout(new(true));
foreach (var element in hudLayout) {
if (element.Value is JArray elementData) {
var id = element.Key;
var left = elementData[0]?.Value<double?>() ?? 0;
var top = elementData[1]?.Value<double?>() ?? 0;
var scale = elementData[2]?.Value<double>() ?? 1;
var shown = elementData[3]?.Value<bool>() ?? true;
hudLayoutData.AddElement(id, left, top, scale, shown);
}
}
hudLayoutData.Save();
Startup.settings.Save();
}
} catch (Exception e) {
Startup.logger.Error("Failed to move HUD layout to separate file", e);
}
}),
}),
};
public static Version TrimVersion(string? version) {
if (version == null) return DEFAULT_VERSION;
return Version.Parse(version.Split('v')[^1].Split('-')[0]);
}
}