Add thread safety somewhere it will never be required.

This commit is contained in:
Dean Herbert 2016-11-14 23:13:47 +09:00
parent 9a8003f6fa
commit e01057df3d
4 changed files with 13 additions and 4 deletions

View File

@ -16,6 +16,8 @@ public class CatchRuleset : Ruleset
public override HitRenderer CreateHitRendererWith(List<HitObject> objects) => new CatchHitRenderer { Objects = objects };
protected override PlayMode PlayMode => PlayMode.Mania;
public override HitObjectParser CreateHitObjectParser() => new OsuHitObjectParser();
}
}

View File

@ -17,6 +17,8 @@ public class ManiaRuleset : Ruleset
public override HitRenderer CreateHitRendererWith(List<HitObject> objects) => new ManiaHitRenderer { Objects = objects };
protected override PlayMode PlayMode => PlayMode.Mania;
public override HitObjectParser CreateHitObjectParser() => new OsuHitObjectParser();
}
}

View File

@ -16,6 +16,8 @@ public class TaikoRuleset : Ruleset
public override HitRenderer CreateHitRendererWith(List<HitObject> objects) => new TaikoHitRenderer { Objects = objects };
protected override PlayMode PlayMode => PlayMode.Taiko;
public override HitObjectParser CreateHitObjectParser() => new OsuHitObjectParser();
}
}

View File

@ -7,13 +7,14 @@
using System.Reflection;
using osu.Framework.Extensions;
using System;
using System.Collections.Concurrent;
using System.Linq;
namespace osu.Game.Modes
{
public abstract class Ruleset
{
private static List<Type> availableRulesets = new List<Type>();
private static ConcurrentDictionary<PlayMode, Type> availableRulesets = new ConcurrentDictionary<PlayMode, Type>();
public abstract ScoreOverlay CreateScoreOverlay();
@ -21,13 +22,15 @@ public abstract class Ruleset
public abstract HitObjectParser CreateHitObjectParser();
public static void Register(Ruleset ruleset) => availableRulesets.Add(ruleset.GetType());
public static void Register(Ruleset ruleset) => availableRulesets.TryAdd(ruleset.PlayMode, ruleset.GetType());
protected virtual PlayMode PlayMode => PlayMode.Osu;
public static Ruleset GetRuleset(PlayMode mode)
{
Type type = availableRulesets.FirstOrDefault(t => t.Name == $@"{mode}Ruleset");
Type type;
if (type == null)
if (!availableRulesets.TryGetValue(mode, out type))
return null;
return Activator.CreateInstance(type) as Ruleset;