Skip to content

Instantly share code, notes, and snippets.

@GuyInGrey
Created April 24, 2020 02:03
Show Gist options
  • Save GuyInGrey/ffcb8bad5bdb76ee8b0f1617ded7a26c to your computer and use it in GitHub Desktop.
Save GuyInGrey/ffcb8bad5bdb76ee8b0f1617ded7a26c to your computer and use it in GitHub Desktop.
public class ModuleInfo
{
public string Name;
}
public abstract class TetherModule
{
public abstract void Startup();
public abstract ModuleInfo GetInfo();
}
using System;
using Tether;
namespace Schedule
{
public class ScheduleModule : TetherModule
{
public override void Startup()
{
Console.WriteLine("Hello World!");
}
public override ModuleInfo GetInfo() =>
new ModuleInfo() { Name = "Schedule", };
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
namespace Tether
{
public class TetherStart
{
string ModulesPath => @"Modules\";
private static Dictionary<string, TetherModule> Modules = new Dictionary<string, TetherModule>();
public TetherStart()
{
foreach (var file in Directory.GetFiles(ModulesPath))
{
if (Path.GetExtension(file) != ".dll")
{
continue;
}
var assembly = Assembly.LoadFrom(file);
foreach (var type in assembly.GetTypes())
{
if (type.IsSubclassOf(typeof(TetherModule)))
{
var module = (TetherModule)Activator.CreateInstance(type);
var info = module.GetInfo();
if (info == null)
{
Console.WriteLine("Failed to load module `" + module.GetType().Name + "`. No info provided.");
}
else
{
Modules.Add(info.Name, module);
}
}
}
}
// Modules startup after all are loaded, some modules require others to be loaded before startup
foreach (var module in Modules)
{
module.Value.Startup();
}
Console.ReadLine();
}
public static TetherModule GetModule(string name) =>
Modules.ContainsKey(name) ? Modules[name] : null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment