1 /**
2 Type to handle file reading and writing operations.
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.file;
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 to handle file reading and writing operations.
26 
27 File type. This is used to permanently store data into the user device's file system and to read from it. This can be used to store game save data or player configuration files, for example.
28 Here's a sample on how to write and read from a file:
29 
30 
31 func save(content):
32     var file = File.new()
33     file.open("user://save_game.dat", File.WRITE)
34     file.store_string(content)
35     file.close()
36 
37 func load():
38     var file = File.new()
39     file.open("user://save_game.dat", File.READ)
40     var content = file.get_as_text()
41     file.close()
42     return content
43 
44 
45 */
46 @GodotBaseClass struct File
47 {
48 	enum string _GODOT_internal_name = "_File";
49 public:
50 @nogc nothrow:
51 	union { godot_object _godot_object; Reference _GODOT_base; }
52 	alias _GODOT_base this;
53 	alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses);
54 	package(godot) __gshared bool _classBindingInitialized = false;
55 	package(godot) static struct _classBinding
56 	{
57 		__gshared:
58 		@GodotName("open_encrypted") GodotMethod!(GodotError, String, long, PoolByteArray) openEncrypted;
59 		@GodotName("open_encrypted_with_pass") GodotMethod!(GodotError, String, long, String) openEncryptedWithPass;
60 		@GodotName("open_compressed") GodotMethod!(GodotError, String, long, long) openCompressed;
61 		@GodotName("open") GodotMethod!(GodotError, String, long) open;
62 		@GodotName("close") GodotMethod!(void) close;
63 		@GodotName("get_path") GodotMethod!(String) getPath;
64 		@GodotName("get_path_absolute") GodotMethod!(String) getPathAbsolute;
65 		@GodotName("is_open") GodotMethod!(bool) isOpen;
66 		@GodotName("seek") GodotMethod!(void, long) seek;
67 		@GodotName("seek_end") GodotMethod!(void, long) seekEnd;
68 		@GodotName("get_position") GodotMethod!(long) getPosition;
69 		@GodotName("get_len") GodotMethod!(long) getLen;
70 		@GodotName("eof_reached") GodotMethod!(bool) eofReached;
71 		@GodotName("get_8") GodotMethod!(long) get8;
72 		@GodotName("get_16") GodotMethod!(long) get16;
73 		@GodotName("get_32") GodotMethod!(long) get32;
74 		@GodotName("get_64") GodotMethod!(long) get64;
75 		@GodotName("get_float") GodotMethod!(double) getFloat;
76 		@GodotName("get_double") GodotMethod!(double) getDouble;
77 		@GodotName("get_real") GodotMethod!(double) getReal;
78 		@GodotName("get_buffer") GodotMethod!(PoolByteArray, long) getBuffer;
79 		@GodotName("get_line") GodotMethod!(String) getLine;
80 		@GodotName("get_as_text") GodotMethod!(String) getAsText;
81 		@GodotName("get_md5") GodotMethod!(String, String) getMd5;
82 		@GodotName("get_sha256") GodotMethod!(String, String) getSha256;
83 		@GodotName("get_endian_swap") GodotMethod!(bool) getEndianSwap;
84 		@GodotName("set_endian_swap") GodotMethod!(void, bool) setEndianSwap;
85 		@GodotName("get_error") GodotMethod!(GodotError) getError;
86 		@GodotName("get_var") GodotMethod!(Variant) getVar;
87 		@GodotName("get_csv_line") GodotMethod!(PoolStringArray, String) getCsvLine;
88 		@GodotName("store_8") GodotMethod!(void, long) store8;
89 		@GodotName("store_16") GodotMethod!(void, long) store16;
90 		@GodotName("store_32") GodotMethod!(void, long) store32;
91 		@GodotName("store_64") GodotMethod!(void, long) store64;
92 		@GodotName("store_float") GodotMethod!(void, double) storeFloat;
93 		@GodotName("store_double") GodotMethod!(void, double) storeDouble;
94 		@GodotName("store_real") GodotMethod!(void, double) storeReal;
95 		@GodotName("store_buffer") GodotMethod!(void, PoolByteArray) storeBuffer;
96 		@GodotName("store_line") GodotMethod!(void, String) storeLine;
97 		@GodotName("store_string") GodotMethod!(void, String) storeString;
98 		@GodotName("store_var") GodotMethod!(void, Variant) storeVar;
99 		@GodotName("store_pascal_string") GodotMethod!(void, String) storePascalString;
100 		@GodotName("get_pascal_string") GodotMethod!(String) getPascalString;
101 		@GodotName("file_exists") GodotMethod!(bool, String) fileExists;
102 		@GodotName("get_modified_time") GodotMethod!(long, String) getModifiedTime;
103 	}
104 	bool opEquals(in File other) const { return _godot_object.ptr is other._godot_object.ptr; }
105 	File opAssign(T : typeof(null))(T n) { _godot_object.ptr = null; }
106 	bool opEquals(typeof(null) n) const { return _godot_object.ptr is null; }
107 	mixin baseCasts;
108 	static File _new()
109 	{
110 		static godot_class_constructor constructor;
111 		if(constructor is null) constructor = _godot_api.godot_get_class_constructor("_File");
112 		if(constructor is null) return typeof(this).init;
113 		return cast(File)(constructor());
114 	}
115 	@disable new(size_t s);
116 	/// 
117 	enum CompressionMode : int
118 	{
119 		/**
120 		Uses the FastLZ compression method.
121 		*/
122 		compressionFastlz = 0,
123 		/**
124 		Uses the Deflate compression method.
125 		*/
126 		compressionDeflate = 1,
127 		/**
128 		Uses the Zstd compression method.
129 		*/
130 		compressionZstd = 2,
131 		/**
132 		Uses the gzip compression method.
133 		*/
134 		compressionGzip = 3,
135 	}
136 	/// 
137 	enum ModeFlags : int
138 	{
139 		/**
140 		Opens the file for read operations.
141 		*/
142 		read = 1,
143 		/**
144 		Opens the file for write operations. Create it if the file does not exist and truncate if it exists.
145 		*/
146 		write = 2,
147 		/**
148 		Opens the file for read and write operations. Does not truncate the file.
149 		*/
150 		readWrite = 3,
151 		/**
152 		Opens the file for read and write operations. Create it if the file does not exist and truncate if it exists.
153 		*/
154 		writeRead = 7,
155 	}
156 	/// 
157 	enum Constants : int
158 	{
159 		compressionFastlz = 0,
160 		read = 1,
161 		compressionDeflate = 1,
162 		compressionZstd = 2,
163 		write = 2,
164 		readWrite = 3,
165 		compressionGzip = 3,
166 		writeRead = 7,
167 	}
168 	/**
169 	Opens an encrypted file in write or read mode. You need to pass a binary key to encrypt/decrypt it.
170 	*/
171 	GodotError openEncrypted(StringArg0)(in StringArg0 path, in long mode_flags, in PoolByteArray key)
172 	{
173 		checkClassBinding!(typeof(this))();
174 		return ptrcall!(GodotError)(_classBinding.openEncrypted, _godot_object, path, mode_flags, key);
175 	}
176 	/**
177 	Opens an encrypted file in write or read mode. You need to pass a password to encrypt/decrypt it.
178 	*/
179 	GodotError openEncryptedWithPass(StringArg0, StringArg2)(in StringArg0 path, in long mode_flags, in StringArg2 pass)
180 	{
181 		checkClassBinding!(typeof(this))();
182 		return ptrcall!(GodotError)(_classBinding.openEncryptedWithPass, _godot_object, path, mode_flags, pass);
183 	}
184 	/**
185 	Opens a compressed file for reading or writing. Use COMPRESSION_* constants to set `compression_mode`.
186 	*/
187 	GodotError openCompressed(StringArg0)(in StringArg0 path, in long mode_flags, in long compression_mode = 0)
188 	{
189 		checkClassBinding!(typeof(this))();
190 		return ptrcall!(GodotError)(_classBinding.openCompressed, _godot_object, path, mode_flags, compression_mode);
191 	}
192 	/**
193 	Opens the file for writing or reading, depending on the flags.
194 	*/
195 	GodotError open(StringArg0)(in StringArg0 path, in long flags)
196 	{
197 		checkClassBinding!(typeof(this))();
198 		return ptrcall!(GodotError)(_classBinding.open, _godot_object, path, flags);
199 	}
200 	/**
201 	Closes the currently opened file.
202 	*/
203 	void close()
204 	{
205 		checkClassBinding!(typeof(this))();
206 		ptrcall!(void)(_classBinding.close, _godot_object);
207 	}
208 	/**
209 	Returns the path as a $(D String) for the current open file.
210 	*/
211 	String getPath() const
212 	{
213 		checkClassBinding!(typeof(this))();
214 		return ptrcall!(String)(_classBinding.getPath, _godot_object);
215 	}
216 	/**
217 	Returns the absolute path as a $(D String) for the current open file.
218 	*/
219 	String getPathAbsolute() const
220 	{
221 		checkClassBinding!(typeof(this))();
222 		return ptrcall!(String)(_classBinding.getPathAbsolute, _godot_object);
223 	}
224 	/**
225 	Returns `true` if the file is currently opened.
226 	*/
227 	bool isOpen() const
228 	{
229 		checkClassBinding!(typeof(this))();
230 		return ptrcall!(bool)(_classBinding.isOpen, _godot_object);
231 	}
232 	/**
233 	Change the file reading/writing cursor to the specified position (in bytes from the beginning of the file).
234 	*/
235 	void seek(in long position)
236 	{
237 		checkClassBinding!(typeof(this))();
238 		ptrcall!(void)(_classBinding.seek, _godot_object, position);
239 	}
240 	/**
241 	Changes the file reading/writing cursor to the specified position (in bytes from the end of the file). Note that this is an offset, so you should use negative numbers or the cursor will be at the end of the file.
242 	*/
243 	void seekEnd(in long position = 0)
244 	{
245 		checkClassBinding!(typeof(this))();
246 		ptrcall!(void)(_classBinding.seekEnd, _godot_object, position);
247 	}
248 	/**
249 	Returns the file cursor's position.
250 	*/
251 	long getPosition() const
252 	{
253 		checkClassBinding!(typeof(this))();
254 		return ptrcall!(long)(_classBinding.getPosition, _godot_object);
255 	}
256 	/**
257 	Returns the size of the file in bytes.
258 	*/
259 	long getLen() const
260 	{
261 		checkClassBinding!(typeof(this))();
262 		return ptrcall!(long)(_classBinding.getLen, _godot_object);
263 	}
264 	/**
265 	Returns `true` if the file cursor has reached the end of the file.
266 	*/
267 	bool eofReached() const
268 	{
269 		checkClassBinding!(typeof(this))();
270 		return ptrcall!(bool)(_classBinding.eofReached, _godot_object);
271 	}
272 	/**
273 	Returns the next 8 bits from the file as an integer.
274 	*/
275 	long get8() const
276 	{
277 		checkClassBinding!(typeof(this))();
278 		return ptrcall!(long)(_classBinding.get8, _godot_object);
279 	}
280 	/**
281 	Returns the next 16 bits from the file as an integer.
282 	*/
283 	long get16() const
284 	{
285 		checkClassBinding!(typeof(this))();
286 		return ptrcall!(long)(_classBinding.get16, _godot_object);
287 	}
288 	/**
289 	Returns the next 32 bits from the file as an integer.
290 	*/
291 	long get32() const
292 	{
293 		checkClassBinding!(typeof(this))();
294 		return ptrcall!(long)(_classBinding.get32, _godot_object);
295 	}
296 	/**
297 	Returns the next 64 bits from the file as an integer.
298 	*/
299 	long get64() const
300 	{
301 		checkClassBinding!(typeof(this))();
302 		return ptrcall!(long)(_classBinding.get64, _godot_object);
303 	}
304 	/**
305 	Returns the next 32 bits from the file as a floating point number.
306 	*/
307 	double getFloat() const
308 	{
309 		checkClassBinding!(typeof(this))();
310 		return ptrcall!(double)(_classBinding.getFloat, _godot_object);
311 	}
312 	/**
313 	Returns the next 64 bits from the file as a floating point number.
314 	*/
315 	double getDouble() const
316 	{
317 		checkClassBinding!(typeof(this))();
318 		return ptrcall!(double)(_classBinding.getDouble, _godot_object);
319 	}
320 	/**
321 	Returns the next bits from the file as a floating point number.
322 	*/
323 	double getReal() const
324 	{
325 		checkClassBinding!(typeof(this))();
326 		return ptrcall!(double)(_classBinding.getReal, _godot_object);
327 	}
328 	/**
329 	Returns next `len` bytes of the file as a $(D PoolByteArray).
330 	*/
331 	PoolByteArray getBuffer(in long len) const
332 	{
333 		checkClassBinding!(typeof(this))();
334 		return ptrcall!(PoolByteArray)(_classBinding.getBuffer, _godot_object, len);
335 	}
336 	/**
337 	Returns the next line of the file as a $(D String).
338 	*/
339 	String getLine() const
340 	{
341 		checkClassBinding!(typeof(this))();
342 		return ptrcall!(String)(_classBinding.getLine, _godot_object);
343 	}
344 	/**
345 	Returns the whole file as a $(D String).
346 	*/
347 	String getAsText() const
348 	{
349 		checkClassBinding!(typeof(this))();
350 		return ptrcall!(String)(_classBinding.getAsText, _godot_object);
351 	}
352 	/**
353 	Returns an MD5 String representing the file at the given path or an empty $(D String) on failure.
354 	*/
355 	String getMd5(StringArg0)(in StringArg0 path) const
356 	{
357 		checkClassBinding!(typeof(this))();
358 		return ptrcall!(String)(_classBinding.getMd5, _godot_object, path);
359 	}
360 	/**
361 	Returns a SHA-256 $(D String) representing the file at the given path or an empty $(D String) on failure.
362 	*/
363 	String getSha256(StringArg0)(in StringArg0 path) const
364 	{
365 		checkClassBinding!(typeof(this))();
366 		return ptrcall!(String)(_classBinding.getSha256, _godot_object, path);
367 	}
368 	/**
369 	
370 	*/
371 	bool getEndianSwap()
372 	{
373 		checkClassBinding!(typeof(this))();
374 		return ptrcall!(bool)(_classBinding.getEndianSwap, _godot_object);
375 	}
376 	/**
377 	
378 	*/
379 	void setEndianSwap(in bool enable)
380 	{
381 		checkClassBinding!(typeof(this))();
382 		ptrcall!(void)(_classBinding.setEndianSwap, _godot_object, enable);
383 	}
384 	/**
385 	Returns the last error that happened when trying to perform operations. Compare with the `ERR_FILE_*` constants from $(D @GlobalScope).
386 	*/
387 	GodotError getError() const
388 	{
389 		checkClassBinding!(typeof(this))();
390 		return ptrcall!(GodotError)(_classBinding.getError, _godot_object);
391 	}
392 	/**
393 	Returns the next $(D Variant) value from the file.
394 	*/
395 	Variant getVar() const
396 	{
397 		checkClassBinding!(typeof(this))();
398 		return ptrcall!(Variant)(_classBinding.getVar, _godot_object);
399 	}
400 	/**
401 	Returns the next value of the file in CSV (Comma Separated Values) format. You can pass a different delimiter to use other than the default "," (comma).
402 	*/
403 	PoolStringArray getCsvLine(StringArg0)(in StringArg0 delim = ",") const
404 	{
405 		checkClassBinding!(typeof(this))();
406 		return ptrcall!(PoolStringArray)(_classBinding.getCsvLine, _godot_object, delim);
407 	}
408 	/**
409 	Stores an integer as 8 bits in the file.
410 	*/
411 	void store8(in long value)
412 	{
413 		checkClassBinding!(typeof(this))();
414 		ptrcall!(void)(_classBinding.store8, _godot_object, value);
415 	}
416 	/**
417 	Stores an integer as 16 bits in the file.
418 	*/
419 	void store16(in long value)
420 	{
421 		checkClassBinding!(typeof(this))();
422 		ptrcall!(void)(_classBinding.store16, _godot_object, value);
423 	}
424 	/**
425 	Stores an integer as 32 bits in the file.
426 	*/
427 	void store32(in long value)
428 	{
429 		checkClassBinding!(typeof(this))();
430 		ptrcall!(void)(_classBinding.store32, _godot_object, value);
431 	}
432 	/**
433 	Stores an integer as 64 bits in the file.
434 	*/
435 	void store64(in long value)
436 	{
437 		checkClassBinding!(typeof(this))();
438 		ptrcall!(void)(_classBinding.store64, _godot_object, value);
439 	}
440 	/**
441 	Stores a floating point number as 32 bits in the file.
442 	*/
443 	void storeFloat(in double value)
444 	{
445 		checkClassBinding!(typeof(this))();
446 		ptrcall!(void)(_classBinding.storeFloat, _godot_object, value);
447 	}
448 	/**
449 	Stores a floating point number as 64 bits in the file.
450 	*/
451 	void storeDouble(in double value)
452 	{
453 		checkClassBinding!(typeof(this))();
454 		ptrcall!(void)(_classBinding.storeDouble, _godot_object, value);
455 	}
456 	/**
457 	Stores a floating point number in the file.
458 	*/
459 	void storeReal(in double value)
460 	{
461 		checkClassBinding!(typeof(this))();
462 		ptrcall!(void)(_classBinding.storeReal, _godot_object, value);
463 	}
464 	/**
465 	Stores the given array of bytes in the file.
466 	*/
467 	void storeBuffer(in PoolByteArray buffer)
468 	{
469 		checkClassBinding!(typeof(this))();
470 		ptrcall!(void)(_classBinding.storeBuffer, _godot_object, buffer);
471 	}
472 	/**
473 	Stores the given $(D String) as a line in the file.
474 	*/
475 	void storeLine(StringArg0)(in StringArg0 line)
476 	{
477 		checkClassBinding!(typeof(this))();
478 		ptrcall!(void)(_classBinding.storeLine, _godot_object, line);
479 	}
480 	/**
481 	Stores the given $(D String) in the file.
482 	*/
483 	void storeString(StringArg0)(in StringArg0 string)
484 	{
485 		checkClassBinding!(typeof(this))();
486 		ptrcall!(void)(_classBinding.storeString, _godot_object, string);
487 	}
488 	/**
489 	Stores any Variant value in the file.
490 	*/
491 	void storeVar(VariantArg0)(in VariantArg0 value)
492 	{
493 		checkClassBinding!(typeof(this))();
494 		ptrcall!(void)(_classBinding.storeVar, _godot_object, value);
495 	}
496 	/**
497 	Stores the given $(D String) as a line in the file in Pascal format (i.e. also store the length of the string).
498 	*/
499 	void storePascalString(StringArg0)(in StringArg0 string)
500 	{
501 		checkClassBinding!(typeof(this))();
502 		ptrcall!(void)(_classBinding.storePascalString, _godot_object, string);
503 	}
504 	/**
505 	Returns a $(D String) saved in Pascal format from the file.
506 	*/
507 	String getPascalString()
508 	{
509 		checkClassBinding!(typeof(this))();
510 		return ptrcall!(String)(_classBinding.getPascalString, _godot_object);
511 	}
512 	/**
513 	Returns `true` if the file exists in the given path.
514 	*/
515 	bool fileExists(StringArg0)(in StringArg0 path) const
516 	{
517 		checkClassBinding!(typeof(this))();
518 		return ptrcall!(bool)(_classBinding.fileExists, _godot_object, path);
519 	}
520 	/**
521 	Returns the last time the `file` was modified in unix timestamp format or returns a $(D String) "ERROR IN `file`". This unix timestamp can be converted to datetime by using $(D OS.getDatetimeFromUnixTime).
522 	*/
523 	long getModifiedTime(StringArg0)(in StringArg0 file) const
524 	{
525 		checkClassBinding!(typeof(this))();
526 		return ptrcall!(long)(_classBinding.getModifiedTime, _godot_object, file);
527 	}
528 	/**
529 	If `true` the file's endianness is swapped. Use this if you're dealing with files written in big endian machines.
530 	Note that this is about the file format, not CPU type. This is always reset to `false` whenever you open the file.
531 	*/
532 	@property bool endianSwap()
533 	{
534 		return getEndianSwap();
535 	}
536 	/// ditto
537 	@property void endianSwap(bool v)
538 	{
539 		setEndianSwap(v);
540 	}
541 }