pspdev / psp-packages

https://pspdev.github.io/psp-packages/
The Unlicense
23 stars 13 forks source link

add physfs package #179

Closed diamant3 closed 1 month ago

diamant3 commented 1 month ago

I'll take the initial port of physfs but the patch is credit to @stdgregwar

this is the code i used to test

-- snip the callback part --

int main(void)
{
    SetupCallbacks();
    pspDebugScreenInit();

    const char *filename = "test.txt";

    PHYSFS_init(NULL); // argv[0] is fine
    PHYSFS_mount(".", NULL, 1);
    PHYSFS_File *file = PHYSFS_openRead(filename);
    PHYSFS_sint64 fileSize = PHYSFS_fileLength(file);
    char *buffer = (char *)malloc(fileSize + 1);
    size_t bytesRead = PHYSFS_readBytes(file, buffer, fileSize);

    pspDebugScreenSetXY(0, 5);
    while (!done)
    {
        //Null-terminate the buffer
        buffer[bytesRead] = '\0';

        //Print the file contents
        pspDebugScreenPrintf("File contents: %s\n", buffer);
        pspDebugScreenPrintf("Error: %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
    }

    sceKernelExitGame();
    return 0;
}
tpimh commented 1 month ago

I remember that this code was not merged previously in https://github.com/pspdev/psp-packages/pull/32. What was the reason for this? Did it not pass some tests?

Also I have found physfs in binary form in Neverball (committed 13 years ago) and in Solarus (committed 9 years ago). Wonder what the origins of these are.

diamant3 commented 1 month ago

I remember that this code was not merged previously in https://github.com/pspdev/psp-packages/pull/32. What was the reason for this? Did it not pass some tests?

I skimmed that and in my understanding, no one posted any findings whether physfs is working or not, that's why it didn't merge. Hopefully this physfs should working now.🙏🏼

tpimh commented 1 month ago

I propose this simple fix for the broken physfs.pc: https://github.com/diamant3/psp-packages/pull/1

diamant3 commented 1 month ago

Thanks again @tpimh for checking!

tpimh commented 1 month ago

Maybe something is wrong with how I am building, but the provided example always crashes PPSSPP for me.

tpimh commented 1 month ago

This is the CMakeLists.txt I wrote to build using pkg-config:

cmake_minimum_required(VERSION 3.5)

project(physfstest)

add_executable(${PROJECT_NAME} main.c)

include(FindPkgConfig)
pkg_search_module(PHYSFS REQUIRED physfs)

target_include_directories(${PROJECT_NAME} PRIVATE
    ${PHYSFS_INCLUDE_DIRS}
)

target_link_libraries(${PROJECT_NAME} PRIVATE
    pspdebug
    pspdisplay
    pspge
    ${PHYSFS_LIBRARIES}
)

# Create an EBOOT.PBP file
create_pbp_file(
    TARGET ${PROJECT_NAME}
    ICON_PATH NULL
    BACKGROUND_PATH NULL
    PREVIEW_PATH NULL
    TITLE ${PROJECT_NAME}
    VERSION 01.00
)
diamant3 commented 1 month ago

You should provide test.txt file first to the same location of EBOOT.PBP, it will work :D

tpimh commented 1 month ago

Oh, my bad, I expected it to print the error message.

tpimh commented 1 month ago

I modified the test code slightly to add error handling after every physfs call. The CMakeLists.txt that I used can be found above.

#include <stdlib.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <physfs.h>

// PSP_MODULE_INFO is required
PSP_MODULE_INFO("PhysFS test", 0, 1, 0);
PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER);

int exit_callback(int arg1, int arg2, void *common) {
    sceKernelExitGame();
    return 0;
}

int callback_thread(SceSize args, void *argp) {
    int cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
    sceKernelRegisterExitCallback(cbid);
    sceKernelSleepThreadCB();
    return 0;
}

int setup_callbacks(void) {
    int thid = sceKernelCreateThread("update_thread", callback_thread, 0x11, 0xFA0, 0, 0);
    if(thid >= 0)
        sceKernelStartThread(thid, 0, 0);
    return thid;
}

void check_error(void) {
    PHYSFS_ErrorCode err = PHYSFS_getLastErrorCode();

    if (err != PHYSFS_ERR_OK) {
        while (1) {
            pspDebugScreenSetXY(0, 0);
            pspDebugScreenPrintf("Error: %s\n", PHYSFS_getErrorByCode(err));
            sceDisplayWaitVblankStart();
        }
    }
}

int main(void) {
    setup_callbacks();
    pspDebugScreenInit();

    const char *filename = "test.txt";

    PHYSFS_init(NULL); // argv[0] is fine
    check_error();
    PHYSFS_mount(".", NULL, 1);
    check_error();
    PHYSFS_File *file = PHYSFS_openRead(filename);
    check_error();
    PHYSFS_sint64 fileSize = PHYSFS_fileLength(file);
    check_error();
    char *buffer = (char *)malloc(fileSize + 1);
    size_t bytesRead = PHYSFS_readBytes(file, buffer, fileSize);
    check_error();

    while (1) {
        //Null-terminate the buffer
        buffer[bytesRead] = '\0';

        //Print the file contents
        pspDebugScreenSetXY(0, 0);
        pspDebugScreenPrintf("File contents: %s\n", buffer);
    sceDisplayWaitVblankStart();
    }

    sceKernelExitGame();
    return 0;
}
sharkwouter commented 1 month ago

That is a very clean patch, I think upstream would take that. Thanks for working on this!