TeamColorManager.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.Linq;
  16. using System.Xml;
  17. namespace MobiusEditor.Utility
  18. {
  19. public class TeamColorManager
  20. {
  21. private readonly Dictionary<string, TeamColor> teamColors = new Dictionary<string, TeamColor>();
  22. private readonly MegafileManager megafileManager;
  23. public TeamColor this[string key] => !string.IsNullOrEmpty(key) ? teamColors[key] : null;
  24. public TeamColorManager(MegafileManager megafileManager)
  25. {
  26. this.megafileManager = megafileManager;
  27. }
  28. public void Reset()
  29. {
  30. teamColors.Clear();
  31. }
  32. public void Load(string xmlPath)
  33. {
  34. XmlDocument xmlDoc = new XmlDocument();
  35. xmlDoc.Load(megafileManager.Open(xmlPath));
  36. foreach (XmlNode teamColorNode in xmlDoc.SelectNodes("/*/TeamColorTypeClass"))
  37. {
  38. var teamColor = new TeamColor(this, megafileManager);
  39. teamColor.Load(teamColorNode.OuterXml);
  40. teamColors[teamColorNode.Attributes["Name"].Value] = teamColor;
  41. }
  42. foreach (var teamColor in TopologicalSortTeamColors())
  43. {
  44. teamColor.Flatten();
  45. }
  46. }
  47. private IEnumerable<TeamColor> TopologicalSortTeamColors()
  48. {
  49. var nodes = teamColors.Values.ToList();
  50. HashSet<(TeamColor, TeamColor)> edges = new HashSet<(TeamColor, TeamColor)>();
  51. foreach (var node in nodes)
  52. {
  53. if (!string.IsNullOrEmpty(node.Variant))
  54. {
  55. edges.Add((this[node.Variant], node));
  56. }
  57. }
  58. var sorted = new List<TeamColor>();
  59. var openSet = new HashSet<TeamColor>(nodes.Where(n => edges.All(e => !e.Item2.Equals(n))));
  60. while (openSet.Count > 0)
  61. {
  62. var node = openSet.First();
  63. openSet.Remove(node);
  64. sorted.Add(node);
  65. foreach (var edge in edges.Where(e => e.Item1.Equals(node)).ToArray())
  66. {
  67. var node2 = edge.Item2;
  68. edges.Remove(edge);
  69. if (edges.All(edge2 => !edge2.Item2.Equals(node2)))
  70. {
  71. openSet.Add(node2);
  72. }
  73. }
  74. }
  75. return (edges.Count == 0) ? sorted : null;
  76. }
  77. }
  78. }