GameObjectMergeActions.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. 
  2. namespace GitMerge
  3. {
  4. using UnityEngine;
  5. using UnityEditor;
  6. using System.Collections.Generic;
  7. using System.Text;
  8. /// <summary>
  9. /// One instance of this class represents one GameObject with relevance to the merge process.
  10. /// Holds all MergeActions that can be applied to the GameObject or its Components.
  11. /// Is considered as "merged" when all its MergeActions are "merged".
  12. /// </summary>
  13. public class GameObjectMergeActions
  14. {
  15. /// <summary>
  16. /// Reference to "our" version of the GameObject.
  17. /// </summary>
  18. public GameObject ours { private set; get; }
  19. /// <summary>
  20. /// Reference to "their" versoin of the GameObject.
  21. /// </summary>
  22. public GameObject theirs { private set; get; }
  23. public string name { private set; get; }
  24. public bool merged { private set; get; }
  25. public bool hasActions
  26. {
  27. get { return actions.Count > 0; }
  28. }
  29. /// <summary>
  30. /// All actions available for solving specific conflicts on the GameObject.
  31. /// </summary>
  32. private List<MergeAction> actions;
  33. public GameObjectMergeActions(GameObject ours, GameObject theirs)
  34. {
  35. actions = new List<MergeAction>();
  36. this.ours = ours;
  37. this.theirs = theirs;
  38. GenerateName();
  39. if (theirs && !ours)
  40. {
  41. actions.Add(new MergeActionNewGameObject(ours, theirs));
  42. }
  43. else if (ours && !theirs)
  44. {
  45. actions.Add(new MergeActionDeleteGameObject(ours, theirs));
  46. }
  47. else if (ours && theirs)
  48. {
  49. FindPropertyDifferences();
  50. FindComponentDifferences();
  51. }
  52. // Some Actions have a default and are merged from the beginning.
  53. // If all the others did was to add GameObjects, we're done with merging from the start.
  54. CheckIfMerged();
  55. }
  56. /// <summary>
  57. /// Generate a title for this object
  58. /// </summary>
  59. private void GenerateName()
  60. {
  61. name = "";
  62. if (ours)
  63. {
  64. name = "Your[" + ours.GetPath() + "]";
  65. }
  66. if (theirs)
  67. {
  68. if (ours)
  69. {
  70. name += " vs. ";
  71. }
  72. name += "Their[" + theirs.GetPath() + "]";
  73. }
  74. }
  75. /// <summary>
  76. /// Finds the differences between properties of the two GameObjects.
  77. /// That means the name, layer, tag... everything that's not part of a Component. Also, the parent.
  78. /// </summary>
  79. private void FindPropertyDifferences()
  80. {
  81. CheckForDifferentParents();
  82. FindPropertyDifferences(ours, theirs);
  83. }
  84. /// <summary>
  85. /// Since parenting is quite special, here's some dedicated handling.
  86. /// </summary>
  87. private void CheckForDifferentParents()
  88. {
  89. var transform = ours.GetComponent<Transform>();
  90. var ourParent = transform.parent;
  91. var theirParent = theirs.GetComponent<Transform>().parent;
  92. if (!ObjectID.GetFor(ourParent).Equals(ObjectID.GetFor(theirParent)))
  93. {
  94. actions.Add(new MergeActionParenting(transform, ourParent, theirParent));
  95. }
  96. }
  97. /// <summary>
  98. /// Check for Components that one of the sides doesn't have, and/or for defferent values
  99. /// on Components.
  100. /// </summary>
  101. private void FindComponentDifferences()
  102. {
  103. var ourComponents = ours.GetComponents<Component>();
  104. var theirComponents = theirs.GetComponents<Component>();
  105. // Map "their" Components to their respective ids.
  106. var theirDict = new Dictionary<ObjectID, Component>();
  107. foreach (var theirComponent in theirComponents)
  108. {
  109. // Ignore null components.
  110. if (theirComponent != null)
  111. {
  112. theirDict.Add(ObjectID.GetFor(theirComponent), theirComponent);
  113. }
  114. }
  115. foreach (var ourComponent in ourComponents)
  116. {
  117. // Ignore null components.
  118. if (ourComponent == null) continue;
  119. // Try to find "their" equivalent to our Components.
  120. var id = ObjectID.GetFor(ourComponent);
  121. Component theirComponent;
  122. theirDict.TryGetValue(id, out theirComponent);
  123. if (theirComponent) // Both Components exist.
  124. {
  125. FindPropertyDifferences(ourComponent, theirComponent);
  126. // Remove "their" Component from the dict to only keep those new to us.
  127. theirDict.Remove(id);
  128. }
  129. else
  130. {
  131. // Component doesn't exist in their version, offer a deletion.
  132. actions.Add(new MergeActionDeleteComponent(ours, ourComponent));
  133. }
  134. }
  135. // Everything left in the dict is a...
  136. foreach (var theirComponent in theirDict.Values)
  137. {
  138. // ...new Component from them.
  139. actions.Add(new MergeActionNewComponent(ours, theirComponent));
  140. }
  141. }
  142. /// <summary>
  143. /// Find all the values different in "our" and "their" version of a component.
  144. /// </summary>
  145. private void FindPropertyDifferences(Object ourObject, Object theirObject)
  146. {
  147. var ourSerializedObject = new SerializedObject(ourObject);
  148. var theirSerializedObject = new SerializedObject(theirObject);
  149. var ourProperty = ourSerializedObject.GetIterator();
  150. if (ourProperty.Next(true))
  151. {
  152. var theirProperty = theirSerializedObject.GetIterator();
  153. theirProperty.Next(true);
  154. var shouldEnterChildren = ourProperty.hasVisibleChildren;
  155. while (ourProperty.NextVisible(shouldEnterChildren))
  156. {
  157. theirProperty.NextVisible(shouldEnterChildren);
  158. // If merging a prefab, ignore the gameobject name.
  159. if (ourObject is GameObject
  160. && MergeManagerBase.isMergingPrefab
  161. && ourProperty.GetPlainName() == "Name")
  162. {
  163. continue;
  164. }
  165. if (DifferentValues(ourProperty, theirProperty))
  166. {
  167. // We found a difference, accordingly add a MergeAction.
  168. actions.Add(new MergeActionChangeValues(ours, ourProperty.Copy(), theirProperty.Copy()));
  169. }
  170. }
  171. }
  172. }
  173. /// <summary>
  174. /// Returns true when the two properties have different values, false otherwise.
  175. /// </summary>
  176. private static bool DifferentValues(SerializedProperty ourProperty, SerializedProperty theirProperty)
  177. {
  178. if (!ourProperty.IsRealArray())
  179. {
  180. // Regular single-value property.
  181. return DifferentValuesFlat(ourProperty, theirProperty);
  182. }
  183. else
  184. {
  185. // Array property.
  186. if (ourProperty.arraySize != theirProperty.arraySize)
  187. {
  188. return true;
  189. }
  190. var op = ourProperty.Copy();
  191. var tp = theirProperty.Copy();
  192. op.Next(true);
  193. op.Next(true);
  194. tp.Next(true);
  195. tp.Next(true);
  196. for (int i = 0; i < ourProperty.arraySize; ++i)
  197. {
  198. op.Next(false);
  199. tp.Next(false);
  200. if (DifferentValuesFlat(op, tp))
  201. {
  202. return true;
  203. }
  204. }
  205. }
  206. return false;
  207. }
  208. private static bool DifferentValuesFlat(SerializedProperty ourProperty, SerializedProperty theirProperty)
  209. {
  210. var our = ourProperty.GetValue();
  211. var their = theirProperty.GetValue();
  212. if (ourProperty.propertyType == SerializedPropertyType.ObjectReference)
  213. {
  214. if (our != null && their != null)
  215. {
  216. our = ObjectID.GetFor(our as Object);
  217. their = ObjectID.GetFor(their as Object);
  218. }
  219. }
  220. return !object.Equals(our, their);
  221. }
  222. private void CheckIfMerged()
  223. {
  224. merged = actions.TrueForAll(action => action.merged);
  225. }
  226. /// <summary>
  227. /// Use "our" version for all conflicts.
  228. /// This is used on all GameObjectMergeActions objects when the merge is aborted.
  229. /// </summary>
  230. public void UseOurs()
  231. {
  232. foreach (var action in actions)
  233. {
  234. action.UseOurs();
  235. }
  236. merged = true;
  237. }
  238. /// <summary>
  239. /// Use "their" version for all conflicts.
  240. /// </summary>
  241. public void UseTheirs()
  242. {
  243. foreach (var action in actions)
  244. {
  245. action.UseTheirs();
  246. }
  247. merged = true;
  248. }
  249. //If the foldout is open
  250. private bool open;
  251. public void OnGUI()
  252. {
  253. if (open)
  254. {
  255. GUI.backgroundColor = new Color(0, 0, 0, .8f);
  256. }
  257. else
  258. {
  259. GUI.backgroundColor = merged ? new Color(0, .5f, 0, .8f) : new Color(.5f, 0, 0, .8f);
  260. }
  261. GUILayout.BeginVertical(Resources.styles.mergeActions);
  262. GUI.backgroundColor = Color.white;
  263. GUILayout.BeginHorizontal();
  264. open = EditorGUILayout.Foldout(open, new GUIContent(name));
  265. if (ours && GUILayout.Button("Focus", EditorStyles.miniButton, GUILayout.Width(100)))
  266. {
  267. // Highlight the instance of the prefab, not the prefab itself.
  268. // Otherwise, "ours".
  269. var objectToHighlight = MergeManagerBase.isMergingPrefab ? MergeManagerPrefab.ourPrefabInstance.GetChildWithEqualPath(ours) : ours;
  270. objectToHighlight.Highlight();
  271. }
  272. GUILayout.EndHorizontal();
  273. if (open)
  274. {
  275. // Display all merge actions.
  276. foreach (var action in actions)
  277. {
  278. if (action.OnGUIMerge())
  279. {
  280. CheckIfMerged();
  281. }
  282. }
  283. }
  284. else
  285. {
  286. GUILayout.BeginHorizontal();
  287. if (GUILayout.Button("Use ours >>>", EditorStyles.miniButton))
  288. {
  289. UseOurs();
  290. }
  291. if (GUILayout.Button("<<< Use theirs", EditorStyles.miniButton))
  292. {
  293. UseTheirs();
  294. }
  295. GUILayout.EndHorizontal();
  296. }
  297. // If "ours" is null, the GameObject doesn't exist in one of the versions.
  298. // Try to get a reference if the object exists in the current merging state.
  299. // If it exists, the new/gelete MergeAction will have a reference.
  300. if (!ours)
  301. {
  302. foreach (var action in actions)
  303. {
  304. ours = action.ours;
  305. }
  306. }
  307. GUILayout.EndVertical();
  308. GUI.backgroundColor = Color.white;
  309. }
  310. }
  311. }