1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 05:32:54 +08:00

Read rulesets from AppDomain in addition to disk (#5216)

Read rulesets from AppDomain in addition to disk

Co-authored-by: Dean Herbert <pe@ppy.sh>
This commit is contained in:
Dean Herbert 2019-07-03 19:02:22 +09:00 committed by GitHub
commit 99ff59be87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,9 +22,11 @@ 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")))
loadRulesetFromFile(file);
// 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.
loadFromAppDomain();
loadFromDisk();
}
public RulesetStore(IDatabaseContextFactory factory)
@ -111,6 +113,34 @@ namespace osu.Game.Rulesets
}
}
private static void loadFromAppDomain()
{
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;
addRuleset(ruleset);
}
}
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);
@ -120,13 +150,27 @@ 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)
{
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}");
}
}
}
}