1
0

YamlTools.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Linq;
  3. using YamlDotNet.RepresentationModel;
  4. namespace Content.Tools
  5. {
  6. public static class YamlTools
  7. {
  8. public static YamlNode CopyYamlNodes(YamlNode other)
  9. {
  10. switch (other)
  11. {
  12. case YamlSequenceNode subSequence:
  13. var tmp1 = new YamlSequenceNode();
  14. MergeYamlSequences(tmp1, new YamlSequenceNode(), subSequence, "");
  15. return tmp1;
  16. case YamlMappingNode subMapping:
  17. var tmp2 = new YamlMappingNode();
  18. MergeYamlMappings(tmp2, new YamlMappingNode(), subMapping, "", Array.Empty<string>());
  19. return tmp2;
  20. case YamlScalarNode subScalar:
  21. var tmp3 = new YamlScalarNode();
  22. CopyYamlScalar(tmp3, subScalar);
  23. return tmp3;
  24. default:
  25. throw new ArgumentException($"Unrecognized YAML node type for copy: {other.GetType()}", nameof(other));
  26. }
  27. }
  28. public static bool TriTypeMatch(YamlNode ours, YamlNode based, YamlNode other)
  29. {
  30. var refType = other.GetType();
  31. if (refType != based.GetType())
  32. return false;
  33. if (refType != ours.GetType())
  34. return false;
  35. return true;
  36. }
  37. public static void MergeYamlNodes(YamlNode ours, YamlNode based, YamlNode other, string path)
  38. {
  39. if (!TriTypeMatch(ours, based, other))
  40. throw new ArgumentException($"Node type mismatch at {path}");
  41. switch (other)
  42. {
  43. case YamlSequenceNode subSequence:
  44. MergeYamlSequences((YamlSequenceNode) ours, (YamlSequenceNode) based, subSequence, path);
  45. break;
  46. case YamlMappingNode subMapping:
  47. MergeYamlMappings((YamlMappingNode) ours, (YamlMappingNode) based, subMapping, path, Array.Empty<string>());
  48. break;
  49. case YamlScalarNode subScalar:
  50. // Console.WriteLine(path + " - " + ours + " || " + based + " || " + other);
  51. var scalarA = (YamlScalarNode) ours;
  52. var scalarB = (YamlScalarNode) based;
  53. var scalarC = subScalar;
  54. var aeb = (scalarA.Value == scalarB.Value);
  55. var cneb = (scalarC.Value != scalarB.Value);
  56. if (aeb || cneb)
  57. CopyYamlScalar(scalarA, scalarC);
  58. // Console.WriteLine(path + " . " + ours + " || " + based + " || " + other);
  59. break;
  60. default:
  61. throw new ArgumentException($"Unrecognized YAML node type at {path}: {other.GetType()}", nameof(other));
  62. }
  63. }
  64. public static void MergeYamlSequences(YamlSequenceNode ours, YamlSequenceNode based, YamlSequenceNode other, string path)
  65. {
  66. if (ours.Children.Count == based.Children.Count && other.Children.Count == ours.Children.Count)
  67. {
  68. // this is terrible and doesn't do proper rearrange detection
  69. // but it looks as if vectors might be arrays
  70. // so rearrange detection might break more stuff...
  71. // nope, they aren't, but still good to have
  72. for (var i = 0; i < ours.Children.Count; i++)
  73. MergeYamlNodes(ours.Children[i], based.Children[i], other.Children[i], path + "/" + i);
  74. return;
  75. }
  76. // for now, just copy other -> ours
  77. // I am aware this is terrible
  78. ours.Children.Clear();
  79. foreach (var c in other.Children)
  80. ours.Add(CopyYamlNodes(c));
  81. }
  82. public static void MergeYamlMappings(YamlMappingNode ours, YamlMappingNode based, YamlMappingNode other, string path, string[] ignoreThese)
  83. {
  84. // Deletions/modifications
  85. foreach (var kvp in based)
  86. {
  87. if (ignoreThese.Contains(kvp.Key.ToString()))
  88. continue;
  89. var localPath = path + "/" + kvp.Key;
  90. var deletedByOurs = !ours.Children.ContainsKey(kvp.Key);
  91. var deletedByOther = !other.Children.ContainsKey(kvp.Key);
  92. if (deletedByOther && !deletedByOurs)
  93. {
  94. // Delete
  95. ours.Children.Remove(kvp.Key);
  96. }
  97. else if (!(deletedByOurs || deletedByOther))
  98. {
  99. // Modify
  100. var a = ours[kvp.Key];
  101. var b = kvp.Value; // based[kvp.Key]
  102. var c = other[kvp.Key];
  103. if (!TriTypeMatch(a, b, c))
  104. {
  105. Console.WriteLine("Warning: Type mismatch (defaulting to value C) at " + localPath);
  106. ours.Children[kvp.Key] = CopyYamlNodes(c);
  107. }
  108. else
  109. {
  110. MergeYamlNodes(a, b, c, localPath);
  111. }
  112. }
  113. }
  114. // Additions
  115. foreach (var kvp in other)
  116. {
  117. if (ignoreThese.Contains(kvp.Key.ToString()))
  118. continue;
  119. var localPath = path + "/" + kvp.Key;
  120. if (!based.Children.ContainsKey(kvp.Key))
  121. {
  122. if (ours.Children.ContainsKey(kvp.Key))
  123. {
  124. // Both sides added the same key. Try to merge.
  125. var a = ours[kvp.Key];
  126. var b = based[kvp.Key];
  127. var c = kvp.Value; // other[kvp.Key]
  128. if (!TriTypeMatch(a, b, c))
  129. {
  130. Console.WriteLine("Warning: Type mismatch (defaulting to value C) at " + localPath);
  131. ours.Children[kvp.Key] = CopyYamlNodes(c);
  132. }
  133. else
  134. {
  135. MergeYamlNodes(a, b, c, localPath);
  136. }
  137. }
  138. else
  139. {
  140. // Well that was easy
  141. ours.Children[kvp.Key] = CopyYamlNodes(kvp.Value);
  142. }
  143. }
  144. }
  145. }
  146. // NOTE: This is a heuristic ONLY! And is also not used at the moment because sequence matching isn't in place.
  147. // It could also be massively improved.
  148. public static float YamlNodesHeuristic(YamlNode a, YamlNode b)
  149. {
  150. if (a.GetType() != b.GetType())
  151. return 0.0f;
  152. return a switch
  153. {
  154. YamlSequenceNode x => YamlSequencesHeuristic(x, (YamlSequenceNode) b),
  155. YamlMappingNode y => YamlMappingsHeuristic(y, (YamlMappingNode) b),
  156. YamlScalarNode z => (z.Value == ((YamlScalarNode) b).Value) ? 1.0f : 0.0f,
  157. _ => throw new ArgumentException($"Unrecognized YAML node type: {a.GetType()}", nameof(a))
  158. };
  159. }
  160. public static float YamlSequencesHeuristic(YamlSequenceNode a, YamlSequenceNode b)
  161. {
  162. if (a.Children.Count != b.Children.Count)
  163. return 0.0f;
  164. if (a.Children.Count == 0)
  165. return 1.0f;
  166. var total = 0.0f;
  167. for (var i = 0; i < a.Children.Count; i++)
  168. total += YamlNodesHeuristic(a.Children[i], b.Children[i]);
  169. return total / a.Children.Count;
  170. }
  171. public static float YamlMappingsHeuristic(YamlMappingNode a, YamlMappingNode b)
  172. {
  173. return Equals(a, b) ? 1.0f : 0.0f;
  174. }
  175. public static void CopyYamlScalar(YamlScalarNode dst, YamlScalarNode src)
  176. {
  177. dst.Value = src.Value;
  178. dst.Style = src.Style;
  179. }
  180. }
  181. }