diff --git a/DirectoryExt.cs b/DirectoryExt.cs new file mode 100644 index 0000000..d4c3d69 --- /dev/null +++ b/DirectoryExt.cs @@ -0,0 +1,93 @@ +using System.Collections.Generic; +using System.Linq; + +using Godot; + +using JetBrains.Annotations; + +namespace GodotExt +{ + /// + /// Contains extension methods for . + /// + [PublicAPI] + public static class DirectoryExt + { + /// + /// Returns the complete file paths of all files inside . + /// + /// The to search in. + /// Whether the search should be conducted recursively (return paths of files inside 's subdirectories and so on) or not. + /// An of the paths of all files inside . + [MustUseReturnValue] + public static IEnumerable GetFiles(this Directory directory, bool recursive = false) + { + return recursive + ? directory.GetDirectories(true) + .SelectMany(path => + { + Directory recursiveDirectory = new Directory(); + recursiveDirectory.Open(path); + return recursiveDirectory.GetElementsNonRecursive(true); + }) + .Concat(directory.GetElementsNonRecursive(true)) + : directory.GetElementsNonRecursive(true); + } + + /// + /// Returns the complete file paths of all files inside whose extensions match any of . + /// + /// The to search in. + /// Whether the search should be conducted recursively (return paths of files inside 's subdirectories and so on) or not. + /// The file extensions to search for. If none are provided, all file paths are returned. + /// An of the paths of all files inside whose extensions match any of . + [MustUseReturnValue] + public static IEnumerable GetFiles(this Directory directory, bool recursive = false, params string[] fileExtensions) + { + return fileExtensions.Any() + ? directory.GetFiles(recursive) + .Where(file => fileExtensions.Any(file.EndsWith)) + : directory.GetFiles(recursive); + } + + /// + /// Returns the complete directory paths of all directories inside . + /// + /// The to search in. + /// Whether the search should be conducted recursively (return paths of directories inside 's subdirectories and so on) or not. + /// An of the paths of all files inside . + [MustUseReturnValue] + public static IEnumerable GetDirectories(this Directory directory, bool recursive = false) + { + return recursive + ? directory.GetElementsNonRecursive(false) + .SelectMany(path => + { + Directory recursiveDirectory = new Directory(); + recursiveDirectory.Open(path); + return recursiveDirectory.GetDirectories(true).Prepend(path); + }) + : directory.GetElementsNonRecursive(false); + } + + [MustUseReturnValue] + private static IEnumerable GetElementsNonRecursive(this Directory directory, bool trueIfFiles) + { + directory.ListDirBegin(true); + while (true) + { + string next = directory.GetNext(); + if (next is "") + { + yield break; + } + if (directory.CurrentIsDir() == trueIfFiles) + { + continue; + } + string current = directory.GetCurrentDir(); + yield return current.EndsWith("/") ? $"{current}{next}" : $"{current}/{next}"; + } + } + } +} \ No newline at end of file diff --git a/ErrorExt.cs b/ErrorExt.cs new file mode 100644 index 0000000..23c345d --- /dev/null +++ b/ErrorExt.cs @@ -0,0 +1,44 @@ +using System; +using System.Runtime.CompilerServices; + +using Godot; + +using JetBrains.Annotations; + +namespace GodotExt +{ + /// + /// Contains extension methods for . + /// + [PublicAPI] + public static class ErrorExt + { + /// + /// Returns if there is no error. + /// + /// The to check. + /// if there is no error, else . + [Pure] + public static bool Success(this Error error) + { + return error is Error.Ok; + } + + /// + /// Throws an if is not . + /// + /// The to check. + /// An optional message to include if an is thrown. + /// The file path of the code that failed. + /// The line number of the code that failed. + public static void ThrowIfFailed(this Error error, [CanBeNull] string message = null, [CallerFilePath] string filePath = "", [CallerLineNumber] int line = -1) + { + if (error is Error.Ok) + { + return; + } + string fullMessage = message is null ? $"Error ({error}) at {filePath}:{line}" : $"Error ({error}) at {filePath}:{line} - {message}"; + throw new Exception(fullMessage); + } + } +} \ No newline at end of file