TilesetManager.cs (4708B)
1 // 2 // Copyright 2020 Electronic Arts Inc. 3 // 4 // The Command & Conquer Map Editor and corresponding source code is free 5 // software: you can redistribute it and/or modify it under the terms of 6 // the GNU General Public License as published by the Free Software Foundation, 7 // either version 3 of the License, or (at your option) any later version. 8 9 // The Command & Conquer Map Editor and corresponding source code is distributed 10 // in the hope that it will be useful, but with permitted additional restrictions 11 // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT 12 // distributed with this program. You should have received a copy of the 13 // GNU General Public License along with permitted additional restrictions 14 // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection 15 using System.Collections.Generic; 16 using System.IO; 17 using System.Linq; 18 using System.Xml; 19 20 namespace MobiusEditor.Utility 21 { 22 public class TilesetManager 23 { 24 private readonly Dictionary<string, Tileset> tilesets = new Dictionary<string, Tileset>(); 25 26 private readonly MegafileManager megafileManager; 27 28 public TilesetManager(MegafileManager megafileManager, TextureManager textureManager, string xmlPath, string texturesPath) 29 { 30 this.megafileManager = megafileManager; 31 32 XmlDocument xmlDoc = new XmlDocument(); 33 xmlDoc.Load(megafileManager.Open(xmlPath)); 34 35 foreach (XmlNode fileNode in xmlDoc.SelectNodes("TilesetFiles/File")) 36 { 37 var xmlFile = Path.Combine(Path.GetDirectoryName(xmlPath), fileNode.InnerText); 38 XmlDocument fileXmlDoc = new XmlDocument(); 39 fileXmlDoc.Load(megafileManager.Open(xmlFile)); 40 41 foreach (XmlNode tilesetNode in fileXmlDoc.SelectNodes("Tilesets/TilesetTypeClass")) 42 { 43 var tileset = new Tileset(textureManager); 44 tileset.Load(tilesetNode.OuterXml, texturesPath); 45 46 tilesets[tilesetNode.Attributes["name"].Value] = tileset; 47 } 48 } 49 } 50 51 public void Reset() 52 { 53 foreach (var item in tilesets) 54 { 55 item.Value.Reset(); 56 } 57 } 58 59 public bool GetTeamColorTileData(IEnumerable<string> searchTilesets, string name, int shape, TeamColor teamColor, out int fps, out Tile[] tiles) 60 { 61 fps = 0; 62 tiles = null; 63 64 foreach (var tileset in tilesets.Join(searchTilesets, x => x.Key, y => y, (x, y) => x.Value)) 65 { 66 if (tileset.GetTileData(name, shape, teamColor, out fps, out tiles)) 67 { 68 return true; 69 } 70 } 71 72 return false; 73 } 74 75 public bool GetTeamColorTileData(IEnumerable<string> searchTilesets, string name, int shape, TeamColor teamColor, out int fps, out Tile tile) 76 { 77 tile = null; 78 if (!GetTeamColorTileData(searchTilesets, name, shape, teamColor, out fps, out Tile[] tiles)) 79 { 80 return false; 81 } 82 tile = tiles[0]; 83 return true; 84 } 85 86 public bool GetTeamColorTileData(IEnumerable<string> searchTilesets, string name, int shape, TeamColor teamColor, out Tile[] tiles) 87 { 88 return GetTeamColorTileData(searchTilesets, name, shape, teamColor, out int fps, out tiles); 89 } 90 91 public bool GetTeamColorTileData(IEnumerable<string> searchTilesets, string name, int shape, TeamColor teamColor, out Tile tile) 92 { 93 return GetTeamColorTileData(searchTilesets, name, shape, teamColor, out int fps, out tile); 94 } 95 96 public bool GetTileData(IEnumerable<string> searchTilesets, string name, int shape, out int fps, out Tile[] tiles) 97 { 98 return GetTeamColorTileData(searchTilesets, name, shape, null, out fps, out tiles); 99 } 100 101 public bool GetTileData(IEnumerable<string> searchTilesets, string name, int shape, out int fps, out Tile tile) 102 { 103 return GetTeamColorTileData(searchTilesets, name, shape, null, out fps, out tile); 104 } 105 106 public bool GetTileData(IEnumerable<string> searchTilesets, string name, int shape, out Tile[] tiles) 107 { 108 return GetTeamColorTileData(searchTilesets, name, shape, null, out tiles); 109 } 110 111 public bool GetTileData(IEnumerable<string> searchTilesets, string name, int shape, out Tile tile) 112 { 113 return GetTeamColorTileData(searchTilesets, name, shape, null, out tile); 114 } 115 } 116 }