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

Make RulestStore initialise at construction time

This commit is contained in:
smoogipoo 2019-10-15 16:14:06 +09:00
parent 6305198d84
commit 12cd57744b
4 changed files with 52 additions and 23 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

@ -14,25 +14,22 @@ 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 +51,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 +59,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 +108,7 @@ namespace osu.Game.Rulesets
}
}
private static void loadFromAppDomain()
private void loadFromAppDomain()
{
foreach (var ruleset in AppDomain.CurrentDomain.GetAssemblies())
{
@ -126,7 +121,7 @@ namespace osu.Game.Rulesets
}
}
private static void loadFromDisk()
private void loadFromDisk()
{
try
{
@ -141,11 +136,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 +153,35 @@ 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}");
}
}
~RulesetStore()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
AppDomain.CurrentDomain.AssemblyResolve -= resolveRulesetAssembly;
}
}
}