Skip to content

Commit

Permalink
Add image support.
Browse files Browse the repository at this point in the history
  • Loading branch information
CloudTheWolf committed Jun 30, 2024
1 parent 86bd492 commit 919d173
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 50 deletions.
3 changes: 1 addition & 2 deletions CloudTheWolf.DSharpPlus.Scaffolding.Worker/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ private static void SetDiscordConfig()
{
Token = Options.Token,
TokenType = TokenType.Bot,
AutoReconnect = true,
LoggerFactory = Logging.Logger.LoggerFactory,
AutoReconnect = true

};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CloudTheWolf.DSharpPlus.Scaffolding.Data" Version="2.1.0.1" />
<PackageReference Include="CloudTheWolf.DSharpPlus.Scaffolding.Shared" Version="2.2.0.2" />
<PackageReference Include="CloudTheWolf.DSharpPlus.Scaffolding.Data" Version="2.1.0.3" />
<PackageReference Include="CloudTheWolf.DSharpPlus.Scaffolding.Shared" Version="2.2.0.4" />
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="13.9.1" />
<PackageReference Include="Magick.NET.Core" Version="13.9.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="8.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.20.1" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
</ItemGroup>

Expand All @@ -45,6 +47,6 @@
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy &quot;$(TargetDir)*.*&quot; &quot;C:\bot\&quot; /E /Y" />
<Exec Command="xcopy &quot;$(TargetDir)*.*&quot; &quot;C:\bot\4.4.4\&quot; /E /Y" />
</Target>
</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
Expand All @@ -13,22 +14,38 @@ namespace CloudTheWolf.DSharpPlus.Scaffolding.Worker.Context
/// </summary>
public class CustomLoadContext : AssemblyLoadContext
{
/// <summary>
/// Load and <see cref="Assembly"/> using out custom context
/// </summary>
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;
}

/// <summary>
/// Load and <see cref="Assembly"/>
/// </summary>
/// <param name="assemblyName">Name of the assembly to load</param>
/// <returns>null</returns>
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;
}
}
Expand Down
24 changes: 22 additions & 2 deletions CloudTheWolf.DSharpPlus.Scaffolding.Worker/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
ENV FONTCONFIG_PATH=/etc/fonts
ENTRYPOINT ["dotnet", "CloudTheWolf.DSharpPlus.Scaffolding.Worker.dll"]
56 changes: 27 additions & 29 deletions CloudTheWolf.DSharpPlus.Scaffolding.Worker/Services/PluginLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -26,18 +26,17 @@ public class PluginLoader
/// </summary>
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
Expand All @@ -50,34 +49,35 @@ 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;
}
}

/// <summary>
/// Load a Dictionary of Plugins supporting Shard Mode as <see cref="IShardPlugin"/>
/// </summary>
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
Expand All @@ -92,14 +92,12 @@ public void LoadShardPlugins()
PluginLoadContexts[pluginInstance.Name] = loadContext;
}
}
catch (Exception e)
{
throw;
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}

}
}
3 changes: 1 addition & 2 deletions CloudTheWolf.DSharpPlus.Scaffolding.Worker/ShardBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ private static void SetDiscordConfig()
{
Token = Options.Token,
TokenType = TokenType.Bot,
AutoReconnect = true,
LoggerFactory = Logging.Logger.LoggerFactory
AutoReconnect = true
};
}

Expand Down

0 comments on commit 919d173

Please sign in to comment.