Skip to content

Commit

Permalink
Fix issue preventing the Android Editor from displaying the project c…
Browse files Browse the repository at this point in the history
…ontent

The issue was causing by a bug within the logic for `FileAccessFilesystemJAndroid#eof_reached()` causing that value to remain false after the eof was reached.
This in turn caused an infinite loop in the file scanner preventing the project's content from showing up.
  • Loading branch information
m4gr3d committed Aug 15, 2022
1 parent ef44e63 commit 3047954
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
13 changes: 13 additions & 0 deletions platform/android/file_access_filesystem_jandroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jmethodID FileAccessFilesystemJAndroid::_file_seek_end = nullptr;
jmethodID FileAccessFilesystemJAndroid::_file_read = nullptr;
jmethodID FileAccessFilesystemJAndroid::_file_tell = nullptr;
jmethodID FileAccessFilesystemJAndroid::_file_eof = nullptr;
jmethodID FileAccessFilesystemJAndroid::_file_set_eof = nullptr;
jmethodID FileAccessFilesystemJAndroid::_file_close = nullptr;
jmethodID FileAccessFilesystemJAndroid::_file_write = nullptr;
jmethodID FileAccessFilesystemJAndroid::_file_flush = nullptr;
Expand Down Expand Up @@ -161,6 +162,16 @@ bool FileAccessFilesystemJAndroid::eof_reached() const {
}
}

void FileAccessFilesystemJAndroid::_set_eof(bool eof) {
if (_file_set_eof) {
ERR_FAIL_COND_MSG(!is_open(), "File must be opened before use.");

JNIEnv *env = get_jni_env();
ERR_FAIL_COND(env == nullptr);
env->CallVoidMethod(file_access_handler, _file_set_eof, id, eof);
}
}

uint8_t FileAccessFilesystemJAndroid::get_8() const {
ERR_FAIL_COND_V_MSG(!is_open(), 0, "File must be opened before use.");
uint8_t byte;
Expand All @@ -183,6 +194,7 @@ String FileAccessFilesystemJAndroid::get_line() const {
while (true) {
size_t line_buffer_size = MIN(buffer_size_limit, file_size - get_position());
if (line_buffer_size <= 0) {
const_cast<FileAccessFilesystemJAndroid *>(this)->_set_eof(true);
break;
}

Expand Down Expand Up @@ -309,6 +321,7 @@ void FileAccessFilesystemJAndroid::setup(jobject p_file_access_handler) {
_file_get_size = env->GetMethodID(cls, "fileGetSize", "(I)J");
_file_tell = env->GetMethodID(cls, "fileGetPosition", "(I)J");
_file_eof = env->GetMethodID(cls, "isFileEof", "(I)Z");
_file_set_eof = env->GetMethodID(cls, "setFileEof", "(IZ)V");
_file_seek = env->GetMethodID(cls, "fileSeek", "(IJ)V");
_file_seek_end = env->GetMethodID(cls, "fileSeekFromEnd", "(IJ)V");
_file_read = env->GetMethodID(cls, "fileRead", "(ILjava/nio/ByteBuffer;)I");
Expand Down
3 changes: 3 additions & 0 deletions platform/android/file_access_filesystem_jandroid.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class FileAccessFilesystemJAndroid : public FileAccess {
static jmethodID _file_seek_end;
static jmethodID _file_tell;
static jmethodID _file_eof;
static jmethodID _file_set_eof;
static jmethodID _file_read;
static jmethodID _file_write;
static jmethodID _file_flush;
Expand All @@ -55,6 +56,8 @@ class FileAccessFilesystemJAndroid : public FileAccess {
String absolute_path;
String path_src;

void _set_eof(bool eof);

public:
virtual Error _open(const String &p_path, int p_mode_flags) override; ///< open a file
virtual void close() override; ///< close a file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ internal abstract class DataAccess(private val filePath: String) {

protected abstract val fileChannel: FileChannel
internal var endOfFile = false
private set

fun close() {
try {
Expand All @@ -125,9 +124,7 @@ internal abstract class DataAccess(private val filePath: String) {
fun seek(position: Long) {
try {
fileChannel.position(position)
if (position <= size()) {
endOfFile = false
}
endOfFile = position >= fileChannel.size()
} catch (e: Exception) {
Log.w(TAG, "Exception when seeking file $filePath.", e)
}
Expand Down Expand Up @@ -161,8 +158,7 @@ internal abstract class DataAccess(private val filePath: String) {
fun read(buffer: ByteBuffer): Int {
return try {
val readBytes = fileChannel.read(buffer)
endOfFile = readBytes == -1
|| (fileChannel.position() >= fileChannel.size() && fileChannel.size() > 0)
endOfFile = readBytes == -1 || (fileChannel.position() >= fileChannel.size())
if (readBytes == -1) {
0
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ class FileAccessHandler(val context: Context) {
return files[fileId].endOfFile
}

fun setFileEof(fileId: Int, eof: Boolean) {
val file = files[fileId] ?: return
file.endOfFile = eof
}

fun fileClose(fileId: Int) {
if (hasFileId(fileId)) {
files[fileId].close()
Expand Down

0 comments on commit 3047954

Please sign in to comment.