2019-01-24 16:43:03 +08:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
2018-06-02 23:28:29 +08:00
|
|
|
|
using osu.Framework.Logging;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Game.Database;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets
|
|
|
|
|
{
|
2019-10-15 15:14:06 +08:00
|
|
|
|
public class RulesetStore : DatabaseBackedStore, IDisposable
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-10-15 15:14:06 +08:00
|
|
|
|
private const string ruleset_library_prefix = "osu.Game.Rulesets";
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-10-15 15:14:06 +08:00
|
|
|
|
private readonly Dictionary<Assembly, Type> loadedAssemblies = new Dictionary<Assembly, Type>();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-10-15 15:14:06 +08:00
|
|
|
|
public RulesetStore(IDatabaseContextFactory factory)
|
|
|
|
|
: base(factory)
|
|
|
|
|
{
|
2019-07-03 17:36:04 +08:00
|
|
|
|
// On android in release configuration assemblies are loaded from the apk directly into memory.
|
2019-07-03 17:41:01 +08:00
|
|
|
|
// We cannot read assemblies from cwd, so should check loaded assemblies instead.
|
2019-07-03 17:42:10 +08:00
|
|
|
|
loadFromAppDomain();
|
|
|
|
|
loadFromDisk();
|
2019-07-15 14:42:54 +08:00
|
|
|
|
addMissingRulesets();
|
2019-10-15 15:14:06 +08:00
|
|
|
|
|
|
|
|
|
AppDomain.CurrentDomain.AssemblyResolve += resolveRulesetAssembly;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieve a ruleset using a known ID.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id">The ruleset's internal ID.</param>
|
|
|
|
|
/// <returns>A ruleset, if available, else null.</returns>
|
|
|
|
|
public RulesetInfo GetRuleset(int id) => AvailableRulesets.FirstOrDefault(r => r.ID == id);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieve a ruleset using a known short name.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="shortName">The ruleset's short name.</param>
|
|
|
|
|
/// <returns>A ruleset, if available, else null.</returns>
|
|
|
|
|
public RulesetInfo GetRuleset(string shortName) => AvailableRulesets.FirstOrDefault(r => r.ShortName == shortName);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// All available rulesets.
|
|
|
|
|
/// </summary>
|
2019-07-15 14:42:54 +08:00
|
|
|
|
public IEnumerable<RulesetInfo> AvailableRulesets { get; private set; }
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-10-15 15:14:06 +08:00
|
|
|
|
private Assembly resolveRulesetAssembly(object sender, ResolveEventArgs args) => loadedAssemblies.Keys.FirstOrDefault(a => a.FullName == args.Name);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-07-15 14:42:54 +08:00
|
|
|
|
private void addMissingRulesets()
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
using (var usage = ContextFactory.GetForWrite())
|
|
|
|
|
{
|
|
|
|
|
var context = usage.Context;
|
|
|
|
|
|
2019-10-15 15:14:06 +08:00
|
|
|
|
var instances = loadedAssemblies.Values.Select(r => (Ruleset)Activator.CreateInstance(r, (RulesetInfo)null)).ToList();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
//add all legacy modes in correct order
|
|
|
|
|
foreach (var r in instances.Where(r => r.LegacyID != null).OrderBy(r => r.LegacyID))
|
|
|
|
|
{
|
|
|
|
|
if (context.RulesetInfo.SingleOrDefault(rsi => rsi.ID == r.RulesetInfo.ID) == null)
|
|
|
|
|
context.RulesetInfo.Add(r.RulesetInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
//add any other modes
|
|
|
|
|
foreach (var r in instances.Where(r => r.LegacyID == null))
|
|
|
|
|
if (context.RulesetInfo.FirstOrDefault(ri => ri.InstantiationInfo == r.RulesetInfo.InstantiationInfo) == null)
|
|
|
|
|
context.RulesetInfo.Add(r.RulesetInfo);
|
|
|
|
|
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
//perform a consistency check
|
|
|
|
|
foreach (var r in context.RulesetInfo)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-07-01 17:03:15 +08:00
|
|
|
|
var instanceInfo = ((Ruleset)Activator.CreateInstance(Type.GetType(r.InstantiationInfo, asm =>
|
|
|
|
|
{
|
|
|
|
|
// for the time being, let's ignore the version being loaded.
|
|
|
|
|
// this allows for debug builds to successfully load rulesets (even though debug rulesets have a 0.0.0 version).
|
|
|
|
|
asm.Version = null;
|
|
|
|
|
return Assembly.Load(asm);
|
|
|
|
|
}, null), (RulesetInfo)null)).RulesetInfo;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-06-28 15:36:42 +08:00
|
|
|
|
r.Name = instanceInfo.Name;
|
|
|
|
|
r.ShortName = instanceInfo.ShortName;
|
|
|
|
|
r.InstantiationInfo = instanceInfo.InstantiationInfo;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
r.Available = true;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
r.Available = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
AvailableRulesets = context.RulesetInfo.Where(r => r.Available).ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-15 15:14:06 +08:00
|
|
|
|
private void loadFromAppDomain()
|
2019-07-02 23:05:04 +08:00
|
|
|
|
{
|
2019-07-02 23:25:12 +08:00
|
|
|
|
foreach (var ruleset in AppDomain.CurrentDomain.GetAssemblies())
|
|
|
|
|
{
|
|
|
|
|
string rulesetName = ruleset.GetName().Name;
|
|
|
|
|
|
|
|
|
|
if (!rulesetName.StartsWith(ruleset_library_prefix, StringComparison.InvariantCultureIgnoreCase) || ruleset.GetName().Name.Contains("Tests"))
|
|
|
|
|
continue;
|
2019-07-02 23:05:04 +08:00
|
|
|
|
|
|
|
|
|
addRuleset(ruleset);
|
2019-07-02 23:25:12 +08:00
|
|
|
|
}
|
2019-07-02 23:05:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-15 15:14:06 +08:00
|
|
|
|
private void loadFromDisk()
|
2019-07-03 17:42:10 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string[] files = Directory.GetFiles(Environment.CurrentDirectory, $"{ruleset_library_prefix}.*.dll");
|
|
|
|
|
|
|
|
|
|
foreach (string file in files.Where(f => !Path.GetFileName(f).Contains("Tests")))
|
|
|
|
|
loadRulesetFromFile(file);
|
|
|
|
|
}
|
2019-10-01 14:41:01 +08:00
|
|
|
|
catch (Exception e)
|
2019-07-03 17:42:10 +08:00
|
|
|
|
{
|
2019-09-30 01:25:44 +08:00
|
|
|
|
Logger.Error(e, $"Could not load rulesets from directory {Environment.CurrentDirectory}");
|
2019-07-03 17:42:10 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-15 15:14:06 +08:00
|
|
|
|
private void loadRulesetFromFile(string file)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
var filename = Path.GetFileNameWithoutExtension(file);
|
|
|
|
|
|
2019-10-15 15:14:06 +08:00
|
|
|
|
if (loadedAssemblies.Values.Any(t => t.Namespace == filename))
|
2018-04-13 17:19:50 +08:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-07-03 15:51:09 +08:00
|
|
|
|
addRuleset(Assembly.LoadFrom(file));
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
2018-06-02 23:28:29 +08:00
|
|
|
|
catch (Exception e)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-02-19 11:12:21 +08:00
|
|
|
|
Logger.Error(e, $"Failed to load ruleset {filename}");
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-02 23:05:04 +08:00
|
|
|
|
|
2019-10-15 15:14:06 +08:00
|
|
|
|
private void addRuleset(Assembly assembly)
|
2019-07-02 23:05:04 +08:00
|
|
|
|
{
|
2019-10-15 15:14:06 +08:00
|
|
|
|
if (loadedAssemblies.ContainsKey(assembly))
|
2019-07-02 23:05:04 +08:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-10-15 15:14:06 +08:00
|
|
|
|
loadedAssemblies[assembly] = assembly.GetTypes().First(t => t.IsPublic && t.IsSubclassOf(typeof(Ruleset)));
|
2019-07-02 23:05:04 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error(e, $"Failed to add ruleset {assembly}");
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-15 15:14:06 +08:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
AppDomain.CurrentDomain.AssemblyResolve -= resolveRulesetAssembly;
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|