-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] More C# friendly API for Godot file access #40775
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
modules/mono/glue/GodotSharp/GodotSharp/Core/Extensions/ErrorExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System; | ||
using System.IO; | ||
|
||
namespace Godot | ||
{ | ||
public static class ErrorExtensions | ||
{ | ||
public static void ThrowOnError(this Error error) | ||
{ | ||
if (error.IsException(out var e)) | ||
throw e; | ||
} | ||
|
||
internal static void DisposeAndThrowOnError(this Error error, IDisposable disposable) | ||
{ | ||
if (error.IsException(out var e)) | ||
{ | ||
disposable.Dispose(); | ||
throw e; | ||
} | ||
} | ||
|
||
public static bool IsException(this Error error, out Exception e) | ||
{ | ||
if (error == Error.Ok) | ||
{ | ||
e = null; | ||
return false; | ||
} | ||
|
||
var msg = $"Method returned with Godot error: '{Enum.GetName(typeof(Error), error)}'"; | ||
|
||
switch (error) | ||
{ | ||
case Error.InvalidParameter: | ||
e = new ArgumentException(message: msg, paramName: null); | ||
return true; | ||
case Error.ParameterRangeError: | ||
e = new ArgumentOutOfRangeException(paramName: null, message: msg); | ||
return true; | ||
case Error.FileBadDrive: | ||
case Error.FileBadPath: | ||
case Error.FileNotFound: | ||
e = new FileNotFoundException(msg); | ||
return true; | ||
case Error.AlreadyInUse: | ||
case Error.CantAcquireResource: | ||
case Error.CantOpen: | ||
case Error.CantCreate: | ||
case Error.FileAlreadyInUse: | ||
case Error.FileCantOpen: | ||
case Error.FileCantRead: | ||
case Error.FileCantWrite: | ||
case Error.FileEof: | ||
case Error.Locked: | ||
e = new IOException(msg); | ||
return true; | ||
case Error.InvalidData: | ||
e = new InvalidDataException(msg); | ||
return true; | ||
case Error.OutOfMemory: | ||
e = new OutOfMemoryException(msg); | ||
return true; | ||
case Error.Timeout: | ||
e = new TimeoutException(msg); | ||
return true; | ||
case Error.FileNoPermission: | ||
case Error.Unauthorized: | ||
e = new UnauthorizedAccessException(msg); | ||
return true; | ||
default: | ||
e = new Exception(msg); | ||
return true; | ||
} | ||
} | ||
} | ||
} |
213 changes: 213 additions & 0 deletions
213
modules/mono/glue/GodotSharp/GodotSharp/Core/IO/Directory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Godot | ||
{ | ||
public partial class Directory | ||
{ | ||
private static void ThrowIfParamPathIsNullOrEmpty(string path, string paramName) | ||
{ | ||
if (path == null) | ||
throw new ArgumentNullException(paramName, "File name cannot be null."); | ||
if (path.Length == 0) | ||
throw new ArgumentException("Empty file name is not legal.", paramName); | ||
} | ||
|
||
// TODO: Need a replacement for DirectoryInfo, which is what this method would return. | ||
public static string GetParent(string path) | ||
{ | ||
ThrowIfParamPathIsNullOrEmpty(path, nameof(path)); | ||
|
||
string parent = path.GetBaseDir(); | ||
|
||
if (string.IsNullOrEmpty(parent)) | ||
return null; | ||
|
||
return parent; | ||
} | ||
|
||
// TODO: Need a replacement for DirectoryInfo, which is what this method would return. | ||
public static string CreateDirectory(string path) | ||
{ | ||
ThrowIfParamPathIsNullOrEmpty(path, nameof(path)); | ||
|
||
using var dir = new Directory(); | ||
|
||
Error error; | ||
|
||
// TODO: Replace this workaround (MakeDirRecursive fails if the directory or any of the sub-directories already exists) | ||
while ((error = dir.MakeDirRecursive(path)) == Error.AlreadyExists && !Exists(path)) | ||
{ | ||
} | ||
|
||
error.ThrowOnError(); | ||
|
||
return path; | ||
} | ||
|
||
public static bool Exists(string path) | ||
{ | ||
try | ||
{ | ||
if (string.IsNullOrEmpty(path)) | ||
return false; | ||
|
||
using var dir = new Directory(); | ||
return dir.DirExists(path); | ||
} | ||
catch (ArgumentException) | ||
{ | ||
} | ||
catch (IOException) | ||
{ | ||
} | ||
catch (UnauthorizedAccessException) | ||
{ | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private enum SearchKind | ||
{ | ||
Files, | ||
Directories, | ||
All | ||
} | ||
|
||
public static string[] GetDirectories(string path) | ||
=> GetDirectories(path, SearchOption.TopDirectoryOnly); | ||
|
||
public static string[] GetDirectories(string path, SearchOption searchOption) | ||
=> EnumerateDirectories(path, searchOption).ToArray(); | ||
|
||
public static string[] GetFiles(string path) | ||
=> GetFiles(path, SearchOption.TopDirectoryOnly); | ||
|
||
public static string[] GetFiles(string path, SearchOption searchOption) | ||
=> EnumerateFiles(path, searchOption).ToArray(); | ||
|
||
public static string[] GetFileSystemEntries(string path) | ||
=> GetFileSystemEntries(path, SearchOption.TopDirectoryOnly); | ||
|
||
public static string[] GetFileSystemEntries(string path, SearchOption searchOption) | ||
=> EnumerateFileSystemEntries(path, searchOption).ToArray(); | ||
|
||
public static IEnumerable<string> EnumerateDirectories(string path) | ||
=> EnumerateDirectories(path, SearchOption.TopDirectoryOnly); | ||
|
||
public static IEnumerable<string> EnumerateDirectories(string path, SearchOption searchOption) | ||
=> _EnumeratePaths(path, SearchKind.Directories, searchOption).ToArray(); | ||
|
||
public static IEnumerable<string> EnumerateFiles(string path) | ||
=> EnumerateFiles(path, SearchOption.TopDirectoryOnly); | ||
|
||
public static IEnumerable<string> EnumerateFiles(string path, SearchOption searchOption) | ||
=> _EnumeratePaths(path, SearchKind.Files, searchOption).ToArray(); | ||
|
||
public static IEnumerable<string> EnumerateFileSystemEntries(string path) | ||
=> EnumerateFileSystemEntries(path, SearchOption.TopDirectoryOnly); | ||
|
||
public static IEnumerable<string> EnumerateFileSystemEntries(string path, SearchOption searchOption) | ||
=> _EnumeratePaths(path, SearchKind.All, searchOption).ToArray(); | ||
|
||
private static IEnumerable<string> _EnumeratePaths( | ||
string path, | ||
SearchKind searchKind, | ||
SearchOption options) | ||
{ | ||
if (path == null) | ||
throw new ArgumentNullException(nameof(path)); | ||
|
||
using var dir = new Directory(); | ||
|
||
var dirQueue = new Queue<string>(); | ||
dirQueue.Enqueue(path); | ||
|
||
while (dirQueue.TryDequeue(out string currentDir)) | ||
{ | ||
dir.Open(currentDir).ThrowOnError(); | ||
|
||
dir.ListDirBegin(skipNavigational: true, skipHidden: false).ThrowOnError(); | ||
|
||
string next; | ||
while (!string.IsNullOrEmpty(next = dir.GetNext())) | ||
{ | ||
next = currentDir.PlusFile(next); | ||
|
||
bool isDir = dir.CurrentIsDir(); | ||
|
||
if (isDir) | ||
{ | ||
if (options == SearchOption.AllDirectories) | ||
dirQueue.Enqueue(next); | ||
|
||
if (searchKind == SearchKind.Directories || searchKind == SearchKind.All) | ||
yield return next; | ||
} | ||
else | ||
{ | ||
if (searchKind == SearchKind.Files || searchKind == SearchKind.All) | ||
yield return next; | ||
} | ||
} | ||
|
||
dir.ListDirEnd(); | ||
} | ||
} | ||
|
||
public static string GetDirectoryRoot(string path) | ||
{ | ||
if (path == null) | ||
throw new ArgumentNullException(nameof(path)); | ||
|
||
string fullPath = Path.GetFullPath(path); | ||
string root = Path.GetPathRoot(fullPath)!; | ||
|
||
return root; | ||
} | ||
|
||
public static string GetCurrentDirectory() => System.Environment.CurrentDirectory; | ||
|
||
public static void Delete(string path) | ||
{ | ||
ThrowIfParamPathIsNullOrEmpty(path, nameof(path)); | ||
|
||
if (File.Exists(path)) | ||
throw new IOException($"A file with the same name and location already exists: '{path}'."); | ||
if (!Exists(path)) | ||
throw new DirectoryNotFoundException($"The directory could not found: '{path}'."); | ||
|
||
using var dir = new Directory(); | ||
dir.Remove(path).ThrowOnError(); | ||
} | ||
|
||
public static void Delete(string path, bool recursive) | ||
=> throw new NotImplementedException(); | ||
|
||
public static string[] GetLogicalDrives() | ||
{ | ||
using var dir = new Directory(); | ||
int driveCount = dir.GetDriveCount(); | ||
var logicalDrives = new string[driveCount]; | ||
|
||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
for (int i = 0; i < driveCount; i++) | ||
{ | ||
logicalDrives[i] = dir.GetDrive(i) + "\\"; | ||
} | ||
} | ||
else | ||
{ | ||
for (int i = 0; i < driveCount; i++) | ||
logicalDrives[i] = dir.GetDrive(i); | ||
} | ||
|
||
return logicalDrives; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering all paths after the first
if
statement returntrue
, maybe these should bebreak
and then we canreturn true
at the bottom to make this clear?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do!