Skip to content

Commit 8905d73

Browse files
committedDec 3, 2023
[FileAccess] Return error codes from store_* methods.
1 parent d76c1d0 commit 8905d73

25 files changed

+293
-133
lines changed
 

‎core/io/file_access.compat.inc

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**************************************************************************/
2+
/* file_access.compat.inc */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#ifndef DISABLE_DEPRECATED
32+
33+
void FileAccess::store_8_78289(uint8_t p_dest) {
34+
store_8(p_dest);
35+
}
36+
37+
void FileAccess::store_16_78289(uint16_t p_dest) {
38+
store_16(p_dest);
39+
}
40+
41+
void FileAccess::store_32_78289(uint32_t p_dest) {
42+
store_32(p_dest);
43+
}
44+
45+
void FileAccess::store_64_78289(uint64_t p_dest) {
46+
store_64(p_dest);
47+
}
48+
49+
void FileAccess::store_buffer_78289(const Vector<uint8_t> &p_buffer) {
50+
store_buffer(p_buffer);
51+
}
52+
53+
void FileAccess::store_var_78289(const Variant &p_var, bool p_full_objects) {
54+
store_var(p_var, p_full_objects);
55+
}
56+
57+
void FileAccess::store_float_78289(float p_dest) {
58+
store_float(p_dest);
59+
}
60+
61+
void FileAccess::store_double_78289(double p_dest) {
62+
store_double(p_dest);
63+
}
64+
65+
void FileAccess::store_real_78289(real_t p_real) {
66+
store_real(p_real);
67+
}
68+
69+
void FileAccess::store_string_78289(const String &p_string) {
70+
store_string(p_string);
71+
}
72+
73+
void FileAccess::store_line_78289(const String &p_line) {
74+
store_line(p_line);
75+
}
76+
77+
void FileAccess::store_csv_line_78289(const Vector<String> &p_values, const String &p_delim) {
78+
store_csv_line(p_values, p_delim);
79+
}
80+
81+
void FileAccess::store_pascal_string_78289(const String &p_string) {
82+
store_pascal_string(p_string);
83+
}
84+
85+
void FileAccess::_bind_compatibility_methods() {
86+
ClassDB::bind_compatibility_method(D_METHOD("store_8", "value"), &FileAccess::store_8_78289);
87+
ClassDB::bind_compatibility_method(D_METHOD("store_16", "value"), &FileAccess::store_16_78289);
88+
ClassDB::bind_compatibility_method(D_METHOD("store_32", "value"), &FileAccess::store_32_78289);
89+
ClassDB::bind_compatibility_method(D_METHOD("store_64", "value"), &FileAccess::store_64_78289);
90+
ClassDB::bind_compatibility_method(D_METHOD("store_float", "value"), &FileAccess::store_float_78289);
91+
ClassDB::bind_compatibility_method(D_METHOD("store_double", "value"), &FileAccess::store_double_78289);
92+
ClassDB::bind_compatibility_method(D_METHOD("store_real", "value"), &FileAccess::store_real_78289);
93+
ClassDB::bind_compatibility_method(D_METHOD("store_buffer", "buffer"), &FileAccess::store_buffer_78289);
94+
ClassDB::bind_compatibility_method(D_METHOD("store_line", "line"), &FileAccess::store_line_78289);
95+
ClassDB::bind_compatibility_method(D_METHOD("store_csv_line", "values", "delim"), &FileAccess::store_csv_line_78289, DEFVAL(","));
96+
ClassDB::bind_compatibility_method(D_METHOD("store_string", "string"), &FileAccess::store_string_78289);
97+
ClassDB::bind_compatibility_method(D_METHOD("store_var", "value", "full_objects"), &FileAccess::store_var_78289, DEFVAL(false));
98+
ClassDB::bind_compatibility_method(D_METHOD("store_pascal_string", "string"), &FileAccess::store_pascal_string_78289);
99+
}
100+
101+
#endif

‎core/io/file_access.cpp

+38-40
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
/**************************************************************************/
3030

3131
#include "file_access.h"
32+
#include "file_access.compat.inc"
3233

3334
#include "core/config/project_settings.h"
3435
#include "core/crypto/crypto_core.h"
@@ -509,7 +510,7 @@ String FileAccess::get_as_utf8_string(bool p_skip_cr) const {
509510
return s;
510511
}
511512

