mirror of
https://github.com/ppy/osu.git
synced 2025-03-28 10:17:19 +08:00
Read ruleset assemblies from project folders
This commit is contained in:
parent
096f53d769
commit
296ebbfc54
@ -6,6 +6,7 @@ using System.Collections.Generic;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using osu.Framework.Development;
|
||||||
using osu.Game.Database;
|
using osu.Game.Database;
|
||||||
using SQLite.Net;
|
using SQLite.Net;
|
||||||
|
|
||||||
@ -16,14 +17,20 @@ namespace osu.Game.Rulesets
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class RulesetStore : DatabaseBackedStore
|
public class RulesetStore : DatabaseBackedStore
|
||||||
{
|
{
|
||||||
|
private readonly List<Ruleset> instances = new List<Ruleset>();
|
||||||
|
|
||||||
public IEnumerable<RulesetInfo> AllRulesets => Query<RulesetInfo>().Where(r => r.Available);
|
public IEnumerable<RulesetInfo> AllRulesets => Query<RulesetInfo>().Where(r => r.Available);
|
||||||
|
|
||||||
public RulesetStore(SQLiteConnection connection) : base(connection)
|
public RulesetStore(SQLiteConnection connection) : base(connection)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private const string ruleset_library_prefix = "osu.Game.Rulesets";
|
||||||
|
|
||||||
protected override void Prepare(bool reset = false)
|
protected override void Prepare(bool reset = false)
|
||||||
{
|
{
|
||||||
|
instances.Clear();
|
||||||
|
|
||||||
Connection.CreateTable<RulesetInfo>();
|
Connection.CreateTable<RulesetInfo>();
|
||||||
|
|
||||||
if (reset)
|
if (reset)
|
||||||
@ -31,24 +38,19 @@ namespace osu.Game.Rulesets
|
|||||||
Connection.DeleteAll<RulesetInfo>();
|
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
|
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"))
|
||||||
var assembly = Assembly.LoadFile(file);
|
loadRulesetFromFile(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 file in Directory.GetFiles(Environment.CurrentDirectory, $"{ruleset_library_prefix}.*.dll"))
|
||||||
|
loadRulesetFromFile(file);
|
||||||
|
|
||||||
Connection.BeginTransaction();
|
Connection.BeginTransaction();
|
||||||
|
|
||||||
//add all legacy modes in correct order
|
//add all legacy modes in correct order
|
||||||
@ -87,6 +89,26 @@ namespace osu.Game.Rulesets
|
|||||||
Connection.Commit();
|
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
|
private RulesetInfo createRulesetInfo(Ruleset ruleset) => new RulesetInfo
|
||||||
{
|
{
|
||||||
Name = ruleset.Description,
|
Name = ruleset.Description,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user