GameObjectMergeActions.cs 13 KB

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