From 778c36c7d7a243696380f14fb0942ad08153cc9d Mon Sep 17 00:00:00 2001 From: miterosan Date: Tue, 2 Jul 2019 17:05:04 +0200 Subject: [PATCH 1/9] 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 @@ namespace osu.Game.Rulesets { 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 @@ namespace osu.Game.Rulesets } } + 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 @@ namespace osu.Game.Rulesets 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}"); + } + } } } From f1ceea8361e18ce23e9e5f4dc031f5bd40a393c5 Mon Sep 17 00:00:00 2001 From: miterosan Date: Tue, 2 Jul 2019 17:25:12 +0200 Subject: [PATCH 2/9] style fixes --- osu.Game/Rulesets/RulesetStore.cs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/osu.Game/Rulesets/RulesetStore.cs b/osu.Game/Rulesets/RulesetStore.cs index 919b5dde22..17834f8545 100644 --- a/osu.Game/Rulesets/RulesetStore.cs +++ b/osu.Game/Rulesets/RulesetStore.cs @@ -24,19 +24,17 @@ namespace osu.Game.Rulesets addLoadedRulesets(); - IEnumerable files = new string[0]; - try { - files = Directory.GetFiles(Environment.CurrentDirectory, $"{ruleset_library_prefix}.*.dll"); + string[] files = Directory.GetFiles(Environment.CurrentDirectory, $"{ruleset_library_prefix}.*.dll"); + + foreach (string file in files.Where(f => !Path.GetFileName(f).Contains("Tests"))) + loadRulesetFromFile(file); } 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); } public RulesetStore(IDatabaseContextFactory factory) @@ -125,13 +123,15 @@ namespace osu.Game.Rulesets 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 AppDomain.CurrentDomain.GetAssemblies()) + { + string rulesetName = ruleset.GetName().Name; + + if (!rulesetName.StartsWith(ruleset_library_prefix, StringComparison.InvariantCultureIgnoreCase) || ruleset.GetName().Name.Contains("Tests")) + continue; - foreach (var ruleset in loadedRulesets) addRuleset(ruleset); + } } private static void loadRulesetFromFile(string file) From d9e646d9ef75290af7cf6a79ef59c006e121928e Mon Sep 17 00:00:00 2001 From: miterosan Date: Wed, 3 Jul 2019 09:51:09 +0200 Subject: [PATCH 3/9] Use addRuleset inside loadRulesetFromFile --- osu.Game/Rulesets/RulesetStore.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/osu.Game/Rulesets/RulesetStore.cs b/osu.Game/Rulesets/RulesetStore.cs index 17834f8545..15adc258a1 100644 --- a/osu.Game/Rulesets/RulesetStore.cs +++ b/osu.Game/Rulesets/RulesetStore.cs @@ -143,8 +143,7 @@ namespace osu.Game.Rulesets try { - var assembly = Assembly.LoadFrom(file); - loaded_assemblies[assembly] = assembly.GetTypes().First(t => t.IsPublic && t.IsSubclassOf(typeof(Ruleset))); + addRuleset(Assembly.LoadFrom(file)); } catch (Exception e) { From ab244d1b7f7cb1ab4abf7c65aa043571cc8165b9 Mon Sep 17 00:00:00 2001 From: miterosan Date: Wed, 3 Jul 2019 10:21:18 +0200 Subject: [PATCH 4/9] Only log that the rulesets could not be loaded from a directory. --- osu.Game/Rulesets/RulesetStore.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/osu.Game/Rulesets/RulesetStore.cs b/osu.Game/Rulesets/RulesetStore.cs index 15adc258a1..31c8d03df4 100644 --- a/osu.Game/Rulesets/RulesetStore.cs +++ b/osu.Game/Rulesets/RulesetStore.cs @@ -31,9 +31,9 @@ namespace osu.Game.Rulesets foreach (string file in files.Where(f => !Path.GetFileName(f).Contains("Tests"))) loadRulesetFromFile(file); } - catch (Exception e) + catch { - Logger.Error(e, $"Could not load rulesets from directory {Environment.CurrentDirectory}"); + Logger.Log($"Could not load rulesets from directory {Environment.CurrentDirectory}"); } } From 43d7f66c5bb095bd63ba02de3d34eaefe4c34d79 Mon Sep 17 00:00:00 2001 From: miterosan Date: Wed, 3 Jul 2019 10:54:10 +0200 Subject: [PATCH 5/9] Add comment about the android ruleset situation. --- osu.Game/Rulesets/RulesetStore.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/osu.Game/Rulesets/RulesetStore.cs b/osu.Game/Rulesets/RulesetStore.cs index 31c8d03df4..d0f6971e1c 100644 --- a/osu.Game/Rulesets/RulesetStore.cs +++ b/osu.Game/Rulesets/RulesetStore.cs @@ -22,6 +22,8 @@ namespace osu.Game.Rulesets { AppDomain.CurrentDomain.AssemblyResolve += currentDomain_AssemblyResolve; + // On android in release configuration the assemblies are loaded from the apk directly into memory. + // In this case there is no way to access the ruleset assemblies from the filesystem. addLoadedRulesets(); try From 5ecb764cdcf65ddd0f9a0bae68f30cda46072542 Mon Sep 17 00:00:00 2001 From: miterosan Date: Wed, 3 Jul 2019 10:57:09 +0200 Subject: [PATCH 6/9] Remove whitespaces and lowercase the comments --- osu.Game/Rulesets/RulesetStore.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/osu.Game/Rulesets/RulesetStore.cs b/osu.Game/Rulesets/RulesetStore.cs index d0f6971e1c..a7d62665db 100644 --- a/osu.Game/Rulesets/RulesetStore.cs +++ b/osu.Game/Rulesets/RulesetStore.cs @@ -22,8 +22,8 @@ namespace osu.Game.Rulesets { AppDomain.CurrentDomain.AssemblyResolve += currentDomain_AssemblyResolve; - // On android in release configuration the assemblies are loaded from the apk directly into memory. - // In this case there is no way to access the ruleset assemblies from the filesystem. + //on android in release configuration the assemblies are loaded from the apk directly into memory. + //in this case there is no way to access the rulesets assemblies from the filesystem. addLoadedRulesets(); try From 62dd89197c7fce70d38744a01beea5ad74056872 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 3 Jul 2019 18:36:04 +0900 Subject: [PATCH 7/9] Refactor comment slightly --- osu.Game/Rulesets/RulesetStore.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/osu.Game/Rulesets/RulesetStore.cs b/osu.Game/Rulesets/RulesetStore.cs index a7d62665db..4476f16e32 100644 --- a/osu.Game/Rulesets/RulesetStore.cs +++ b/osu.Game/Rulesets/RulesetStore.cs @@ -22,8 +22,8 @@ namespace osu.Game.Rulesets { AppDomain.CurrentDomain.AssemblyResolve += currentDomain_AssemblyResolve; - //on android in release configuration the assemblies are loaded from the apk directly into memory. - //in this case there is no way to access the rulesets assemblies from the filesystem. + // On android in release configuration assemblies are loaded from the apk directly into memory. + // We cannot read assemblies from cwd, so should check loaded assemblies isntead. addLoadedRulesets(); try From 06ef8f71e96e2af7242c558c2cd279afe3c72a6c Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 3 Jul 2019 18:41:01 +0900 Subject: [PATCH 8/9] Fix typo --- osu.Game/Rulesets/RulesetStore.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Rulesets/RulesetStore.cs b/osu.Game/Rulesets/RulesetStore.cs index 4476f16e32..69ce5f97b6 100644 --- a/osu.Game/Rulesets/RulesetStore.cs +++ b/osu.Game/Rulesets/RulesetStore.cs @@ -23,7 +23,7 @@ namespace osu.Game.Rulesets AppDomain.CurrentDomain.AssemblyResolve += currentDomain_AssemblyResolve; // On android in release configuration assemblies are loaded from the apk directly into memory. - // We cannot read assemblies from cwd, so should check loaded assemblies isntead. + // We cannot read assemblies from cwd, so should check loaded assemblies instead. addLoadedRulesets(); try From 8120bb36bc598acadb0d30952bd370fdeb639fae Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 3 Jul 2019 18:42:10 +0900 Subject: [PATCH 9/9] More methods --- osu.Game/Rulesets/RulesetStore.cs | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/osu.Game/Rulesets/RulesetStore.cs b/osu.Game/Rulesets/RulesetStore.cs index 69ce5f97b6..fd42f96c92 100644 --- a/osu.Game/Rulesets/RulesetStore.cs +++ b/osu.Game/Rulesets/RulesetStore.cs @@ -24,19 +24,9 @@ namespace osu.Game.Rulesets // On android in release configuration assemblies are loaded from the apk directly into memory. // We cannot read assemblies from cwd, so should check loaded assemblies instead. - addLoadedRulesets(); + loadFromAppDomain(); - try - { - string[] files = Directory.GetFiles(Environment.CurrentDirectory, $"{ruleset_library_prefix}.*.dll"); - - foreach (string file in files.Where(f => !Path.GetFileName(f).Contains("Tests"))) - loadRulesetFromFile(file); - } - catch - { - Logger.Log($"Could not load rulesets from directory {Environment.CurrentDirectory}"); - } + loadFromDisk(); } public RulesetStore(IDatabaseContextFactory factory) @@ -123,7 +113,7 @@ namespace osu.Game.Rulesets } } - private static void addLoadedRulesets() + private static void loadFromAppDomain() { foreach (var ruleset in AppDomain.CurrentDomain.GetAssemblies()) { @@ -136,6 +126,21 @@ namespace osu.Game.Rulesets } } + private static void loadFromDisk() + { + try + { + string[] files = Directory.GetFiles(Environment.CurrentDirectory, $"{ruleset_library_prefix}.*.dll"); + + foreach (string file in files.Where(f => !Path.GetFileName(f).Contains("Tests"))) + loadRulesetFromFile(file); + } + catch + { + Logger.Log($"Could not load rulesets from directory {Environment.CurrentDirectory}"); + } + } + private static void loadRulesetFromFile(string file) { var filename = Path.GetFileNameWithoutExtension(file);