public static void ResetConfig()
 {
     VCSPrefs.DeleteKey(UsernamePrefKey);
     VCSPrefs.DeleteKey(PreventEditsOnLockKey);
     VCSPrefs.DeleteKey(SceneAutoLockKey);
     VCSPrefs.DeleteKey(LFSEnabledKey);
     VCSPrefs.DeleteKey(AutoUpdateSubmoduleKey);
 }
    void LoadLockedFilesFromVCSPrefs(bool forceRebuild = false)
    {
        if (!GitHelper.LFSEnabled)
        {
            return;
        }

        if (_lockedFiles != null)
        {
            foreach (var v in _lockedFiles)
            {
                if (v.Value.FileLock != null)
                {
                    v.Value.FileLock.Dispose();
                }
            }
        }

        _lockedFiles = new Dictionary <string, LockedFile>();
        if (VCSPrefs.HasKey(LockedFileKey))
        {
            var storage = JsonUtility.FromJson <LockFileStorage>(VCSPrefs.GetString(LockedFileKey));

            // If this is a different run, let's refresh git locks
            if ((storage.PID != Process.GetCurrentProcess().Id) || forceRebuild)
            {
                RefreshGitLocks();
            }
            else
            {
                // Otherwise, this is just an assembly load and we can probably use the old locks
                foreach (var v in storage.Locks)
                {
                    _lockedFiles.Add(v.Path, v);
                    if (v.User != GitHelper.Username)
                    {
                        if (File.Exists(v.Path))
                        {
                            try
                            {
                                v.FileLock = new FileStream(v.Path, FileMode.Open, FileAccess.Read, FileShare.Read);
                            }
                            catch (System.Exception ex)
                            {
                                Debug.LogError("Failed to create file lock for " + v.Path + ": " + ex);
                            }
                        }
                    }
                }
            }
        }
    }
    void UpdateLockedFiles()
    {
        if (!GitHelper.LFSEnabled)
        {
            return;
        }

        var storage = new LockFileStorage();

        storage.PID = Process.GetCurrentProcess().Id;

        var list = new List <LockedFile>();

        foreach (var v in LockedFiles)
        {
            list.Add(v.Value);
        }
        storage.Locks = list;
        VCSPrefs.SetString(LockedFileKey, JsonUtility.ToJson(storage));
    }
        public void Draw(GitVCS vcs)
        {
            if ((configFoldout = EditorGUILayout.Foldout(configFoldout, ConfigLabel)))
            {
                ++EditorGUI.indentLevel;
                // Username
                {
                    string value    = VCSPrefs.HasKey(GitHelper.VCSPrefsKeys.UsernamePrefKey) ? VCSPrefs.GetString(GitHelper.VCSPrefsKeys.UsernamePrefKey) : "";
                    string newvalue = EditorGUILayout.TextField("Github Username", value);
                    if (newvalue != value)
                    {
                        VCSPrefs.SetString(GitHelper.VCSPrefsKeys.UsernamePrefKey, newvalue);
                    }
                }

                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Auto Update Submodules");
                GitHelper.AutoUpdateSubmodules = EditorGUILayout.Toggle(GitHelper.AutoUpdateSubmodules, GUILayout.ExpandWidth(true));
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("VCS Enabled");
                GitHelper.VCSEnabled = EditorGUILayout.Toggle(GitHelper.VCSEnabled, GUILayout.ExpandWidth(true));
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("LFS Enabled");
                GitHelper.LFSEnabled = EditorGUILayout.Toggle(GitHelper.LFSEnabled, GUILayout.ExpandWidth(true));
                EditorGUILayout.EndHorizontal();

                if (GitHelper.LFSEnabled)
                {
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.LabelField("Automatically Lock Scenes on Save");
                    GitHelper.SceneAutoLock = EditorGUILayout.Toggle(GitHelper.SceneAutoLock, GUILayout.ExpandWidth(true));
                    EditorGUILayout.EndHorizontal();

                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.LabelField("Remote Lock Prevents Edits");
                    GitHelper.PreventEditsOnRemoteLock = EditorGUILayout.Toggle(GitHelper.PreventEditsOnRemoteLock, GUILayout.ExpandWidth(true));
                    EditorGUILayout.EndHorizontal();
                }

                if (!hasCheckedVersion)
                {
                    if (GUILayout.Button("Check Git Compatibility"))
                    {
                        hasCheckedVersion = true;
                        GitHelper.RunGitCommand("version",
                                                (proc) =>
                        {
                            if (!proc.WaitForExit(2000))
                            {
                                return(true);
                            }
                            return(false);
                        },
                                                result =>
                        {
                            // We know what version is required on Windows, must check
                            // later for OSX/Linux.
                            if (result.Contains("windows"))
                            {
                                result = result.Replace("git version ", "");

                                var split        = result.Trim().Split('.');
                                var majorVersion = int.Parse(split[0].Trim());
                                var minorVersion = int.Parse(split[1].Trim());
                                if (majorVersion > 2 || (majorVersion == 2 && minorVersion >= 16))
                                {
                                    versionWorks = true;
                                }
                                else
                                {
                                    versionWorks = false;
                                    if (EditorUtility.DisplayDialog("Version Control", "Git for Windows out of date, this plugin requires at least version 2.16.  Would you like to open the GitHub Releases page in your web browser?", "Yes", "No"))
                                    {
                                        OpenGitForWindowsDLPage();
                                    }
                                }
                            }
                            else
                            {
                                Debug.Log("Version checking only supported on Windows.  If the plugin doesn't work, ");
                                Debug.Log("Git Version: " + result);
                            }
                        },
                                                error =>
                        {
                            Debug.LogError(error);
                            return(true);
                        }
                                                );
                    }
                }
                else
                {
                    if (Application.platform != RuntimePlatform.WindowsEditor)
                    {
                        EditorGUILayout.LabelField("Check Console, version checking only functional on Windows");
                    }
                    else if (versionWorks)
                    {
                        EditorGUILayout.LabelField("Local git compatibility good!");
                    }
                    else
                    {
                        if (GUILayout.Button("Update Git for Windows (opens GitHub Releases page)"))
                        {
                            OpenGitForWindowsDLPage();
                        }
                    }
                }
                --EditorGUI.indentLevel;
            }

            if ((lockedFilesFoldout = EditorGUILayout.Foldout(lockedFilesFoldout, LockedFileLabel)))
            {
                ++EditorGUI.indentLevel;
                foreach (var v in vcs.LockedFiles)
                {
                    GUILayout.Label(v.Key + ": Locked by " + v.Value.User);
                    EditorGUILayout.BeginHorizontal();

                    if (GUILayout.Button("Select"))
                    {
                        Selection.activeObject = AssetDatabase.LoadAssetAtPath <Object>(v.Value.Path);
                    }

                    if (vcs.IsFileLockedByLocalUser(v.Key))
                    {
                        if (GUILayout.Button("Unlock"))
                        {
                            try
                            {
                                vcs.GitUnlockFile(new string[] { v.Key });
                            }
                            catch (System.Exception ex)
                            {
                                // Do nothing, it'll tell them
                            }
                            break;
                        }
                    }
                    EditorGUILayout.EndHorizontal();
                    EditorGUILayout.Space();
                }
                --EditorGUI.indentLevel;
            }

            if ((modifiedFilesFoldout = EditorGUILayout.Foldout(modifiedFilesFoldout, ModifiedFilesLabel)))
            {
                ++EditorGUI.indentLevel;
                foreach (var v in vcs.ModifiedPaths)
                {
                    if (!File.Exists(v))
                    {
                        continue;
                    }

                    EditorGUILayout.BeginHorizontal();
                    GUILayout.Label(v);
                    if (GUILayout.Button("Select", GUILayout.ExpandWidth(false)))
                    {
                        Selection.activeObject = AssetDatabase.LoadAssetAtPath <Object>(v);
                    }

                    if (GUILayout.Button("Discard Changes", GUILayout.ExpandWidth(false)))
                    {
                        var oldSelection = Selection.activeObject;
                        Selection.activeObject = AssetDatabase.LoadAssetAtPath <Object>(v);
                        vcs.DiscardChanges();
                        Selection.activeObject = oldSelection;
                    }

                    EditorGUILayout.EndHorizontal();

                    EditorGUILayout.Space();
                }
                --EditorGUI.indentLevel;
            }

            if (GUILayout.Button("Refresh Locks"))
            {
                vcs.RefreshGitLockTypes();
                vcs.RefreshGitLocks();
            }

            if (GUILayout.Button("Reset Configuration"))
            {
                GitHelper.VCSPrefsKeys.ResetConfig();
            }
        }