Created
April 24, 2020 02:03
-
-
Save GuyInGrey/ffcb8bad5bdb76ee8b0f1617ded7a26c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class ModuleInfo | |
{ | |
public string Name; | |
} | |
public abstract class TetherModule | |
{ | |
public abstract void Startup(); | |
public abstract ModuleInfo GetInfo(); | |
} |
This file contains hidden or 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
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", }; | |
} | |
} |
This file contains hidden or 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
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