-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConchPluginManager.cs
More file actions
428 lines (391 loc) · 18.6 KB
/
ConchPluginManager.cs
File metadata and controls
428 lines (391 loc) · 18.6 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
using System.Text.Json;
using System.IO.Compression;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
using Microsoft.Extensions.Logging;
using System.Net.Http.Headers;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API;
using System.Text.Json.Serialization;
namespace ConchPluginManager;
[MinimumApiVersion(143)]
public class ConchPluginManager : BasePlugin, IPluginConfig<ConchPluginManagerConfig>
{
public override string ModuleName => "Conch Plugin Manager";
public override string ModuleVersion => "0.1.1";
public override string ModuleAuthor => "Conch";
private static readonly HttpClient httpClient = new ();
public ConchPluginManagerConfig Config { get; set; } = new ();
public Manifest Manifest { get; set; } = new ();
public string? manifestPath;
public DirectoryInfo? gameDir;
public DirectoryInfo? pluginsDir;
public void OnConfigParsed(ConchPluginManagerConfig config)
{
if (config.Version < 2)
{
throw new Exception("Conch Plugin Manager config is outdated. Delete old config and restart the plugin");
}
Config = config;
}
[ConsoleCommand("css_cpm_list", "List installed plugins")]
[CommandHelper(whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
[RequiresPermissions("@css/root")]
public void OnCommandList(CCSPlayerController? _, CommandInfo command)
{
foreach (var plugin in Manifest.PluginsInstalled)
{
command.ReplyToCommand(plugin.ToString());
}
}
[ConsoleCommand("css_cpm_update_all", "Update all")]
[CommandHelper(whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
[RequiresPermissions("@css/root")]
public async void OnCommandUpdateAll(CCSPlayerController? _, CommandInfo command)
{
await CheckForUpdates();
}
[ConsoleCommand("css_cpm_install", "Install a plugin")]
[CommandHelper(minArgs: 1, usage: "<github_author>/<repository_name>", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
[RequiresPermissions("@css/root")]
public async void OnCommandInstall(CCSPlayerController? _, CommandInfo command)
{
var downloadString = command.GetArg(1);
if (Manifest.PluginsInstalled.Exists(plugin => plugin.DownloadString == downloadString))
{
command.ReplyToCommand($"Plugin {downloadString} already installed");
return;
}
var newPlugin = new PluginInfo(downloadString);
if (!await CheckForUpdate(newPlugin, httpClient))
{
Logger.LogInformation("Failed to install plugin.");
return;
}
Manifest.PluginsInstalled.Add(newPlugin);
WriteManifest();
Logger.LogInformation("loading plugin {plugin}", newPlugin.Directory);
Server.NextFrame(() => Server.ExecuteCommand($"css_plugins load {newPlugin.Directory}"));
}
[ConsoleCommand("css_cpm_remove", "Remove a plugin")]
[CommandHelper(minArgs: 1, usage: "<plugin download_string/directory>", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
[RequiresPermissions("@css/root")]
public void OnCommandRemove(CCSPlayerController? _, CommandInfo command)
{
var arg = command.GetArg(1);
var pluginToRemove = Manifest.PluginsInstalled.Find((plugin) => plugin.DownloadString == arg);
pluginToRemove ??= Manifest.PluginsInstalled.Find((plugin) => plugin.Directory == arg);
if (pluginToRemove == null)
{
Logger.LogInformation("Plugin {plugin} not found", arg);
return;
}
Logger.LogInformation("Removing plugin {plugin}", arg);
var pluginToRemovePath = Path.Join(pluginsDir!.FullName, pluginToRemove.Directory);
if (Directory.Exists(pluginToRemovePath))
{
Logger.LogInformation("Deleting directory {dir}", pluginToRemovePath);
Directory.Delete(pluginToRemovePath, true);
Logger.LogInformation("Plugin directory {dir} has been deleted. You may need to unload the plugin with css_plugins unload <plugin_name>", pluginToRemove.Directory);
}
else
{
Logger.LogInformation("couldn't find directory {plugin} in {plugins}", pluginToRemove.Directory, pluginsDir.FullName);
}
Manifest.PluginsInstalled.Remove(pluginToRemove);
WriteManifest();
}
public override async void Load(bool hotReload)
{
gameDir = new DirectoryInfo(ModulePath).Parent?.Parent?.Parent?.Parent?.Parent?.Parent;
pluginsDir = new DirectoryInfo(ModulePath)?.Parent?.Parent;
if (gameDir == null) throw new Exception("'game' directory not found");
if (pluginsDir == null) throw new Exception("counterstrikesharp/plugins directory not found");
httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("ConchPluginManager", ModuleVersion));
if (!String.IsNullOrEmpty(Config.GithubAuthToken))
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Config.GithubAuthToken);
if (!await TestAuth()) return;
}
manifestPath = Path.Join(ModuleDirectory, "plugins.json");
if (!LoadManifest()) return;
var updateOnReload = Config?.UpdateOnReload ?? false;
var updateOnServerStart = Config?.UpdateOnServerStart ?? false;
var updateOnMapChange = Config?.UpdateOnMapChange ?? false;
if ((updateOnServerStart && !hotReload) || (updateOnReload && hotReload))
await CheckForUpdates();
if (updateOnMapChange && !hotReload) RegisterListener<Listeners.OnMapEnd>(async () => await CheckForUpdates());
}
public async Task<bool> TestAuth()
{
var response = await httpClient.GetAsync("https://api.github.com/user");
if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
Logger.LogError("{code}: Github auth token didn't work. Try restarting plugin with a different token or get rid of token in config.", response.StatusCode);
return false;
}
if (!response.IsSuccessStatusCode)
{
var errorContent = await response.Content.ReadAsStringAsync();
Logger.LogError("{code}: Test Github reequest didn't work. Response: {response}", response.StatusCode, errorContent);
return false;
}
Logger.LogInformation("Github auth token successful!");
return true;
}
public bool LoadManifest()
{
if (File.Exists(manifestPath)) {
try
{
Manifest = JsonSerializer.Deserialize<Manifest>(File.ReadAllText(manifestPath), new JsonSerializerOptions() { ReadCommentHandling = JsonCommentHandling.Skip })!;
return true;
}
catch (Exception ex)
{
Logger.LogError("{exception}\nFailed to parse manifest (plugins.json)", ex);
}
} else
{
try
{
WriteManifest();
return true;
} catch (Exception ex)
{
Logger.LogError("{exception}\nFailed to generate manifest (plugins.json)", ex);
}
};
return false;
}
public async Task CheckForUpdates()
{
foreach (var plugin in Manifest.PluginsInstalled)
{
if (!plugin.AutoUpdate) continue;
await CheckForUpdate(plugin, httpClient);
}
WriteManifest();
}
private void WriteManifest()
{
File.WriteAllText(manifestPath!, JsonSerializer.Serialize(Manifest, new JsonSerializerOptions { WriteIndented = true }));
}
private async Task<bool> CheckForUpdate(PluginInfo plugin, HttpClient httpClient)
{
var githubLink = $"https://api.github.com/repos/{plugin.DownloadString}/releases/latest";
try
{
Logger.LogInformation("querying https://api.github.com/repos/{plugin}/releases/latest", plugin.DownloadString);
HttpResponseMessage response = await httpClient.GetAsync(githubLink);
response.EnsureSuccessStatusCode();
try
{
var res = JsonSerializer.Deserialize<GithubApiResponse>(await response.Content.ReadAsStringAsync());
if (res == null || String.IsNullOrEmpty(res!.TagName))
{
Logger.LogError("Couldn't retrieve Github release information after querying {download_string}", plugin.DownloadString);
return false;
}
if (res.TagName == plugin.TagName)
{
Logger.LogInformation("Plugin {plugin} up to date", plugin.DownloadString);
return false;
}
Logger.LogInformation("Plugin {plugin} is not up to date! Downloading new release {tag_name} from {url}", plugin.DownloadString, res!.TagName, res!.Assets.First().BrowserDownloadUrl);
var extractPath = await Download(plugin, res);
try
{
var pluginDir = GetPluginDir(extractPath);
if (pluginDir == null)
{
Logger.LogError("Couldn't find plugin directory with matching dll. Aborting installation.");
return false;
}
var pluginDirName = new DirectoryInfo(pluginDir).Name;
if (!String.IsNullOrEmpty(plugin.Directory) && plugin.Directory != pluginDirName)
{
Logger.LogError("New version of {plugin} uses a different directory from before. OLD: {old_dir} | NEW: {new_dir}", plugin.TagName, plugin.Directory, pluginDirName);
return false;
}
Merge(extractPath, pluginDir);
plugin.Directory ??= pluginDirName;
Logger.LogInformation("updating tag name from {previous_tag} to {next_tag}", plugin.TagName, res.TagName);
plugin.TagName = res.TagName;
return true;
}
catch (Exception ex)
{
Logger.LogError("{error_message}", ex.Message);
}
}
catch (HttpRequestException ex)
{
Logger.LogError("{error_message}", ex.Message);
Logger.LogError("{response_content}", await response.Content.ReadAsStringAsync());
}
catch (Exception ex)
{
Logger.LogError("{error_message}", ex.Message);
}
}
catch (Exception ex)
{
Logger.LogError("An error occurred while querying {github_link}:\n {message}", githubLink, ex.Message);
}
return false;
}
private void Merge(string extractPath, string pluginDir)
{
Logger.LogInformation("Merging");
// check for matching directory structure in download
// if there's a match, merge on the outer-most matching directory
string[] targetSubDirs = { "csgo", "addons", "counterstrikesharp", "plugins" };
for (int i = 0; i < targetSubDirs.Length; i++)
{
Logger.LogInformation("Extract path: {ep}. searching for {td}", extractPath, targetSubDirs[i]);
string[] matchedDirectories = Directory.GetDirectories(extractPath, targetSubDirs[i], SearchOption.AllDirectories);
if (matchedDirectories.Length == 0) continue;
Logger.LogInformation("Found common directory: {dir}", targetSubDirs[i]);
foreach (var matchedDir in matchedDirectories)
{
Logger.LogInformation(matchedDir);
var pathToPluginsDir = Path.Combine(matchedDir, Path.Join(targetSubDirs.Skip(i + 1).ToArray()));
Logger.LogInformation(pathToPluginsDir);
if (Directory.Exists(pathToPluginsDir))
{
var parentOfMatchedDir = Path.GetDirectoryName(matchedDir)!;
Logger.LogInformation("parentOfMatchedDir: {dir}", parentOfMatchedDir);
foreach (var fileSystemEntry in Directory.GetFileSystemEntries(parentOfMatchedDir, "*", SearchOption.TopDirectoryOnly))
{
var destination = Path.Join(targetSubDirs.Take(i).ToArray());
Logger.LogInformation("Moving {file} into --> {destination_dir}", fileSystemEntry, Path.Join(gameDir!.FullName, destination));
Copy(fileSystemEntry, Path.Combine(gameDir.FullName, destination, Path.GetFileName(fileSystemEntry)));
}
Directory.Delete(extractPath, true);
return;
} else
{
Logger.LogInformation("Tried merging {matched_dir}, but directory {dir} doesn't exist", matchedDir, pathToPluginsDir);
}
}
}
// else,
Logger.LogInformation("Moving {dir} into --> {plugins_dir}", pluginDir, pluginsDir);
Copy(pluginDir, Path.Join(pluginsDir!.FullName, Path.GetFileName(pluginDir)));
Directory.Delete(extractPath, true);
}
public string? GetPluginDir(string extractPath)
{
// search extract path for directory and .dll that have the same name.
foreach (var dir in new DirectoryInfo(extractPath).GetDirectories())
{
var pluginDir = GetPluginDir(dir.FullName);
if (pluginDir != null)
{
return pluginDir;
}
}
Logger.LogInformation("Looking through {dir} for a matching .dll", extractPath);
var matchingDlls = Directory.GetFiles(extractPath, $"{new DirectoryInfo(extractPath).Name}.dll", SearchOption.TopDirectoryOnly);
if (matchingDlls.Length == 1)
{
return Path.GetDirectoryName(matchingDlls.First());
}
return null;
}
static void Copy(string sourceDirectory, string destinationDirectory)
{
DirectoryInfo dir = new (sourceDirectory);
if (!dir.Exists)
{
throw new DirectoryNotFoundException($"Source directory does not exist or could not be found: {sourceDirectory}");
}
Directory.CreateDirectory(destinationDirectory);
DirectoryInfo[] dirs = dir.GetDirectories();
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string tempPath = Path.Combine(destinationDirectory, file.Name);
file.CopyTo(tempPath, true);
}
foreach (DirectoryInfo subdir in dirs)
{
string tempPath = Path.Combine(destinationDirectory, subdir.Name);
Copy(subdir.FullName, tempPath);
}
}
public async Task<string> Download(PluginInfo plugin, GithubApiResponse ghRes)
{
// for now, we problematically assume the download is in assets[0] and that it's a zip file
// TODO figure out a way to either automatically get the correct asset or allow the user to decide which one they want in the form of a chat menu thing
HttpResponseMessage res = await httpClient.GetAsync(ghRes.Assets[0].BrowserDownloadUrl);
var fileName = (res.Content.Headers.ContentDisposition?.FileName) ?? throw new Exception("File name not found in download");
if (fileName.EndsWith(".rar"))
{
throw new Exception("Rar packages currently not supported");
}
string tempDirectory = Path.Combine(Path.GetTempPath(), "ConchPluginManagerDownloads");
Directory.CreateDirectory(tempDirectory);
string filePath = Path.Combine(tempDirectory, fileName);
using (Stream contentStream = await res.Content.ReadAsStreamAsync(),
fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true))
{
var buffer = new byte[1024 * 1024];
int bytesRead;
long totalBytesRead = 0;
long totalBytes = res.Content.Headers.ContentLength.GetValueOrDefault();
while ((bytesRead = await contentStream.ReadAsync(buffer)) > 0)
{
await fileStream.WriteAsync(buffer.AsMemory(0, bytesRead));
totalBytesRead += bytesRead;
double percentage = ((double)totalBytesRead / totalBytes) * 100;
Logger.LogInformation("Downloaded: {totalBytesRead}/{totalBytes} bytes ({percentage}%)", totalBytesRead, totalBytes, percentage);
}
}
Logger.LogInformation("File downloaded successfully: {path}", filePath);
var extractPath = Path.Combine(tempDirectory, Path.GetFileNameWithoutExtension(filePath));
Logger.LogInformation("unzipping...");
ZipFile.ExtractToDirectory(filePath, extractPath, overwriteFiles: true);
Logger.LogInformation("deleting...");
File.Delete(filePath);
Logger.LogInformation("Extracted file {path} to {dir}", filePath, extractPath);
return extractPath;
}
}
public class Manifest
{
public List<PluginInfo> PluginsInstalled { get; set; } = new() { new ( "connercsbn/ConchPluginManager", "ConchPluginManager" ) };
}
public class PluginInfo
{
public PluginInfo() { }
public PluginInfo (string downloadString, string directory)
{
DownloadString = downloadString;
Directory = directory;
}
public PluginInfo (string downloadString)
{
DownloadString = downloadString;
}
public string? DownloadString { get; set; }
public string? Directory { get; set; }
public string? TagName { get; set; }
public bool AutoUpdate { get; set; } = true;
public override string ToString()
{
return $"{DownloadString} is installed in plugins/{Directory} with TagName {TagName}";
}
}
public class ConchPluginManagerConfig : BasePluginConfig
{
[JsonPropertyName("ConfigVersion")]
public override int Version { get; set; } = 2;
public string? GithubAuthToken { get; set; }
public bool UpdateOnMapChange { get; set; } = false;
public bool UpdateOnServerStart { get; set; } = true;
public bool UpdateOnReload { get; set; } = false;
}