-
Notifications
You must be signed in to change notification settings - Fork 815
Expand file tree
/
Copy pathInstallOptionsFactory.cs
More file actions
215 lines (190 loc) · 9.1 KB
/
InstallOptionsFactory.cs
File metadata and controls
215 lines (190 loc) · 9.1 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
using System.Collections.Concurrent;
using System.Text.Json;
using System.Text.Json.Nodes;
using UniGetUI.Core.Data;
using UniGetUI.Core.Logging;
using UniGetUI.Core.SettingsEngine.SecureSettings;
using UniGetUI.Core.Tools;
using UniGetUI.PackageEngine.Interfaces;
using UniGetUI.PackageEngine.Serializable;
namespace UniGetUI.PackageEngine.PackageClasses
{
/// <summary>
/// This class represents the options in which a package must be installed, updated or uninstalled.
/// </summary>
public static class InstallOptionsFactory
{
private static class StoragePath
{
public static string Get(IPackageManager manager)
=> "GlobalValues." + manager.Name.Replace(" ", "").Replace(".", "");
public static string Get(IPackage package)
=> package.Manager.Name.Replace(" ", "").Replace(".", "") + "." + package.Id;
}
// Loading from disk (package and manager)
public static InstallOptions LoadForPackage(IPackage package)
=> _loadFromDisk(StoragePath.Get(package));
public static Task<InstallOptions> LoadForPackageAsync(IPackage package)
=> Task.Run(() => LoadForPackage(package));
public static InstallOptions LoadForManager(IPackageManager manager)
=> _loadFromDisk(StoragePath.Get(manager));
public static Task<InstallOptions> LoadForManagerAsync(IPackageManager manager)
=> Task.Run(() => LoadForManager(manager));
// Saving to disk (package and manager)
public static void SaveForPackage(InstallOptions options, IPackage package)
=> _saveToDisk(options, StoragePath.Get(package));
public static Task SaveForPackageAsync(InstallOptions options, IPackage package)
=> Task.Run(() => _saveToDisk(options, StoragePath.Get(package)));
public static void SaveForManager(InstallOptions options, IPackageManager manager)
=> _saveToDisk(options, StoragePath.Get(manager));
public static Task SaveForManagerAsync(InstallOptions options, IPackageManager manager)
=> Task.Run(() => _saveToDisk(options, StoragePath.Get(manager)));
/// <summary>
/// Loads the applicable InstallationOptions, and applies
/// any required transformations in case that generic options are being used
/// </summary>
/// <param name="package">The package whose options to load</param>
/// <param name="elevated">Overrides the RunAsAdmin property</param>
/// <param name="interactive">Overrides the Interactive property</param>
/// <param name="no_integrity">Overrides the SkipHashCheck property</param>
/// <param name="remove_data">Overrides the RemoveDataOnUninstall property</param>
/// <param name="overridePackageOptions">In case of on-the-fly command generation, the PACKAGE
/// options can be overriden with this object </param>
/// <returns>The applicable InstallOptions</returns>
public static InstallOptions LoadApplicable(
IPackage package,
bool? elevated = null,
bool? interactive = null,
bool? no_integrity = null,
bool? remove_data = null,
InstallOptions? overridePackageOptions = null)
{
var instance = overridePackageOptions ?? LoadForPackage(package);
if (!instance.OverridesNextLevelOpts)
{
Logger.Debug($"Package {package.Id} does not override options, will use package manager's default...");
instance = LoadForManager(package.Manager);
var legalizedId = CoreTools.MakeValidFileName(package.Id);
instance.CustomInstallLocation = instance.CustomInstallLocation.Replace("%PACKAGE%", legalizedId);
}
if (elevated is not null) instance.RunAsAdministrator = (bool)elevated;
if (interactive is not null) instance.InteractiveInstallation = (bool)interactive;
if (no_integrity is not null) instance.SkipHashCheck = (bool)no_integrity;
if (remove_data is not null) instance.RemoveDataOnUninstall = (bool)remove_data;
return EnsureSecureOptions(instance);
}
/// <summary>
/// Loads the applicable InstallationOptions, and applies
/// any required transformations in case that generic options are being used
/// </summary>
/// <param name="package">The package whose options to load</param>
/// <param name="elevated">Overrides the RunAsAdmin property</param>
/// <param name="interactive">Overrides the Interactive property</param>
/// <param name="no_integrity">Overrides the SkipHashCheck property</param>
/// <param name="remove_data">Overrides the RemoveDataOnUninstall property</param>
/// <param name="overridePackageOptions">In case of on-the-fly command generation, the PACKAGE
/// options can be overriden with this object </param>
/// <returns>The applicable InstallOptions</returns>
public static Task<InstallOptions> LoadApplicableAsync(
IPackage package,
bool? elevated = null,
bool? interactive = null,
bool? no_integrity = null,
bool? remove_data = null,
InstallOptions? overridePackageOptions = null)
=> Task.Run(() => LoadApplicable(package, elevated, interactive, no_integrity, remove_data, overridePackageOptions));
/*
*
* SAVE TO DISK MECHANISMS
*
*/
private static readonly ConcurrentDictionary<string, InstallOptions> _optionsCache = new();
private static void _saveToDisk(InstallOptions options, string key)
{
try
{
var filePath = Path.Join(CoreData.UniGetUIInstallationOptionsDirectory, key);
_optionsCache[key] = options.Copy();
string fileContents = JsonSerializer.Serialize(
options,
SerializationHelpers.DefaultOptions
);
File.WriteAllText(filePath, fileContents);
}
catch (Exception ex)
{
Logger.Error($"Could not save {key} options to disk");
Logger.Error(ex);
}
}
private static InstallOptions _loadFromDisk(string key)
{
try
{
InstallOptions serializedOptions;
if (_optionsCache.TryGetValue(key, out var cached))
{
// If the wanted instance is already cached
return cached.Copy();
}
else
{
var filePath = Path.Join(CoreData.UniGetUIInstallationOptionsDirectory, key);
if (!File.Exists(filePath))
{
// If the file where it should be stored does not exist
_optionsCache[key] = new InstallOptions();
return new InstallOptions();
}
else
{
// If the options are not cached, and the save file exists
var rawData = File.ReadAllText(filePath);
JsonNode? jsonData = JsonNode.Parse(rawData);
ArgumentNullException.ThrowIfNull(jsonData);
serializedOptions = new InstallOptions(jsonData);
_optionsCache[key] = serializedOptions;
return serializedOptions.Copy();
}
}
}
catch (JsonException)
{
Logger.Warn("An error occurred while parsing package " + key + ". The file will be overwritten");
File.WriteAllText(key, "{}");
return new();
}
catch (Exception e)
{
Logger.Error("Loading installation options for file " + key + " have failed: ");
Logger.Error(e);
return new();
}
}
private static InstallOptions EnsureSecureOptions(InstallOptions options)
{
if (SecureSettings.Get("AllowCLIArguments"))
{
// If CLI arguments are allowed, sanitize them
for (int i = 0; i < options.CustomParameters.Count; i++)
{
options.CustomParameters[i] = options.CustomParameters[i]
.Replace("&", "")
.Replace("|", "")
.Replace(";", "")
.Replace("<", "")
.Replace(">", "")
.Replace("\n", "");
}
}
else
{
// Otherwhise, clear them
if (options.CustomParameters.Count > 0)
Logger.Warn($"Custom CLI parameters [{string.Join(' ', options.CustomParameters)}] will be discarded");
options.CustomParameters = [];
}
return options;
}
}
}