From 296ebbfc54e8fc18f026b4395a688080216a8464 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 19 Sep 2017 13:30:09 +0900 Subject: [PATCH] Read ruleset assemblies from project folders --- osu.Game/Rulesets/RulesetStore.cs | 50 ++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/osu.Game/Rulesets/RulesetStore.cs b/osu.Game/Rulesets/RulesetStore.cs index 2956c11d7f..8d66ad8985 100644 --- a/osu.Game/Rulesets/RulesetStore.cs +++ b/osu.Game/Rulesets/RulesetStore.cs @@ -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 /// public class RulesetStore : DatabaseBackedStore { + private readonly List instances = new List(); + public IEnumerable AllRulesets => Query().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(); if (reset) @@ -31,24 +38,19 @@ namespace osu.Game.Rulesets Connection.DeleteAll(); } - List instances = new List(); + // 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,