512-
void FileAccess::store_16(uint16_t p_dest) {
513+
bool FileAccess::store_16(uint16_t p_dest) {
513514
uint8_t a, b;
514515

515516
a = p_dest & 0xFF;
@@ -519,11 +520,10 @@ void FileAccess::store_16(uint16_t p_dest) {
519520
SWAP(a, b);
520521
}
521522

522-
store_8(a);
523-
store_8(b);
523+
return store_8(a) && store_8(b);
524524
}
525525

526-
void FileAccess::store_32(uint32_t p_dest) {
526+
bool FileAccess::store_32(uint32_t p_dest) {
527527
uint16_t a, b;
528528

529529
a = p_dest & 0xFFFF;
@@ -533,11 +533,10 @@ void FileAccess::store_32(uint32_t p_dest) {
533533
SWAP(a, b);
534534
}
535535

536-
store_16(a);
537-
store_16(b);
536+
return store_16(a) && store_16(b);
538537
}
539538

540-
void FileAccess::store_64(uint64_t p_dest) {
539+
bool FileAccess::store_64(uint64_t p_dest) {
541540
uint32_t a, b;
542541

543542
a = p_dest & 0xFFFFFFFF;
@@ -547,28 +546,27 @@ void FileAccess::store_64(uint64_t p_dest) {
547546
SWAP(a, b);
548547
}
549548

550-
store_32(a);
551-
store_32(b);
549+
return store_32(a) && store_32(b);
552550
}
553551

554-
void FileAccess::store_real(real_t p_real) {
552+
bool FileAccess::store_real(real_t p_real) {
555553
if constexpr (sizeof(real_t) == 4) {
556-
store_float(p_real);
554+
return store_float(p_real);
557555
} else {
558-
store_double(p_real);
556+
return store_double(p_real);
559557
}
560558
}
561559

562-
void FileAccess::store_float(float p_dest) {
560+
bool FileAccess::store_float(float p_dest) {
563561
MarshallFloat m;
564562
m.f = p_dest;
565-
store_32(m.i);
563+
return store_32(m.i);
566564
}
567565

568-
void FileAccess::store_double(double p_dest) {
566+
bool FileAccess::store_double(double p_dest) {
569567
MarshallDouble m;
570568
m.d = p_dest;
571-
store_64(m.l);
569+
return store_64(m.l);
572570
}
573571

574572
uint64_t FileAccess::get_modified_time(const String &p_file) {
@@ -652,19 +650,18 @@ Error FileAccess::set_read_only_attribute(const String &p_file, bool p_ro) {
652650
return err;
653651
}
654652

655-
void FileAccess::store_string(const String &p_string) {
653+
bool FileAccess::store_string(const String &p_string) {
656654
if (p_string.length() == 0) {
657-
return;
655+
return true;
658656
}
659657

660658
CharString cs = p_string.utf8();
661-
store_buffer((uint8_t *)&cs[0], cs.length());
659+
return store_buffer((uint8_t *)&cs[0], cs.length());
662660
}
663661

664-
void FileAccess::store_pascal_string(const String &p_string) {
662+
bool FileAccess::store_pascal_string(const String &p_string) {
665663
CharString cs = p_string.utf8();
666-
store_32(cs.length());
667-
store_buffer((uint8_t *)&cs[0], cs.length());
664+
return store_32(cs.length()) && store_buffer((uint8_t *)&cs[0], cs.length());
668665
}
669666

670667
String FileAccess::get_pascal_string() {
@@ -679,13 +676,12 @@ String FileAccess::get_pascal_string() {
679676
return ret;
680677
}
681678

682-
void FileAccess::store_line(const String &p_line) {
683-
store_string(p_line);
684-
store_8('\n');
679+
bool FileAccess::store_line(const String &p_line) {
680+
return store_string(p_line) && store_8('\n');
685681
}
686682

687-
void FileAccess::store_csv_line(const Vector<String> &p_values, const String &p_delim) {
688-
ERR_FAIL_COND(p_delim.length() != 1);
683+
bool FileAccess::store_csv_line(const Vector<String> &p_values, const String &p_delim) {
684+
ERR_FAIL_COND_V(p_delim.length() != 1, false);
689685

690686
String line = "";
691687
int size = p_values.size();
@@ -702,41 +698,43 @@ void FileAccess::store_csv_line(const Vector<String> &p_values, const String &p_
702698
line += value;
703699
}
704700

705-
store_line(line);
701+
return store_line(line);
706702
}
707703

708-
void FileAccess::store_buffer(const uint8_t *p_src, uint64_t p_length) {
709-
ERR_FAIL_COND(!p_src && p_length > 0);
704+
bool FileAccess::store_buffer(const uint8_t *p_src, uint64_t p_length) {
705+
ERR_FAIL_COND_V(!p_src && p_length > 0, false);
710706
for (uint64_t i = 0; i < p_length; i++) {
711-
store_8(p_src[i]);
707+
if (unlikely(!store_8(p_src[i]))) {
708+
return false;
709+
}
712710
}
711+
return true;
713712
}
714713

715-
void FileAccess::store_buffer(const Vector<uint8_t> &p_buffer) {
714+
bool FileAccess::store_buffer(const Vector<uint8_t> &p_buffer) {
716715
uint64_t len = p_buffer.size();
717716
if (len == 0) {
718-
return;
717+
return true;
719718
}
720719

721720
const uint8_t *r = p_buffer.ptr();
722721

723-
store_buffer(&r[0], len);
722+
return store_buffer(&r[0], len);
724723
}
725724

726-
void FileAccess::store_var(const Variant &p_var, bool p_full_objects) {
725+
bool FileAccess::store_var(const Variant &p_var, bool p_full_objects) {
727726
int len;
728727
Error err = encode_variant(p_var, nullptr, len, p_full_objects);
729-
ERR_FAIL_COND_MSG(err != OK, "Error when trying to encode Variant.");
728+
ERR_FAIL_COND_V_MSG(err != OK, false, "Error when trying to encode Variant.");
730729

731730
Vector<uint8_t> buff;
732731
buff.resize(len);
733732

734733
uint8_t *w = buff.ptrw();
735734
err = encode_variant(p_var, &w[0], len, p_full_objects);
736-
ERR_FAIL_COND_MSG(err != OK, "Error when trying to encode Variant.");
735+
ERR_FAIL_COND_V_MSG(err != OK, false, "Error when trying to encode Variant.");
737736

738-
store_32(len);
739-
store_buffer(buff);
737+
return store_32(len) && store_buffer(buff);
740738
}
741739

742740
Vector<uint8_t> FileAccess::get_file_as_bytes(const String &p_path, Error *r_error) {
@@ -896,7 +894,7 @@ void FileAccess::_bind_methods() {
896894
ClassDB::bind_method(D_METHOD("store_float", "value"), &FileAccess::store_float);
897895
ClassDB::bind_method(D_METHOD("store_double", "value"), &FileAccess::store_double);
898896
ClassDB::bind_method(D_METHOD("store_real", "value"), &FileAccess::store_real);
899-
ClassDB::bind_method(D_METHOD("store_buffer", "buffer"), (void(FileAccess::*)(const Vector<uint8_t> &)) & FileAccess::store_buffer);
897+
ClassDB::bind_method(D_METHOD("store_buffer", "buffer"), (bool(FileAccess::*)(const Vector<uint8_t> &)) & FileAccess::store_buffer);
900898
ClassDB::bind_method(D_METHOD("store_line", "line"), &FileAccess::store_line);
901899
ClassDB::bind_method(D_METHOD("store_csv_line", "values", "delim"), &FileAccess::store_csv_line, DEFVAL(","));
902900
ClassDB::bind_method(D_METHOD("store_string", "string"), &FileAccess::store_string);

‎core/io/file_access.h

+32-14
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,24 @@ class FileAccess : public RefCounted {
108108

109109
static FileCloseFailNotify close_fail_notify;
110110

111+
#ifndef DISABLE_DEPRECATED
112+
void store_8_78289(uint8_t p_dest);
113+
void store_16_78289(uint16_t p_dest);
114+
void store_32_78289(uint32_t p_dest);
115+
void store_64_78289(uint64_t p_dest);
116+
void store_buffer_78289(const Vector<uint8_t> &p_buffer);
117+
void store_var_78289(const Variant &p_var, bool p_full_objects = false);
118+
void store_float_78289(float p_dest);
119+
void store_double_78289(double p_dest);
120+
void store_real_78289(real_t p_real);
121+
void store_string_78289(const String &p_string);
122+
void store_line_78289(const String &p_line);
123+
void store_csv_line_78289(const Vector<String> &p_values, const String &p_delim = ",");
124+
void store_pascal_string_78289(const String &p_string);
125+
126+
static void _bind_compatibility_methods();
127+
#endif
128+
111129
private:
112130
static bool backup_save;
113131
thread_local static Error last_file_open_error;
@@ -166,26 +184,26 @@ class FileAccess : public RefCounted {
166184
virtual Error get_error() const = 0; ///< get last error
167185

168186
virtual void flush() = 0;
169-
virtual void store_8(uint8_t p_dest) = 0; ///< store a byte
170-
virtual void store_16(uint16_t p_dest); ///< store 16 bits uint
171-
virtual void store_32(uint32_t p_dest); ///< store 32 bits uint
172-
virtual void store_64(uint64_t p_dest); ///< store 64 bits uint
187+
virtual bool store_8(uint8_t p_dest) = 0; ///< store a byte
188+
virtual bool store_16(uint16_t p_dest); ///< store 16 bits uint
189+
virtual bool store_32(uint32_t p_dest); ///< store 32 bits uint
190+
virtual bool store_64(uint64_t p_dest); ///< store 64 bits uint
173191

174-
virtual void store_float(float p_dest);
175-
virtual void store_double(double p_dest);
176-
virtual void store_real(real_t p_real);
192+
virtual bool store_float(float p_dest);
193+
virtual bool store_double(double p_dest);
194+
virtual bool store_real(real_t p_real);
177195

178-
virtual void store_string(const String &p_string);
179-
virtual void store_line(const String &p_line);
180-
virtual void store_csv_line(const Vector<String> &p_values, const String &p_delim = ",");
196+
virtual bool store_string(const String &p_string);
197+
virtual bool store_line(const String &p_line);
198+
virtual bool store_csv_line(const Vector<String> &p_values, const String &p_delim = ",");
181199

182-
virtual void store_pascal_string(const String &p_string);
200+
virtual bool store_pascal_string(const String &p_string);
183201
virtual String get_pascal_string();
184202

185-
virtual void store_buffer(const uint8_t *p_src, uint64_t p_length); ///< store an array of bytes
186-
void store_buffer(const Vector<uint8_t> &p_buffer);
203+
virtual bool store_buffer(const uint8_t *p_src, uint64_t p_length); ///< store an array of bytes
204+
bool store_buffer(const Vector<uint8_t> &p_buffer);
187205

188-
void store_var(const Variant &p_var, bool p_full_objects = false);
206+
bool store_var(const Variant &p_var, bool p_full_objects = false);
189207

190208
virtual void close() = 0;
191209

‎core/io/file_access_compressed.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,13 @@ void FileAccessCompressed::flush() {
341341
// compressed files keep data in memory till close()
342342
}
343343

344-
void FileAccessCompressed::store_8(uint8_t p_dest) {
345-
ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use.");
346-
ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode.");
344+
bool FileAccessCompressed::store_8(uint8_t p_dest) {
345+
ERR_FAIL_COND_V_MSG(f.is_null(), false, "File must be opened before use.");
346+
ERR_FAIL_COND_V_MSG(!writing, false, "File has not been opened in write mode.");
347347

348348
WRITE_FIT(1);
349349
write_ptr[write_pos++] = p_dest;
350+
return true;
350351
}
351352

352353
bool FileAccessCompressed::file_exists(const String &p_name) {

‎core/io/file_access_compressed.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class FileAccessCompressed : public FileAccess {
8989
virtual Error get_error() const override; ///< get last error
9090

9191
virtual void flush() override;
92-
virtual void store_8(uint8_t p_dest) override; ///< store a byte
92+
virtual bool store_8(uint8_t p_dest) override; ///< store a byte
9393

9494
virtual bool file_exists(const String &p_name) override; ///< return true if a file exists
9595

0 commit comments

Comments
 (0)