#region Copyright & License Information /* * Copyright 2007-2015 The OpenRA Developers (see AUTHORS) * This file is part of OpenRA, which is free software. It is made * available to you under the terms of the GNU General Public License * as published by the Free Software Foundation. For more information, * see COPYING. */ #endregion using System.Collections.Generic; using System.Linq; namespace OpenRA.Graphics { public static class ChromeProvider { struct Collection { public string Src; public Dictionary Regions; } static Dictionary collections; static Dictionary cachedSheets; static Dictionary> cachedSprites; public static void Initialize(ModData modData) { Deinitialize(); collections = new Dictionary(); cachedSheets = new Dictionary(); cachedSprites = new Dictionary>(); var chrome = MiniYaml.Merge(modData.Manifest.Chrome .Select(s => MiniYaml.FromStream(modData.ModFiles.Open(s)))); foreach (var c in chrome) LoadCollection(c.Key, c.Value); } public static void Deinitialize() { if (cachedSheets != null) foreach (var sheet in cachedSheets.Values) sheet.Dispose(); collections = null; cachedSheets = null; cachedSprites = null; } public static void Save(string file) { var root = new List(); foreach (var kv in collections) root.Add(new MiniYamlNode(kv.Key, SaveCollection(kv.Value))); root.WriteToFile(file); } static MiniYaml SaveCollection(Collection collection) { var root = new List(); foreach (var kv in collection.Regions) root.Add(new MiniYamlNode(kv.Key, kv.Value.Save(collection.Src))); return new MiniYaml(collection.Src, root); } static void LoadCollection(string name, MiniYaml yaml) { if (Game.ModData.LoadScreen != null) Game.ModData.LoadScreen.Display(); var collection = new Collection() { Src = yaml.Value, Regions = yaml.Nodes.ToDictionary(n => n.Key, n => new MappedImage(yaml.Value, n.Value)) }; collections.Add(name, collection); } public static Sprite GetImage(string collectionName, string imageName) { // Cached sprite Dictionary cachedCollection; Sprite sprite; if (cachedSprites.TryGetValue(collectionName, out cachedCollection) && cachedCollection.TryGetValue(imageName, out sprite)) return sprite; Collection collection; if (!collections.TryGetValue(collectionName, out collection)) { Log.Write("debug", "Could not find collection '{0}'", collectionName); return null; } MappedImage mi; if (!collection.Regions.TryGetValue(imageName, out mi)) return null; // Cached sheet Sheet sheet; if (cachedSheets.ContainsKey(mi.Src)) sheet = cachedSheets[mi.Src]; else { using (var stream = Game.ModData.ModFiles.Open(mi.Src)) sheet = new Sheet(SheetType.BGRA, stream); cachedSheets.Add(mi.Src, sheet); } // Cache the sprite if (cachedCollection == null) { cachedCollection = new Dictionary(); cachedSprites.Add(collectionName, cachedCollection); } var image = mi.GetImage(sheet); cachedCollection.Add(imageName, image); return image; } } }