Restore FileAccess.close method.

This commit is contained in:
bruvzg 2023-02-16 15:25:32 +02:00
parent 953383328a
commit bc95b0b171
No known key found for this signature in database
GPG key ID: 7960FCF39844EC38
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("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("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);
virtual void close() = 0;
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

View file

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

View file

@ -97,6 +97,8 @@ public:
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 void close() override;
FileAccessCompressed() {}
virtual ~FileAccessCompressed();
};

View file

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

View file

@ -88,6 +88,8 @@ public:
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 void close() override;
FileAccessEncrypted() {}
~FileAccessEncrypted();
};

View file

@ -71,6 +71,8 @@ public:
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 void close() override {}
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);
}
void FileAccessNetwork::close() {
_close();
FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
nc->lock_mutex();
nc->accesses.erase(id);
nc->unlock_mutex();
}
FileAccessNetwork::FileAccessNetwork() {
FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
nc->lock_mutex();

View file

@ -156,6 +156,8 @@ public:
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 void close() override;
static void configure();
FileAccessNetwork();

View file

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

View file

@ -178,6 +178,8 @@ public:
virtual bool file_exists(const String &p_name) override;
virtual void close() override;
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;
}
void FileAccessZip::close() {
_close();
}
FileAccessZip::FileAccessZip(const String &p_path, const PackedData::PackedFile &p_file) {
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 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();
};

View file

@ -33,7 +33,7 @@
[/csharp]
[/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.
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]
[gdscript]
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>
</tutorials>
<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">
<return type="bool" />
<description>

View file

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

View file

@ -82,6 +82,8 @@ public:
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 void close() override;
FileAccessUnix() {}
virtual ~FileAccessUnix();
};

View file

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

View file

@ -82,6 +82,8 @@ public:
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 void close() override;
static void initialize();
static void finalize();

View file

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

View file

@ -78,6 +78,8 @@ public:
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 void close() override;
~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");
}
void FileAccessFilesystemJAndroid::close() {
if (is_open()) {
_close();
}
}
FileAccessFilesystemJAndroid::FileAccessFilesystemJAndroid() {
id = 0;
}

View file

@ -93,6 +93,8 @@ public:
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 void close() override;
FileAccessFilesystemJAndroid();
~FileAccessFilesystemJAndroid();
};