Merge pull request #73435 from bruvzg/file_close

Restore FileAccess.close method.
This commit is contained in:
Rémi Verschelde 2023-02-16 18:34:48 +01:00
commit ec2f9a78f7
No known key found for this signature in database
GPG Key ID: C3336907360768E1
22 changed files with 76 additions and 1 deletions

View File

@ -856,6 +856,8 @@ void FileAccess::_bind_methods() {
ClassDB::bind_method(D_METHOD("store_pascal_string", "string"), &FileAccess::store_pascal_string); ClassDB::bind_method(D_METHOD("store_pascal_string", "string"), &FileAccess::store_pascal_string);
ClassDB::bind_method(D_METHOD("get_pascal_string"), &FileAccess::get_pascal_string); ClassDB::bind_method(D_METHOD("get_pascal_string"), &FileAccess::get_pascal_string);
ClassDB::bind_method(D_METHOD("close"), &FileAccess::close);
ClassDB::bind_static_method("FileAccess", D_METHOD("file_exists", "path"), &FileAccess::exists); ClassDB::bind_static_method("FileAccess", D_METHOD("file_exists", "path"), &FileAccess::exists);
ClassDB::bind_static_method("FileAccess", D_METHOD("get_modified_time", "file"), &FileAccess::get_modified_time); ClassDB::bind_static_method("FileAccess", D_METHOD("get_modified_time", "file"), &FileAccess::get_modified_time);

View File

@ -166,6 +166,8 @@ public:
void store_var(const Variant &p_var, bool p_full_objects = false); void store_var(const Variant &p_var, bool p_full_objects = false);
virtual void close() = 0;
virtual bool file_exists(const String &p_name) = 0; ///< return true if a file exists virtual bool file_exists(const String &p_name) = 0; ///< return true if a file exists
virtual Error reopen(const String &p_path, int p_mode_flags); ///< does not change the AccessType virtual Error reopen(const String &p_path, int p_mode_flags); ///< does not change the AccessType

View File

@ -385,6 +385,10 @@ Error FileAccessCompressed::_set_unix_permissions(const String &p_file, uint32_t
return FAILED; return FAILED;
} }
void FileAccessCompressed::close() {
_close();
}
FileAccessCompressed::~FileAccessCompressed() { FileAccessCompressed::~FileAccessCompressed() {
_close(); _close();
} }

View File

@ -97,6 +97,8 @@ public:
virtual uint32_t _get_unix_permissions(const String &p_file) override; virtual uint32_t _get_unix_permissions(const String &p_file) override;
virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) override; virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) override;
virtual void close() override;
FileAccessCompressed() {} FileAccessCompressed() {}
virtual ~FileAccessCompressed(); virtual ~FileAccessCompressed();
}; };

View File

@ -294,6 +294,10 @@ Error FileAccessEncrypted::_set_unix_permissions(const String &p_file, uint32_t
return ERR_UNAVAILABLE; return ERR_UNAVAILABLE;
} }
void FileAccessEncrypted::close() {
_close();
}
FileAccessEncrypted::~FileAccessEncrypted() { FileAccessEncrypted::~FileAccessEncrypted() {
_close(); _close();
} }

View File

@ -88,6 +88,8 @@ public:
virtual uint32_t _get_unix_permissions(const String &p_file) override; virtual uint32_t _get_unix_permissions(const String &p_file) override;
virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) override; virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) override;
virtual void close() override;
FileAccessEncrypted() {} FileAccessEncrypted() {}
~FileAccessEncrypted(); ~FileAccessEncrypted();
}; };

View File

@ -71,6 +71,8 @@ public:
virtual uint32_t _get_unix_permissions(const String &p_file) override { return 0; } virtual uint32_t _get_unix_permissions(const String &p_file) override { return 0; }
virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) override { return FAILED; } virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) override { return FAILED; }
virtual void close() override {}
FileAccessMemory() {} FileAccessMemory() {}
}; };

View File

