Browse Source

Tweaked some feature parsing

Added AddInFeature
pull/1/head
DaNike 5 years ago
parent
commit
fc7148c4cc
7 changed files with 72 additions and 9 deletions
  1. +1
    -0
      IPA.Loader/IPA.Loader.csproj
  2. +21
    -0
      IPA.Loader/Loader/Features/AddInFeature.cs
  3. +2
    -0
      IPA.Loader/Loader/Features/DefineFeature.cs
  4. +21
    -7
      IPA.Loader/Loader/Features/Feature.cs
  5. +18
    -0
      IPA.Loader/Loader/Features/PrintFeature.cs
  6. +6
    -2
      IPA.Loader/Loader/PluginLoader.cs
  7. +3
    -0
      IPA.Loader/Loader/manifest.json

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

@ -62,6 +62,7 @@
<Compile Include="Config\IConfigProvider.cs" /> <Compile Include="Config\IConfigProvider.cs" />
<Compile Include="Config\SelfConfig.cs" /> <Compile Include="Config\SelfConfig.cs" />
<Compile Include="Loader\Composite\CompositeBSPlugin.cs" /> <Compile Include="Loader\Composite\CompositeBSPlugin.cs" />
<Compile Include="Loader\Features\AddInFeature.cs" />
<Compile Include="Loader\Features\DefineFeature.cs" /> <Compile Include="Loader\Features\DefineFeature.cs" />
<Compile Include="Loader\Features\NoUpdateFeature.cs" /> <Compile Include="Loader\Features\NoUpdateFeature.cs" />
<Compile Include="Loader\Features\PrintFeature.cs" /> <Compile Include="Loader\Features\PrintFeature.cs" />


+ 21
- 0
IPA.Loader/Loader/Features/AddInFeature.cs View File

@ -0,0 +1,21 @@
namespace IPA.Loader.Features
{
internal class AddInFeature : Feature
{
private PluginLoader.PluginMetadata selfMeta;
public override bool Initialize(PluginLoader.PluginMetadata meta, string[] parameters)
{
selfMeta = meta;
RequireLoaded(meta);
return true;
}
public override bool BeforeLoad(PluginLoader.PluginMetadata plugin)
{
return plugin != selfMeta;
}
}
}

+ 2
- 0
IPA.Loader/Loader/Features/DefineFeature.cs View File

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


+ 21
- 7
IPA.Loader/Loader/Features/Feature.cs View File

