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.traits; 17 import godot.core; 18 import godot.c; 19 import godot.d.bind; 20 import godot.d.reference; 21 import godot.globalenums; 22 import godot.object; 23 import godot.classdb; 24 import godot.reference; 25 /** 26 Type used to handle the filesystem. 27 28 Directory type. It is used to manage directories and their content (not restricted to the project folder). 29 When creating a new $(D Directory), its default opened directory will be `res://`. This may change in the future, so it is advised to always use $(D open) to initialize your $(D Directory) where you want to operate, with explicit error checking. 30 $(B Note:) Many resources types are imported (e.g. textures or sound files), and their source asset will not be included in the exported game, as only the imported version is used. Use $(D ResourceLoader) to access imported resources. 31 Here is an example on how to iterate through the files of a directory: 32 33 34 func dir_contents(path): 35 var dir = Directory.new() 36 if dir.open(path) == OK: 37 dir.list_dir_begin() 38 var file_name = dir.get_next() 39 while file_name != "": 40 if dir.current_is_dir(): 41 print("Found directory: " + file_name) 42 else: 43 print("Found file: " + file_name) 44 file_name = dir.get_next() 45 else: 46 print("An error occurred when trying to access the path.") 47 48 49 */ 50 @GodotBaseClass struct Directory 51 { 52 package(godot) enum string _GODOT_internal_name = "_Directory"; 53 public: 54 @nogc nothrow: 55 union { /** */ godot_object _godot_object; /** */ Reference _GODOT_base; } 56 alias _GODOT_base this; 57 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 58 package(godot) __gshared bool _classBindingInitialized = false; 59 package(godot) static struct GDNativeClassBinding 60 { 61 __gshared: 62 @GodotName("change_dir") GodotMethod!(GodotError, String) changeDir; 63 @GodotName("copy") GodotMethod!(GodotError, String, String) copy; 64 @GodotName("current_is_dir") GodotMethod!(bool) currentIsDir; 65 @GodotName("dir_exists") GodotMethod!(bool, String) dirExists; 66 @GodotName("file_exists") GodotMethod!(bool, String) fileExists; 67 @GodotName("get_current_dir") GodotMethod!(String) getCurrentDir; 68 @GodotName("get_current_drive") GodotMethod!(long) getCurrentDrive; 69 @GodotName("get_drive") GodotMethod!(String, long) getDrive; 70 @GodotName("get_drive_count") GodotMethod!(long) getDriveCount; 71 @GodotName("get_next") GodotMethod!(String) getNext; 72 @GodotName("get_space_left") GodotMethod!(long) getSpaceLeft; 73 @GodotName("list_dir_begin") GodotMethod!(GodotError, bool, bool) listDirBegin; 74 @GodotName("list_dir_end") GodotMethod!(void) listDirEnd; 75 @GodotName("make_dir") GodotMethod!(GodotError, String) makeDir; 76 @GodotName("make_dir_recursive") GodotMethod!(GodotError, String) makeDirRecursive; 77 @GodotName("open") GodotMethod!(GodotError, String) open; 78 @GodotName("remove") GodotMethod!(GodotError, String) remove; 79 @GodotName("rename") GodotMethod!(GodotError, String, String) rename; 80 } 81 /// 82 pragma(inline, true) bool opEquals(in Directory other) const 83 { return _godot_object.ptr is other._godot_object.ptr; } 84 /// 85 pragma(inline, true) typeof(null) opAssign(typeof(null) n) 86 { _godot_object.ptr = n; return null; } 87 /// 88 pragma(inline, true) bool opEquals(typeof(null) n) const 89 { return _godot_object.ptr is n; } 90 /// 91 size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; } 92 mixin baseCasts; 93 /// Construct a new instance of Directory. 94 /// Note: use `memnew!Directory` instead. 95 static Directory _new() 96 { 97 static godot_class_constructor constructor; 98 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("_Directory"); 99 if(constructor is null) return typeof(this).init; 100 return cast(Directory)(constructor()); 101 } 102 @disable new(size_t s); 103 /** 104 Changes 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`). 105 Returns one of the $(D error) code constants (`OK` on success). 106 */ 107 GodotError changeDir(in String todir) 108 { 109 checkClassBinding!(typeof(this))(); 110 return ptrcall!(GodotError)(GDNativeClassBinding.changeDir, _godot_object, todir); 111 } 112 /** 113 Copies the `from` file to the `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. 114 Returns one of the $(D error) code constants (`OK` on success). 115 */ 116 GodotError copy(in String from, in String to) 117 { 118 checkClassBinding!(typeof(this))(); 119 return ptrcall!(GodotError)(GDNativeClassBinding.copy, _godot_object, from, to); 120 } 121 /** 122 Returns whether the current item processed with the last $(D getNext) call is a directory (`.` and `..` are considered directories). 123 */ 124 bool currentIsDir() const 125 { 126 checkClassBinding!(typeof(this))(); 127 return ptrcall!(bool)(GDNativeClassBinding.currentIsDir, _godot_object); 128 } 129 /** 130 Returns whether the target directory exists. The argument can be relative to the current directory, or an absolute path. 131 */ 132 bool dirExists(in String path) 133 { 134 checkClassBinding!(typeof(this))(); 135 return ptrcall!(bool)(GDNativeClassBinding.dirExists, _godot_object, path); 136 } 137 /** 138 Returns whether the target file exists. The argument can be relative to the current directory, or an absolute path. 139 */ 140 bool fileExists(in String path) 141 { 142 checkClassBinding!(typeof(this))(); 143 return ptrcall!(bool)(GDNativeClassBinding.fileExists, _godot_object, path); 144 } 145 /** 146 Returns the absolute path to the currently opened directory (e.g. `res://folder` or `C:\tmp\folder`). 147 */ 148 String getCurrentDir() 149 { 150 checkClassBinding!(typeof(this))(); 151 return ptrcall!(String)(GDNativeClassBinding.getCurrentDir, _godot_object); 152 } 153 /** 154 Returns the currently opened directory's drive index. See $(D getDrive) to convert returned index to the name of the drive. 155 */ 156 long getCurrentDrive() 157 { 158 checkClassBinding!(typeof(this))(); 159 return ptrcall!(long)(GDNativeClassBinding.getCurrentDrive, _godot_object); 160 } 161 /** 162 On Windows, returns the name of the drive (partition) passed as an argument (e.g. `C:`). On other platforms, or if the requested drive does not exist, the method returns an empty String. 163 */ 164 String getDrive(in long idx) 165 { 166 checkClassBinding!(typeof(this))(); 167 return ptrcall!(String)(GDNativeClassBinding.getDrive, _godot_object, idx); 168 } 169 /** 170 On Windows, returns the number of drives (partitions) mounted on the current filesystem. On other platforms, the method returns 0. 171 */ 172 long getDriveCount() 173 { 174 checkClassBinding!(typeof(this))(); 175 return ptrcall!(long)(GDNativeClassBinding.getDriveCount, _godot_object); 176 } 177 /** 178 Returns the next element (file or directory) in the current directory (including `.` and `..`, unless `skip_navigational` was given to $(D listDirBegin)). 179 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). 180 */ 181 String getNext() 182 { 183 checkClassBinding!(typeof(this))(); 184 return ptrcall!(String)(GDNativeClassBinding.getNext, _godot_object); 185 } 186 /** 187 On UNIX desktop systems, returns the available space on the current directory's disk. On other platforms, this information is not available and the method returns 0 or -1. 188 */ 189 long getSpaceLeft() 190 { 191 checkClassBinding!(typeof(this))(); 192 return ptrcall!(long)(GDNativeClassBinding.getSpaceLeft, _godot_object); 193 } 194 /** 195 Initializes the stream used to list all files and directories using the $(D getNext) function, closing the currently opened stream if needed. Once the stream has been processed, it should typically be closed with $(D listDirEnd). 196 If `skip_navigational` is `true`, `.` and `..` are filtered out. 197 If `skip_hidden` is `true`, hidden files are filtered out. 198 */ 199 GodotError listDirBegin(in bool skip_navigational = false, in bool skip_hidden = false) 200 { 201 checkClassBinding!(typeof(this))(); 202 return ptrcall!(GodotError)(GDNativeClassBinding.listDirBegin, _godot_object, skip_navigational, skip_hidden); 203 } 204 /** 205 Closes the current stream opened with $(D listDirBegin) (whether it has been fully processed with $(D getNext) does not matter). 206 */ 207 void listDirEnd() 208 { 209 checkClassBinding!(typeof(this))(); 210 ptrcall!(void)(GDNativeClassBinding.listDirEnd, _godot_object); 211 } 212 /** 213 Creates 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)). 214 Returns one of the $(D error) code constants (`OK` on success). 215 */ 216 GodotError makeDir(in String path) 217 { 218 checkClassBinding!(typeof(this))(); 219 return ptrcall!(GodotError)(GDNativeClassBinding.makeDir, _godot_object, path); 220 } 221 /** 222 Creates 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. 223 Returns one of the $(D error) code constants (`OK` on success). 224 */ 225 GodotError makeDirRecursive(in String path) 226 { 227 checkClassBinding!(typeof(this))(); 228 return ptrcall!(GodotError)(GDNativeClassBinding.makeDirRecursive, _godot_object, path); 229 } 230 /** 231 Opens an existing directory of the filesystem. The `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`). 232 Returns one of the $(D error) code constants (`OK` on success). 233 */ 234 GodotError open(in String path) 235 { 236 checkClassBinding!(typeof(this))(); 237 return ptrcall!(GodotError)(GDNativeClassBinding.open, _godot_object, path); 238 } 239 /** 240 Deletes 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. 241 Returns one of the $(D error) code constants (`OK` on success). 242 */ 243 GodotError remove(in String path) 244 { 245 checkClassBinding!(typeof(this))(); 246 return ptrcall!(GodotError)(GDNativeClassBinding.remove, _godot_object, path); 247 } 248 /** 249 Renames (move) the `from` file or directory to the `to` destination. Both arguments should be paths to files or directories, either relative or absolute. If the destination file or directory exists and is not access-protected, it will be overwritten. 250 Returns one of the $(D error) code constants (`OK` on success). 251 */ 252 GodotError rename(in String from, in String to) 253 { 254 checkClassBinding!(typeof(this))(); 255 return ptrcall!(GodotError)(GDNativeClassBinding.rename, _godot_object, from, to); 256 } 257 }