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;
|
2017-07-26 15:28:32 +08:00
|
|
|
|
using osu.Game.Database;
|
2017-04-17 16:43:48 +08:00
|
|
|
|
using SQLite.Net;
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
public IEnumerable<RulesetInfo> AllRulesets => Query<RulesetInfo>().Where(r => r.Available);
|
|
|
|
|
|
2017-07-27 15:56:41 +08:00
|
|
|
|
public RulesetStore(SQLiteConnection connection) : base(connection)
|
2017-04-17 16:43:48 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-17 18:44:03 +08:00
|
|
|
|
protected override void Prepare(bool reset = false)
|
2017-04-17 16:43:48 +08:00
|
|
|
|
{
|
|
|
|
|
Connection.CreateTable<RulesetInfo>();
|
|
|
|
|
|
2017-04-17 18:44:03 +08:00
|
|
|
|
if (reset)
|
|
|
|
|
{
|
|
|
|
|
Connection.DeleteAll<RulesetInfo>();
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-17 16:43:48 +08:00
|
|
|
|
List<Ruleset> instances = new List<Ruleset>();
|
|
|
|
|
|
2017-04-18 15:05:58 +08:00
|
|
|
|
foreach (string file in Directory.GetFiles(Environment.CurrentDirectory, @"osu.Game.Rulesets.*.dll"))
|
2017-04-17 16:43:48 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var assembly = Assembly.LoadFile(file);
|
|
|
|
|
var rulesets = assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(Ruleset)));
|
|
|
|
|
|
|
|
|
|
if (rulesets.Count() != 1)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
foreach (Type rulesetType in rulesets)
|
2017-08-09 12:04:11 +08:00
|
|
|
|
instances.Add((Ruleset)Activator.CreateInstance(rulesetType, new RulesetInfo()));
|
2017-04-17 16:43:48 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception) { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Connection.BeginTransaction();
|
|
|
|
|
|
|
|
|
|
//add all legacy modes in correct order
|
|
|
|
|
foreach (var r in instances.Where(r => r.LegacyID >= 0).OrderBy(r => r.LegacyID))
|
|
|
|
|
{
|
|
|
|
|
Connection.InsertOrReplace(createRulesetInfo(r));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//add any other modes
|
|
|
|
|
foreach (var r in instances.Where(r => r.LegacyID < 0))
|
|
|
|
|
{
|
|
|
|
|
var us = createRulesetInfo(r);
|
|
|
|
|
|
2017-08-01 16:37:21 +08:00
|
|
|
|
var existing = Query<RulesetInfo>().Where(ri => ri.InstantiationInfo == us.InstantiationInfo).FirstOrDefault();
|
2017-04-17 16:43:48 +08:00
|
|
|
|
|
|
|
|
|
if (existing == null)
|
|
|
|
|
Connection.Insert(us);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//perform a consistency check
|
|
|
|
|
foreach (var r in Query<RulesetInfo>())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
r.CreateInstance();
|
|
|
|
|
r.Available = true;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
r.Available = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Connection.Update(r);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Connection.Commit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|