-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from DJDaemonix/feature/core/plugins
0.2.0 - YumeCore Overhaul
- Loading branch information
Showing
21 changed files
with
447 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
using Nodsoft.YumeChan.Core; | ||
using System.Threading.Tasks; | ||
using System; | ||
using static Nodsoft.YumeChan.Core.YumeCore; | ||
|
||
namespace Nodsoft.YumeChan.ConsoleRunner | ||
{ | ||
static class Program | ||
{ | ||
static void Main(string[] args) => new YumeCore(new Logger()).RunBot(); | ||
static void Main(string[] _) | ||
{ | ||
Instance.Logger = new Logger(); | ||
Instance.RunBot(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Nodsoft.YumeChan.PluginBase; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Nodsoft.YumeChan.Core.Modules | ||
{ | ||
internal class InternalPlugin : IPlugin | ||
{ | ||
public Version PluginVersion { get; } = typeof(InternalPlugin).Assembly.GetName().Version; | ||
|
||
public string PluginDisplayName { get; } = "YumeCore Internals"; | ||
|
||
public bool PluginStealth { get; } = false; | ||
|
||
public bool PluginLoaded { get; internal set; } | ||
|
||
public Task LoadPlugin() | ||
{ | ||
PluginLoaded = true; | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task UnloadPlugin() | ||
{ | ||
PluginLoaded = false; | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using Discord; | ||
using Discord.Commands; | ||
using System.Threading.Tasks; | ||
|
||
using static Nodsoft.YumeChan.Core.YumeCore; | ||
using Nodsoft.YumeChan.PluginBase; | ||
|
||
namespace Nodsoft.YumeChan.Core.Modules.Status | ||
{ | ||
[Group("status")] | ||
public class Status : ModuleBase<SocketCommandContext> | ||
{ | ||
public static string MissingVersionSubstitute { get; } = "Unknown"; | ||
|
||
[Command] | ||
public async Task CoreStatusAsync() | ||
{ | ||
EmbedBuilder embed = new EmbedBuilder() | ||
.WithTitle("Yume-Chan") | ||
.WithDescription($"Status : {Instance.CoreState.ToString()}") | ||
.AddField("Core", $"Version : {(CoreVersion != null ? CoreVersion.ToString() : MissingVersionSubstitute)}", true) | ||
.AddField("Loaded Modules", $"Count : {(Instance.Plugins != null ? Instance.Plugins.Count.ToString() : "None")}", true); | ||
#if DEBUG | ||
embed.AddField("Debug", "Debug Build Active."); | ||
#endif | ||
|
||
await ReplyAsync(embed: embed.Build()); | ||
} | ||
|
||
[Command("plugins")] | ||
public async Task PluginsStatusAsync() | ||
{ | ||
EmbedBuilder embed = new EmbedBuilder() | ||
.WithTitle("Plugins") | ||
.WithDescription($"Currently Loaded : **{Instance.Plugins.Count}** Plugins."); | ||
|
||
foreach (IPlugin pluginManifest in Instance.Plugins) | ||
{ | ||
embed.AddField(pluginManifest.PluginDisplayName, | ||
$"Version : {(pluginManifest.PluginVersion != null ? pluginManifest.PluginVersion.ToString() : MissingVersionSubstitute)}\n" + | ||
$"Loaded : {(pluginManifest.PluginLoaded ? "Yes" : "No")}", true); | ||
} | ||
|
||
await ReplyAsync(embed: embed.Build()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using Nodsoft.YumeChan.PluginBase; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
using System.Linq; | ||
|
||
namespace Nodsoft.YumeChan.Core | ||
{ | ||
internal class ModulesLoader | ||
{ | ||
internal List<Assembly> PluginAssemblies { get; set; } | ||
internal List<FileInfo> PluginFiles { get; set; } | ||
internal List<IPlugin> PluginManifests { get; set; } | ||
|
||
internal DirectoryInfo PluginsLoadDirectory { get; set; } | ||
internal string PluginsLoadDiscriminator { get; set; } = string.Empty; | ||
|
||
public ModulesLoader(string pluginsLoadDirectoryPath) | ||
{ | ||
PluginsLoadDirectory = string.IsNullOrEmpty(pluginsLoadDirectoryPath) | ||
? SetDefaultPluginsDirectoryEnvironmentVariable() | ||
: Directory.Exists(pluginsLoadDirectoryPath) | ||
? new DirectoryInfo(pluginsLoadDirectoryPath) | ||
: Directory.CreateDirectory(pluginsLoadDirectoryPath); | ||
} | ||
|
||
internal DirectoryInfo SetDefaultPluginsDirectoryEnvironmentVariable() | ||
{ | ||
FileInfo file = new FileInfo(Assembly.GetExecutingAssembly().Location); | ||
PluginsLoadDirectory = Directory.CreateDirectory(file.DirectoryName + Path.DirectorySeparatorChar + "Modules" + Path.DirectorySeparatorChar); | ||
|
||
Environment.SetEnvironmentVariable("YumeChan.PluginsLocation", PluginsLoadDirectory.FullName); | ||
return PluginsLoadDirectory; | ||
} | ||
|
||
public Task LoadModuleAssemblies() | ||
{ | ||
PluginFiles = new List<FileInfo>(PluginsLoadDirectory.GetFiles($"*{PluginsLoadDiscriminator}*.dll")); | ||
|
||
if (PluginAssemblies is null) | ||
{ | ||
PluginAssemblies = new List<Assembly>(); | ||
} | ||
|
||
foreach (FileInfo file in PluginFiles) | ||
{ | ||
if (file !is null || file.Name != "Nodsoft.YumeChan.PluginBase.dll") | ||
{ | ||
PluginAssemblies.Add(Assembly.LoadFile(file.ToString())); | ||
} | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public Task<List<IPlugin>> LoadModuleManifests() | ||
{ | ||
List<IPlugin> manifestsList = new List<IPlugin>(); | ||
List<Type> pluginTypes = new List<Type>(); | ||
foreach (Assembly assembly in PluginAssemblies) | ||
{ | ||
pluginTypes.AddRange | ||
( | ||
from Type t in assembly.ExportedTypes | ||
where t.ImplementsInterface(typeof(IPlugin)) | ||
select t | ||
); | ||
} | ||
foreach (Type pluginType in pluginTypes) | ||
{ | ||
manifestsList.Add(InstantiateManifest(pluginType).GetAwaiter().GetResult()); | ||
} | ||
|
||
return Task.FromResult(manifestsList); | ||
} | ||
|
||
internal static Task<IPlugin> InstantiateManifest(Type typePlugin) | ||
{ | ||
object obj = Activator.CreateInstance(typePlugin); | ||
IPlugin pluginManifest = obj as IPlugin; | ||
|
||
if (pluginManifest is null) | ||
{ | ||
throw new InvalidCastException(); | ||
} | ||
return Task.FromResult(pluginManifest); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,31 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
<Version>0.1.3</Version> | ||
<Authors>DJ Daemonix</Authors> | ||
<Company>Nodsoft ES</Company> | ||
<Product>YumeChan</Product> | ||
<Copyright>GNU GPLv3</Copyright> | ||
<NeutralLanguage>en</NeutralLanguage> | ||
<RepositoryUrl>https://github.com/DJDaemonix/YumeChan</RepositoryUrl> | ||
<RepositoryType>Git</RepositoryType> | ||
<SignAssembly>false</SignAssembly> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
<Version>0.2.0</Version> | ||
<Authors>DJ Daemonix</Authors> | ||
<Company>Nodsoft ES</Company> | ||
<Product>YumeChan</Product> | ||
<Copyright>GNU GPLv3</Copyright> | ||
<NeutralLanguage>en</NeutralLanguage> | ||
<RepositoryUrl>https://github.com/DJDaemonix/YumeChan</RepositoryUrl> | ||
<RepositoryType>Git</RepositoryType> | ||
<SignAssembly>false</SignAssembly> | ||
<AssemblyVersion>0.2.0.0</AssemblyVersion> | ||
<FileVersion>0.2.0.0</FileVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Discord.Net" Version="2.1.0" /> | ||
<PackageReference Include="Discord.Net.Commands" Version="2.1.0" /> | ||
<PackageReference Include="SourceLink.Create.CommandLine" Version="2.8.3"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Discord.Net" Version="2.1.1" /> | ||
<PackageReference Include="Discord.Net.Commands" Version="2.1.1" /> | ||
<PackageReference Include="SourceLink.Create.CommandLine" Version="2.8.3"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Nodsoft.YumeChan.Modules\Nodsoft.YumeChan.Modules.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Nodsoft.YumeChan.PluginBase\Nodsoft.YumeChan.PluginBase.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Nodsoft.YumeChan.Core | ||
{ | ||
public static class Utilities | ||
{ | ||
public static bool ImplementsInterface(this Type type, Type interfaceType) | ||
{ | ||
Type[] intf = type.GetInterfaces(); | ||
foreach (Type t in intf) | ||
{ | ||
if (t == interfaceType) | ||
{ | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
} |
Oops, something went wrong.