2018-05-09 20:42:22 -05:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEditor;
|
2018-05-15 23:11:34 -05:00
|
|
|
|
using UnityEditor.SceneManagement;
|
2018-05-09 20:42:22 -05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace GitMerge
|
|
|
|
|
{
|
|
|
|
|
public class MergeManagerScene : MergeManager
|
|
|
|
|
{
|
|
|
|
|
public MergeManagerScene(GitMergeWindow window, VCS vcs)
|
|
|
|
|
: base(window, vcs)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool InitializeMerge()
|
|
|
|
|
{
|
|
|
|
|
isMergingScene = true;
|
|
|
|
|
|
|
|
|
|
//Ask if the scene should be saved, because...
|
2018-05-15 23:11:34 -05:00
|
|
|
|
if(!EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
|
2018-05-09 20:42:22 -05:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
//...we are reloading it to prevent objects from not having a scene id.
|
2018-05-15 23:11:34 -05:00
|
|
|
|
EditorSceneManager.OpenScene(EditorSceneManager.GetActiveScene().ToString());
|
2018-05-09 20:42:22 -05:00
|
|
|
|
|
|
|
|
|
MergeAction.inMergePhase = false;
|
|
|
|
|
|
|
|
|
|
ObjectDictionaries.Clear();
|
|
|
|
|
|
|
|
|
|
//checkout "their" version
|
2018-05-15 23:11:34 -05:00
|
|
|
|
GetTheirVersionOf(EditorSceneManager.GetActiveScene().ToString());
|
2018-05-09 20:42:22 -05:00
|
|
|
|
AssetDatabase.Refresh();
|
|
|
|
|
|
|
|
|
|
//find all of "our" objects
|
|
|
|
|
var ourObjects = GetAllSceneObjects();
|
|
|
|
|
ObjectDictionaries.SetAsOurObjects(ourObjects);
|
|
|
|
|
|
|
|
|
|
//add "their" objects
|
2018-05-15 23:11:34 -05:00
|
|
|
|
EditorSceneManager.OpenScene(theirFilename);
|
2018-05-09 20:42:22 -05:00
|
|
|
|
|
|
|
|
|
//delete scene file
|
|
|
|
|
AssetDatabase.DeleteAsset(theirFilename);
|
|
|
|
|
|
|
|
|
|
//find all of "their" objects
|
|
|
|
|
var addedObjects = GetAllNewSceneObjects(ourObjects);
|
|
|
|
|
ObjectDictionaries.SetAsTheirObjects(addedObjects);
|
|
|
|
|
|
|
|
|
|
//create list of differences that have to be merged
|
|
|
|
|
BuildAllMergeActions(ourObjects, addedObjects);
|
|
|
|
|
|
|
|
|
|
if(allMergeActions.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
window.ShowNotification(new GUIContent("No conflict found for this scene."));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
MergeAction.inMergePhase = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static List<GameObject> GetAllSceneObjects()
|
|
|
|
|
{
|
|
|
|
|
var objects = (GameObject[])Object.FindObjectsOfType(typeof(GameObject));
|
|
|
|
|
return new List<GameObject>(objects);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds all GameObjects in the scene, minus the ones passed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static List<GameObject> GetAllNewSceneObjects(List<GameObject> oldObjects)
|
|
|
|
|
{
|
|
|
|
|
var all = GetAllSceneObjects();
|
|
|
|
|
var old = oldObjects;
|
|
|
|
|
|
|
|
|
|
foreach(var obj in old)
|
|
|
|
|
{
|
|
|
|
|
all.Remove(obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return all;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Completes the merge process after solving all conflicts.
|
|
|
|
|
/// Cleans up the scene by deleting "their" GameObjects, clears merge related data structures,
|
|
|
|
|
/// executes git add scene_name.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override void CompleteMerge()
|
|
|
|
|
{
|
|
|
|
|
MergeAction.inMergePhase = false;
|
|
|
|
|
|
|
|
|
|
ObjectDictionaries.DestroyTheirObjects();
|
|
|
|
|
ObjectDictionaries.Clear();
|
2018-05-15 23:11:34 -05:00
|
|
|
|
EditorSceneManager.SaveOpenScenes();
|
2018-05-09 20:42:22 -05:00
|
|
|
|
|
|
|
|
|
allMergeActions = null;
|
|
|
|
|
|
|
|
|
|
//Mark as merged for git
|
|
|
|
|
vcs.MarkAsMerged(fileName);
|
|
|
|
|
|
|
|
|
|
//directly committing here might not be that smart, since there might be more conflicts
|
|
|
|
|
|
|
|
|
|
window.ShowNotification(new GUIContent("Scene successfully merged."));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Aborts merge by using "our" version in all conflicts.
|
|
|
|
|
/// Cleans up merge related data.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override void AbortMerge()
|
|
|
|
|
{
|
|
|
|
|
base.AbortMerge();
|
|
|
|
|
|
|
|
|
|
//Save scene
|
2018-05-15 23:11:34 -05:00
|
|
|
|
EditorSceneManager.SaveOpenScenes();
|
2018-05-09 20:42:22 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|