1 /** 2 Type used to handle the filesystem. 3 4 Copyright: 5 Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. 6 Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) 7 Copyright (c) 2017-2018 Godot-D contributors 8 9 License: $(LINK2 https://opensource.org/licenses/MIT, MIT License) 10 11 12 */ 13 module godot.directory; 14 import std.meta : AliasSeq, staticIndexOf; 15 import std.traits : Unqual; 16 import godot.d.meta; 17 import godot.core; 18 import godot.c; 19 import godot.d.bind; 20 import godot.d.reference; 21 import godot.object; 22 import godot.classdb; 23 import godot.reference; 24 /** 25 Type used to handle the filesystem. 26 27 Directory type. It is used to manage directories and their content (not restricted to the project folder). 28 Here is an example on how to iterate through the files of a directory: 29 30 31 func dir_contents(path): 32 var dir = Directory.new() 33 if dir.open(path) == OK: 34 dir.list_dir_begin() 35 var file_name = dir.get_next() 36 while (file_name != ""): 37 if dir.current_is_dir(): 38 print("Found directory: " + file_name) 39 else: 40 print("Found file: " + file_name) 41 file_name = dir.get_next() 42 else: 43 print("An error occurred when trying to access the path.") 44 45 46 */ 47 @GodotBaseClass struct Directory 48 { 49 enum string _GODOT_internal_name = "_Directory"; 50 public: 51 @nogc nothrow: 52 union { godot_object _godot_object; Reference _GODOT_base; } 53 alias _GODOT_base this; 54 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 55 package(godot) __gshared bool _classBindingInitialized = false; 56 package(godot) static struct _classBinding 57 { 58 __gshared: 59 @GodotName("open") GodotMethod!(GodotError, String) open; 60 @GodotName("list_dir_begin") GodotMethod!(GodotError, bool, bool) listDirBegin; 61 @GodotName("get_next") GodotMethod!(String) getNext; 62 @GodotName("current_is_dir") GodotMethod!(bool) currentIsDir; 63 @GodotName("list_dir_end") GodotMethod!(void) listDirEnd; 64 @GodotName("get_drive_count") GodotMethod!(long) getDriveCount; 65 @GodotName("get_drive") GodotMethod!(String, long) getDrive; 66 @GodotName("get_current_drive") GodotMethod!(long) getCurrentDrive; 67 @GodotName("change_dir") GodotMethod!(GodotError, String) changeDir; 68 @GodotName("get_current_dir") GodotMethod!(String) getCurrentDir; 69 @GodotName("make_dir") GodotMethod!(GodotError, String) makeDir; 70 @GodotName("make_dir_recursive") GodotMethod!(GodotError, String) makeDirRecursive; 71 @GodotName("file_exists") GodotMethod!(bool, String) fileExists; 72 @GodotName("dir_exists") GodotMethod!(bool, String) dirExists; 73 @GodotName("get_space_left") GodotMethod!(long) getSpaceLeft; 74 @GodotName("copy") GodotMethod!(GodotError, String, String) copy; 75 @GodotName("rename") GodotMethod!(GodotError, String, String) rename; 76 @GodotName("remove") GodotMethod!(GodotError, String) remove; 77 } 78 bool opEquals(in Directory other) const { return _godot_object.ptr is other._godot_object.ptr; } 79 Directory opAssign(T : typeof(null))(T n) { _godot_object.ptr = null; } 80 bool opEquals(typeof(null) n) const { return _godot_object.ptr is null; } 81 mixin baseCasts; 82 static Directory _new() 83 { 84 static godot_class_constructor constructor; 85 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("_Directory"); 86 if(constructor is null) return typeof(this).init; 87 return cast(Directory)(constructor()); 88 } 89 @disable new(size_t s); 90 /** 91 Open an existing directory of the filesystem. The $(I path) argument can be within the project tree (`res://folder`), the user directory (`user://folder`) or an absolute path of the user filesystem (e.g. `/tmp/folder` or `C:\tmp\folder`). 92 The method returns one of the error code constants defined in $(D @GlobalScope) (OK or ERR_*). 93 */ 94 GodotError open(StringArg0)(in StringArg0 path) 95 { 96 checkClassBinding!(typeof(this))(); 97 return ptrcall!(GodotError)(_classBinding.open, _godot_object, path); 98 } 99 /** 100 Initialise the stream used to list all files and directories using the $(D getNext) function, closing the current opened stream if needed. Once the stream has been processed, it should typically be closed with $(D listDirEnd). 101 If you pass `skip_navigational`, then `.` and `..` would be filtered out. 102 If you pass `skip_hidden`, then hidden files would be filtered out. 103 */ 104 GodotError listDirBegin(in bool skip_navigational = false, in bool skip_hidden = false) 105 { 106 checkClassBinding!(typeof(this))(); 107 return ptrcall!(GodotError)(_classBinding.listDirBegin, _godot_object, skip_navigational, skip_hidden); 108 } 109 /** 110 Return the next element (file or directory) in the current directory (including `.` and `..`, unless `skip_navigational` was given to $(D listDirBegin)). 111 The name of the file or directory is returned (and not its full path). Once the stream has been fully processed, the method returns an empty String and closes the stream automatically (i.e. $(D listDirEnd) would not be mandatory in such a case). 112 */ 113 String getNext() 114 { 115 checkClassBinding!(typeof(this))(); 116 return ptrcall!(String)(_classBinding.getNext, _godot_object); 117 } 118 /** 119 Return whether the current item processed with the last $(D getNext) call is a directory (`.` and `..` are considered directories). 120 */ 121 bool currentIsDir() const 122 { 123 checkClassBinding!(typeof(this))(); 124 return ptrcall!(bool)(_classBinding.currentIsDir, _godot_object); 125 } 126 /** 127 Close the current stream opened with $(D listDirBegin) (whether it has been fully processed with $(D getNext) or not does not matter). 128 */ 129 void listDirEnd() 130 { 131 checkClassBinding!(typeof(this))(); 132 ptrcall!(void)(_classBinding.listDirEnd, _godot_object); 133 } 134 /** 135 On Windows, return the number of drives (partitions) mounted on the current filesystem. On other platforms, the method returns 0. 136 */ 137 long getDriveCount() 138 { 139 checkClassBinding!(typeof(this))(); 140 return ptrcall!(long)(_classBinding.getDriveCount, _godot_object); 141 } 142 /** 143 On Windows, return the name of the drive (partition) passed as an argument (e.g. `C:`). On other platforms, or if the requested drive does not existed, the method returns an empty String. 144 */ 145 String getDrive(in long idx) 146 { 147 checkClassBinding!(typeof(this))(); 148 return ptrcall!(String)(_classBinding.getDrive, _godot_object, idx); 149 } 150 /** 151 Returns the currently opened directory's drive index. See $(D getDrive) to convert returned index to the name of the drive. 152 */ 153 long getCurrentDrive() 154 { 155 checkClassBinding!(typeof(this))(); 156 return ptrcall!(long)(_classBinding.getCurrentDrive, _godot_object); 157 } 158 /** 159 Change the currently opened directory to the one passed as an argument. The argument can be relative to the current directory (e.g. `newdir` or `../newdir`), or an absolute path (e.g. `/tmp/newdir` or `res://somedir/newdir`). 160 The method returns one of the error code constants defined in $(D @GlobalScope) (OK or ERR_*). 161 */ 162 GodotError changeDir(StringArg0)(in StringArg0 todir) 163 { 164 checkClassBinding!(typeof(this))(); 165 return ptrcall!(GodotError)(_classBinding.changeDir, _godot_object, todir); 166 } 167 /** 168 Return the absolute path to the currently opened directory (e.g. `res://folder` or `C:\tmp\folder`). 169 */ 170 String getCurrentDir() 171 { 172 checkClassBinding!(typeof(this))(); 173 return ptrcall!(String)(_classBinding.getCurrentDir, _godot_object); 174 } 175 /** 176 Create a directory. The argument can be relative to the current directory, or an absolute path. The target directory should be placed in an already existing directory (to create the full path recursively, see $(D makeDirRecursive)). 177 The method returns one of the error code constants defined in $(D @GlobalScope) (OK, FAILED or ERR_*). 178 */ 179 GodotError makeDir(StringArg0)(in StringArg0 path) 180 { 181 checkClassBinding!(typeof(this))(); 182 return ptrcall!(GodotError)(_classBinding.makeDir, _godot_object, path); 183 } 184 /** 185 Create a target directory and all necessary intermediate directories in its path, by calling $(D makeDir) recursively. The argument can be relative to the current directory, or an absolute path. 186 Return one of the error code constants defined in $(D @GlobalScope) (OK, FAILED or ERR_*). 187 */ 188 GodotError makeDirRecursive(StringArg0)(in StringArg0 path) 189 { 190 checkClassBinding!(typeof(this))(); 191 return ptrcall!(GodotError)(_classBinding.makeDirRecursive, _godot_object, path); 192 } 193 /** 194 Return whether the target file exists. The argument can be relative to the current directory, or an absolute path. 195 */ 196 bool fileExists(StringArg0)(in StringArg0 path) 197 { 198 checkClassBinding!(typeof(this))(); 199 return ptrcall!(bool)(_classBinding.fileExists, _godot_object, path); 200 } 201 /** 202 Return whether the target directory exists. The argument can be relative to the current directory, or an absolute path. 203 */ 204 bool dirExists(StringArg0)(in StringArg0 path) 205 { 206 checkClassBinding!(typeof(this))(); 207 return ptrcall!(bool)(_classBinding.dirExists, _godot_object, path); 208 } 209 /** 210 On Unix desktop systems, return the available space on the current directory's disk. On other platforms, this information is not available and the method returns 0 or -1. 211 */ 212 long getSpaceLeft() 213 { 214 checkClassBinding!(typeof(this))(); 215 return ptrcall!(long)(_classBinding.getSpaceLeft, _godot_object); 216 } 217 /** 218 Copy the $(I from) file to the $(I to) destination. Both arguments should be paths to files, either relative or absolute. If the destination file exists and is not access-protected, it will be overwritten. 219 Returns one of the error code constants defined in $(D @GlobalScope) (OK, FAILED or ERR_*). 220 */ 221 GodotError copy(StringArg0, StringArg1)(in StringArg0 from, in StringArg1 to) 222 { 223 checkClassBinding!(typeof(this))(); 224 return ptrcall!(GodotError)(_classBinding.copy, _godot_object, from, to); 225 } 226 /** 227 Rename (move) the $(I from) file to the $(I to) destination. Both arguments should be paths to files, either relative or absolute. If the destination file exists and is not access-protected, it will be overwritten. 228 Return one of the error code constants defined in $(D @GlobalScope) (OK or FAILED). 229 */ 230 GodotError rename(StringArg0, StringArg1)(in StringArg0 from, in StringArg1 to) 231 { 232 checkClassBinding!(typeof(this))(); 233 return ptrcall!(GodotError)(_classBinding.rename, _godot_object, from, to); 234 } 235 /** 236 Delete the target file or an empty directory. The argument can be relative to the current directory, or an absolute path. If the target directory is not empty, the operation will fail. 237 Return one of the error code constants defined in $(D @GlobalScope) (OK or FAILED). 238 */ 239 GodotError remove(StringArg0)(in StringArg0 path) 240 { 241 checkClassBinding!(typeof(this))(); 242 return ptrcall!(GodotError)(_classBinding.remove, _godot_object, path); 243 } 244 }