|
| 1 | +using Microsoft.Win32; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +namespace UnityLauncherPro |
| 8 | +{ |
| 9 | + public static class GetProjects |
| 10 | + { |
| 11 | + // which registries we want to scan for projects |
| 12 | + static readonly string[] registryPathsToCheck = new string[] { @"SOFTWARE\Unity Technologies\Unity Editor 5.x", @"SOFTWARE\Unity Technologies\Unity Editor 4.x" }; |
| 13 | + |
| 14 | + public static Project[] Scan() |
| 15 | + { |
| 16 | + List<Project> projectsFound = new List<Project>(); |
| 17 | + |
| 18 | + var hklm = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64); |
| 19 | + |
| 20 | + // check each version path |
| 21 | + for (int i = 0, len = registryPathsToCheck.Length; i < len; i++) |
| 22 | + { |
| 23 | + RegistryKey key = hklm.OpenSubKey(registryPathsToCheck[i]); |
| 24 | + |
| 25 | + if (key == null) |
| 26 | + { |
| 27 | + continue; |
| 28 | + } |
| 29 | + else |
| 30 | + { |
| 31 | + //Console.WriteLine("Null registry key at " + registryPathsToCheck[i]); |
| 32 | + } |
| 33 | + |
| 34 | + // parse recent project path |
| 35 | + foreach (var valueName in key.GetValueNames()) |
| 36 | + { |
| 37 | + if (valueName.IndexOf("RecentlyUsedProjectPaths-") == 0) |
| 38 | + { |
| 39 | + string projectPath = ""; |
| 40 | + // check if binary or not |
| 41 | + var valueKind = key.GetValueKind(valueName); |
| 42 | + if (valueKind == RegistryValueKind.Binary) |
| 43 | + { |
| 44 | + byte[] projectPathBytes = (byte[])key.GetValue(valueName); |
| 45 | + projectPath = Encoding.UTF8.GetString(projectPathBytes, 0, projectPathBytes.Length - 1); |
| 46 | + } |
| 47 | + else // should be string then |
| 48 | + { |
| 49 | + projectPath = (string)key.GetValue(valueName); |
| 50 | + } |
| 51 | + |
| 52 | + //Console.WriteLine("projectPath=" + projectPath); |
| 53 | + |
| 54 | + // first check if whole folder exists, if not, skip |
| 55 | + if (Directory.Exists(projectPath) == false) |
| 56 | + { |
| 57 | + //Console.WriteLine("Recent project directory not found, skipping: " + projectPath); |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + string projectName = ""; |
| 62 | + |
| 63 | + // get project name from full path |
| 64 | + if (projectPath.IndexOf(Path.DirectorySeparatorChar) > -1) |
| 65 | + { |
| 66 | + projectName = projectPath.Substring(projectPath.LastIndexOf(Path.DirectorySeparatorChar) + 1); |
| 67 | + } |
| 68 | + else if (projectPath.IndexOf(Path.AltDirectorySeparatorChar) > -1) |
| 69 | + { |
| 70 | + projectName = projectPath.Substring(projectPath.LastIndexOf(Path.AltDirectorySeparatorChar) + 1); |
| 71 | + } |
| 72 | + else // no path separator found |
| 73 | + { |
| 74 | + projectName = projectPath; |
| 75 | + } |
| 76 | + |
| 77 | + string csprojFile = Path.Combine(projectPath, projectName + ".csproj"); |
| 78 | + |
| 79 | + // solution only |
| 80 | + if (File.Exists(csprojFile) == false) |
| 81 | + { |
| 82 | + csprojFile = Path.Combine(projectPath, projectName + ".sln"); |
| 83 | + } |
| 84 | + |
| 85 | + // editor only project |
| 86 | + if (File.Exists(csprojFile) == false) |
| 87 | + { |
| 88 | + csprojFile = Path.Combine(projectPath, projectName + ".Editor.csproj"); |
| 89 | + } |
| 90 | + |
| 91 | + // maybe 4.x project |
| 92 | + if (File.Exists(csprojFile) == false) |
| 93 | + { |
| 94 | + csprojFile = Path.Combine(projectPath, "Assembly-CSharp.csproj"); |
| 95 | + } |
| 96 | + |
| 97 | + // get last modified date |
| 98 | + DateTime? lastUpdated = Tools.GetLastModifiedTime(csprojFile); |
| 99 | + Console.WriteLine(lastUpdated.ToString()); |
| 100 | + |
| 101 | + // get project version |
| 102 | + string projectVersion = Tools.GetProjectVersion(projectPath); |
| 103 | + |
| 104 | + /* |
| 105 | + // TODO |
| 106 | + // get custom launch arguments, only if column in enabled |
| 107 | + string customArgs = ""; |
| 108 | + if (chkShowLauncherArgumentsColumn.Checked == true) |
| 109 | + { |
| 110 | + customArgs = Tools.ReadCustomLaunchArguments(projectPath, launcherArgumentsFile); |
| 111 | + } |
| 112 | + // get git branchinfo, only if column in enabled |
| 113 | + string gitBranch = ""; |
| 114 | + if (chkShowGitBranchColumn.Checked == true) |
| 115 | + { |
| 116 | + gitBranch = Tools.ReadGitBranchInfo(projectPath); |
| 117 | + } |
| 118 | + */ |
| 119 | + |
| 120 | + var p = new Project(); |
| 121 | + p.Title = projectName; |
| 122 | + p.Version = projectVersion; |
| 123 | + p.Path = projectPath; |
| 124 | + p.Modified = lastUpdated; |
| 125 | + //p.Arguments = customArgs; |
| 126 | + //p.GITBranch = gitBranch; |
| 127 | + |
| 128 | + projectsFound.Add(p); |
| 129 | + } // valid key |
| 130 | + } // each key |
| 131 | + } // for each registry root |
| 132 | + |
| 133 | + return projectsFound.ToArray(); |
| 134 | + } // Scan() |
| 135 | + |
| 136 | + } // class |
| 137 | +} // namespace |
0 commit comments