TilesetManager.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. // The Command & Conquer Map Editor and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Xml;
  18. namespace MobiusEditor.Utility
  19. {
  20. public class TilesetManager
  21. {
  22. private readonly Dictionary<string, Tileset> tilesets = new Dictionary<string, Tileset>();
  23. private readonly MegafileManager megafileManager;
  24. public TilesetManager(MegafileManager megafileManager, TextureManager textureManager, string xmlPath, string texturesPath)
  25. {
  26. this.megafileManager = megafileManager;
  27. XmlDocument xmlDoc = new XmlDocument();
  28. xmlDoc.Load(megafileManager.Open(xmlPath));
  29. foreach (XmlNode fileNode in xmlDoc.SelectNodes("TilesetFiles/File"))
  30. {
  31. var xmlFile = Path.Combine(Path.GetDirectoryName(xmlPath), fileNode.InnerText);
  32. XmlDocument fileXmlDoc = new XmlDocument();
  33. fileXmlDoc.Load(megafileManager.Open(xmlFile));
  34. foreach (XmlNode tilesetNode in fileXmlDoc.SelectNodes("Tilesets/TilesetTypeClass"))
  35. {
  36. var tileset = new Tileset(textureManager);
  37. tileset.Load(tilesetNode.OuterXml, texturesPath);
  38. tilesets[tilesetNode.Attributes["name"].Value] = tileset;
  39. }
  40. }
  41. }
  42. public void Reset()
  43. {
  44. foreach (var item in tilesets)
  45. {
  46. item.Value.Reset();
  47. }
  48. }
  49. public bool GetTeamColorTileData(IEnumerable<string> searchTilesets, string name, int shape, TeamColor teamColor, out int fps, out Tile[] tiles)
  50. {
  51. fps = 0;
  52. tiles = null;
  53. foreach (var tileset in tilesets.Join(searchTilesets, x => x.Key, y => y, (x, y) => x.Value))
  54. {
  55. if (tileset.GetTileData(name, shape, teamColor, out fps, out tiles))
  56. {
  57. return true;
  58. }
  59. }
  60. return false;
  61. }
  62. public bool GetTeamColorTileData(IEnumerable<string> searchTilesets, string name, int shape, TeamColor teamColor, out int fps, out Tile tile)
  63. {
  64. tile = null;
  65. if (!GetTeamColorTileData(searchTilesets, name, shape, teamColor, out fps, out Tile[] tiles))
  66. {
  67. return false;
  68. }
  69. tile = tiles[0];
  70. return true;
  71. }
  72. public bool GetTeamColorTileData(IEnumerable<string> searchTilesets, string name, int shape, TeamColor teamColor, out Tile[] tiles)
  73. {
  74. return GetTeamColorTileData(searchTilesets, name, shape, teamColor, out int fps, out tiles);
  75. }
  76. public bool GetTeamColorTileData(IEnumerable<string> searchTilesets, string name, int shape, TeamColor teamColor, out Tile tile)
  77. {
  78. return GetTeamColorTileData(searchTilesets, name, shape, teamColor, out int fps, out tile);
  79. }
  80. public bool GetTileData(IEnumerable<string> searchTilesets, string name, int shape, out int fps, out Tile[] tiles)
  81. {
  82. return GetTeamColorTileData(searchTilesets, name, shape, null, out fps, out tiles);
  83. }
  84. public bool GetTileData(IEnumerable<string> searchTilesets, string name, int shape, out int fps, out Tile tile)
  85. {
  86. return GetTeamColorTileData(searchTilesets, name, shape, null, out fps, out tile);
  87. }
  88. public bool GetTileData(IEnumerable<string> searchTilesets, string name, int shape, out Tile[] tiles)
  89. {
  90. return GetTeamColorTileData(searchTilesets, name, shape, null, out tiles);
  91. }
  92. public bool GetTileData(IEnumerable<string> searchTilesets, string name, int shape, out Tile tile)
  93. {
  94. return GetTeamColorTileData(searchTilesets, name, shape, null, out tile);
  95. }
  96. }
  97. }