mirror of
https://github.com/ppy/osu.git
synced 2024-11-11 09:07:52 +08:00
Merge pull request #8607 from Game4all/rulesetstore-ruleset-dir
Load user rulesets from the game data directory
This commit is contained in:
commit
e035a493c8
@ -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()
|
||||
|
@ -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,24 @@ 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");
|
||||
|
||||
// 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();
|
||||
addMissingRulesets();
|
||||
|
||||
AppDomain.CurrentDomain.AssemblyResolve += resolveRulesetAssembly;
|
||||
// the event handler contains code for resolving dependency on the game assembly for rulesets located outside the base game directory.
|
||||
// It needs to be attached to the assembly lookup event before the actual call to loadUserRulesets() else rulesets located out of the base game directory will fail
|
||||
// to load as unable to locate the game core assembly.
|
||||
AppDomain.CurrentDomain.AssemblyResolve += resolveRulesetDependencyAssembly;
|
||||
loadUserRulesets();
|
||||
addMissingRulesets();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -48,7 +57,21 @@ 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);
|
||||
|
||||
// the requesting assembly may be located out of the executable's base directory, thus requiring manual resolving of its dependencies.
|
||||
// this attempts resolving the ruleset dependencies on game core and framework assemblies by returning assemblies with the same assembly name
|
||||
// already loaded in the AppDomain.
|
||||
foreach (var curAsm in AppDomain.CurrentDomain.GetAssemblies())
|
||||
{
|
||||
if (asm.Name.Equals(curAsm.GetName().Name, StringComparison.Ordinal))
|
||||
return curAsm;
|
||||
}
|
||||
|
||||
return loadedAssemblies.Keys.FirstOrDefault(a => a.FullName == asm.FullName);
|
||||
}
|
||||
|
||||
private void addMissingRulesets()
|
||||
{
|
||||
@ -120,6 +143,16 @@ namespace osu.Game.Rulesets
|
||||
}
|
||||
}
|
||||
|
||||
private void loadUserRulesets()
|
||||
{
|
||||
if (rulesetStorage == null) return;
|
||||
|
||||
var rulesets = rulesetStorage.GetFiles(".", $"{ruleset_library_prefix}.*.dll");
|
||||
|
||||
foreach (var ruleset in rulesets.Where(f => !f.Contains("Tests")))
|
||||
loadRulesetFromFile(rulesetStorage.GetFullPath(ruleset));
|
||||
}
|
||||
|
||||
private void loadFromDisk()
|
||||
{
|
||||
try
|
||||
@ -175,7 +208,7 @@ namespace osu.Game.Rulesets
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
AppDomain.CurrentDomain.AssemblyResolve -= resolveRulesetAssembly;
|
||||
AppDomain.CurrentDomain.AssemblyResolve -= resolveRulesetDependencyAssembly;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user