Read ruleset assemblies from project folders

This commit is contained in:
Dean Herbert 2017-09-19 13:30:09 +09:00
parent 096f53d769
commit 296ebbfc54

View File

@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using osu.Framework.Development;
using osu.Game.Database;
using SQLite.Net;
@ -16,14 +17,20 @@ namespace osu.Game.Rulesets
/// </summary>
public class RulesetStore : DatabaseBackedStore
{
private readonly List<Ruleset> instances = new List<Ruleset>();
public IEnumerable<RulesetInfo> AllRulesets => Query<RulesetInfo>().Where(r => r.Available);
public RulesetStore(SQLiteConnection connection) : base(connection)
{
}
private const string ruleset_library_prefix = "osu.Game.Rulesets";
protected override void Prepare(bool reset = false)
{
instances.Clear();
Connection.CreateTable<RulesetInfo>();
if (reset)
@ -31,24 +38,19 @@ namespace osu.Game.Rulesets
Connection.DeleteAll<RulesetInfo>();
}
List<Ruleset> instances = new List<Ruleset>();
// todo: don't do this on deploy
var sln = DebugUtils.GetSolutionPath();
foreach (string file in Directory.GetFiles(Environment.CurrentDirectory, @"osu.Game.Rulesets.*.dll"))
if (sln != null)
{
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)
instances.Add((Ruleset)Activator.CreateInstance(rulesetType, new RulesetInfo()));
}
catch (Exception) { }
foreach (string dir in Directory.GetDirectories(sln, $"{ruleset_library_prefix}.*"))
foreach (string file in Directory.GetFiles(Path.Combine(dir, "bin", DebugUtils.IsDebug ? "Debug" : "Release"), $"{ruleset_library_prefix}.*.dll"))
loadRulesetFromFile(file);
}
foreach (string file in Directory.GetFiles(Environment.CurrentDirectory, $"{ruleset_library_prefix}.*.dll"))
loadRulesetFromFile(file);
Connection.BeginTransaction();
//add all legacy modes in correct order
@ -87,6 +89,26 @@ namespace osu.Game.Rulesets
Connection.Commit();
}
private void loadRulesetFromFile(string file)
{
var filename = Path.GetFileNameWithoutExtension(file);
if (instances.Any(i => i.GetType().Namespace == filename))
return;
try
{
var assembly = Assembly.LoadFile(file);
var rulesets = assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(Ruleset)));
if (rulesets.Count() != 1)
return;
instances.Add((Ruleset)Activator.CreateInstance(rulesets.First(), new RulesetInfo()));
}
catch (Exception) { }
}
private RulesetInfo createRulesetInfo(Ruleset ruleset) => new RulesetInfo
{
Name = ruleset.Description,