1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-14 03:25:11 +08:00

Make RulesetStore initialise at construction time (#6515)

Make RulesetStore initialise at construction time

Co-authored-by: Dean Herbert <pe@ppy.sh>
This commit is contained in:
Dean Herbert 2019-10-17 16:10:32 +09:00 committed by GitHub
commit a1bb061405
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 26 deletions

View File

@ -285,6 +285,12 @@ namespace osu.Game.Tests.Visual.Background
});
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
rulesets?.Dispose();
}
private class DummySongSelect : PlaySongSelect
{
protected override BackgroundScreen CreateBackground()

View File

@ -349,5 +349,11 @@ namespace osu.Game.Tests.Visual.SongSelect
DateAdded = DateTimeOffset.UtcNow,
};
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
rulesets?.Dispose();
}
}
}

View File

@ -298,6 +298,12 @@ namespace osu.Game
public string[] HandledExtensions => fileImporters.SelectMany(i => i.HandledExtensions).ToArray();
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
RulesetStore?.Dispose();
}
private class OsuUserInputManager : UserInputManager
{
protected override MouseButtonEventManager CreateButtonManagerFor(MouseButton button)

View File

@ -11,28 +11,22 @@ using osu.Game.Database;
namespace osu.Game.Rulesets
{
/// <summary>
/// Todo: All of this needs to be moved to a RulesetStore.
/// </summary>
public class RulesetStore : DatabaseBackedStore
public class RulesetStore : DatabaseBackedStore, IDisposable
{
private static readonly Dictionary<Assembly, Type> loaded_assemblies = new Dictionary<Assembly, Type>();
private const string ruleset_library_prefix = "osu.Game.Rulesets";
static RulesetStore()
{
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 instead.
loadFromAppDomain();
loadFromDisk();
}
private readonly Dictionary<Assembly, Type> loadedAssemblies = new Dictionary<Assembly, Type>();
public RulesetStore(IDatabaseContextFactory factory)
: base(factory)
{
// 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;
}
/// <summary>
@ -54,9 +48,7 @@ namespace osu.Game.Rulesets
/// </summary>
public IEnumerable<RulesetInfo> AvailableRulesets { get; private set; }
private static Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args) => loaded_assemblies.Keys.FirstOrDefault(a => a.FullName == args.Name);
private const string ruleset_library_prefix = "osu.Game.Rulesets";
private Assembly resolveRulesetAssembly(object sender, ResolveEventArgs args) => loadedAssemblies.Keys.FirstOrDefault(a => a.FullName == args.Name);
private void addMissingRulesets()
{
@ -64,7 +56,7 @@ namespace osu.Game.Rulesets
{
var context = usage.Context;
var instances = loaded_assemblies.Values.Select(r => (Ruleset)Activator.CreateInstance(r, (RulesetInfo)null)).ToList();
var instances = loadedAssemblies.Values.Select(r => (Ruleset)Activator.CreateInstance(r, (RulesetInfo)null)).ToList();
//add all legacy modes in correct order
foreach (var r in instances.Where(r => r.LegacyID != null).OrderBy(r => r.LegacyID))
@ -113,7 +105,7 @@ namespace osu.Game.Rulesets
}
}
private static void loadFromAppDomain()
private void loadFromAppDomain()
{
foreach (var ruleset in AppDomain.CurrentDomain.GetAssemblies())
{
@ -126,7 +118,7 @@ namespace osu.Game.Rulesets
}
}
private static void loadFromDisk()
private void loadFromDisk()
{
try
{
@ -141,11 +133,11 @@ namespace osu.Game.Rulesets
}
}
private static void loadRulesetFromFile(string file)
private void loadRulesetFromFile(string file)
{
var filename = Path.GetFileNameWithoutExtension(file);
if (loaded_assemblies.Values.Any(t => t.Namespace == filename))
if (loadedAssemblies.Values.Any(t => t.Namespace == filename))
return;
try
@ -158,19 +150,30 @@ namespace osu.Game.Rulesets
}
}
private static void addRuleset(Assembly assembly)
private void addRuleset(Assembly assembly)
{
if (loaded_assemblies.ContainsKey(assembly))
if (loadedAssemblies.ContainsKey(assembly))
return;
try
{
loaded_assemblies[assembly] = assembly.GetTypes().First(t => t.IsPublic && t.IsSubclassOf(typeof(Ruleset)));
loadedAssemblies[assembly] = assembly.GetTypes().First(t => t.IsPublic && t.IsSubclassOf(typeof(Ruleset)));
}
catch (Exception e)
{
Logger.Error(e, $"Failed to add ruleset {assembly}");
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
AppDomain.CurrentDomain.AssemblyResolve -= resolveRulesetAssembly;
}
}
}