You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using DotNetCTFDumper.MMFParser.EXE;
using DotNetCTFDumper.MMFParser.EXE.Loaders;
using DotNetCTFDumper.MMFParser.MFA;
using DotNetCTFDumper.MMFParser.MFA.Loaders;
using Frame = DotNetCTFDumper.MMFParser.MFA.Loaders.Frame;
using Layer = DotNetCTFDumper.MMFParser.MFA.Loaders.Layer;
namespace DotNetCTFDumper.PluginAPI
{
public static class PluginAPI
{
public static string PluginPath = System.IO.Path.Combine(
Directory.GetCurrentDirectory(),
"Plugins");
public static List<Plugin> Plugins = new List<Plugin>();
public static void InitializePlugins()
{
Plugins.Clear();
DirectoryInfo pluginDirectory = new DirectoryInfo(PluginPath);
if (!pluginDirectory.Exists)
pluginDirectory.Create();
var pluginFiles = Directory.GetFiles(PluginPath, "*.dll");
foreach (var file in pluginFiles)
{
Assembly asm = Assembly.LoadFrom(file);
var types = asm.GetTypes().Where(t =>
t.GetInterfaces().Where(i => i.FullName == typeof(IPlugin).FullName).Any());
foreach (var type in types)
{
var pluginClass = asm.CreateInstance(type.FullName) as IPlugin;
string name = "ERROR";
foreach (var attribute in asm.GetCustomAttributes(typeof(CTFDumperPluginAttribute)))
{
name = ((CTFDumperPluginAttribute) attribute).Name;
}
var plugin = new Plugin(name,"Kostya",asm,pluginClass);
Plugins.Add(plugin);
}
}
}
public static object ActivatePlugin(Plugin plugin)
{
foreach (var attribute in plugin.Asm.GetCustomAttributes(typeof(CTFDumperPluginAttribute)))
{
if (((CTFDumperPluginAttribute) attribute).Type == PluginIOType.GameData)
{
return plugin.pluginClass.Activate(Exe.Instance.GameData);
}
else throw new NotImplementedException("Not Supported");
}
throw new NotImplementedException("Critical error ");
}
public static void Message(string msg, bool showTime = true)
{
var date = DateTime.Now;
if (showTime)
Program.MyForm.pluginLogBox.AppendText(
$"[{date.Hour,2}:{date.Minute,2}:{date.Second,2}:{date.Millisecond,3}] " + msg + "\r\n");
else Program.MyForm.pluginLogBox.AppendText(msg + "\r\n");
}
}
public class Plugin
{
public string Name;
public Assembly Asm;
public string Author;
public IPlugin pluginClass;
public Plugin(string name, string author,Assembly asm, IPlugin pluginClass)
{
Name = name;
Author = author;
Asm=asm;
this.pluginClass = pluginClass;
}
}
public enum PluginIOType
{
GameData,
PackData,
MFA,
Chunk,
ChunkLoader,
MFALoader
}
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false)]
public class CTFDumperPluginAttribute : Attribute
{
public string Name { get; }
public PluginIOType Type { get; }
public CTFDumperPluginAttribute(string name, PluginIOType type)
{
Name = name;
Type = type;
}
}
}