/// <summary> /// Creates a symlink between destinationPath and sourcePath (Windows version). /// </summary> /// <param name="sourcePath"></param> /// <param name="destinationPath"></param> private static void CreateLinkWin(string sourcePath, string destinationPath) { string cmd = "/C mklink /J " + string.Format("\"{0}\" \"{1}\"", destinationPath, sourcePath); Debug.Log("Windows junction: " + cmd); ClonesManager.StartHiddenConsoleProcess("cmd.exe", cmd); }
/// <summary> /// Deletes the clone of the currently open project, if such exists. /// </summary> public static void DeleteClone(string cloneProjectPath) { /// Clone won't be able to delete itself. if (ClonesManager.IsClone()) { return; } ///Extra precautions. if (cloneProjectPath == string.Empty) { return; } if (cloneProjectPath == ClonesManager.GetOriginalProjectPath()) { return; } //Check what OS is string identifierFile; string args; switch (Application.platform) { case (RuntimePlatform.WindowsEditor): Debug.Log("Attempting to delete folder \"" + cloneProjectPath + "\""); //The argument file will be deleted first at the beginning of the project deletion process //to prevent any further reading and writing to it(There's a File.Exist() check at the (file)editor windows.) //If there's any file in the directory being write/read during the deletion process, the directory can't be fully removed. identifierFile = Path.Combine(cloneProjectPath, ClonesManager.ArgumentFileName); File.Delete(identifierFile); args = "/c " + @"rmdir /s/q " + string.Format("\"{0}\"", cloneProjectPath); StartHiddenConsoleProcess("cmd.exe", args); break; case (RuntimePlatform.OSXEditor): Debug.Log("Attempting to delete folder \"" + cloneProjectPath + "\""); //The argument file will be deleted first at the beginning of the project deletion process //to prevent any further reading and writing to it(There's a File.Exist() check at the (file)editor windows.) //If there's any file in the directory being write/read during the deletion process, the directory can't be fully removed. identifierFile = Path.Combine(cloneProjectPath, ClonesManager.ArgumentFileName); File.Delete(identifierFile); FileUtil.DeleteFileOrDirectory(cloneProjectPath); break; case (RuntimePlatform.LinuxEditor): throw new System.NotImplementedException("No linux support yet :("); //break; default: Debug.LogWarning("Not in a known editor. Where are you!?"); break; } }
/// <summary> /// Returns the path to the original project. /// If currently open project is the original, returns its own path. /// If the original project folder cannot be found, retuns an empty string. /// </summary> /// <returns></returns> public static string GetOriginalProjectPath() { if (IsClone()) { /// If this is a clone... /// Original project path can be deduced by removing the suffix from the clone's path. string cloneProjectPath = ClonesManager.GetCurrentProject().projectPath; int index = cloneProjectPath.LastIndexOf(ClonesManager.CloneNameSuffix); if (index > 0) { string originalProjectPath = cloneProjectPath.Substring(0, index); if (Directory.Exists(originalProjectPath)) { return(originalProjectPath); } } return(string.Empty); } else { /// If this is the original, we return its own path. return(ClonesManager.GetCurrentProjectPath()); } }
/// <summary> /// Copies directory located at sourcePath to destinationPath. Displays a progress bar. /// Same as the previous method, but uses recursion to copy all nested folders as well. /// </summary> /// <param name="source">Directory to be copied.</param> /// <param name="destination">Destination directory (created automatically if needed).</param> /// <param name="totalBytes">Total bytes to be copied. Calculated automatically, initialize at 0.</param> /// <param name="copiedBytes">To track already copied bytes. Calculated automatically, initialize at 0.</param> /// <param name="progressBarPrefix">Optional string added to the beginning of the progress bar window header.</param> private static void CopyDirectoryWithProgressBarRecursive(DirectoryInfo source, DirectoryInfo destination, ref long totalBytes, ref long copiedBytes, string progressBarPrefix = "") { /// Directory cannot be copied into itself. if (source.FullName.ToLower() == destination.FullName.ToLower()) { Debug.LogError("Cannot copy directory into itself."); return; } /// Calculate total bytes, if required. if (totalBytes == 0) { totalBytes = ClonesManager.GetDirectorySize(source, true, progressBarPrefix); } /// Create destination directory, if required. if (!Directory.Exists(destination.FullName)) { Directory.CreateDirectory(destination.FullName); } /// Copy all files from the source. foreach (FileInfo file in source.GetFiles()) { try { file.CopyTo(Path.Combine(destination.ToString(), file.Name), true); } catch (IOException) { /// Some files may throw IOException if they are currently open in Unity editor. /// Just ignore them in such case. } /// Account the copied file size. copiedBytes += file.Length; /// Display the progress bar. float progress = (float)copiedBytes / (float)totalBytes; bool cancelCopy = EditorUtility.DisplayCancelableProgressBar( progressBarPrefix + "Copying '" + source.FullName + "' to '" + destination.FullName + "'...", "(" + (progress * 100f).ToString("F2") + "%) Copying file '" + file.Name + "'...", progress); if (cancelCopy) { return; } } /// Copy all nested directories from the source. foreach (DirectoryInfo sourceNestedDir in source.GetDirectories()) { DirectoryInfo nextDestingationNestedDir = destination.CreateSubdirectory(sourceNestedDir.Name); ClonesManager.CopyDirectoryWithProgressBarRecursive(sourceNestedDir, nextDestingationNestedDir, ref totalBytes, ref copiedBytes, progressBarPrefix); } }
/// <summary> /// Creates a symlink between destinationPath and sourcePath (Linux version). /// </summary> /// <param name="sourcePath"></param> /// <param name="destinationPath"></param> private static void CreateLinkLinux(string sourcePath, string destinationPath) { sourcePath = sourcePath.Replace(" ", "\\ "); destinationPath = destinationPath.Replace(" ", "\\ "); var command = string.Format("ln -s {0} {1}", sourcePath, destinationPath); Debug.Log("Linux Symlink " + command); ClonesManager.ExecuteBashCommand(command); }
/// <summary> /// Creates a symlink between destinationPath and sourcePath (Mac version). /// </summary> /// <param name="sourcePath"></param> /// <param name="destinationPath"></param> private static void CreateLinkMac(string sourcePath, string destinationPath) { sourcePath = sourcePath.Replace(" ", "\\ "); destinationPath = destinationPath.Replace(" ", "\\ "); var command = $"ln -s {sourcePath} {destinationPath}"; Debug.Log("Mac hard link " + command); ClonesManager.ExecuteBashCommand(command); }
/// <summary> /// Returns true if the project currently open in Unity Editor is a clone. /// </summary> /// <returns></returns> public static bool IsClone() { if (isCloneFileExistCache == null) { /// The project is a clone if its root directory contains an empty file named ".clone". string cloneFilePath = Path.Combine(ClonesManager.GetCurrentProjectPath(), ClonesManager.CloneFileName); isCloneFileExistCache = File.Exists(cloneFilePath); } return((bool)isCloneFileExistCache); }
/// <summary> /// Creates a symlink between destinationPath and sourcePath (Mac version). /// </summary> /// <param name="sourcePath"></param> /// <param name="destinationPath"></param> private static void CreateLinkMac(string sourcePath, string destinationPath) { Debug.LogWarning("This hasn't been tested yet!"); string cmd = "ln " + string.Format("\"{0}\" \"{1}\"", destinationPath, sourcePath); Debug.Log("Mac hard link " + cmd); ClonesManager.StartHiddenConsoleProcess("/bin/bash", cmd); }
/// <summary> /// Copies the full contents of the unity library. We want to do this to avoid the lengthy re-serialization of the whole project when it opens up the clone. /// </summary> /// <param name="sourceProject"></param> /// <param name="destinationProject"></param> public static void CopyLibraryFolder(Project sourceProject, Project destinationProject) { if (Directory.Exists(destinationProject.libraryPath)) { Debug.LogWarning("Library copy: destination path already exists! "); return; } Debug.Log("Library copy: " + destinationProject.libraryPath); ClonesManager.CopyDirectoryWithProgressBar(sourceProject.libraryPath, destinationProject.libraryPath, "Cloning project '" + sourceProject.name + "'. "); }
/// <summary> /// Copies directory located at sourcePath to destinationPath. Displays a progress bar. /// </summary> /// <param name="source">Directory to be copied.</param> /// <param name="destination">Destination directory (created automatically if needed).</param> /// <param name="progressBarPrefix">Optional string added to the beginning of the progress bar window header.</param> public static void CopyDirectoryWithProgressBar(string sourcePath, string destinationPath, string progressBarPrefix = "") { var source = new DirectoryInfo(sourcePath); var destination = new DirectoryInfo(destinationPath); long totalBytes = 0; long copiedBytes = 0; ClonesManager.CopyDirectoryWithProgressBarRecursive(source, destination, ref totalBytes, ref copiedBytes, progressBarPrefix); EditorUtility.ClearProgressBar(); }
/// <summary> /// Creates clone from the project currently open in Unity Editor. /// </summary> /// <returns></returns> public static Project CreateCloneFromCurrent() { if (IsClone()) { Debug.LogError("This project is already a clone. Cannot clone it."); return(null); } string currentProjectPath = ClonesManager.GetCurrentProjectPath(); return(ClonesManager.CreateCloneFromPath(currentProjectPath)); }
/// <summary> /// Called once on editor startup. /// Validate copied folders integrity in clone project /// </summary> static ValidateCopiedFoldersIntegrity() { if (!SessionState.GetBool(SessionStateKey, false)) { SessionState.SetBool(SessionStateKey, true); if (!ClonesManager.IsClone()) { return; } ValidateFolder(ClonesManager.GetCurrentProjectPath(), ClonesManager.GetOriginalProjectPath(), "Packages"); } }
/// <summary> /// Creates clone of the project located at the given path. /// </summary> /// <param name="sourceProjectPath"></param> /// <returns></returns> public static Project CreateCloneFromPath(string sourceProjectPath) { Project sourceProject = new Project(sourceProjectPath, Preferences.CustomFoldersPref.Value.Split(';')); string cloneProjectPath = null; //Find available clone suffix id for (int i = 0; i < MaxCloneProjectCount; i++) { string originalProjectPath = ClonesManager.GetCurrentProject().projectPath; string possibleCloneProjectPath = originalProjectPath + ClonesManager.CloneNameSuffix + "_" + i; if (!Directory.Exists(possibleCloneProjectPath)) { cloneProjectPath = possibleCloneProjectPath; break; } } if (string.IsNullOrEmpty(cloneProjectPath)) { Debug.LogError("The number of cloned projects has reach its limit. Limit: " + MaxCloneProjectCount); return(null); } Project cloneProject = new Project(cloneProjectPath, Preferences.CustomFoldersPref.Value.Split(';')); Debug.Log("Start cloning project, original project: " + sourceProject + ", clone project: " + cloneProject); ClonesManager.CreateProjectFolder(cloneProject); ClonesManager.CopyLibraryFolder(sourceProject, cloneProject); ClonesManager.LinkFolders(sourceProject.assetPath, cloneProject.assetPath); ClonesManager.LinkFolders(sourceProject.projectSettingsPath, cloneProject.projectSettingsPath); ClonesManager.LinkFolders(sourceProject.packagesPath, cloneProject.packagesPath); ClonesManager.LinkFolders(sourceProject.autoBuildPath, cloneProject.autoBuildPath); ClonesManager.LinkFolders(sourceProject.localPackages, cloneProject.localPackages); if (sourceProject.customFolders != null) { for (int i = 0; i < sourceProject.customFolders.Length; i++) { ClonesManager.LinkFolders(sourceProject.customFolders[i], cloneProject.customFolders[i]); } } ClonesManager.RegisterClone(cloneProject); return(cloneProject); }
/// <summary> /// Creates clone of the project located at the given path. /// </summary> /// <param name="sourceProjectPath"></param> /// <returns></returns> public static Project CreateCloneFromPath(string sourceProjectPath) { Project sourceProject = new Project(sourceProjectPath); string cloneProjectPath = null; //Find available clone suffix id for (int i = 0; i < MaxCloneProjectCount; i++) { string originalProjectPath = ClonesManager.GetCurrentProject().projectPath; string possibleCloneProjectPath = originalProjectPath + ClonesManager.CloneNameSuffix + "_" + i; if (!Directory.Exists(possibleCloneProjectPath)) { cloneProjectPath = possibleCloneProjectPath; break; } } if (string.IsNullOrEmpty(cloneProjectPath)) { Debug.LogError("The number of cloned projects has reach its limit. Limit: " + MaxCloneProjectCount); return(null); } Project cloneProject = new Project(cloneProjectPath); Debug.Log("Start cloning project, original project: " + sourceProject + ", clone project: " + cloneProject); ClonesManager.CreateProjectFolder(cloneProject); //Copy Folders Debug.Log("Library copy: " + cloneProject.libraryPath); ClonesManager.CopyDirectoryWithProgressBar(sourceProject.libraryPath, cloneProject.libraryPath, "Cloning Project Library '" + sourceProject.name + "'. "); Debug.Log("Packages copy: " + cloneProject.libraryPath); ClonesManager.CopyDirectoryWithProgressBar(sourceProject.packagesPath, cloneProject.packagesPath, "Cloning Project Packages '" + sourceProject.name + "'. "); //Link Folders ClonesManager.LinkFolders(sourceProject.assetPath, cloneProject.assetPath); ClonesManager.LinkFolders(sourceProject.projectSettingsPath, cloneProject.projectSettingsPath); ClonesManager.LinkFolders(sourceProject.autoBuildPath, cloneProject.autoBuildPath); ClonesManager.LinkFolders(sourceProject.localPackages, cloneProject.localPackages); ClonesManager.RegisterClone(cloneProject); return(cloneProject); }
/// <summary> /// Returns all clone projects path. /// </summary> /// <returns></returns> public static List <string> GetCloneProjectsPath() { List <string> projectsPath = new List <string>(); for (int i = 0; i < MaxCloneProjectCount; i++) { string originalProjectPath = ClonesManager.GetCurrentProject().projectPath; string cloneProjectPath = originalProjectPath + ClonesManager.CloneNameSuffix + "_" + i; if (Directory.Exists(cloneProjectPath)) { projectsPath.Add(cloneProjectPath); } } return(projectsPath); }
private void OnGUI() { if (ClonesManager.IsClone()) { EditorGUILayout.HelpBox( "This is a clone project. Please use the original project editor to change preferences.", MessageType.Info); return; } GUILayout.BeginVertical("HelpBox"); GUILayout.Label("Preferences"); GUILayout.BeginVertical("GroupBox"); AssetModPref.Value = EditorGUILayout.ToggleLeft( new GUIContent( "(recommended) Disable asset saving in clone editors- require re-open clone editors", "Disable asset saving in clone editors so all assets can only be modified from the original project editor" ), AssetModPref.Value); if (Application.platform == RuntimePlatform.WindowsEditor) { AlsoCheckUnityLockFileStaPref.Value = EditorGUILayout.ToggleLeft( new GUIContent( "Also check UnityLockFile lock status while checking clone projects running status", "Disable this can slightly increase Clones Manager window performance, but will lead to in-correct clone project running status" + "(the Clones Manager window show the clone project is still running even it's not) if the clone editor crashed" ), AlsoCheckUnityLockFileStaPref.Value); } GUILayout.Label("Custom Folders"); CustomFoldersPref.Value = EditorGUILayout.TextField(CustomFoldersPref.Value); GUILayout.EndVertical(); if (GUILayout.Button("Reset to default")) { AssetModPref.ClearValue(); AlsoCheckUnityLockFileStaPref.ClearValue(); Debug.Log("Editor preferences cleared"); } GUILayout.EndVertical(); }
/// <summary> /// Opens a project located at the given path (if one exists). /// </summary> /// <param name="projectPath"></param> public static void OpenProject(string projectPath) { if (!Directory.Exists(projectPath)) { Debug.LogError("Cannot open the project - provided folder (" + projectPath + ") does not exist."); return; } if (projectPath == ClonesManager.GetCurrentProjectPath()) { Debug.LogError("Cannot open the project - it is already open."); return; } string fileName = EditorApplication.applicationPath; string args = "-projectPath \"" + projectPath + "\""; Debug.Log("Opening project \"" + fileName + " " + args + "\""); ClonesManager.StartHiddenConsoleProcess(fileName, args); }
/// <summary> /// Calculates the size of the given directory. Displays a progress bar. /// </summary> /// <param name="directory">Directory, which size has to be calculated.</param> /// <param name="includeNested">If true, size will include all nested directories.</param> /// <param name="progressBarPrefix">Optional string added to the beginning of the progress bar window header.</param> /// <returns>Size of the directory in bytes.</returns> private static long GetDirectorySize(DirectoryInfo directory, bool includeNested = false, string progressBarPrefix = "") { EditorUtility.DisplayProgressBar(progressBarPrefix + "Calculating size of directories...", "Scanning '" + directory.FullName + "'...", 0f); /// Calculate size of all files in directory. long filesSize = directory.EnumerateFiles().Sum((FileInfo file) => file.Length); /// Calculate size of all nested directories. long directoriesSize = 0; if (includeNested) { IEnumerable <DirectoryInfo> nestedDirectories = directory.EnumerateDirectories(); foreach (DirectoryInfo nestedDir in nestedDirectories) { directoriesSize += ClonesManager.GetDirectorySize(nestedDir, true, progressBarPrefix); } } return(filesSize + directoriesSize); }
/// <summary> /// Opens a project located at the given path (if one exists). /// </summary> /// <param name="projectPath"></param> public static void OpenProject(string projectPath) { if (!Directory.Exists(projectPath)) { Debug.LogError("Cannot open the project - provided folder (" + projectPath + ") does not exist."); return; } if (projectPath == ClonesManager.GetCurrentProjectPath()) { Debug.LogError("Cannot open the project - it is already open."); return; } //Validate (and update if needed) the "Packages" folder before opening clone project to ensure the clone project will have the //same "compiling environment" as the original project ValidateCopiedFoldersIntegrity.ValidateFolder(projectPath, GetOriginalProjectPath(), "Packages"); string fileName = GetApplicationPath(); string args = "-projectPath \"" + projectPath + "\""; Debug.Log("Opening project \"" + fileName + " " + args + "\""); ClonesManager.StartHiddenConsoleProcess(fileName, args); }
public static string[] OnWillSaveAssets(string[] paths) { if (paths != null && paths.Length > 0 && ClonesManager.IsClone()) { if (!EditorQuit.IsQuiting) { EditorUtility.DisplayDialog( ClonesManager.ProjectName + ": Asset modifications saving detected and blocked", "Asset modifications saving are blocked in the clone instance. \n\n" + "This is a clone of the original project. \n" + "Making changes to asset files via the clone editor is not recommended. \n" + "Please use the original editor instance if you want to make changes the project files.", "ok" ); foreach (var path in paths) { Debug.Log("Attempting to save " + path + " are blocked."); } } return(new string[0] { }); } return(paths); }
private void OnGUI() { if (Application.platform == RuntimePlatform.LinuxEditor) { EditorGUILayout.HelpBox( "Sorry, but " + ClonesManager.ProjectName + " doesn't support Linux currently.\n" + "Please create a feature request on GitHub issue page if you want it to be added.", MessageType.Info); if (GUILayout.Button("Open GitHub issue Page")) { Application.OpenURL(ExternalLinks.GitHubIssue); } return; } /// If it is a clone project... if (ClonesManager.IsClone()) { //Find out the original project name and show the help box string originalProjectPath = ClonesManager.GetOriginalProjectPath(); if (originalProjectPath == string.Empty) { /// If original project cannot be found, display warning message. EditorGUILayout.HelpBox( "This project is a clone, but the link to the original seems lost.\nYou have to manually open the original and create a new clone instead of this one.\n", MessageType.Warning); } else { /// If original project is present, display some usage info. EditorGUILayout.HelpBox( "This project is a clone of the project '" + Path.GetFileName(originalProjectPath) + "'.\nIf you want to make changes the project files or manage clones, please open the original project through Unity Hub.", MessageType.Info); } //Clone project custom argument. GUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Arguments", GUILayout.Width(70)); if (GUILayout.Button("?", GUILayout.Width(20))) { Application.OpenURL(ExternalLinks.CustomArgumentHelpLink); } GUILayout.EndHorizontal(); string argumentFilePath = Path.Combine(ClonesManager.GetCurrentProjectPath(), ClonesManager.ArgumentFileName); //Need to be careful with file reading / writing since it will effect the deletion of // the clone project(The directory won't be fully deleted if there's still file inside being read or write). //The argument file will be deleted first at the beginning of the project deletion process //to prevent any further being read and write. //Will need to take some extra cautious if want to change the design of how file editing is handled. if (File.Exists(argumentFilePath)) { string argument = File.ReadAllText(argumentFilePath, System.Text.Encoding.UTF8); string argumentTextAreaInput = EditorGUILayout.TextArea(argument, GUILayout.Height(50), GUILayout.MaxWidth(300) ); File.WriteAllText(argumentFilePath, argumentTextAreaInput, System.Text.Encoding.UTF8); } else { EditorGUILayout.LabelField("No argument file found."); } } else // If it is an original project... { if (isCloneCreated) { GUILayout.BeginVertical("HelpBox"); GUILayout.Label("Clones of this Project"); //List all clones clonesScrollPos = EditorGUILayout.BeginScrollView(clonesScrollPos); var cloneProjectsPath = ClonesManager.GetCloneProjectsPath(); for (int i = 0; i < cloneProjectsPath.Count; i++) { GUILayout.BeginVertical("GroupBox"); string cloneProjectPath = cloneProjectsPath[i]; bool isOpenInAnotherInstance = ClonesManager.IsCloneProjectRunning(cloneProjectPath); if (isOpenInAnotherInstance == true) { EditorGUILayout.LabelField("Clone " + i + " (Running)", EditorStyles.boldLabel); } else { EditorGUILayout.LabelField("Clone " + i); } GUILayout.BeginHorizontal(); EditorGUILayout.TextField("Clone project path", cloneProjectPath, EditorStyles.textField); if (GUILayout.Button("View Folder", GUILayout.Width(80))) { ClonesManager.OpenProjectInFileExplorer(cloneProjectPath); } GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Arguments", GUILayout.Width(70)); if (GUILayout.Button("?", GUILayout.Width(20))) { Application.OpenURL(ExternalLinks.CustomArgumentHelpLink); } GUILayout.EndHorizontal(); string argumentFilePath = Path.Combine(cloneProjectPath, ClonesManager.ArgumentFileName); //Need to be careful with file reading/writing since it will effect the deletion of //the clone project(The directory won't be fully deleted if there's still file inside being read or write). //The argument file will be deleted first at the beginning of the project deletion process //to prevent any further being read and write. //Will need to take some extra cautious if want to change the design of how file editing is handled. if (File.Exists(argumentFilePath)) { string argument = File.ReadAllText(argumentFilePath, System.Text.Encoding.UTF8); string argumentTextAreaInput = EditorGUILayout.TextArea(argument, GUILayout.Height(50), GUILayout.MaxWidth(300) ); File.WriteAllText(argumentFilePath, argumentTextAreaInput, System.Text.Encoding.UTF8); } else { EditorGUILayout.LabelField("No argument file found."); } EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUI.BeginDisabledGroup(isOpenInAnotherInstance); if (GUILayout.Button("Open in New Editor")) { ClonesManager.OpenProject(cloneProjectPath); } GUILayout.BeginHorizontal(); if (GUILayout.Button("Delete")) { bool delete = EditorUtility.DisplayDialog( "Delete the clone?", "Are you sure you want to delete the clone project '" + ClonesManager.GetCurrentProject().name + "_clone'?", "Delete", "Cancel"); if (delete) { ClonesManager.DeleteClone(cloneProjectPath); } } GUILayout.EndHorizontal(); EditorGUI.EndDisabledGroup(); GUILayout.EndVertical(); } EditorGUILayout.EndScrollView(); if (GUILayout.Button("Add new clone")) { ClonesManager.CreateCloneFromCurrent(); } GUILayout.EndVertical(); GUILayout.FlexibleSpace(); } else { /// If no clone created yet, we must create it. EditorGUILayout.HelpBox("No project clones found. Create a new one!", MessageType.Info); if (GUILayout.Button("Create new clone")) { ClonesManager.CreateCloneFromCurrent(); } } } }
/// <summary> /// Return a project object that describes all the paths we need to clone it. /// </summary> /// <returns></returns> public static Project GetCurrentProject() { string pathString = ClonesManager.GetCurrentProjectPath(); return(new Project(pathString)); }