Skip to content

Commit

Permalink
refactor(Server): add PluginInfo
Browse files Browse the repository at this point in the history
- manage WebApi hook.
- manage Assets.
- better for future improvements.
  • Loading branch information
poirierlouis committed Dec 17, 2024
1 parent a161c80 commit a0c940a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 31 deletions.
63 changes: 40 additions & 23 deletions code/server/loader/Systems/Plugins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text.Json;
using CurseForge.APIClient;
using CurseForge.APIClient.Models.Enums;
using CurseForge.APIClient.Models.Files;
using CurseForge.APIClient.Models.Mods;
using CyberpunkSdk;
using CyberpunkSdk.Systems;
Expand Down Expand Up @@ -37,21 +40,31 @@ internal class ModDef
public string Checksum { get; set; } = "";
}

internal class PluginInfo
{
public string Name { get; set; } = "";
public string FullName { get; set; } = "";
public IWebApiHook? WebApi { get; set; }
public string? Assets { get; set; }

public string GetWebApiUrl => $"/api/v1/plugins/{Name.ToLower()}";

public string GetAssetsUrl => $"/api/v1/plugins/{Name.ToLower()}/assets/";
public string GetAssetsPath => Assets!;
}

internal class Plugins
{
private Configuration configuration = new();
private Cache cache = new();
private List<ModDef> serverMods = [];
private List<ModDef> clientMods = [];
private Dictionary<string, IWebApiHook> hooks = [];
private Dictionary<string, string> assets = [];
private List<PluginInfo> plugins = [];
private Logger logger = new("SDK");

public IList<ModDef> ClientMods => clientMods;

public IDictionary<string, IWebApiHook> Hooks => hooks;

public IDictionary<string, string> Assets => assets;
public IList<PluginInfo> GetPlugins(Func<PluginInfo, bool> predicate) => plugins.Where(predicate).ToList();

private void LoadConfiguration(string path)
{
Expand Down Expand Up @@ -103,7 +116,7 @@ private void GetMods(List<int>? mods, bool client)
if (mods == null || mods.Count == 0)
return;

using (var cfApiClient = new CurseForge.APIClient.ApiClient(configuration.ApiKey))
using (var cfApiClient = new ApiClient(configuration.ApiKey))
{
var modList = cfApiClient.GetModsByIdListAsync(new GetModsByIdsListRequestBody
{
Expand All @@ -122,12 +135,12 @@ private void GetMods(List<int>? mods, bool client)
if (f.Id != m.MainFileId)
continue;

if (f.FileStatus == CurseForge.APIClient.Models.Files.FileStatus.Approved)
if (f.FileStatus == FileStatus.Approved)
{
def.Url = f.DownloadUrl;
foreach (var hash in f.Hashes)
{
if (hash.Algo == CurseForge.APIClient.Models.Enums.HashAlgo.Sha1)
if (hash.Algo == HashAlgo.Sha1)
{
def.Checksum = hash.Value;
break;
Expand Down Expand Up @@ -263,39 +276,36 @@ private void DownloadServerMods(string root)
File.WriteAllText(filepath, content);
}

private bool DetectWebApiHook(Type plugin, string name)
private IWebApiHook? DetectWebApiHook(Type plugin, string name)
{
var property = plugin.GetProperty("Instance", BindingFlags.Static | BindingFlags.Public);
if (property == null)
{
return false;
return null;
}

var instance = property.GetValue(null);
if (instance is not IWebApiHook hook)
{
return false;
return null;
}

hooks.Add(name, hook);
return true;
return hook;
}

private bool DetectAssets(string path, string name)
private string? DetectAssets(string path, string name)
{
path = Path.Combine(path, "Assets");
path = Path.Combine(path, "assets");

if (!Directory.Exists(path))
{
return false;
return null;
}

if (Directory.GetFiles(path).Length == 0)
{
return false;
return null;
}
assets.Add(path, name);
return true;
return path;
}

internal Plugins(RpcManager rpcManager)
Expand Down Expand Up @@ -332,9 +342,16 @@ internal Plugins(RpcManager rpcManager)
RuntimeHelpers.RunClassConstructor(plugin.TypeHandle);

rpcManager.ParseAssembly(assembly);
var pluginName = directoryName[..^"System".Length];
var hasHook = DetectWebApiHook(plugin, pluginName);
var hasAssets = DetectAssets(directory, pluginName.ToLower());

var info = new PluginInfo();
info.Name = directoryName[..^"System".Length];
info.FullName = directoryName;
info.WebApi = DetectWebApiHook(plugin, info.Name);
info.Assets = DetectAssets(directory, info.Name.ToLower());
plugins.Add(info);

var hasHook = info.WebApi != null;
var hasAssets = info.Assets != null;
logger.Info($"Loaded Plugin" +
$"{(hasHook ? " + WebApi" : "")}" +
$"{(hasAssets ? " + Assets" : "")}: {directoryName}");
Expand Down
21 changes: 13 additions & 8 deletions code/server/loader/Systems/WebApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,25 @@ private WebServer CreateWebServer(string url)

private void RegisterPlugins(WebServer server)
{
foreach (var (path, name) in Plugins.Assets)
var withAssets = Plugins.GetPlugins(p => p.Assets != null);

foreach (var plugin in withAssets)
{
server.WithStaticFolder(
baseRoute: $"/api/v1/plugins/{name}/assets/",
fileSystemPath: path,
baseRoute: plugin.GetAssetsUrl,
fileSystemPath: plugin.GetAssetsPath,
isImmutable: EnvironmentHelper.IsRelease,
configure: m => { m.AddCustomMimeType(".umd.js", "application/javascript"); });
}
var withHooks = Plugins.GetPlugins(p => p.WebApi != null);

foreach (var (name, hook) in Plugins.Hooks)
foreach (var plugin in withHooks)
{
var hook = plugin.WebApi!;
var controller = hook.BuildController()();

server.WithWebApi(
baseRoute: $"/api/v1/plugins/{name}",
baseRoute: plugin.GetWebApiUrl,
configure: m => m.WithController(controller));
}

Expand Down Expand Up @@ -135,11 +139,12 @@ private Task HandleStatistics(IHttpContext context)

private Task HandleListPlugins(IHttpContext context)
{
var withHooks = Plugins.GetPlugins(p => p.WebApi != null);

return context.SendDataAsync(
Plugins.Hooks
.Select(hook => new WebApiPluginDto
withHooks.Select(plugin => new WebApiPluginDto
{
Name = hook.Key
Name = plugin.Name,
})
.ToList()
);
Expand Down

0 comments on commit a0c940a

Please sign in to comment.