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

68 lines
2.0 KiB

using System;
using System.IO;
namespace IPA.Loader.Features
{
internal class DefineFeature : Feature
{
public static bool NewFeature = true;
internal override bool StoreOnPlugin => false;
public override bool Initialize(PluginLoader.PluginMetadata meta, string[] parameters)
{ // parameters should be (name, fully qualified type)
if (parameters.Length != 2)
{
InvalidMessage = "Incorrect number of parameters";
return false;
}
RequireLoaded(meta);
Type type;
try
{
type = meta.Assembly.GetType(parameters[1]);
}
catch (ArgumentException)
{
InvalidMessage = $"Invalid type name {parameters[1]}";
return false;
}
catch (Exception e) when (e is FileNotFoundException || e is FileLoadException || e is BadImageFormatException)
{
var filename = "";
switch (e)
{
case FileNotFoundException fn:
filename = fn.FileName;
break;
case FileLoadException fl:
filename = fl.FileName;
break;
case BadImageFormatException bi:
filename = bi.FileName;
break;
}
InvalidMessage = $"Could not find {filename} while loading type";
return false;
}
try
{
if (RegisterFeature(parameters[0], type)) return NewFeature = true;
InvalidMessage = $"Feature with name {parameters[0]} already exists";
return false;
}
catch (ArgumentException)
{
InvalidMessage = $"{type.FullName} not a subclass of {nameof(Feature)}";
return false;
}
}
}
}