-
Notifications
You must be signed in to change notification settings - Fork 809
Expand file tree
/
Copy pathInstallOptions.cs
More file actions
232 lines (198 loc) · 10.2 KB
/
InstallOptions.cs
File metadata and controls
232 lines (198 loc) · 10.2 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using System.Text.Json;
using System.Text.Json.Nodes;
using UniGetUI.Core.Data;
namespace UniGetUI.PackageEngine.Serializable
{
public class InstallOptions: SerializableComponent<InstallOptions>
{
private readonly IReadOnlyDictionary<string, bool> DefaultBoolValues = new Dictionary<string, bool>()
{ // OverridesNextLevelOpts is deliberately skipped here
{ "SkipHashCheck", false },
{ "InteractiveInstallation", false },
{ "RunAsAdministrator", false },
{ "PreRelease", false },
{ "SkipMinorUpdates", false },
{ "RemoveDataOnUninstall", false },
{ "UninstallPreviousVersionsOnUpdate", false },
{ "AbortOnPreInstallFail", true },
{ "AbortOnPreUpdateFail", true },
{ "AbortOnPreUninstallFail", true },
};
private readonly IReadOnlyDictionary<string, string> DefaultStringValues = new Dictionary<string, string>()
{
{"Architecture", ""},
{"InstallationScope", ""},
{"CustomInstallLocation", ""},
{"Version", ""},
{"PreInstallCommand", ""},
{"PostInstallCommand", ""},
{"PreUpdateCommand", ""},
{"PostUpdateCommand", ""},
{"PreUninstallCommand", ""},
{"PostUninstallCommand", ""},
};
private readonly IReadOnlyList<string> DefaultListValues = new List<string>()
{
"CustomParameters_Install",
"CustomParameters_Update",
"CustomParameters_Uninstall",
"KillBeforeOperation",
};
public bool SkipHashCheck { get; set; }
public bool InteractiveInstallation { get; set; }
public bool RunAsAdministrator { get; set; }
public string Architecture { get; set; } = "";
public string InstallationScope { get; set; } = "";
public List<string> CustomParameters_Install { get; set; } = [];
public List<string> CustomParameters_Update { get; set; } = [];
public List<string> CustomParameters_Uninstall { get; set; } = [];
public bool PreRelease { get; set; }
public string CustomInstallLocation { get; set; } = "";
public string Version { get; set; } = "";
public bool SkipMinorUpdates { get; set; }
public bool RemoveDataOnUninstall { get; set; }
public bool UninstallPreviousVersionsOnUpdate { get; set; }
public List<string> KillBeforeOperation { get; set; } = [];
public string PreInstallCommand { get; set; } = "";
public string PostInstallCommand { get; set; } = "";
public bool AbortOnPreInstallFail { get; set; } = true;
public string PreUpdateCommand { get; set; } = "";
public string PostUpdateCommand { get; set; } = "";
public bool AbortOnPreUpdateFail { get; set; } = true;
public string PreUninstallCommand { get; set; } = "";
public string PostUninstallCommand { get; set; } = "";
public bool AbortOnPreUninstallFail { get; set; } = true;
public bool OverridesNextLevelOpts { get; set; }
public override InstallOptions Copy()
{
var copy = new InstallOptions();
foreach (var (boolKey, _) in DefaultBoolValues)
copy.SetValueToProperty(boolKey, GetValueFromProperty<bool>(boolKey));
foreach (var (stringKey, _) in DefaultStringValues)
copy.SetValueToProperty(stringKey, GetValueFromProperty<string>(stringKey));
foreach (var listKey in DefaultListValues)
copy.SetValueToProperty(listKey, GetValueFromProperty<IReadOnlyList<string>>(listKey).ToList());
// Handle non-automated OverridesNextLevelOpts
copy.OverridesNextLevelOpts = OverridesNextLevelOpts;
return copy;
}
public void SetValueToProperty<T>(string name, T value)
{
var property = this.GetType().GetProperty(name);
property?.SetValue(this, value);
}
public T GetValueFromProperty<T>(string name)
{
var property = this.GetType().GetProperty(name);
return (T)(property?.GetValue(this) ?? throw new InvalidDataException($"Invalid datatype for property {name} (expected {nameof(T)})"));
}
public override void LoadFromJson(JsonNode data)
{
foreach (var (boolKey, defValue) in DefaultBoolValues)
{
// RemoveDataOnUninstall should not be loaded from disk
if(boolKey == "RemoveDataOnUninstall") continue;
SetValueToProperty(boolKey, data[boolKey]?.GetVal<bool>() ?? defValue);
}
foreach (var (stringKey, defValue) in DefaultStringValues)
SetValueToProperty(stringKey, data[stringKey]?.GetVal<string>() ?? defValue);
foreach (var listKey in DefaultListValues)
SetValueToProperty(listKey, ReadArrayFromJson(data, listKey));
// Handle case where setting has not been migrated yet
if (this.CustomParameters_Install.Count is 0 &&
this.CustomParameters_Update.Count is 0 &&
this.CustomParameters_Uninstall.Count is 0 &&
((data as JsonObject)?.ContainsKey("CustomParameters") ?? false))
{
this.CustomParameters_Install = ReadArrayFromJson(data, "CustomParameters");
this.CustomParameters_Update = ReadArrayFromJson(data, "CustomParameters");
this.CustomParameters_Uninstall = ReadArrayFromJson(data, "CustomParameters");
}
// if OverridesNextLevelOpts is not found on the JSON, set it to true or false depending
// on whether the current settings instances are different from the default values.
// This entry shall be checked the last one, to ensure all other properties are set
this.OverridesNextLevelOpts = data[nameof(OverridesNextLevelOpts)]?.GetValue<bool>() ?? DiffersFromDefault();
}
public override JsonNode AsJsonNode()
{
JsonObject obj = new();
if (OverridesNextLevelOpts is not false)
obj.Add(nameof(OverridesNextLevelOpts), OverridesNextLevelOpts);
foreach (var (boolKey, defValue) in DefaultBoolValues)
{
bool currentValue = GetValueFromProperty<bool>(boolKey);
if (currentValue != defValue) obj.Add(boolKey, currentValue);
}
foreach (var (stringKey, defValue) in DefaultStringValues)
{
string currentValue = GetValueFromProperty<string>(stringKey);
if (currentValue != defValue) obj.Add(stringKey, currentValue);
}
foreach (var listKey in DefaultListValues)
{
IReadOnlyList<string> currentValue = GetValueFromProperty<IReadOnlyList<string>>(listKey);
if (currentValue.Where(x => x.Any()).Any())
{
obj.Add(listKey, new JsonArray(currentValue.Select(x => JsonValue.Create(x) as JsonNode).ToArray()));
}
}
return obj;
}
private static List<string> ReadArrayFromJson(JsonNode data, string name)
{
List<string> result = new List<string>();
foreach (var element in data[name]?.AsArray2() ?? [])
if (element is not null)
result.Add(element.GetVal<string>());
return result.Where(x => x.Any()).ToList();
}
public bool DiffersFromDefault()
{
foreach (var (boolKey, defValue) in DefaultBoolValues)
if (GetValueFromProperty<bool>(boolKey) != defValue) return true;
foreach (var (stringKey, defValue) in DefaultStringValues)
if (GetValueFromProperty<string>(stringKey) != defValue) return true;
foreach (var listKey in DefaultListValues)
{
IReadOnlyList<string> currentValue = GetValueFromProperty<IReadOnlyList<string>>(listKey);
if (currentValue.Where(x => x.Any()).Any()) return true;
}
return false;
// OverridesNextLevelOpts does not need to be checked here, since
// this method is invoked before this property has been set
}
public InstallOptions() : base()
{
}
public InstallOptions(JsonNode data) : base(data)
{
}
public override string ToString()
{
string customparams = CustomParameters_Install.Any() ? string.Join(",", CustomParameters_Install) : "[],";
customparams += CustomParameters_Update.Any() ? string.Join(",", CustomParameters_Update) : "[],";
customparams += CustomParameters_Uninstall.Any() ? string.Join(",", CustomParameters_Uninstall) : "[]";
return $"<InstallOptions: SkipHashCheck={SkipHashCheck};" +
$"InteractiveInstallation={InteractiveInstallation};" +
$"RunAsAdministrator={RunAsAdministrator};" +
$"Version={Version};" +
$"Architecture={Architecture};" +
$"InstallationScope={InstallationScope};" +
$"InstallationScope={CustomInstallLocation};" +
$"CustomParameters={customparams};" +
$"RemoveDataOnUninstall={RemoveDataOnUninstall};" +
$"KillBeforeOperation={KillBeforeOperation};" +
$"PreInstallCommand={PreInstallCommand};" +
$"PostInstallCommand={PostInstallCommand};" +
$"AbortOnPreInstallFail={AbortOnPreInstallFail};" +
$"PreUpdateCommand={PreUpdateCommand};" +
$"PostUpdateCommand={PostUpdateCommand};" +
$"AbortOnPreUpdateFail={AbortOnPreUpdateFail};" +
$"PreUninstallCommand={PreUninstallCommand};" +
$"PostUninstallCommand={PostUninstallCommand};" +
$"UninstallPreviousVersionsOnUpdate={UninstallPreviousVersionsOnUpdate}" +
$"AbortOnPreUninstallFail={AbortOnPreUninstallFail};" +
$"PreRelease={PreRelease}>";
}
}
}