Load user rulesets from the game data directory

This commit is contained in:
Lucas A 2020-04-03 17:32:37 +02:00
parent c85705e935
commit 8cdae790c3
2 changed files with 36 additions and 6 deletions

View File

@ -168,7 +168,7 @@ namespace osu.Game
var defaultBeatmap = new DummyWorkingBeatmap(Audio, Textures);
dependencies.Cache(RulesetStore = new RulesetStore(contextFactory));
dependencies.Cache(RulesetStore = new RulesetStore(contextFactory, Storage));
dependencies.Cache(FileStore = new FileStore(contextFactory, Storage));
// ordering is important here to ensure foreign keys rules are not broken in ModelStore.Cleanup()

View File

@ -7,6 +7,7 @@ using System.IO;
using System.Linq;
using System.Reflection;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Game.Database;
namespace osu.Game.Rulesets
@ -17,16 +18,20 @@ namespace osu.Game.Rulesets
private readonly Dictionary<Assembly, Type> loadedAssemblies = new Dictionary<Assembly, Type>();
public RulesetStore(IDatabaseContextFactory factory)
private readonly Storage rulesetStorage;
public RulesetStore(IDatabaseContextFactory factory, Storage storage = null)
: base(factory)
{
rulesetStorage = storage?.GetStorageForDirectory("rulesets");
AppDomain.CurrentDomain.AssemblyResolve += resolveRulesetDependencyAssembly;
// 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();
loadUserRulesets();
addMissingRulesets();
AppDomain.CurrentDomain.AssemblyResolve += resolveRulesetAssembly;
}
/// <summary>
@ -48,7 +53,17 @@ namespace osu.Game.Rulesets
/// </summary>
public IEnumerable<RulesetInfo> AvailableRulesets { get; private set; }
private Assembly resolveRulesetAssembly(object sender, ResolveEventArgs args) => loadedAssemblies.Keys.FirstOrDefault(a => a.FullName == args.Name);
private Assembly resolveRulesetDependencyAssembly(object sender, ResolveEventArgs args)
{
var asm = new AssemblyName(args.Name);
// this assumes the only explicit dependency of the ruleset is the game core assembly.
// the ruleset dependency on the game core assembly requires manual resolving, transient dependencies should be resolved automatically
if (asm.Name.Equals(typeof(OsuGame).Assembly.GetName().Name, StringComparison.Ordinal))
return Assembly.GetExecutingAssembly();
return null;
}
private void addMissingRulesets()
{
@ -120,6 +135,21 @@ namespace osu.Game.Rulesets
}
}
private void loadUserRulesets()
{
try
{
var rulesets = rulesetStorage?.GetFiles(".", $"{ruleset_library_prefix}.*.dll");
foreach (var ruleset in rulesets.Where(f => !f.Contains("Tests")))
loadRulesetFromFile(rulesetStorage?.GetFullPath(ruleset));
}
catch (Exception e)
{
Logger.Error(e, "Couldn't load user rulesets");
}
}
private void loadFromDisk()
{
try
@ -175,7 +205,7 @@ namespace osu.Game.Rulesets
protected virtual void Dispose(bool disposing)
{
AppDomain.CurrentDomain.AssemblyResolve -= resolveRulesetAssembly;
AppDomain.CurrentDomain.AssemblyResolve -= resolveRulesetDependencyAssembly;
}
}
}