// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace Microsoft.MixedReality.Toolkit.Utilities.Editor
{
///
/// A set of utilities for working with files.
///
public static class FileUtilities
{
///
/// Locates the files that match the specified name within the Assets folder structure.
///
/// The name of the file to locate (ex: "TestFile.asmdef")
/// Array of FileInfo objects representing the located file(s).
public static FileInfo[] FindFilesInAssets(string fileName)
{
// FindAssets doesn't take a file extension
string[] assetGuids = AssetDatabase.FindAssets(Path.GetFileNameWithoutExtension(fileName));
List fileInfos = new List();
for (int i = 0; i < assetGuids.Length; i++)
{
string assetPath = AssetDatabase.GUIDToAssetPath(assetGuids[i]);
// Since this is an asset search without extension, some filenames may contain parts of other filenames.
// Therefore, double check that the path actually contains the filename with extension.
if (assetPath.Contains(fileName))
{
fileInfos.Add(new FileInfo(assetPath));
}
}
return fileInfos.ToArray();
}
///
/// Locates the files that match the specified name within the package cache folder structure.
///
/// The name of the file to locate (ex: "TestFile.asmdef")
/// Array of FileInfo objects representing the located file(s).
public static FileInfo[] FindFilesInPackageCache(string fileName)
{
DirectoryInfo root = GetPackageCache();
return FindFiles(fileName, root);
}
///
/// Gets the package cache folder of this project.
///
///
/// A DirectoryInfo object that describes the package cache folder.
///
public static DirectoryInfo GetPackageCache()
{
string packageCacheFolderName = Path.Combine("Library", "PackageCache");
DirectoryInfo projectRoot = new DirectoryInfo(Application.dataPath).Parent;
return new DirectoryInfo(Path.Combine(projectRoot.FullName, packageCacheFolderName));
}
///
/// Finds all files matching the specified name.
///
/// The name of the file to locate (ex: "TestFile.asmdef")
/// The folder in which to perform the search.
/// Array of FileInfo objects containing the search results.
private static FileInfo[] FindFiles(string fileName, DirectoryInfo root)
{
return root.GetFiles(fileName, SearchOption.AllDirectories);
}
}
}