mirror of
https://github.com/osukey/osukey.git
synced 2025-06-05 12:57:39 +09: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);
|
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));
|
dependencies.Cache(FileStore = new FileStore(contextFactory, Storage));
|
||||||
|
|
||||||
// ordering is important here to ensure foreign keys rules are not broken in ModelStore.Cleanup()
|
// 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.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using osu.Framework.Logging;
|
using osu.Framework.Logging;
|
||||||
|
using osu.Framework.Platform;
|
||||||
using osu.Game.Database;
|
using osu.Game.Database;
|
||||||
|
|
||||||
namespace osu.Game.Rulesets
|
namespace osu.Game.Rulesets
|
||||||
@ -17,16 +18,24 @@ namespace osu.Game.Rulesets
|
|||||||
|
|
||||||
private readonly Dictionary<Assembly, Type> loadedAssemblies = new Dictionary<Assembly, Type>();
|
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)
|
: base(factory)
|
||||||
{
|
{
|
||||||
|
rulesetStorage = storage?.GetStorageForDirectory("rulesets");
|
||||||
|
|
||||||
// On android in release configuration assemblies are loaded from the apk directly into memory.
|
// 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.
|
// We cannot read assemblies from cwd, so should check loaded assemblies instead.
|
||||||
loadFromAppDomain();
|
loadFromAppDomain();
|
||||||
loadFromDisk();
|
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>
|
/// <summary>
|
||||||
@ -48,7 +57,21 @@ namespace osu.Game.Rulesets
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public IEnumerable<RulesetInfo> AvailableRulesets { get; private set; }
|
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()
|
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()
|
private void loadFromDisk()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -175,7 +208,7 @@ namespace osu.Game.Rulesets
|
|||||||
|
|
||||||
protected virtual void Dispose(bool disposing)
|
protected virtual void Dispose(bool disposing)
|
||||||
{
|
{
|
||||||
AppDomain.CurrentDomain.AssemblyResolve -= resolveRulesetAssembly;
|
AppDomain.CurrentDomain.AssemblyResolve -= resolveRulesetDependencyAssembly;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user