-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModuleUpdates.cs
205 lines (171 loc) · 6.79 KB
/
ModuleUpdates.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
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
using BBRAPIModules;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Text.Json;
using System.Threading.Tasks;
namespace BattleBitBaseModules;
[Module("Check for and download module updates from the module repository", "1.0.0")]
public class ModuleUpdates : BattleBitModule
{
public static ModuleUpdatesConfiguration Configuration { get; set; } = null!;
PropertyInfo nameProperty = null!;
PropertyInfo versionProperty = null!;
PropertyInfo moduleFilePathProperty = null!;
private static dynamic[] modulesToUpdate = Array.Empty<dynamic>();
private static bool running = false;
public override void OnModulesLoaded()
{
if (!running)
{
running = true;
Task.Run(versionChecker);
}
}
public override void OnModuleUnloading()
{
running = false;
}
private static DateTime lastChecked = DateTime.MinValue;
private async Task versionChecker()
{
while (running)
{
if (lastChecked.AddMinutes(Configuration.CheckDelay) < DateTime.Now)
{
await doVersionCheck();
}
await Task.Delay(60000);
}
}
private async Task doVersionCheck()
{
lastChecked = DateTime.Now;
IReadOnlyList<dynamic> collection = null!;
try
{
Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetTypes().Any(t => t.Namespace == "BattleBitAPIRunner"))!;
Type moduleType = assembly.GetTypes().FirstOrDefault(t => t.Name == "Module")!;
PropertyInfo modulesProperty = moduleType.GetProperty("Modules")!;
collection = (IReadOnlyList<dynamic>)modulesProperty.GetValue(null)!;
this.nameProperty = moduleType.GetProperty("Name")!;
this.versionProperty = moduleType.GetProperty("Version")!;
this.moduleFilePathProperty = moduleType.GetProperty("ModuleFilePath")!;
}
catch (Exception ex)
{
this.Logger.Error($"Error retrieving loaded modules: {ex.Message}");
return;
}
List<dynamic> modulesToUpdate = new();
foreach (dynamic item in collection)
{
string? moduleName = null;
string? moduleVersion = null;
try
{
moduleName = this.nameProperty.GetValue(item).ToString();
moduleVersion = this.versionProperty.GetValue(item).ToString();
}
catch (Exception ex)
{
this.Logger.Error($"Error retrieving module name and version: {ex.Message}");
continue;
}
if (moduleName == null || moduleVersion == null)
{
continue;
}
string? latestVersion = null;
try
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent", "BattleBitAPIRunner");
string response = await client.GetStringAsync($"{Configuration.APIEndpoint}/Modules/GetModule/{moduleName}");
using (JsonDocument responseDocument = JsonDocument.Parse(response))
{
latestVersion = responseDocument.RootElement.GetProperty("versions").EnumerateArray().First().GetProperty("Version_v_number").GetString();
}
}
catch (Exception ex)
{
this.Logger.Error($"Error checking for module {moduleName} updates: {ex.Message}");
continue;
}
if (latestVersion == null)
{
continue;
}
if (moduleVersion != latestVersion)
{
this.Logger.Warn($"Module {moduleName} is out of date! Installed version: {moduleVersion}, Latest version: {latestVersion}");
modulesToUpdate.Add(item);
}
}
if (modulesToUpdate.Count > 0)
{
ModuleUpdates.modulesToUpdate = modulesToUpdate.ToArray();
this.Logger.Warn($"There are {modulesToUpdate.Count} modules out of date. Run 'updateall' to update all modules or 'update modulename' to update an individual module.");
}
}
private async Task doUpdate(string? module = null)
{
if (module is null)
{
if (modulesToUpdate.Length == 0)
{
this.Logger.Info("There are no modules to update. Run 'update' to fetch latest versions.");
return;
}
foreach (dynamic item in modulesToUpdate)
{
await doUpdate(this.nameProperty.GetValue(item).ToString());
}
return;
}
dynamic? moduleToUpdate = modulesToUpdate.FirstOrDefault(m => module.Equals(this.nameProperty.GetValue(m).ToString(), StringComparison.InvariantCultureIgnoreCase));
if (moduleToUpdate is null)
{
this.Logger.Error($"Module {module} is not out of date.");
return;
}
this.Logger.Info($"Updating module {module}...");
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent", "BattleBitAPIRunner");
byte[] response = await client.GetByteArrayAsync($"{Configuration.APIEndpoint}/Download/{module}/latest");
File.WriteAllBytes(this.moduleFilePathProperty.GetValue(moduleToUpdate).ToString(), response);
this.Logger.Info($"Module {module} updated successfully.");
}
public override void OnConsoleCommand(string command)
{
if (!command.Trim().ToLower().StartsWith("update"))
{
return;
}
if (command.Trim().ToLower() == "updateall")
{
Task.Run(() => doUpdate()).ContinueWith(t => this.Logger.Error($"Error updating modules: {t.Exception!.Message}"), TaskContinuationOptions.OnlyOnFaulted);
return;
}
string[] args = command.Split(' ');
if (args.Length > 2)
{
this.Logger.Error("Usage: update [module name]");
return;
}
if (args.Length == 1)
{
doVersionCheck().ContinueWith(t => this.Logger.Error($"Error checking for module updates: {t.Exception!.Message}"), TaskContinuationOptions.OnlyOnFaulted);
return;
}
Task.Run(() => doUpdate(args[1])).ContinueWith(t => this.Logger.Error($"Error updating module {args[1]}: {t.Exception!.Message}"), TaskContinuationOptions.OnlyOnFaulted);
}
}
public class ModuleUpdatesConfiguration : ModuleConfiguration
{
public string APIEndpoint { get; set; } = "https://modules.battlebit.community/api";
public int CheckDelay { get; set; } = 30;
}