From 778c36c7d7a243696380f14fb0942ad08153cc9d Mon Sep 17 00:00:00 2001 From: miterosan Date: Tue, 2 Jul 2019 17:05:04 +0200 Subject: [PATCH] Allow discover of rulesets that are already loaded. --- osu.Game/Rulesets/RulesetStore.cs | 42 +++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/osu.Game/Rulesets/RulesetStore.cs b/osu.Game/Rulesets/RulesetStore.cs index 0ebadd73d2..919b5dde22 100644 --- a/osu.Game/Rulesets/RulesetStore.cs +++ b/osu.Game/Rulesets/RulesetStore.cs @@ -22,8 +22,20 @@ static RulesetStore() { AppDomain.CurrentDomain.AssemblyResolve += currentDomain_AssemblyResolve; - foreach (string file in Directory.GetFiles(Environment.CurrentDirectory, $"{ruleset_library_prefix}.*.dll") - .Where(f => !Path.GetFileName(f).Contains("Tests"))) + addLoadedRulesets(); + + IEnumerable 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); } @@ -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) { var filename = Path.GetFileNameWithoutExtension(file); @@ -128,5 +151,20 @@ private static void loadRulesetFromFile(string file) 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}"); + } + } } }