diff options
author | bruvzg <7645683+bruvzg@users.noreply.github.com> | 2024-04-08 22:09:34 +0300 |
---|---|---|
committer | bruvzg <7645683+bruvzg@users.noreply.github.com> | 2024-04-12 19:20:49 +0300 |
commit | 88b3e68f93fad5681b27b11ddacc562b03d5ef45 (patch) | |
tree | 03f57722ab74282b862a5aba5433ff928bfdd972 /platform/android | |
parent | 029aadef563fb69cf49aa9795b62f27171f8c3f4 (diff) | |
download | redot-engine-88b3e68f93fad5681b27b11ddacc562b03d5ef45.tar.gz |
[FileAccess] Implement `resize` method.
Diffstat (limited to 'platform/android')
5 files changed, 57 insertions, 0 deletions
diff --git a/platform/android/file_access_android.h b/platform/android/file_access_android.h index ec613b6687..bfe590b22f 100644 --- a/platform/android/file_access_android.h +++ b/platform/android/file_access_android.h @@ -65,6 +65,7 @@ public: virtual bool eof_reached() const override; // reading passed EOF + virtual Error resize(int64_t p_length) override { return ERR_UNAVAILABLE; } virtual uint8_t get_8() const override; // get a byte virtual uint16_t get_16() const override; virtual uint32_t get_32() const override; diff --git a/platform/android/file_access_filesystem_jandroid.cpp b/platform/android/file_access_filesystem_jandroid.cpp index 46d9728632..cc7acffdad 100644 --- a/platform/android/file_access_filesystem_jandroid.cpp +++ b/platform/android/file_access_filesystem_jandroid.cpp @@ -53,6 +53,7 @@ jmethodID FileAccessFilesystemJAndroid::_file_write = nullptr; jmethodID FileAccessFilesystemJAndroid::_file_flush = nullptr; jmethodID FileAccessFilesystemJAndroid::_file_exists = nullptr; jmethodID FileAccessFilesystemJAndroid::_file_last_modified = nullptr; +jmethodID FileAccessFilesystemJAndroid::_file_resize = nullptr; String FileAccessFilesystemJAndroid::get_path() const { return path_src; @@ -324,6 +325,27 @@ Error FileAccessFilesystemJAndroid::get_error() const { return OK; } +Error FileAccessFilesystemJAndroid::resize(int64_t p_length) { + if (_file_resize) { + JNIEnv *env = get_jni_env(); + ERR_FAIL_NULL_V(env, FAILED); + ERR_FAIL_COND_V_MSG(!is_open(), FAILED, "File must be opened before use."); + int res = env->CallIntMethod(file_access_handler, _file_resize, id, p_length); + switch (res) { + case 0: + return OK; + case -3: + return ERR_INVALID_PARAMETER; + case -2: + return ERR_FILE_CANT_OPEN; + default: + return FAILED; + } + } else { + return ERR_UNAVAILABLE; + } +} + void FileAccessFilesystemJAndroid::flush() { if (_file_flush) { JNIEnv *env = get_jni_env(); @@ -383,6 +405,7 @@ void FileAccessFilesystemJAndroid::setup(jobject p_file_access_handler) { _file_flush = env->GetMethodID(cls, "fileFlush", "(I)V"); _file_exists = env->GetMethodID(cls, "fileExists", "(Ljava/lang/String;)Z"); _file_last_modified = env->GetMethodID(cls, "fileLastModified", "(Ljava/lang/String;)J"); + _file_resize = env->GetMethodID(cls, "fileResize", "(IJ)I"); } void FileAccessFilesystemJAndroid::close() { diff --git a/platform/android/file_access_filesystem_jandroid.h b/platform/android/file_access_filesystem_jandroid.h index f33aa64ebe..4a28b6e3d5 100644 --- a/platform/android/file_access_filesystem_jandroid.h +++ b/platform/android/file_access_filesystem_jandroid.h @@ -52,6 +52,7 @@ class FileAccessFilesystemJAndroid : public FileAccess { static jmethodID _file_close; static jmethodID _file_exists; static jmethodID _file_last_modified; + static jmethodID _file_resize; int id; String absolute_path; @@ -76,6 +77,7 @@ public: virtual bool eof_reached() const override; ///< reading passed EOF + virtual Error resize(int64_t p_length) override; virtual uint8_t get_8() const override; ///< get a byte virtual uint16_t get_16() const override; virtual uint32_t get_32() const override; diff --git a/platform/android/java/lib/src/org/godotengine/godot/io/file/DataAccess.kt b/platform/android/java/lib/src/org/godotengine/godot/io/file/DataAccess.kt index 0f447f0b05..b155c4e488 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/io/file/DataAccess.kt +++ b/platform/android/java/lib/src/org/godotengine/godot/io/file/DataAccess.kt @@ -36,7 +36,9 @@ import android.util.Log import org.godotengine.godot.io.StorageScope import java.io.IOException import java.nio.ByteBuffer +import java.nio.channels.ClosedChannelException import java.nio.channels.FileChannel +import java.nio.channels.NonWritableChannelException import kotlin.math.max /** @@ -50,6 +52,11 @@ internal abstract class DataAccess(private val filePath: String) { companion object { private val TAG = DataAccess::class.java.simpleName + private const val OK_ERROR_ID = 0; + private const val FAILED_ERROR_ID = -1; + private const val FILE_CANT_OPEN_ERROR_ID = -2; + private const val INVALID_PARAMETER_ERROR_ID = -3; + fun generateDataAccess( storageScope: StorageScope, context: Context, @@ -135,6 +142,21 @@ internal abstract class DataAccess(private val filePath: String) { seek(positionFromBeginning) } + fun resize(length: Long): Int { + return try { + fileChannel.truncate(length) + OK_ERROR_ID + } catch (e: NonWritableChannelException) { + FILE_CANT_OPEN_ERROR_ID + } catch (e: ClosedChannelException) { + FILE_CANT_OPEN_ERROR_ID + } catch (e: IllegalArgumentException) { + INVALID_PARAMETER_ERROR_ID + } catch (e: IOException) { + FAILED_ERROR_ID + } + } + fun position(): Long { return try { fileChannel.position() diff --git a/platform/android/java/lib/src/org/godotengine/godot/io/file/FileAccessHandler.kt b/platform/android/java/lib/src/org/godotengine/godot/io/file/FileAccessHandler.kt index 50741c1aab..6a8a10e56f 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/io/file/FileAccessHandler.kt +++ b/platform/android/java/lib/src/org/godotengine/godot/io/file/FileAccessHandler.kt @@ -45,6 +45,7 @@ class FileAccessHandler(val context: Context) { companion object { private val TAG = FileAccessHandler::class.java.simpleName + private const val FAILED_ERROR_ID = -1; private const val FILE_NOT_FOUND_ERROR_ID = -1 internal const val INVALID_FILE_ID = 0 private const val STARTING_FILE_ID = 1 @@ -190,6 +191,14 @@ class FileAccessHandler(val context: Context) { } } + fun fileResize(fileId: Int, length: Long): Int { + if (!hasFileId(fileId)) { + return FAILED_ERROR_ID + } + + return files[fileId].resize(length) + } + fun fileGetPosition(fileId: Int): Long { if (!hasFileId(fileId)) { return 0L |