using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace LibAC
{
public interface IPlugin
{
///
/// This function is called when the plugin is first activated by the Plugin Loader. Tasks, such as loading configuration should be executed in this function
///
#pragma warning disable IDE1006 // Naming Styles
public void onActivate();
#pragma warning restore IDE1006 // Naming Styles
///
/// Called upon unloading the assembly
///
#pragma warning disable IDE1006 // Naming Styles
public void onDeactivate();
#pragma warning restore IDE1006 // Naming Styles
///
/// This function is called every tick. A tick should ideally run every 2 seconds
///
/// The last time a tick was executed by the host program
#pragma warning disable IDE1006 // Naming Styles
public void onTick(TimeSpan lastTick);
#pragma warning restore IDE1006 // Naming Styles
public string PluginName { get; }
public VersionNumber PluginVersion { get; }
}
public class VersionNumber
{
public int Major;
public int Minor;
public int Build;
public int Revision;
public static bool operator >(VersionNumber a, VersionNumber b)
{
bool ret = true;
bool f = false;
if (a.Major < b.Major) ret=f;
if (a.Minor < b.Minor) ret = f;
if (a.Build < b.Build) ret = f;
if (a.Revision < b.Revision) ret = f;
return ret;
}
public static bool operator <(VersionNumber a, VersionNumber b)
{
bool ret = true;
bool f = false;
if (a.Major > b.Major) ret = f;
if (a.Minor > b.Minor) ret = f;
if (a.Build > b.Build) ret = f;
if (a.Revision > b.Revision) ret = f;
return ret;
}
public static VersionNumber operator +(VersionNumber a, VersionNumber b)
{
a.Major += b.Major;
a.Minor += b.Minor;
a.Build += b.Build;
a.Revision += b.Revision;
return a;
}
public static VersionNumber operator -(VersionNumber a, VersionNumber b)
{
a.Major -= b.Major;
a.Minor -= b.Minor;
a.Build -= b.Build;
a.Revision -= b.Revision;
return a;
}
public override string ToString()
{
return $"{Major}.{Minor}.{Build}.{Revision}";
}
}
public class PluginSystem
{
public Assembly LoadedASM = null;
public Assembly LoadLibrary(string DLL)
{
LoadedASM = Assembly.LoadFrom(DLL);
return LoadedASM;
}
public List Activate(Assembly asm)
{
List ret = new List();
foreach(Type A in asm.GetTypes())
{
Type check = A.GetInterface("IPlugin");
if (check != null)
{
IPlugin plugin = Activator.CreateInstance(A) as IPlugin;
plugin.onActivate();
ret.Add(plugin);
}
}
return ret;
}
///
/// Scans memory for commands
///
/// Attribute that should be scanned for
public static void ScanForCommands()
{
}
}
public class CommandRegistry
{
public static CommandRegistry _reg = null;
public static readonly object lckreg = new object();
static CommandRegistry() { }
public static CommandRegistry Master
{
get
{
if (_reg != null) return _reg;
else
{
lock (lckreg)
{
if (_reg == null) _reg = new CommandRegistry();
return _reg;
}
}
}
}
// We now want to have as universal of a command registry as possible.
// The structure of this should be equivalent to a Hive
// All functions should return a CommandResult instance which will provide a error code, and response message.
// The CommandResult should also be able to convey if it is meant to be responded to privately, or in the same context where the command was issued.
///
/// This provides the actual registry.
///
private Hive Registry { get; set; } = new Hive();
#pragma warning disable IDE1006 // Naming Styles
public void register(CommandBase baseApi)
#pragma warning restore IDE1006 // Naming Styles
{
Registry.register(baseApi);
}
#pragma warning disable IDE1006 // Naming Styles
public CommandBase getCommand(string sCmd)
#pragma warning restore IDE1006 // Naming Styles
{
return Registry.get(sCmd);
}
}
///
/// The registry hive. Subject to change to a real registry format.
///
public class Hive
{
// This functionality, is just a registry hive
private Dictionary commands = new Dictionary();
#pragma warning disable IDE1006 // Naming Styles
public void register(CommandBase api)
#pragma warning restore IDE1006 // Naming Styles
{
commands[api.Command] = api;
}
#pragma warning disable IDE1006 // Naming Styles
public CommandBase get(string sCmd)
#pragma warning restore IDE1006 // Naming Styles
{
if (commands.ContainsKey(sCmd) == false) return null;
return commands[sCmd];
}
}
public class KVP
{
public string Key;
public T Value;
}
public class CommandArguments
{
public Dictionary args = new Dictionary();
public int AuthorityLevel;
public CommandArguments(params KVP