2018-01-05 19:21:19 +08:00
|
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
2017-04-17 16:43:48 +08:00
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
2017-07-26 15:28:32 +08:00
|
|
|
|
using osu.Game.Database;
|
2017-04-17 16:43:48 +08:00
|
|
|
|
|
2017-07-26 15:28:32 +08:00
|
|
|
|
namespace osu.Game.Rulesets
|
2017-04-17 16:43:48 +08:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2017-07-27 15:56:41 +08:00
|
|
|
|
/// Todo: All of this needs to be moved to a RulesetStore.
|
2017-04-17 16:43:48 +08:00
|
|
|
|
/// </summary>
|
2017-07-27 15:56:41 +08:00
|
|
|
|
public class RulesetStore : DatabaseBackedStore
|
2017-04-17 16:43:48 +08:00
|
|
|
|
{
|
2017-09-19 16:19:37 +08:00
|
|
|
|
private static readonly Dictionary<Assembly, Type> loaded_assemblies = new Dictionary<Assembly, Type>();
|
2017-09-19 12:30:09 +08:00
|
|
|
|
|
2017-09-19 16:19:37 +08:00
|
|
|
|
static RulesetStore()
|
2017-04-17 16:43:48 +08:00
|
|
|
|
{
|
2017-09-19 16:19:37 +08:00
|
|
|
|
AppDomain.CurrentDomain.AssemblyResolve += currentDomain_AssemblyResolve;
|
2017-04-17 18:44:03 +08:00
|
|
|
|
|
2017-09-19 12:30:09 +08:00
|
|
|
|
foreach (string file in Directory.GetFiles(Environment.CurrentDirectory, $"{ruleset_library_prefix}.*.dll"))
|
|
|
|
|
loadRulesetFromFile(file);
|
2017-09-19 16:19:37 +08:00
|
|
|
|
}
|
2017-09-19 12:30:09 +08:00
|
|
|
|
|
2017-10-17 14:00:27 +08:00
|
|
|
|
public RulesetStore(Func<OsuDbContext> factory)
|
|
|
|
|
: base(factory)
|
2017-10-16 16:02:31 +08:00
|
|
|
|
{
|
2017-10-25 19:53:20 +08:00
|
|
|
|
AddMissingRulesets();
|
2017-10-16 16:02:31 +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);
|
|
|
|
|
|
2017-12-27 16:33:34 +08:00
|
|
|
|
/// <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);
|
|
|
|
|
|
2017-10-16 16:02:31 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// All available rulesets.
|
|
|
|
|
/// </summary>
|
2017-10-25 21:14:36 +08:00
|
|
|
|
public IEnumerable<RulesetInfo> AvailableRulesets;
|
2017-10-16 16:02:31 +08:00
|
|
|
|
|
2017-09-19 16:19:37 +08:00
|
|
|
|
private static Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args) => loaded_assemblies.Keys.FirstOrDefault(a => a.FullName == args.Name);
|
2017-04-17 16:43:48 +08:00
|
|
|
|
|
2017-09-19 16:19:37 +08:00
|
|
|
|
private const string ruleset_library_prefix = "osu.Game.Rulesets";
|
2017-04-17 16:43:48 +08:00
|
|
|
|
|
2017-10-25 19:53:20 +08:00
|
|
|
|
protected void AddMissingRulesets()
|
2017-09-19 16:19:37 +08:00
|
|
|
|
{
|
2017-10-17 14:50:42 +08:00
|
|
|
|
var context = GetContext();
|
|
|
|
|
|
2018-01-10 15:58:10 +08:00
|
|
|
|
var instances = loaded_assemblies.Values.Select(r => (Ruleset)Activator.CreateInstance(r, (RulesetInfo)null)).ToList();
|
2017-09-19 16:19:37 +08:00
|
|
|
|
|
2017-10-05 03:52:12 +08:00
|
|
|
|
//add all legacy modes in correct order
|
|
|
|
|
foreach (var r in instances.Where(r => r.LegacyID >= 0).OrderBy(r => r.LegacyID))
|
2017-04-17 16:43:48 +08:00
|
|
|
|
{
|
2018-01-10 15:58:10 +08:00
|
|
|
|
if (context.RulesetInfo.SingleOrDefault(rsi => rsi.ID == r.RulesetInfo.ID) == null)
|
|
|
|
|
context.RulesetInfo.Add(r.RulesetInfo);
|
2017-10-05 03:52:12 +08:00
|
|
|
|
}
|
2017-10-16 16:02:31 +08:00
|
|
|
|
|
2017-10-17 14:50:42 +08:00
|
|
|
|
context.SaveChanges();
|
2017-09-19 16:19:37 +08:00
|
|
|
|
|
2017-10-05 03:52:12 +08:00
|
|
|
|
//add any other modes
|
|
|
|
|
foreach (var r in instances.Where(r => r.LegacyID < 0))
|
2018-01-10 15:58:10 +08:00
|
|
|
|
if (context.RulesetInfo.FirstOrDefault(ri => ri.InstantiationInfo == r.RulesetInfo.InstantiationInfo) == null)
|
|
|
|
|
context.RulesetInfo.Add(r.RulesetInfo);
|
2017-10-16 16:02:31 +08:00
|
|
|
|
|
2017-10-17 14:50:42 +08:00
|
|
|
|
context.SaveChanges();
|
2017-04-17 16:43:48 +08:00
|
|
|
|
|
2017-10-05 03:52:12 +08:00
|
|
|
|
//perform a consistency check
|
2017-10-17 14:50:42 +08:00
|
|
|
|
foreach (var r in context.RulesetInfo)
|
2017-09-19 16:19:37 +08:00
|
|
|
|
{
|
2017-10-05 03:52:12 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-12-09 11:56:44 +08:00
|
|
|
|
var instance = r.CreateInstance();
|
|
|
|
|
|
|
|
|
|
r.Name = instance.Description;
|
|
|
|
|
r.ShortName = instance.ShortName;
|
|
|
|
|
|
2017-10-05 03:52:12 +08:00
|
|
|
|
r.Available = true;
|
|
|
|
|
}
|
|
|
|
|
catch
|
2017-09-19 16:19:37 +08:00
|
|
|
|
{
|
2017-10-05 03:52:12 +08:00
|
|
|
|
r.Available = false;
|
2017-09-19 16:19:37 +08:00
|
|
|
|
}
|
2017-10-05 03:52:12 +08:00
|
|
|
|
}
|
2017-10-16 16:02:31 +08:00
|
|
|
|
|
2017-10-17 14:50:42 +08:00
|
|
|
|
context.SaveChanges();
|
2017-10-25 21:14:36 +08:00
|
|
|
|
|
|
|
|
|
AvailableRulesets = context.RulesetInfo.Where(r => r.Available).ToList();
|
2017-04-17 16:43:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-19 16:19:37 +08:00
|
|
|
|
private static void loadRulesetFromFile(string file)
|
2017-09-19 12:30:09 +08:00
|
|
|
|
{
|
|
|
|
|
var filename = Path.GetFileNameWithoutExtension(file);
|
|
|
|
|
|
2017-09-19 16:19:37 +08:00
|
|
|
|
if (loaded_assemblies.Values.Any(t => t.Namespace == filename))
|
2017-09-19 12:30:09 +08:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-09-19 16:19:37 +08:00
|
|
|
|
var assembly = Assembly.LoadFrom(file);
|
|
|
|
|
loaded_assemblies[assembly] = assembly.GetTypes().First(t => t.IsSubclassOf(typeof(Ruleset)));
|
2017-09-19 12:30:09 +08:00
|
|
|
|
}
|
2017-10-16 12:11:35 +08:00
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
}
|
2017-09-19 12:30:09 +08:00
|
|
|
|
}
|
2017-04-17 16:43:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|