diff --git a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Bot.cs b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Bot.cs index 7b511d8..eafc749 100644 --- a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Bot.cs +++ b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Bot.cs @@ -107,8 +107,7 @@ private static void SetDiscordConfig() { Token = Options.Token, TokenType = TokenType.Bot, - AutoReconnect = true, - LoggerFactory = Logging.Logger.LoggerFactory, + AutoReconnect = true }; } diff --git a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/CloudTheWolf.DSharpPlus.Scaffolding.Worker.csproj b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/CloudTheWolf.DSharpPlus.Scaffolding.Worker.csproj index e4b296c..2ca80e7 100644 --- a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/CloudTheWolf.DSharpPlus.Scaffolding.Worker.csproj +++ b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/CloudTheWolf.DSharpPlus.Scaffolding.Worker.csproj @@ -24,12 +24,14 @@ - - + + + + - + @@ -45,6 +47,6 @@ - + diff --git a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Context/CustomLoadContext.cs b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Context/CustomLoadContext.cs index b9331e7..85e31f0 100644 --- a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Context/CustomLoadContext.cs +++ b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Context/CustomLoadContext.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Reflection; using System.Runtime.Loader; @@ -13,22 +14,38 @@ namespace CloudTheWolf.DSharpPlus.Scaffolding.Worker.Context /// public class CustomLoadContext : AssemblyLoadContext { - /// - /// Load and using out custom context - /// - public CustomLoadContext() : base(isCollectible: true) + private string PluginDirectory; + + public CustomLoadContext(string pluginDirectory) : base(isCollectible: true) { - // Nothing to do here, we just need to have isCollectible set + PluginDirectory = pluginDirectory; } - /// - /// Load and - /// - /// Name of the assembly to load - /// null protected override Assembly Load(AssemblyName assemblyName) { - // We don't actually need to do anything here so let's just return null + + // Check if the assembly is already loaded + var assembly = Default.LoadFromAssemblyName(assemblyName); + if (assembly != null) + { + return assembly; + } + + // Try to load from the main application directory + string assemblyPath = Path.Combine(AppContext.BaseDirectory, $"{assemblyName.Name}.dll"); + if (File.Exists(assemblyPath)) + { + return LoadFromAssemblyPath(assemblyPath); + } + + // Try to load from the plugin directory + assemblyPath = Path.Combine(PluginDirectory, $"{assemblyName.Name}.dll"); + if (File.Exists(assemblyPath)) + { + return LoadFromAssemblyPath(assemblyPath); + } + + Console.WriteLine($"Failed to load assembly: {assemblyName.FullName}"); return null; } } diff --git a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Dockerfile b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Dockerfile index bc0c1c8..b658106 100644 --- a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Dockerfile +++ b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Dockerfile @@ -1,9 +1,29 @@ -#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging. +# See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging. FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base WORKDIR /app +# Install necessary packages +RUN apt-get update && \ + apt-get install -y --no-install-recommends \ + libgdiplus \ + fonts-freefont-ttf \ + libssl1.1 \ + libc6-dev \ + libx11-dev \ + libx11-6 \ + libxext-dev \ + libxext6 \ + libxrender1 \ + fontconfig \ + libfreetype6-dev \ + libpng-dev && \ + rm -rf /var/lib/apt/lists/* && \ + curl -O http://ftp.us.debian.org/debian/pool/main/i/imagemagick/imagemagick_6.9.11.60+dfsg-1.3+deb11u3_amd64.deb && \ + dpkg -i imagemagick_6.9.11.60+dfsg-1.3+deb11u3_amd64.deb + FROM base AS final WORKDIR /app COPY ./publish . -ENTRYPOINT ["dotnet", "CloudTheWolf.DSharpPlus.Scaffolding.Worker.dll"] \ No newline at end of file +ENV FONTCONFIG_PATH=/etc/fonts +ENTRYPOINT ["dotnet", "CloudTheWolf.DSharpPlus.Scaffolding.Worker.dll"] diff --git a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Services/PluginLoader.cs b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Services/PluginLoader.cs index 35a7872..328df69 100644 --- a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Services/PluginLoader.cs +++ b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Services/PluginLoader.cs @@ -2,9 +2,9 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Reflection; using CloudTheWolf.DSharpPlus.Scaffolding.Shared.Interfaces; using CloudTheWolf.DSharpPlus.Scaffolding.Worker.Context; +using Microsoft.Extensions.Logging; namespace CloudTheWolf.DSharpPlus.Scaffolding.Worker.Services { @@ -26,18 +26,17 @@ public class PluginLoader /// public void LoadPlugins() { - try + //Load the DLLs from the Plugins directory + if (!Directory.Exists(Constants.PluginsFolder)) return; + var pluginDirectories = Directory.GetDirectories(Constants.PluginsFolder); + foreach (var pluginDir in pluginDirectories) { - //Load the DLLs from the Plugins directory - if (!Directory.Exists(Constants.PluginsFolder)) return; - var plugins = Directory.GetDirectories(Constants.PluginsFolder); - foreach (var plugin in plugins) + var files = Directory.GetFiles(pluginDir,"*.dll"); + foreach (var file in files) { - var files = Directory.GetFiles(plugin); - foreach (var file in files) + try { - if (!file.EndsWith("dll")) continue; - var loadContext = new CustomLoadContext(); + var loadContext = new CustomLoadContext(pluginDir); var assembly = loadContext.LoadFromAssemblyPath(Path.GetFullPath(file)); // Get types that implement IPlugin @@ -50,15 +49,15 @@ public void LoadPlugins() var pluginInstance = (IPlugin)Activator.CreateInstance(type); Plugins[pluginInstance.Name] = pluginInstance; PluginLoadContexts[pluginInstance.Name] = loadContext; + Bot.Logger.LogInformation($"Loaded plugin: {pluginInstance.Name}"); } } + catch (Exception e) + { + continue; + } } } - catch (Exception e) - { - Console.WriteLine(e); - throw; - } } /// @@ -66,18 +65,19 @@ public void LoadPlugins() /// public void LoadShardPlugins() { - try + + //Load the DLLs from the Plugins directory + if (!Directory.Exists(Constants.PluginsFolder)) return; + var plugins = Directory.GetDirectories(Constants.PluginsFolder); + foreach (var plugin in plugins) { - //Load the DLLs from the Plugins directory - if (!Directory.Exists(Constants.PluginsFolder)) return; - var plugins = Directory.GetDirectories(Constants.PluginsFolder); - foreach (var plugin in plugins) + var files = Directory.GetFiles(plugin); + foreach (var file in files) { - var files = Directory.GetFiles(plugin); - foreach (var file in files) + try { if (!file.EndsWith("dll")) continue; - var loadContext = new CustomLoadContext(); + var loadContext = new CustomLoadContext(plugin); var assembly = loadContext.LoadFromAssemblyPath(Path.GetFullPath(file)); // Get types that implement IShardPlugin @@ -92,14 +92,12 @@ public void LoadShardPlugins() PluginLoadContexts[pluginInstance.Name] = loadContext; } } + catch (Exception e) + { + throw; + } } } - catch (Exception e) - { - Console.WriteLine(e); - throw; - } } - } } diff --git a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/ShardBot.cs b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/ShardBot.cs index 6ed223e..7424aed 100644 --- a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/ShardBot.cs +++ b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/ShardBot.cs @@ -111,8 +111,7 @@ private static void SetDiscordConfig() { Token = Options.Token, TokenType = TokenType.Bot, - AutoReconnect = true, - LoggerFactory = Logging.Logger.LoggerFactory + AutoReconnect = true }; }