From 750fc4e02c14936e8bc92b4a9ae90b3006c0272b Mon Sep 17 00:00:00 2001 From: Pavlos Touboulidis Date: Sat, 10 May 2014 10:29:26 +0300 Subject: [PATCH] Merge ModRuleset and MapRuleset into Ruleset --- OpenRA.Editor/Program.cs | 2 +- OpenRA.Game/GameRules/RulesetCache.cs | 36 +++++++-------- OpenRA.Game/Map/Map.cs | 8 ++-- OpenRA.Game/Map/MapCache.cs | 2 +- OpenRA.Game/Map/Ruleset.cs | 45 ++++++------------- OpenRA.Game/ModData.cs | 7 +-- OpenRA.Game/Sound.cs | 2 +- OpenRA.Game/Widgets/ButtonWidget.cs | 4 +- OpenRA.Game/Widgets/CheckboxWidget.cs | 2 +- OpenRA.Game/Widgets/DropDownButtonWidget.cs | 2 +- OpenRA.Game/Widgets/ScrollItemWidget.cs | 2 +- .../Widgets/Logic/CncInstallMusicLogic.cs | 2 +- .../Widgets/Logic/ProductionTooltipLogic.cs | 4 +- .../Widgets/ProductionTypeButtonWidget.cs | 2 +- OpenRA.Mods.RA/Buildings/FootprintUtils.cs | 2 +- OpenRA.Mods.RA/Widgets/BuildPaletteWidget.cs | 2 +- OpenRA.Mods.RA/Widgets/Logic/LobbyUtils.cs | 4 +- OpenRA.Utility/LegacyMapImporter.cs | 6 +-- 18 files changed, 54 insertions(+), 80 deletions(-) diff --git a/OpenRA.Editor/Program.cs b/OpenRA.Editor/Program.cs index a79eb4a349..c216d69176 100644 --- a/OpenRA.Editor/Program.cs +++ b/OpenRA.Editor/Program.cs @@ -19,7 +19,7 @@ namespace OpenRA.Editor { static class Program { - public static MapRuleset Rules; + public static Ruleset Rules; [STAThread] static void Main(string[] args) diff --git a/OpenRA.Game/GameRules/RulesetCache.cs b/OpenRA.Game/GameRules/RulesetCache.cs index 7e8617cc49..8d35b3de03 100755 --- a/OpenRA.Game/GameRules/RulesetCache.cs +++ b/OpenRA.Game/GameRules/RulesetCache.cs @@ -36,30 +36,12 @@ namespace OpenRA this.modData = modData; } - public ModRuleset LoadModRules() - { - var m = modData.Manifest; - - Dictionary music; - Dictionary movies; - Dictionary tileSets; - - using (new PerfTimer("Music")) - music = LoadYamlRules(musicCache, m.Music, new List(), (k, _) => new MusicInfo(k.Key, k.Value)); - using (new PerfTimer("Movies")) - movies = LoadYamlRules(movieCache, m.Movies, new List(), (k, v) => k.Value.Value); - using (new PerfTimer("TileSets")) - tileSets = LoadTileSets(tileSetCache, m.TileSets); - - return new ModRuleset(music, movies, tileSets); - } - - public MapRuleset LoadDefaultRules() + public Ruleset LoadDefaultRules() { return LoadMapRules(new Map()); } - public MapRuleset LoadMapRules(Map map) + public Ruleset LoadMapRules(Map map) { var m = modData.Manifest; @@ -67,6 +49,9 @@ namespace OpenRA Dictionary weapons; Dictionary voices; Dictionary notifications; + Dictionary music; + Dictionary movies; + Dictionary tileSets; OnProgress(); using (new PerfTimer("Actors")) @@ -80,9 +65,18 @@ namespace OpenRA OnProgress(); using (new PerfTimer("Notifications")) notifications = LoadYamlRules(notificationCache, m.Notifications, map.NotificationDefinitions, (k, _) => new SoundInfo(k.Value)); + OnProgress(); + using (new PerfTimer("Music")) + music = LoadYamlRules(musicCache, m.Music, new List(), (k, _) => new MusicInfo(k.Key, k.Value)); + OnProgress(); + using (new PerfTimer("Movies")) + movies = LoadYamlRules(movieCache, m.Movies, new List(), (k, v) => k.Value.Value); + OnProgress(); + using (new PerfTimer("TileSets")) + tileSets = LoadTileSets(tileSetCache, m.TileSets); OnProgress(); - return new MapRuleset(LoadModRules(), actors, weapons, voices, notifications); + return new Ruleset(actors, weapons, voices, notifications, music, movies, tileSets); } Dictionary LoadYamlRules( diff --git a/OpenRA.Game/Map/Map.cs b/OpenRA.Game/Map/Map.cs index d371c3749c..32934f274b 100644 --- a/OpenRA.Game/Map/Map.cs +++ b/OpenRA.Game/Map/Map.cs @@ -111,8 +111,8 @@ namespace OpenRA [FieldLoader.Ignore] public Lazy[,]> MapResources; [FieldLoader.Ignore] public string[,] CustomTerrain; - [FieldLoader.Ignore] Lazy rules; - public MapRuleset Rules { get { return rules != null ? rules.Value : null; } } + [FieldLoader.Ignore] Lazy rules; + public Ruleset Rules { get { return rules != null ? rules.Value : null; } } public SequenceProvider SequenceProvider { get; private set; } public static Map FromTileset(TileSet tileset) @@ -249,7 +249,7 @@ namespace OpenRA SequenceProvider = new SequenceProvider(this); } - public MapRuleset PreloadRules() + public Ruleset PreloadRules() { return rules.Value; } @@ -508,7 +508,7 @@ namespace OpenRA }); } - public void FixOpenAreas(MapRuleset rules) + public void FixOpenAreas(Ruleset rules) { var r = new Random(); var tileset = rules.TileSets[Tileset]; diff --git a/OpenRA.Game/Map/MapCache.cs b/OpenRA.Game/Map/MapCache.cs index 729e7a6617..0af6d30509 100644 --- a/OpenRA.Game/Map/MapCache.cs +++ b/OpenRA.Game/Map/MapCache.cs @@ -149,7 +149,7 @@ namespace OpenRA // the next render cycle. // (d) Any partially written bytes from the next minimap is in an // unallocated area, and will be committed in the next cycle. - var bitmap = p.CustomPreview ?? Minimap.RenderMapPreview(modData.ModRules.TileSets[p.Map.Tileset], p.Map, true); + var bitmap = p.CustomPreview ?? Minimap.RenderMapPreview(modData.DefaultRules.TileSets[p.Map.Tileset], p.Map, true); p.Minimap = sheetBuilder.Add(bitmap); lock (syncRoot) diff --git a/OpenRA.Game/Map/Ruleset.cs b/OpenRA.Game/Map/Ruleset.cs index 13179b2c44..117a07ee59 100644 --- a/OpenRA.Game/Map/Ruleset.cs +++ b/OpenRA.Game/Map/Ruleset.cs @@ -15,24 +15,29 @@ using OpenRA.GameRules; namespace OpenRA { - public class ModRuleset + public class Ruleset { + public readonly IReadOnlyDictionary Actors; + public readonly IReadOnlyDictionary Weapons; + public readonly IReadOnlyDictionary Voices; + public readonly IReadOnlyDictionary Notifications; public readonly IReadOnlyDictionary Music; public readonly IReadOnlyDictionary Movies; public readonly IReadOnlyDictionary TileSets; - public ModRuleset(ModRuleset other) - { - this.Music = other.Music; - this.Movies = other.Movies; - this.TileSets = other.TileSets; - } - - public ModRuleset( + public Ruleset( + IDictionary actors, + IDictionary weapons, + IDictionary voices, + IDictionary notifications, IDictionary music, IDictionary movies, IDictionary tileSets) { + this.Actors = new ReadOnlyDictionary(actors); + this.Weapons = new ReadOnlyDictionary(weapons); + this.Voices = new ReadOnlyDictionary(voices); + this.Notifications = new ReadOnlyDictionary(notifications); this.Music = new ReadOnlyDictionary(music); this.Movies = new ReadOnlyDictionary(movies); this.TileSets = new ReadOnlyDictionary(tileSets); @@ -40,26 +45,4 @@ namespace OpenRA public IEnumerable> InstalledMusic { get { return Music.Where(m => m.Value.Exists); } } } - - public class MapRuleset : ModRuleset - { - public readonly IReadOnlyDictionary Actors; - public readonly IReadOnlyDictionary Weapons; - public readonly IReadOnlyDictionary Voices; - public readonly IReadOnlyDictionary Notifications; - - public MapRuleset( - ModRuleset modRuleset, - IDictionary actors, - IDictionary weapons, - IDictionary voices, - IDictionary notifications) - : base(modRuleset) - { - this.Actors = new ReadOnlyDictionary(actors); - this.Weapons = new ReadOnlyDictionary(weapons); - this.Voices = new ReadOnlyDictionary(voices); - this.Notifications = new ReadOnlyDictionary(notifications); - } - } } diff --git a/OpenRA.Game/ModData.cs b/OpenRA.Game/ModData.cs index a5203ee385..cb9ba5cf91 100644 --- a/OpenRA.Game/ModData.cs +++ b/OpenRA.Game/ModData.cs @@ -29,10 +29,8 @@ namespace OpenRA public readonly RulesetCache RulesetCache; public CursorProvider CursorProvider { get; private set; } - Lazy modRules; - public ModRuleset ModRules { get { return modRules.Value; } } - Lazy defaultRules; - public MapRuleset DefaultRules { get { return defaultRules.Value; } } + Lazy defaultRules; + public Ruleset DefaultRules { get { return defaultRules.Value; } } public ModData(string mod) { @@ -51,7 +49,6 @@ namespace OpenRA foreach (var dir in Manifest.Folders) GlobalFileSystem.Mount(dir); - modRules = Exts.Lazy(() => RulesetCache.LoadModRules()); defaultRules = Exts.Lazy(() => RulesetCache.LoadDefaultRules()); } diff --git a/OpenRA.Game/Sound.cs b/OpenRA.Game/Sound.cs index 38b0ecc883..3da54058d5 100644 --- a/OpenRA.Game/Sound.cs +++ b/OpenRA.Game/Sound.cs @@ -296,7 +296,7 @@ namespace OpenRA } // Returns true if played successfully - public static bool PlayPredefined(MapRuleset ruleset, Player p, Actor voicedUnit, string type, string definition, string variant, bool relative, WPos pos, float volumeModifier, bool attenuateVolume) + public static bool PlayPredefined(Ruleset ruleset, Player p, Actor voicedUnit, string type, string definition, string variant, bool relative, WPos pos, float volumeModifier, bool attenuateVolume) { if (ruleset == null) throw new ArgumentNullException("ruleset"); diff --git a/OpenRA.Game/Widgets/ButtonWidget.cs b/OpenRA.Game/Widgets/ButtonWidget.cs index 30ead96c0c..e6641726cb 100644 --- a/OpenRA.Game/Widgets/ButtonWidget.cs +++ b/OpenRA.Game/Widgets/ButtonWidget.cs @@ -55,10 +55,10 @@ namespace OpenRA.Widgets public Action OnDoubleClick = () => {}; public Action OnKeyPress = _ => {}; - readonly MapRuleset rules; + readonly Ruleset rules; [ObjectCreator.UseCtor] - public ButtonWidget(MapRuleset rules) + public ButtonWidget(Ruleset rules) { this.rules = rules; diff --git a/OpenRA.Game/Widgets/CheckboxWidget.cs b/OpenRA.Game/Widgets/CheckboxWidget.cs index 83eb757061..e1d4981fd0 100644 --- a/OpenRA.Game/Widgets/CheckboxWidget.cs +++ b/OpenRA.Game/Widgets/CheckboxWidget.cs @@ -25,7 +25,7 @@ namespace OpenRA.Widgets public bool HasPressedState = ChromeMetrics.Get("CheckboxPressedState"); [ObjectCreator.UseCtor] - public CheckboxWidget(MapRuleset rules) + public CheckboxWidget(Ruleset rules) : base(rules) { GetCheckType = () => CheckType; diff --git a/OpenRA.Game/Widgets/DropDownButtonWidget.cs b/OpenRA.Game/Widgets/DropDownButtonWidget.cs index 02a79a0b5f..c161cf1303 100644 --- a/OpenRA.Game/Widgets/DropDownButtonWidget.cs +++ b/OpenRA.Game/Widgets/DropDownButtonWidget.cs @@ -22,7 +22,7 @@ namespace OpenRA.Widgets MaskWidget fullscreenMask; [ObjectCreator.UseCtor] - public DropDownButtonWidget(MapRuleset rules) + public DropDownButtonWidget(Ruleset rules) : base(rules) { } protected DropDownButtonWidget(DropDownButtonWidget widget) : base(widget) { } diff --git a/OpenRA.Game/Widgets/ScrollItemWidget.cs b/OpenRA.Game/Widgets/ScrollItemWidget.cs index 1b5945c2b9..10348ba663 100644 --- a/OpenRA.Game/Widgets/ScrollItemWidget.cs +++ b/OpenRA.Game/Widgets/ScrollItemWidget.cs @@ -20,7 +20,7 @@ namespace OpenRA.Widgets public string BaseName = "scrollitem"; [ObjectCreator.UseCtor] - public ScrollItemWidget(MapRuleset rules) + public ScrollItemWidget(Ruleset rules) : base(rules) { IsVisible = () => false; diff --git a/OpenRA.Mods.Cnc/Widgets/Logic/CncInstallMusicLogic.cs b/OpenRA.Mods.Cnc/Widgets/Logic/CncInstallMusicLogic.cs index b32020156d..89c454ee5a 100644 --- a/OpenRA.Mods.Cnc/Widgets/Logic/CncInstallMusicLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/Logic/CncInstallMusicLogic.cs @@ -20,7 +20,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic public class CncInstallMusicLogic { [ObjectCreator.UseCtor] - public CncInstallMusicLogic(Widget widget, MapRuleset rules, Action onExit) + public CncInstallMusicLogic(Widget widget, Ruleset rules, Action onExit) { var installButton = widget.GetOrNull("INSTALL_BUTTON"); if (installButton != null) diff --git a/OpenRA.Mods.Cnc/Widgets/Logic/ProductionTooltipLogic.cs b/OpenRA.Mods.Cnc/Widgets/Logic/ProductionTooltipLogic.cs index d845a08108..6caca43c44 100644 --- a/OpenRA.Mods.Cnc/Widgets/Logic/ProductionTooltipLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/Logic/ProductionTooltipLogic.cs @@ -21,7 +21,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic public class ProductionTooltipLogic { [ObjectCreator.UseCtor] - public ProductionTooltipLogic(Widget widget, MapRuleset rules, TooltipContainerWidget tooltipContainer, ProductionPaletteWidget palette) + public ProductionTooltipLogic(Widget widget, Ruleset rules, TooltipContainerWidget tooltipContainer, ProductionPaletteWidget palette) { var pm = palette.World.LocalPlayer.PlayerActor.Trait(); var pr = palette.World.LocalPlayer.PlayerActor.Trait(); @@ -92,7 +92,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic }; } - static string ActorName(MapRuleset rules, string a) + static string ActorName(Ruleset rules, string a) { ActorInfo ai; if (rules.Actors.TryGetValue(a.ToLowerInvariant(), out ai) && ai.Traits.Contains()) diff --git a/OpenRA.Mods.Cnc/Widgets/ProductionTypeButtonWidget.cs b/OpenRA.Mods.Cnc/Widgets/ProductionTypeButtonWidget.cs index 157f8b11d4..eeb164b111 100644 --- a/OpenRA.Mods.Cnc/Widgets/ProductionTypeButtonWidget.cs +++ b/OpenRA.Mods.Cnc/Widgets/ProductionTypeButtonWidget.cs @@ -19,7 +19,7 @@ namespace OpenRA.Mods.Cnc.Widgets public readonly string ProductionGroup; [ObjectCreator.UseCtor] - public ProductionTypeButtonWidget(MapRuleset rules) + public ProductionTypeButtonWidget(Ruleset rules) : base(rules) { } protected ProductionTypeButtonWidget(ProductionTypeButtonWidget other) diff --git a/OpenRA.Mods.RA/Buildings/FootprintUtils.cs b/OpenRA.Mods.RA/Buildings/FootprintUtils.cs index 8c5729e5a5..aa9fb076df 100644 --- a/OpenRA.Mods.RA/Buildings/FootprintUtils.cs +++ b/OpenRA.Mods.RA/Buildings/FootprintUtils.cs @@ -16,7 +16,7 @@ namespace OpenRA.Mods.RA.Buildings { public static class FootprintUtils { - public static IEnumerable Tiles(MapRuleset rules, string name, BuildingInfo buildingInfo, CPos topLeft) + public static IEnumerable Tiles(Ruleset rules, string name, BuildingInfo buildingInfo, CPos topLeft) { var dim = (CVec)buildingInfo.Dimensions; diff --git a/OpenRA.Mods.RA/Widgets/BuildPaletteWidget.cs b/OpenRA.Mods.RA/Widgets/BuildPaletteWidget.cs index 5b2bd1d994..c82a2153b4 100644 --- a/OpenRA.Mods.RA/Widgets/BuildPaletteWidget.cs +++ b/OpenRA.Mods.RA/Widgets/BuildPaletteWidget.cs @@ -337,7 +337,7 @@ namespace OpenRA.Mods.RA.Widgets }; } - static string Description(MapRuleset rules, string a) + static string Description(Ruleset rules, string a) { ActorInfo ai; rules.Actors.TryGetValue(a.ToLowerInvariant(), out ai); diff --git a/OpenRA.Mods.RA/Widgets/Logic/LobbyUtils.cs b/OpenRA.Mods.RA/Widgets/Logic/LobbyUtils.cs index b3d33b1eb3..877850a013 100644 --- a/OpenRA.Mods.RA/Widgets/Logic/LobbyUtils.cs +++ b/OpenRA.Mods.RA/Widgets/Logic/LobbyUtils.cs @@ -37,7 +37,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic } } - public static void ShowSlotDropDown(MapRuleset rules, DropDownButtonWidget dropdown, Session.Slot slot, + public static void ShowSlotDropDown(Ruleset rules, DropDownButtonWidget dropdown, Session.Slot slot, Session.Client client, OrderManager orderManager) { var options = new Dictionary>() {{"Slot", new List() @@ -280,7 +280,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic name.GetText = () => c.Name; } - public static void SetupEditableSlotWidget(Widget parent, Session.Slot s, Session.Client c, OrderManager orderManager, MapRuleset rules) + public static void SetupEditableSlotWidget(Widget parent, Session.Slot s, Session.Client c, OrderManager orderManager, Ruleset rules) { var slot = parent.Get("SLOT_OPTIONS"); slot.IsVisible = () => true; diff --git a/OpenRA.Utility/LegacyMapImporter.cs b/OpenRA.Utility/LegacyMapImporter.cs index ea49ab550a..429d44fab4 100644 --- a/OpenRA.Utility/LegacyMapImporter.cs +++ b/OpenRA.Utility/LegacyMapImporter.cs @@ -107,18 +107,18 @@ namespace OpenRA.Utility int mapSize; int actorCount = 0; Map map = new Map(); - MapRuleset rules; + Ruleset rules; List players = new List(); Action errorHandler; - LegacyMapImporter(string filename, MapRuleset rules, Action errorHandler) + LegacyMapImporter(string filename, Ruleset rules, Action errorHandler) { this.rules = rules; this.errorHandler = errorHandler; ConvertIniMap(filename); } - public static Map Import(string filename, MapRuleset rules, Action errorHandler) + public static Map Import(string filename, Ruleset rules, Action errorHandler) { var converter = new LegacyMapImporter(filename, rules, errorHandler); return converter.map;