Allow discover of rulesets that are already loaded.

This commit is contained in:
miterosan 2019-07-02 17:05:04 +02:00
parent 6bddee5169
commit 778c36c7d7
1 changed files with 40 additions and 2 deletions

View File

@ -22,8 +22,20 @@ static RulesetStore()
{ {
AppDomain.CurrentDomain.AssemblyResolve += currentDomain_AssemblyResolve; AppDomain.CurrentDomain.AssemblyResolve += currentDomain_AssemblyResolve;
foreach (string file in Directory.GetFiles(Environment.CurrentDirectory, $"{ruleset_library_prefix}.*.dll") addLoadedRulesets();
.Where(f => !Path.GetFileName(f).Contains("Tests")))
IEnumerable<string> files = new string[0];
try
{
files = Directory.GetFiles(Environment.CurrentDirectory, $"{ruleset_library_prefix}.*.dll");
}
catch (Exception e)
{
Logger.Error(e, $"Could not load rulesets from directory {Environment.CurrentDirectory}");
}
foreach (string file in files.Where(f => !Path.GetFileName(f).Contains("Tests")))
loadRulesetFromFile(file); loadRulesetFromFile(file);
} }
@ -111,6 +123,17 @@ protected void AddMissingRulesets()
} }
} }
private static void addLoadedRulesets()
{
// on android the rulesets are already loaded
var loadedRulesets = AppDomain.CurrentDomain.GetAssemblies()
.Where(assembly => assembly.GetName().Name.StartsWith(ruleset_library_prefix, StringComparison.InvariantCultureIgnoreCase))
.Where(assembly => !assembly.GetName().Name.Contains("Tests"));
foreach (var ruleset in loadedRulesets)
addRuleset(ruleset);
}
private static void loadRulesetFromFile(string file) private static void loadRulesetFromFile(string file)
{ {
var filename = Path.GetFileNameWithoutExtension(file); var filename = Path.GetFileNameWithoutExtension(file);
@ -128,5 +151,20 @@ private static void loadRulesetFromFile(string file)
Logger.Error(e, $"Failed to load ruleset {filename}"); Logger.Error(e, $"Failed to load ruleset {filename}");
} }
} }
private static void addRuleset(Assembly assembly)
{
if (loaded_assemblies.ContainsKey(assembly))
return;
try
{
loaded_assemblies[assembly] = assembly.GetTypes().First(t => t.IsPublic && t.IsSubclassOf(typeof(Ruleset)));
}
catch (Exception e)
{
Logger.Error(e, $"Failed to add ruleset {assembly}");
}
}
} }
} }