Browse Source

Opened Feature.StoreOnPlugin to all subclasses

Added InitInjectorFeature as `init-injector`
pull/11/head
Anairkoen Schno 5 years ago
parent
commit
d86d140d56
5 changed files with 110 additions and 3 deletions
  1. +1
    -0
      IPA.Loader/IPA.Loader.csproj
  2. +1
    -1
      IPA.Loader/Loader/Features/DefineFeature.cs
  3. +4
    -1
      IPA.Loader/Loader/Features/Feature.cs
  4. +102
    -0
      IPA.Loader/Loader/Features/InitInjectorFeature.cs
  5. +2
    -1
      IPA.Loader/Loader/manifest.json

+ 1
- 0
IPA.Loader/IPA.Loader.csproj View File

@ -64,6 +64,7 @@
<Compile Include="Loader\Composite\CompositeBSPlugin.cs" />
<Compile Include="Loader\Features\AddInFeature.cs" />
<Compile Include="Loader\Features\DefineFeature.cs" />
<Compile Include="Loader\Features\InitInjectorFeature.cs" />
<Compile Include="Loader\Features\NoUpdateFeature.cs" />
<Compile Include="Loader\Features\PrintFeature.cs" />
<Compile Include="Loader\PluginInitInjector.cs" />


+ 1
- 1
IPA.Loader/Loader/Features/DefineFeature.cs View File

@ -7,7 +7,7 @@ namespace IPA.Loader.Features
{
public static bool NewFeature = true;
internal override bool StoreOnPlugin => false;
protected internal override bool StoreOnPlugin => false;
public override bool Initialize(PluginLoader.PluginMetadata meta, string[] parameters)
{ // parameters should be (name, fully qualified type)


+ 4
- 1
IPA.Loader/Loader/Features/Feature.cs View File

@ -69,7 +69,10 @@ namespace IPA.Loader.Features
/// <param name="plugin">the plugin to ensure is loaded.</param>
protected void RequireLoaded(PluginLoader.PluginMetadata plugin) => PluginLoader.Load(plugin);
internal virtual bool StoreOnPlugin => true;
/// <summary>
/// Defines whether or not this feature will be accessible from the plugin metadata once loaded.
/// </summary>
protected internal virtual bool StoreOnPlugin => true;
private static readonly Dictionary<string, Type> featureTypes = new Dictionary<string, Type>
{


+ 102
- 0
IPA.Loader/Loader/Features/InitInjectorFeature.cs View File

@ -0,0 +1,102 @@
using System;
using System.IO;
using System.Reflection;
namespace IPA.Loader.Features
{
internal class InitInjectorFeature : Feature
{
protected internal override bool StoreOnPlugin => false;
public override bool Initialize(PluginLoader.PluginMetadata meta, string[] parameters)
{ // parameters should be (assembly qualified lookup type, [fully qualified type]:[method name])
// method should be static
if (parameters.Length != 2)
{
InvalidMessage = "Incorrect number of parameters";
return false;
}
RequireLoaded(meta);
var methodParts = parameters[1].Split(':');
var type = Type.GetType(parameters[0], false);
if (type == null)
{
InvalidMessage = $"Could not find type {parameters[0]}";
return false;
}
Type getType;
try
{
getType = meta.Assembly.GetType(methodParts[0]);
}
catch (ArgumentException)
{
InvalidMessage = $"Invalid type name {methodParts[0]}";
return false;
}
catch (Exception e) when (e is FileNotFoundException || e is FileLoadException || e is BadImageFormatException)
{
string filename;
switch (e)
{
case FileNotFoundException fn:
filename = fn.FileName;
goto hasFilename;
case FileLoadException fl:
filename = fl.FileName;
goto hasFilename;
case BadImageFormatException bi:
filename = bi.FileName;
hasFilename:
InvalidMessage = $"Could not find {filename} while loading type";
break;
default:
InvalidMessage = $"Error while loading type: {e}";
break;
}
return false;
}
MethodInfo method;
try
{
method = getType.GetMethod(methodParts[1], BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic,
null, new[]
{
typeof(object),
typeof(ParameterInfo),
typeof(PluginLoader.PluginMetadata)
}, new ParameterModifier[0]);
}
catch (Exception e)
{
InvalidMessage = $"Error while loading type: {e}";
return false;
}
if (method == null)
{
InvalidMessage = $"Could not find method {methodParts[1]} in type {methodParts[0]}";
return false;
}
try
{
var del = (PluginInitInjector.InjectParameter)Delegate.CreateDelegate(typeof(PluginInitInjector.InjectParameter), null, method);
PluginInitInjector.AddInjector(type, del);
return true;
}
catch (Exception e)
{
InvalidMessage = $"Error generated while creating delegate: {e}";
return false;
}
}
}
}

+ 2
- 1
IPA.Loader/Loader/manifest.json View File

@ -11,6 +11,7 @@
"define-feature(debug, IPA.Loader.Features.DebugFeature)",
"define-feature(warn, IPA.Loader.Features.WarnFeature)",
"define-feature(no-update, IPA.Loader.Features.NoUpdateFeature)",
"define-feature(add-in, IPA.Loader.Features.AddInFeature)"
"define-feature(add-in, IPA.Loader.Features.AddInFeature)",
"define-feature(init-injector, IPA.Loader.Features.InitInjectorFeature)"
]
}

Loading…
Cancel
Save