@ -25,6 +25,14 @@ namespace IPA.Loader.Features
/// <returns><see langword="true"/> if the feature is valid for the plugin, <see langword="false"/> otherwise</returns> /// <returns><see langword="true"/> if the feature is valid for the plugin, <see langword="false"/> otherwise</returns>
public abstract bool Initialize(PluginLoader.PluginMetadata meta, string[] parameters); public abstract bool Initialize(PluginLoader.PluginMetadata meta, string[] parameters);
/// <summary>
/// Evaluates the Feature for use in conditional meta-Features. This should be re-calculated on every call, unless it can be proven to not change.
///
/// This will be called on every feature that returns <see langword="true" /> from <see cref="Initialize"/>
/// </summary>
/// <returns>the truthiness of the Feature.</returns>
public virtual bool Evaluate() => true;
/// <summary> /// <summary>
/// The message to be logged when the feature is not valid for a plugin. /// The message to be logged when the feature is not valid for a plugin.
/// This should also be set whenever either <see cref="BeforeLoad"/> or <see cref="BeforeInit"/> returns false. /// This should also be set whenever either <see cref="BeforeLoad"/> or <see cref="BeforeInit"/> returns false.
@ -61,11 +69,15 @@ namespace IPA.Loader.Features
/// <param name="plugin">the plugin to ensure is loaded.</param> /// <param name="plugin">the plugin to ensure is loaded.</param>
protected void RequireLoaded(PluginLoader.PluginMetadata plugin) => PluginLoader.Load(plugin); protected void RequireLoaded(PluginLoader.PluginMetadata plugin) => PluginLoader.Load(plugin);
internal virtual bool StoreOnPlugin => true;
private static readonly Dictionary<string, Type> featureTypes = new Dictionary<string, Type> private static readonly Dictionary<string, Type> featureTypes = new Dictionary<string, Type>
{ {
{ "define-feature", typeof(DefineFeature) } { "define-feature", typeof(DefineFeature) }
}; };
internal static bool HasFeature(string name) => featureTypes.ContainsKey(name);
internal static bool RegisterFeature(string name, Type type) internal static bool RegisterFeature(string name, Type type)
{ {
if (!typeof(Feature).IsAssignableFrom(type)) if (!typeof(Feature).IsAssignableFrom(type))
@ -103,7 +115,7 @@ namespace IPA.Loader.Features
var parameters = new List<string>(); var parameters = new List<string>();
bool escape = false; bool escape = false;
bool readingParams = false;
int parens = 0;
bool removeWhitespace = true; bool removeWhitespace = true;
foreach (var chr in featureString) foreach (var chr in featureString)
{ {
@ -119,18 +131,20 @@ namespace IPA.Loader.Features
case '\\': case '\\':
escape = true; escape = true;
break; break;
case '(' when !readingParams:
case '(':
parens++;
if (parens != 1) goto default;
removeWhitespace = true; removeWhitespace = true;
readingParams = true;
name = builder.ToString(); name = builder.ToString();
builder.Clear(); builder.Clear();
break; break;
case ')' when readingParams:
readingParams = false;
case ')':
parens--;
if (parens != 0) goto default;
goto case ','; goto case ',';
case ',': case ',':
if (parens > 1) goto default;
parameters.Add(builder.ToString()); parameters.Add(builder.ToString());
if (!readingParams) break;
builder.Clear(); builder.Clear();
removeWhitespace = true; removeWhitespace = true;
break; break;
@ -149,7 +163,7 @@ namespace IPA.Loader.Features
parsed = new FeatureParse(name, parameters.ToArray()); parsed = new FeatureParse(name, parameters.ToArray());
if (readingParams)
if (parens != 0)
{ {
failException = new Exception("Malformed feature definition"); failException = new Exception("Malformed feature definition");
return false; return false;


+ 18
- 0
IPA.Loader/Loader/Features/PrintFeature.cs View File

@ -11,4 +11,22 @@ namespace IPA.Loader.Features
return true; return true;
} }
} }
internal class DebugFeature : Feature
{
public override bool Initialize(PluginLoader.PluginMetadata meta, string[] parameters)
{
Logger.features.Debug($"{meta.Name}: {string.Join(" ", parameters)}");
return true;
}
}
internal class WarnFeature : Feature
{
public override bool Initialize(PluginLoader.PluginMetadata meta, string[] parameters)
{
Logger.features.Debug($"{meta.Name}: {string.Join(" ", parameters)}");
return true;
}
}
} }

+ 6
- 2
IPA.Loader/Loader/PluginLoader.cs View File

@ -322,9 +322,9 @@ namespace IPA.Loader
feature.Item2.Value = parsed; feature.Item2.Value = parsed;
else if (success) else if (success)
{ {
if (valid)
if (valid && featureObj.StoreOnPlugin)
plugin.Item1.InternalFeatures.Add(featureObj); plugin.Item1.InternalFeatures.Add(featureObj);
else
else if (!valid)
Logger.features.Warn( Logger.features.Warn(
$"Feature not valid on {plugin.Item1.Name}: {featureObj.InvalidMessage}"); $"Feature not valid on {plugin.Item1.Name}: {featureObj.InvalidMessage}");
plugin.Item2.RemoveAt(i--); plugin.Item2.RemoveAt(i--);
@ -336,6 +336,10 @@ namespace IPA.Loader
plugin.Item2.RemoveAt(i--); plugin.Item2.RemoveAt(i--);
} }
} }
foreach (var plugin in PluginsMetadata)
foreach (var feature in plugin.Features)
feature.Evaluate();
} }
foreach (var plugin in parsedFeatures) foreach (var plugin in parsedFeatures)


+ 3
- 0
IPA.Loader/Loader/manifest.json View File

@ -8,7 +8,10 @@
"version": "3.12.0", "version": "3.12.0",
"features": [ "features": [
"define-feature(print, IPA.Loader.Features.PrintFeature)", "define-feature(print, IPA.Loader.Features.PrintFeature)",
"define-feature(debug, IPA.Loader.Features.DebugFeature)",
"define-feature(warn, IPA.Loader.Features.WarnFeature)",
"define-feature(no-update, IPA.Loader.Features.NoUpdateFeature)", "define-feature(no-update, IPA.Loader.Features.NoUpdateFeature)",
"define-feature(add-in, IPA.Loader.Features.AddInFeature)",
"print(YO! Howz it goin\\, its ya boi desinc here)" "print(YO! Howz it goin\\, its ya boi desinc here)"
] ]
} }

Loading…
Cancel
Save