@ -469,6 +469,15 @@ void FileAccessNetwork::configure() {
GLOBAL_DEF(PropertyInfo(Variant::INT, "network/remote_fs/page_read_ahead", PROPERTY_HINT_RANGE, "0,8,1,or_greater"), 4); GLOBAL_DEF(PropertyInfo(Variant::INT, "network/remote_fs/page_read_ahead", PROPERTY_HINT_RANGE, "0,8,1,or_greater"), 4);
} }
void FileAccessNetwork::close() {
_close();
FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
nc->lock_mutex();
nc->accesses.erase(id);
nc->unlock_mutex();
}
FileAccessNetwork::FileAccessNetwork() { FileAccessNetwork::FileAccessNetwork() {
FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton; FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
nc->lock_mutex(); nc->lock_mutex();

View File

@ -156,6 +156,8 @@ public:
virtual uint32_t _get_unix_permissions(const String &p_file) override; virtual uint32_t _get_unix_permissions(const String &p_file) override;
virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) override; virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) override;
virtual void close() override;
static void configure(); static void configure();
FileAccessNetwork(); FileAccessNetwork();

View File

@ -366,6 +366,10 @@ bool FileAccessPack::file_exists(const String &p_name) {
return false; return false;
} }
void FileAccessPack::close() {
f = Ref<FileAccess>();
}
FileAccessPack::FileAccessPack(const String &p_path, const PackedData::PackedFile &p_file) : FileAccessPack::FileAccessPack(const String &p_path, const PackedData::PackedFile &p_file) :
pf(p_file), pf(p_file),
f(FileAccess::open(pf.pack, FileAccess::READ)) { f(FileAccess::open(pf.pack, FileAccess::READ)) {

View File

@ -178,6 +178,8 @@ public:
virtual bool file_exists(const String &p_name) override; virtual bool file_exists(const String &p_name) override;
virtual void close() override;
FileAccessPack(const String &p_path, const PackedData::PackedFile &p_file); FileAccessPack(const String &p_path, const PackedData::PackedFile &p_file);
}; };

View File

@ -336,6 +336,10 @@ bool FileAccessZip::file_exists(const String &p_name) {
return false; return false;
} }
void FileAccessZip::close() {
_close();
}
FileAccessZip::FileAccessZip(const String &p_path, const PackedData::PackedFile &p_file) { FileAccessZip::FileAccessZip(const String &p_path, const PackedData::PackedFile &p_file) {
open_internal(p_path, FileAccess::READ); open_internal(p_path, FileAccess::READ);
} }

View File

@ -109,6 +109,8 @@ public:
virtual uint32_t _get_unix_permissions(const String &p_file) override { return 0; } virtual uint32_t _get_unix_permissions(const String &p_file) override { return 0; }
virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) override { return FAILED; } virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) override { return FAILED; }
virtual void close() override;
FileAccessZip(const String &p_path, const PackedData::PackedFile &p_file); FileAccessZip(const String &p_path, const PackedData::PackedFile &p_file);
~FileAccessZip(); ~FileAccessZip();
}; };

View File

@ -33,7 +33,7 @@
[/csharp] [/csharp]
[/codeblocks] [/codeblocks]
In the example above, the file will be saved in the user data folder as specified in the [url=$DOCS_URL/tutorials/io/data_paths.html]Data paths[/url] documentation. In the example above, the file will be saved in the user data folder as specified in the [url=$DOCS_URL/tutorials/io/data_paths.html]Data paths[/url] documentation.
There is no method to close a file in order to free it from use. Instead, [FileAccess] will close when it's freed, which happens when it goes out of scope or when it gets assigned with [code]null[/code]. In C# the reference must be disposed after we are done using it, this can be done with the [code]using[/code] statement or calling the [code]Dispose[/code] method directly. [FileAccess] will close when it's freed, which happens when it goes out of scope or when it gets assigned with [code]null[/code]. In C# the reference must be disposed after we are done using it, this can be done with the [code]using[/code] statement or calling the [code]Dispose[/code] method directly.
[codeblocks] [codeblocks]
[gdscript] [gdscript]
var file = FileAccess.open("res://something") # File is opened and locked for use. var file = FileAccess.open("res://something") # File is opened and locked for use.
@ -52,6 +52,13 @@
<link title="3D Voxel Demo">https://godotengine.org/asset-library/asset/676</link> <link title="3D Voxel Demo">https://godotengine.org/asset-library/asset/676</link>
</tutorials> </tutorials>
<methods> <methods>
<method name="close">
<return type="void" />
<description>
Closes the currently opened file and prevents subsequent read/write operations. Use flush to persist the data to disk without closing the file.
[b]Note:[/b] [FileAccess] will automatically close when it's freed, which happens when it goes out of scope or when it gets assigned with [code]null[/code]. In C# the reference must be disposed after we are done using it, this can be done with the [code]using[/code] statement or calling the [code]Dispose[/code] method directly.
</description>
</method>
<method name="eof_reached" qualifiers="const"> <method name="eof_reached" qualifiers="const">
<return type="bool" /> <return type="bool" />
<description> <description>

