Started adding asset api/auto import
This commit is contained in:
@@ -1,13 +1,19 @@
|
||||
using System.Text.Json;
|
||||
using MoonCore.Helpers;
|
||||
using MoonCore.Models;
|
||||
using Moonlight.ApiServer.Models;
|
||||
using Moonlight.Shared.Http.Responses.PluginsStream;
|
||||
|
||||
namespace Moonlight.ApiServer.Services;
|
||||
|
||||
public class PluginService
|
||||
{
|
||||
public readonly List<PluginMeta> Plugins = new();
|
||||
|
||||
public readonly Dictionary<string, string> AssetMap = new();
|
||||
public HostedPluginsManifest HostedPluginsManifest;
|
||||
public PluginsAssetManifest PluginsAssetManifest;
|
||||
public Dictionary<string, string> AssemblyMap;
|
||||
|
||||
private static string PluginsFolder = PathBuilder.Dir("storage", "plugins");
|
||||
private readonly ILogger<PluginService> Logger;
|
||||
|
||||
@@ -74,14 +80,34 @@ public class PluginService
|
||||
plugin.Path,
|
||||
dependency
|
||||
);
|
||||
|
||||
|
||||
pluginsToNotLoad.Add(plugin.Manifest.Id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Remove unloadable plugins from cache
|
||||
Plugins.RemoveAll(x => pluginsToNotLoad.Contains(x.Manifest.Id));
|
||||
|
||||
// Generate assembly map for client
|
||||
AssemblyMap = GetAssemblies("client");
|
||||
|
||||
// Generate plugin stream manifest for client
|
||||
HostedPluginsManifest = new()
|
||||
{
|
||||
Assemblies = AssemblyMap.Keys.ToArray(),
|
||||
Entrypoints = GetEntrypoints("client")
|
||||
};
|
||||
|
||||
// Generate asset map
|
||||
GenerateAssetMap();
|
||||
|
||||
// Generate asset manifest
|
||||
PluginsAssetManifest = new()
|
||||
{
|
||||
CssFiles = AssetMap.Keys.Where(x => x.EndsWith(".css")).ToArray(),
|
||||
JavascriptFiles = AssetMap.Keys.Where(x => x.EndsWith(".js")).ToArray(),
|
||||
};
|
||||
}
|
||||
|
||||
public Dictionary<string, string> GetAssemblies(string section)
|
||||
@@ -92,7 +118,7 @@ public class PluginService
|
||||
{
|
||||
foreach (var file in Directory.EnumerateFiles(PathBuilder.Dir(plugin.Path, "bin", section)))
|
||||
{
|
||||
if(!file.EndsWith(".dll"))
|
||||
if (!file.EndsWith(".dll"))
|
||||
continue;
|
||||
|
||||
var fileName = Path.GetFileName(file);
|
||||
@@ -110,4 +136,49 @@ public class PluginService
|
||||
.SelectMany(x => x.Manifest.Entrypoints[section])
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
private void GenerateAssetMap()
|
||||
{
|
||||
AssetMap.Clear();
|
||||
|
||||
foreach (var plugin in Plugins)
|
||||
{
|
||||
var assetPath = PathBuilder.Dir(plugin.Path, "wwwroot");
|
||||
|
||||
if (!Directory.Exists(assetPath))
|
||||
continue;
|
||||
|
||||
var files = new List<string>();
|
||||
GetFilesInDirectory(assetPath, files);
|
||||
|
||||
foreach (var file in files)
|
||||
{
|
||||
var mapPath = Formatter.ReplaceStart(file, assetPath, "");
|
||||
|
||||
mapPath = mapPath.Replace("\\", "/"); // To handle fucking windows
|
||||
mapPath = mapPath.StartsWith("/") ? mapPath : "/" + mapPath; // Ensure starting /
|
||||
|
||||
if (AssetMap.ContainsKey(mapPath))
|
||||
{
|
||||
Logger.LogWarning(
|
||||
"The plugin '{name}' tries to map an asset to the path '{path}' which is already used by another plugin. Ignoring asset mapping",
|
||||
plugin.Manifest.Id,
|
||||
mapPath
|
||||
);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
AssetMap[mapPath] = file;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void GetFilesInDirectory(string directory, List<string> files)
|
||||
{
|
||||
files.AddRange(Directory.EnumerateFiles(directory));
|
||||
|
||||
foreach (var dir in Directory.EnumerateDirectories(directory))
|
||||
GetFilesInDirectory(dir, files);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user