Skip to content

Commit

Permalink
Merge pull request #12 from DJDaemonix/feature/core/plugins
Browse files Browse the repository at this point in the history
0.2.0 - YumeCore Overhaul
  • Loading branch information
DJ Daemonix authored Jul 10, 2019
2 parents f887566 + 906121c commit 2426986
Show file tree
Hide file tree
Showing 21 changed files with 447 additions and 152 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ bld/
[Bb]in/
[Oo]bj/
[Ll]og/
[Bb]uild/

# Visual Studio 2015/2017 cache/options directory
.vs/
Expand Down Expand Up @@ -327,4 +328,4 @@ ASALocalRun/
*.nvuser

# MFractors (Xamarin productivity tool) working folder
.mfractor/
.mfractor/
31 changes: 31 additions & 0 deletions YumeChan.sln
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,64 @@ MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nodsoft.YumeChan.ConsoleRunner", "src\Nodsoft.YumeChan.ConsoleRunner\Nodsoft.YumeChan.ConsoleRunner.csproj", "{B4E6E6B5-B495-4668-BB0D-1107B457C420}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nodsoft.YumeChan.Core", "src\Nodsoft.YumeChan.Core\Nodsoft.YumeChan.Core.csproj", "{58155AE6-D0DF-45E3-9B38-64ED0ACA8AC0}"
ProjectSection(ProjectDependencies) = postProject
{FD4B7EFA-7B34-4544-AD4A-EA67F006A1E8} = {FD4B7EFA-7B34-4544-AD4A-EA67F006A1E8}
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nodsoft.YumeChan.Modules", "src\Nodsoft.YumeChan.Modules\Nodsoft.YumeChan.Modules.csproj", "{FD4B7EFA-7B34-4544-AD4A-EA67F006A1E8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nodsoft.YumeChan.NetRunner", "src\Nodsoft.YumeChan.NetRunner\Nodsoft.YumeChan.NetRunner.csproj", "{AD328D2F-1AD0-414A-96A0-456820765C37}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nodsoft.YumeChan.PluginBase", "src\Nodsoft.YumeChan.PluginBase\Nodsoft.YumeChan.PluginBase.csproj", "{BA3AB42C-6875-4F32-9476-89EB8432716C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B4E6E6B5-B495-4668-BB0D-1107B457C420}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B4E6E6B5-B495-4668-BB0D-1107B457C420}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B4E6E6B5-B495-4668-BB0D-1107B457C420}.Debug|x64.ActiveCfg = Debug|Any CPU
{B4E6E6B5-B495-4668-BB0D-1107B457C420}.Debug|x64.Build.0 = Debug|Any CPU
{B4E6E6B5-B495-4668-BB0D-1107B457C420}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B4E6E6B5-B495-4668-BB0D-1107B457C420}.Release|Any CPU.Build.0 = Release|Any CPU
{B4E6E6B5-B495-4668-BB0D-1107B457C420}.Release|x64.ActiveCfg = Release|Any CPU
{B4E6E6B5-B495-4668-BB0D-1107B457C420}.Release|x64.Build.0 = Release|Any CPU
{58155AE6-D0DF-45E3-9B38-64ED0ACA8AC0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{58155AE6-D0DF-45E3-9B38-64ED0ACA8AC0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{58155AE6-D0DF-45E3-9B38-64ED0ACA8AC0}.Debug|x64.ActiveCfg = Debug|Any CPU
{58155AE6-D0DF-45E3-9B38-64ED0ACA8AC0}.Debug|x64.Build.0 = Debug|Any CPU
{58155AE6-D0DF-45E3-9B38-64ED0ACA8AC0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{58155AE6-D0DF-45E3-9B38-64ED0ACA8AC0}.Release|Any CPU.Build.0 = Release|Any CPU
{58155AE6-D0DF-45E3-9B38-64ED0ACA8AC0}.Release|x64.ActiveCfg = Release|Any CPU
{58155AE6-D0DF-45E3-9B38-64ED0ACA8AC0}.Release|x64.Build.0 = Release|Any CPU
{FD4B7EFA-7B34-4544-AD4A-EA67F006A1E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FD4B7EFA-7B34-4544-AD4A-EA67F006A1E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FD4B7EFA-7B34-4544-AD4A-EA67F006A1E8}.Debug|x64.ActiveCfg = Debug|Any CPU
{FD4B7EFA-7B34-4544-AD4A-EA67F006A1E8}.Debug|x64.Build.0 = Debug|Any CPU
{FD4B7EFA-7B34-4544-AD4A-EA67F006A1E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FD4B7EFA-7B34-4544-AD4A-EA67F006A1E8}.Release|Any CPU.Build.0 = Release|Any CPU
{FD4B7EFA-7B34-4544-AD4A-EA67F006A1E8}.Release|x64.ActiveCfg = Release|Any CPU
{FD4B7EFA-7B34-4544-AD4A-EA67F006A1E8}.Release|x64.Build.0 = Release|Any CPU
{AD328D2F-1AD0-414A-96A0-456820765C37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AD328D2F-1AD0-414A-96A0-456820765C37}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AD328D2F-1AD0-414A-96A0-456820765C37}.Debug|x64.ActiveCfg = Debug|Any CPU
{AD328D2F-1AD0-414A-96A0-456820765C37}.Debug|x64.Build.0 = Debug|Any CPU
{AD328D2F-1AD0-414A-96A0-456820765C37}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD328D2F-1AD0-414A-96A0-456820765C37}.Release|Any CPU.Build.0 = Release|Any CPU
{AD328D2F-1AD0-414A-96A0-456820765C37}.Release|x64.ActiveCfg = Release|Any CPU
{AD328D2F-1AD0-414A-96A0-456820765C37}.Release|x64.Build.0 = Release|Any CPU
{BA3AB42C-6875-4F32-9476-89EB8432716C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BA3AB42C-6875-4F32-9476-89EB8432716C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BA3AB42C-6875-4F32-9476-89EB8432716C}.Debug|x64.ActiveCfg = Debug|Any CPU
{BA3AB42C-6875-4F32-9476-89EB8432716C}.Debug|x64.Build.0 = Debug|Any CPU
{BA3AB42C-6875-4F32-9476-89EB8432716C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BA3AB42C-6875-4F32-9476-89EB8432716C}.Release|Any CPU.Build.0 = Release|Any CPU
{BA3AB42C-6875-4F32-9476-89EB8432716C}.Release|x64.ActiveCfg = Release|Any CPU
{BA3AB42C-6875-4F32-9476-89EB8432716C}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<LangVersion>preview</LangVersion>
<Version>0.1.3</Version>
<Authors>DJ Daemonix</Authors>
<Company>Nodsoft ES</Company>
Expand All @@ -13,6 +13,10 @@
<RepositoryType>Git</RepositoryType>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutDir>$(SolutionDir)build</OutDir>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SourceLink.Create.CommandLine" Version="2.8.3">
<PrivateAssets>all</PrivateAssets>
Expand Down
10 changes: 7 additions & 3 deletions src/Nodsoft.YumeChan.ConsoleRunner/Program.cs
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();
}
}
}
29 changes: 29 additions & 0 deletions src/Nodsoft.YumeChan.Core/Modules/InternalPlugin.cs
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;
}
}
}
47 changes: 47 additions & 0 deletions src/Nodsoft.YumeChan.Core/Modules/Status/Status.cs
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());
}
}
}
91 changes: 91 additions & 0 deletions src/Nodsoft.YumeChan.Core/ModulesLoader.cs
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);
}
}
}
48 changes: 25 additions & 23 deletions src/Nodsoft.YumeChan.Core/Nodsoft.YumeChan.Core.csproj
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>
22 changes: 22 additions & 0 deletions src/Nodsoft.YumeChan.Core/Utilities.cs
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;
}
}
}
Loading

0 comments on commit 2426986

Please sign in to comment.