// Copyright (c) 2015 - 2023 Doozy Entertainment. All Rights Reserved.
// This code can only be used under the standard Unity Asset Store End User License Agreement
// A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms
using System.Collections.Generic;
using System.IO;
using Doozy.Runtime.Common.Extensions;
using UnityEngine;
// ReSharper disable MemberCanBePrivate.Global
namespace Doozy.Runtime.Common.Utils
{
/// Contains methods for path manipulation, creation and deletion
public static class PathUtils
{
/// Creates the given folder path
/// Target path
public static void CreatePath(string path)
{
Directory.CreateDirectory(ToAbsolutePath(path));
#if UNITY_EDITOR
UnityEditor.AssetDatabase.Refresh(UnityEditor.ImportAssetOptions.ForceUpdate);
#endif
}
/// Clean the given path by adjusting the directory separators according to the OS
/// Target path
public static string CleanPath(string path) =>
path.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
.Replace($"{Path.AltDirectorySeparatorChar}{Path.AltDirectorySeparatorChar}", Path.AltDirectorySeparatorChar.ToString());
/// Convert the given path to data path
/// Target path
public static string ToAbsolutePath(string path) =>
CleanPath
(
path.Contains(Application.dataPath)
? path
: path.RemoveFirst("Assets".Length).AppendPrefixIfMissing(Application.dataPath)
);
/// Check if the given path is a data path
/// Target path
public static bool IsAbsolutePath(string path) =>
path.StartsWith(Application.dataPath);
/// Convert the given path to assets path
/// Target path
public static string ToRelativePath(string path) =>
CleanPath
(
IsAbsolutePath(path)
? "Assets" + path.Substring(Application.dataPath.Length)
: path
);
/// Check if the given path is an assets path
/// Target path
public static bool IsRelativePath(string path) =>
!IsAbsolutePath(path);
/// Get all available Resources directory paths within the current project
public static string[] GetResourcesDirectories()
{
var result = new List();
var stack = new Stack();
stack.Push(Application.dataPath); // Add the root directory to the stack
while (stack.Count > 0) // While we have directories to process...
{
string currentDir = stack.Pop(); // Grab a directory off the stack
try
{
foreach (string dir in Directory.GetDirectories(currentDir))
{
if (Path.GetFileName(dir).Equals("Resources"))
result.Add(dir); // If one of the found directories is a Resources dir, add it to the result
stack.Push(dir); // Add directories at the current level into the stack
}
}
catch
{
Debug.LogError($"Directory {currentDir} couldn't be read from");
}
}
return result.ToArray();
}
/// Check if the given path is a directory
public static bool PathIsDirectory(string path) =>
(File.GetAttributes(ToAbsolutePath(path)) & FileAttributes.Directory) == FileAttributes.Directory;
/// Get the directory information for the specified path
/// Target path
public static string GetDirectoryName(string path) =>
CleanPath(Path.GetDirectoryName(path));
/// Get the file name and extension of the specified path string
/// Target path
public static string GetFileName(string path) =>
CleanPath(Path.GetFileName(path));
/// Get the file name of the specified path string without the extension
/// Target path
public static string GetFileNameWithoutExtension(string path) =>
CleanPath(Path.GetFileNameWithoutExtension(path));
/// Get the extension (including the period ".") of the specified path string
/// Target path
public static string GetExtension(string path) =>
CleanPath(Path.GetExtension(path));
/// Determine if the given path includes a file name extension
/// Target path
public static bool HasExtension(string path) =>
Path.HasExtension(CleanPath(path));
}
}