Skip to content

LoadFileText method not working as intended #70

Open

Description

@redfox4ever

When I call LoadFileText it returns a ByteBuffer with the capacity of 1 regardless of the content of the file loaded.

package com.redfox.platformer;

import java.nio.ByteBuffer;

import static com.raylib.Colors.RAYWHITE;
import com.raylib.Raylib;
import static com.raylib.Raylib.BeginDrawing;
import static com.raylib.Raylib.ClearBackground;
import static com.raylib.Raylib.CloseWindow;
import static com.raylib.Raylib.EndDrawing;
import static com.raylib.Raylib.InitWindow;
import static com.raylib.Raylib.WindowShouldClose;

public class Game {
    public static void main(String[] args) {
        int screenWidth = 800;
        int screenHeight = 600;
        InitWindow(screenWidth, screenHeight, "raygui");
        while (!WindowShouldClose()) {
            BeginDrawing();
            ClearBackground(RAYWHITE);
            EndDrawing();
        }
        CloseWindow();
        ByteBuffer text = Raylib.LoadFileText("resources\\Text.txt");
        System.out.println(text.capacity());  // 1
        while(text.hasRemaining())
        {
            System.out.printf("%c", (char)text.get());
        }   
        System.out.println(); 
    }
    
}

resources/Text.txt:

esfsdfsdfsdfs
sdfsdfds

Activity

  1. electronstudio commented on Dec 26, 2024

    @electronstudio
    Owner

    This is probably a bug in JavaCPP. I will investigate to try to be sure.

    Fortunately there is a work-around: use Java standard library instead of Raylib for loading text. All the Raylib text manipulation functions are only there because C lacks them so they shouldn't really be used in languages like Java.

  2. electronstudio commented on Dec 26, 2024

    @electronstudio
    Owner

    The code generated by JavaCPP:

    JNIEXPORT jobject JNICALL Java_com_raylib_Raylib_LoadFileText__Ljava_lang_String_2(JNIEnv* env, jclass cls, jstring arg0) {
        const char* ptr0 = JavaCPP_getStringBytes(env, arg0);
        jobject rarg = NULL;
        char* rptr;
        jthrowable exc = NULL;
        try {
            rptr = (char*)LoadFileText(ptr0);
            jlong rcapacity = rptr != NULL ? 1 : 0;
            if (rptr != NULL) {
                jlong rcapacityptr = rcapacity * sizeof(rptr[0]);
                rarg = env->NewDirectByteBuffer((void*)rptr, rcapacityptr < INT_MAX ? rcapacityptr : INT_MAX);
            }
        } catch (...) {
            exc = JavaCPP_handleException(env, 8);
        }
    
        JavaCPP_releaseStringBytes(env, arg0, ptr0);
        if (exc != NULL) {
            env->Throw(exc);
        }
        return rarg;
    }

    So problem is rcapacity is always set to 1.

    This is kind of the C pointer decay issue. There is no way of knowing if Raylib has returned a pointer to a single char, or to an array of chars. And if it is an array, the only way of knowing what rcapacity should be is to scan it for for the null terminator at the end. It's all pretty unsafe.

    However JavaCPP manages to handle other functions that return char arrays OK... it does

     rarg = JavaCPP_createStringFromBytes(env, rptr);
    

    Seems it knows to do that because the others have return type const char* not char*

    So I guess probably not reasonable to expect JavaCPP to handle Raylib having a shitty API? We'll just have to modify raylib API to return const I think?

  3. electronstudio commented on Dec 26, 2024

    @electronstudio
    Owner

    Or probably easier: we could just remove this function, since it's not the best way to read text anyway?

  4. electronstudio commented on Dec 26, 2024

    @electronstudio
    Owner

    There are 5 functions in the API that have this same issue, so we should fix or remove them all. I worry that if we fix them then there may be other text manipulation functions that also don't work properly, because it's generally a bad idea to use them, and I don't have time to test them all. So I'm thinking either remove all the text functions, or mark them as deprecated just in case someone does have a valid use for them.

  5. electronstudio commented on Dec 26, 2024

    @electronstudio
    Owner

    Adding deprecated annotation to functions in JavaCPP doesn't seem to work - it just deletes the function. The only examples I can find of adding annotations seem to be for fields not functions. So we would have to patch the generated java files to do this, which is annoying.

    Modifying function signatures to fix them in JavaCPP doesn't seem to be possible either. Possibly I just haven't looked hard enough. So we have to patch the .h files with fixed signatures, which is annoying

    The only thing that is easy to do in JavaCPP is to skip lines in .h file entirely to remove functions but that's my least prefered option.

  6. glowiak commented on Dec 28, 2024

    @glowiak
    Contributor

    The function's here, because in C reading a file to an array is painful (I experienced that myself), but happily in Java it's easy, so yes, this function is useless, and should be removed.

  7. electronstudio commented on Dec 31, 2024

    @electronstudio
    Owner

    Have fixed it to return a string and also marked it as deprecated. But it required manually copying the whole function sig so isn't a good way of handling it really. Need to automate it to mark all the text functions deprecated. Maybe a script that searches for any function named 'Text*' and inserts @deprecated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions