1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 12:07:23 +08:00
osu-lazer/osu.Game/Rulesets/RulesetStore.cs

117 lines
4.0 KiB
C#
Raw Normal View History

2017-04-17 16:43:48 +08:00
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// 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;
using osu.Game.Database;
2017-04-17 16:43:48 +08:00
using SQLite.Net;
namespace osu.Game.Rulesets
2017-04-17 16:43:48 +08:00
{
/// <summary>
/// Todo: All of this needs to be moved to a RulesetStore.
2017-04-17 16:43:48 +08:00
/// </summary>
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-04-17 16:43:48 +08:00
public IEnumerable<RulesetInfo> AllRulesets => Query<RulesetInfo>().Where(r => r.Available);
public RulesetStore(SQLiteConnection connection) : base(connection)
2017-04-17 16:43:48 +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
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 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-09-19 16:19:37 +08:00
protected override void Prepare(bool reset = false)
{
Connection.CreateTable<RulesetInfo>();
2017-04-17 16:43:48 +08:00
2017-09-19 16:19:37 +08:00
if (reset)
{
Connection.DeleteAll<RulesetInfo>();
2017-04-17 16:43:48 +08:00
}
2017-09-19 16:19:37 +08:00
var instances = loaded_assemblies.Values.Select(r => (Ruleset)Activator.CreateInstance(r, new RulesetInfo()));
Connection.RunInTransaction(() =>
2017-04-17 16:43:48 +08:00
{
2017-09-19 16:19:37 +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
{
2017-09-19 16:19:37 +08:00
Connection.InsertOrReplace(createRulesetInfo(r));
2017-04-17 16:43:48 +08:00
}
2017-09-19 16:19:37 +08:00
//add any other modes
foreach (var r in instances.Where(r => r.LegacyID < 0))
2017-04-17 16:43:48 +08:00
{
2017-09-19 16:19:37 +08:00
var us = createRulesetInfo(r);
2017-04-17 16:43:48 +08:00
2017-09-19 16:19:37 +08:00
var existing = Query<RulesetInfo>().Where(ri => ri.InstantiationInfo == us.InstantiationInfo).FirstOrDefault();
if (existing == null)
Connection.Insert(us);
}
});
2017-04-17 16:43:48 +08:00
2017-09-19 16:19:37 +08:00
Connection.RunInTransaction(() =>
{
//perform a consistency check
foreach (var r in Query<RulesetInfo>())
{
try
{
r.CreateInstance();
r.Available = true;
}
catch
{
r.Available = false;
}
Connection.Update(r);
}
});
2017-04-17 16:43:48 +08:00
}
2017-09-19 16:19:37 +08:00
private static void loadRulesetFromFile(string file)
{
var filename = Path.GetFileNameWithoutExtension(file);
2017-09-19 16:19:37 +08:00
if (loaded_assemblies.Values.Any(t => t.Namespace == filename))
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)));
}
catch (Exception) { }
}
2017-04-17 16:43:48 +08:00
private RulesetInfo createRulesetInfo(Ruleset ruleset) => new RulesetInfo
{
Name = ruleset.Description,
InstantiationInfo = ruleset.GetType().AssemblyQualifiedName,
ID = ruleset.LegacyID
};
protected override Type[] ValidTypes => new[] { typeof(RulesetInfo) };
public RulesetInfo GetRuleset(int id) => Query<RulesetInfo>().First(r => r.ID == id);
}
}