/// <summary> /// We can't load the scene to collect dependencies, so let's get dirty and /// parse the file for GUIDs. /// </summary> /// <param name="path">Path.</param> /// <param name="output">Output.</param> private static void GetSceneDependencies(string path, List <string> output) { using (StreamReader reader = new StreamReader(path)) { while (reader.Peek() >= 0) { string line = reader.ReadLine(); if (!line.Contains(GUID_PREFIX)) { continue; } string guidPart = line.Split(',')[1]; string guid = StringUtils.RemoveSubstring(guidPart, GUID_PREFIX).Trim(); string assetPath = AssetDatabase.GUIDToAssetPath(guid); if (string.IsNullOrEmpty(assetPath)) { continue; } if (!MaintenanceUtils.InAssetsDir(assetPath)) { continue; } output.Add(guid); } } }
/// <summary> /// Returns true if the asset at the given path should be ignored. /// </summary> /// <returns><c>true</c>, if path should be ignored, <c>false</c> otherwise.</returns> /// <param name="path">Path.</param> private static bool IgnorePath(string path) { if (!MaintenanceUtils.InAssetsDir(path)) { return(true); } string ext = Path.GetExtension(path); if (Array.Exists(s_ExtIgnore, element => element == ext)) { return(true); } for (int index = 0; index < s_DirIgnore.Length; index++) { string dirName = s_DirIgnore[index]; if (MaintenanceUtils.ContainsDirName(path, dirName)) { return(true); } } return(false); }
/// <summary> /// Gets the dependencies. /// </summary> /// <returns>The dependencies.</returns> /// <param name="path">Path.</param> private static void GetDependencies(string path, List <string> output) { if (Path.GetExtension(path) == ".unity") { GetSceneDependencies(path, output); return; } string guid = AssetDatabase.AssetPathToGUID(path); Object[] assets = AssetDatabase.LoadAllAssetsAtPath(path); Object[] dependencies = EditorUtility.CollectDependencies(assets); for (int dependencyIndex = 0; dependencyIndex < dependencies.Length; dependencyIndex++) { Object dependency = dependencies[dependencyIndex]; string dependencyPath = AssetDatabase.GetAssetPath(dependency); if (!MaintenanceUtils.InAssetsDir(dependencyPath)) { continue; } string dependencyGuid = AssetDatabase.AssetPathToGUID(dependencyPath); if (dependencyGuid == guid) { continue; } output.Add(dependencyGuid); } }
/// <summary> /// Caches the dependencies. /// </summary> private static void CacheDependencies() { s_CachedGuidDependencies.Clear(); string[] paths = AssetDatabase.GetAllAssetPaths(); for (int index = 0; index < paths.Length; index++) { string path = paths[index]; if (!File.Exists(path)) { continue; } if (MaintenanceUtils.IsDirectory(path)) { continue; } if (!MaintenanceUtils.InAssetsDir(path)) { continue; } List <string> dependencies = new List <string>(); GetDependencies(path, dependencies); string guid = AssetDatabase.AssetPathToGUID(path); s_CachedGuidDependencies[guid] = dependencies; UpdateProgressBar("Caching dependencies:", index + 1); } }
/// <summary> /// Deletes the asset with the specified guid from disk. /// </summary> /// <param name="guid">GUID.</param> private static void Delete(string guid) { string local = AssetDatabase.GUIDToAssetPath(guid); // Remove from disk if (!AssetDatabase.DeleteAsset(local)) { Debug.LogWarning(string.Format("Couldn't delete {0}", local)); return; } // Clear empty directories string directory = Path.GetDirectoryName(local); MaintenanceUtils.RemoveEmptyDirectories(directory); // Update the cache List <string> dependencies = s_CachedGuidDependencies[guid]; s_CachedGuidDependencies.Remove(guid); s_GuidOrphans.Remove(guid); if (dependencies.Count == 0) { return; } CacheParents(); CacheOrphans(); EditorUtility.ClearProgressBar(); }
/// <summary> /// Adds the header to new scripts. /// </summary> /// <param name="imported">Imported.</param> /// <param name="deleted">Deleted.</param> /// <param name="moved">Moved.</param> /// <param name="movedFromAssetPaths">Moved from asset paths.</param> private static void OnPostprocessAllAssets(string[] imported, string[] deleted, string[] moved, string[] movedFromAssetPaths) { for (int index = 0; index < imported.Length; index++) { string path = imported[index]; if (!MaintenanceUtils.ContainsDirName(path, "Hydra")) { continue; } string ext = Path.GetExtension(path); if (ext != ".cs") { continue; } WriteHeader(imported[index]); } }
/// <summary> /// Called when the editor starts, and every time the assembly is recompiled. /// </summary> static Autorun() { string path = Path.Combine(Application.dataPath, "Hydra"); MaintenanceUtils.RemoveEmptyDirectories(path); }