View File

@ -318,6 +318,10 @@ Error FileAccessUnix::_set_unix_permissions(const String &p_file, uint32_t p_per
return FAILED; return FAILED;
} }
void FileAccessUnix::close() {
_close();
}
CloseNotificationFunc FileAccessUnix::close_notification_func = nullptr; CloseNotificationFunc FileAccessUnix::close_notification_func = nullptr;
FileAccessUnix::~FileAccessUnix() { FileAccessUnix::~FileAccessUnix() {

View File

@ -82,6 +82,8 @@ public:
virtual uint32_t _get_unix_permissions(const String &p_file) override; virtual uint32_t _get_unix_permissions(const String &p_file) override;
virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) override; virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) override;
virtual void close() override;
FileAccessUnix() {} FileAccessUnix() {}
virtual ~FileAccessUnix(); virtual ~FileAccessUnix();
}; };

View File

@ -375,6 +375,10 @@ Error FileAccessWindows::_set_unix_permissions(const String &p_file, uint32_t p_
return ERR_UNAVAILABLE; return ERR_UNAVAILABLE;
} }
void FileAccessWindows::close() {
_close();
}
FileAccessWindows::~FileAccessWindows() { FileAccessWindows::~FileAccessWindows() {
_close(); _close();
} }
@ -391,6 +395,7 @@ void FileAccessWindows::initialize() {
reserved_file_index++; reserved_file_index++;
} }
} }
void FileAccessWindows::finalize() { void FileAccessWindows::finalize() {
invalid_files.clear(); invalid_files.clear();
} }

View File

@ -82,6 +82,8 @@ public:
virtual uint32_t _get_unix_permissions(const String &p_file) override; virtual uint32_t _get_unix_permissions(const String &p_file) override;
virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) override; virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) override;
virtual void close() override;
static void initialize(); static void initialize();
static void finalize(); static void finalize();

View File

@ -169,6 +169,10 @@ bool FileAccessAndroid::file_exists(const String &p_path) {
return true; return true;
} }
void FileAccessAndroid::close() {
_close();
}
FileAccessAndroid::~FileAccessAndroid() { FileAccessAndroid::~FileAccessAndroid() {
_close(); _close();
} }

View File

@ -78,6 +78,8 @@ public:
virtual uint32_t _get_unix_permissions(const String &p_file) override { return 0; } virtual uint32_t _get_unix_permissions(const String &p_file) override { return 0; }
virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) override { return FAILED; } virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) override { return FAILED; }
virtual void close() override;
~FileAccessAndroid(); ~FileAccessAndroid();
}; };

View File

@ -333,6 +333,12 @@ void FileAccessFilesystemJAndroid::setup(jobject p_file_access_handler) {
_file_last_modified = env->GetMethodID(cls, "fileLastModified", "(Ljava/lang/String;)J"); _file_last_modified = env->GetMethodID(cls, "fileLastModified", "(Ljava/lang/String;)J");
} }
void FileAccessFilesystemJAndroid::close() {
if (is_open()) {
_close();
}
}
FileAccessFilesystemJAndroid::FileAccessFilesystemJAndroid() { FileAccessFilesystemJAndroid::FileAccessFilesystemJAndroid() {
id = 0; id = 0;
} }

View File

@ -93,6 +93,8 @@ public:
virtual uint32_t _get_unix_permissions(const String &p_file) override { return 0; } virtual uint32_t _get_unix_permissions(const String &p_file) override { return 0; }
virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) override { return FAILED; } virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) override { return FAILED; }
virtual void close() override;
FileAccessFilesystemJAndroid(); FileAccessFilesystemJAndroid();
~FileAccessFilesystemJAndroid(); ~FileAccessFilesystemJAndroid();
}; };