/// <summary> /// Try to find the matching modules associated with path by looking for the presence of /// MRTK sentinel files. /// </summary> /// <remarks> /// In certain consumption situations, the content can be placed within the same folder, /// meaning it's possible for multiple sentinel values to exist within the same folder. /// </remarks> public static bool FindMatchingModule(string path, out List <MixedRealityToolkitModuleType> result) { result = null; if (path.Length > 0) { // Note that path can be a file or a directory DirectoryInfo directoryInfo = new DirectoryInfo(path); var sentinelFiles = Directory.EnumerateFiles(directoryInfo.FullName, SentinelFilePattern); foreach (var sentinelFile in sentinelFiles) { if (result == null) { result = new List <MixedRealityToolkitModuleType>(); } MixedRealityToolkitModuleType moduleType = MatchModuleType(sentinelFile); if (moduleType != MixedRealityToolkitModuleType.None) { result.Add(moduleType); } } } return(result != null); }
public static bool FindMatchingModule(string path, out MixedRealityToolkitModuleType result) { // Matches an optional module suffix, e.g. ".Services" const string modulePattern = @"(\.(?<module>[a-zA-Z]+))?"; // Matches a version string, e.g. "2.0.0-20190611.2" const string versionPattern = @"(?<version>[.\-0-9]+)"; // Matches the naming pattern in the MRTK repository // e.g. "MixedRealityToolkit.Services" const string mrtkPattern = @"^MixedRealityToolkit" + modulePattern + @"$"; // Matches "Microsoft.MixedReality.Toolkit", followed by optional module name, followed by version number // e.g.: "Microsoft.MixedReality.Toolkit.Services.2.0.0-20190611.2" // This alternate path is used if above isn't found. This is to work around long paths issue with NuGetForUnity // https://github.com/GlitchEnzo/NuGetForUnity/issues/246 const string nugetParentPattern = @"^Microsoft\.MixedReality\.Toolkit" + modulePattern + @"\." + versionPattern + @"$"; if (path.Length > 0) { var dirInfo = new DirectoryInfo(path); if (TryMatchFolderPattern(dirInfo.Name, mrtkPattern, out result)) { return(true); } else if (dirInfo.Name == "MRTK" && dirInfo.Parent != null && TryMatchFolderPattern(dirInfo.Parent.Name, nugetParentPattern, out result)) { return(true); } } result = MixedRealityToolkitModuleType.Core; return(false); }
/// <summary> /// Get list of discovered directories for provided module type /// </summary> /// <param name="module">Module type to filter against</param> /// <returns>string list of discovered directory paths</returns> /// <remarks> /// File/Folder paths returned are absolute, not relative /// </remarks> public static IEnumerable <string> GetDirectories(MixedRealityToolkitModuleType module) { if (mrtkFolders.TryGetValue(module, out HashSet <string> folders)) { return(folders); } return(null); }
public void TestValidPath(string path, MixedRealityToolkitModuleType expectedModule) { foreach (string basePath in basePaths) { string fullPath = Path.Combine(basePath, path); Assert.True(MixedRealityToolkitFiles.FindMatchingModule(fullPath, out MixedRealityToolkitModuleType module)); Assert.AreEqual(module, expectedModule); } }
private static void TryRegisterModuleViaFile(string filePath) { MixedRealityToolkitModuleType moduleType = GetModuleType(filePath); if (moduleType != MixedRealityToolkitModuleType.None) { string folderPath = Path.GetDirectoryName(filePath); RegisterFolderToModule(folderPath, moduleType); } }
private static void RegisterFolderToModule(string folderPath, MixedRealityToolkitModuleType module) { string normalizedFolder = NormalizeSeparators(folderPath); if (!mrtkFolders.TryGetValue(module, out HashSet <string> modFolders)) { modFolders = new HashSet <string>(); mrtkFolders.Add(module, modFolders); } modFolders.Add(normalizedFolder); }
private static bool TryMatchFolderPattern(string name, string pattern, out MixedRealityToolkitModuleType result) { var folderMatches = System.Text.RegularExpressions.Regex.Matches(name, pattern); if (folderMatches.Count == 1) { var moduleName = folderMatches[0].Groups["module"].Value; if (moduleNameMap.TryGetValue(moduleName, out result)) { return(true); } } result = MixedRealityToolkitModuleType.Core; return(false); }
private static void TryRegisterModuleFile(string fullAssetPath) { MixedRealityToolkitModuleType moduleType = MatchModuleType(fullAssetPath); if (moduleType != MixedRealityToolkitModuleType.None) { if (!mrtkFolders.TryGetValue(moduleType, out HashSet <string> modFolders)) { modFolders = new HashSet <string>(); mrtkFolders.Add(moduleType, modFolders); } string folder = Path.GetDirectoryName(fullAssetPath); modFolders.Add(NormalizeSeparators(folder)); } }
/// <summary> /// Maps a single relative path (file or folder) in MRTK folders to its absolute path, if found. /// Otherwise returns null. /// </summary> private static string MapRelativePathToAbsolutePath(SearchType searchType, MixedRealityToolkitModuleType module, string mrtkPath) { if (!AreFoldersAvailable) { Debug.LogWarning("Failed to locate MixedRealityToolkit folders in the project."); return(null); } if (mrtkFolders.TryGetValue(module, out HashSet <string> modFolders)) { string path = modFolders .Select(t => Path.Combine(t, mrtkPath)) .FirstOrDefault(t => searchType == SearchType.File ? File.Exists(t) : Directory.Exists(t)); return(path); } return(null); }
/// <summary> /// Maps a single relative path file to a concrete path from one of the MRTK folders, if found. Otherwise returns null. /// </summary> /// <param name="mrtkPathToFile">The MRTK folder relative path to the file.</param> /// <returns>The project relative path to the file.</returns> public static string MapRelativeFilePath(MixedRealityToolkitModuleType module, string mrtkPathToFile) { if (!AreFoldersAvailable) { Debug.LogError("Failed to locate MixedRealityToolkit folders in the project."); return(null); } if (mrtkFolders.TryGetValue(module, out HashSet <string> modFolders)) { string path = modFolders .Select(t => Path.Combine(t, mrtkPathToFile)) .FirstOrDefault(t => File.Exists(t)); return(path != null?GetAssetDatabasePath(path) : null); } return(null); }
private static bool TryRegisterModuleFolder(string folder, out MixedRealityToolkitModuleType module) { string normalizedFolder = NormalizeSeparators(folder); if (FindMatchingModule(normalizedFolder, out module)) { if (!mrtkFolders.TryGetValue(module, out HashSet <string> modFolders)) { modFolders = new HashSet <string>(); mrtkFolders.Add(module, modFolders); } modFolders.Add(normalizedFolder); return(true); } return(false); }
/// <summary> /// Returns files from all folder instances of the MRTK folder relative path. /// </summary> /// <param name="mrtkRelativeFolder">The MRTK folder relative path to the target folder.</param> /// <returns>The array of files.</returns> public static string[] GetFiles(MixedRealityToolkitModuleType module, string mrtkRelativeFolder) { if (!AreFoldersAvailable) { Debug.LogWarning("Failed to locate MixedRealityToolkit folders in the project."); return(null); } if (mrtkFolders.TryGetValue(module, out HashSet <string> modFolders)) { return(modFolders .Select(t => Path.Combine(t, mrtkRelativeFolder)) .Where(Directory.Exists) .SelectMany(t => Directory.GetFiles(t)) .Select(GetAssetDatabasePath) .ToArray()); } return(null); }
private static string MixedRealityToolkitDirectory(MixedRealityToolkitModuleType module, string basePath = "MixedRealityToolkit") { switch (module) { case MixedRealityToolkitModuleType.Core: return(basePath); case MixedRealityToolkitModuleType.Providers: return(basePath + ".Providers"); case MixedRealityToolkitModuleType.Services: return(basePath + ".Services"); case MixedRealityToolkitModuleType.SDK: return(basePath + ".SDK"); case MixedRealityToolkitModuleType.Examples: return(basePath + ".Examples"); case MixedRealityToolkitModuleType.Tests: return(basePath + ".Tests"); } Debug.Assert(false); return(null); }
public void TestGetDirectories() { MixedRealityToolkitModuleType[] moduleTypes = new MixedRealityToolkitModuleType[] { MixedRealityToolkitModuleType.Core, MixedRealityToolkitModuleType.Providers, MixedRealityToolkitModuleType.Services, MixedRealityToolkitModuleType.SDK, MixedRealityToolkitModuleType.Examples, MixedRealityToolkitModuleType.Tests, MixedRealityToolkitModuleType.Extensions, MixedRealityToolkitModuleType.Tools, }; MixedRealityToolkitFiles.RefreshFolders(); foreach (var moduleType in moduleTypes) { // Validate that each module has a corresponding found folder Assert.IsTrue(MixedRealityToolkitFiles.GetDirectories(moduleType).Any()); } }
/// <summary> /// Get the relative asset folder path to the provided Module type /// </summary> /// <param name="module">Module type to search for</param> /// <remarks> /// Returns first valid module folder path (relative) found. Returns null otherwise. /// </remarks> public static string MapModulePath(MixedRealityToolkitModuleType module) { var path = MapRelativeFolderPathToAbsolutePath(module, ""); return(path != null?GetAssetDatabasePath(path) : null); }
/// <summary> /// Similar to MapRelativeFilePathToAbsolutePath, except this checks for the existence of a folder instead of file. /// </summary> /// <remarks> /// Returns first valid path found /// </remarks> public static string MapRelativeFolderPathToAbsolutePath(MixedRealityToolkitModuleType module, string mrtkPathToFolder) { return(MapRelativePathToAbsolutePath(SearchType.Folder, module, mrtkPathToFolder)); }
/// <summary> /// Maps a single relative path file to a concrete path from one of the MRTK folders, if found. Otherwise returns null. /// </summary> /// <param name="mrtkPathToFile">The MRTK folder relative path to the file.</param> /// <returns>The project relative path to the file.</returns> public static string MapRelativeFilePath(MixedRealityToolkitModuleType module, string mrtkPathToFile) { string absolutePath = MapRelativeFilePathToAbsolutePath(module, mrtkPathToFile); return(absolutePath != null?GetAssetDatabasePath(absolutePath) : null); }
// This alternate path is used if above isn't found. This is to work around long paths issue with NuGetForUnity // https://github.com/GlitchEnzo/NuGetForUnity/issues/246 private static string AlternateMixedRealityToolkitDirectory(MixedRealityToolkitModuleType module) { return(MixedRealityToolkitDirectory(module, "MRTK")); }
/// <summary> /// Get the relative asset folder path to the provided Module type /// </summary> /// <param name="module">Module type to search for</param> /// <remarks> /// Returns first valid module folder path (relative) found /// </remarks> public static string MapModulePath(MixedRealityToolkitModuleType module) { return(GetAssetDatabasePath(MapRelativeFolderPathToAbsolutePath(module, ""))); }