Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pntr_physfs and pntr_app_load_arg_file() #2

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cart/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ set(triple wasm32-wasi)

# When building from source, WASI_SDK_PREFIX represents the generated directory
if(NOT WASI_SDK_PREFIX)
set(WASI_SDK_PREFIX /opt/wasi-sdk)
set(WASI_SDK_PREFIX /home/rob/.asdf/installs/wasi-sdk/23/wasi-sdk)
endif()

set(CMAKE_C_COMPILER ${WASI_SDK_PREFIX}/bin/clang${WASI_HOST_EXE_SUFFIX})
Expand Down
17 changes: 1 addition & 16 deletions host/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,9 @@
#include "null0_api_web.h"
#endif

// global that tracks the cart-name
char* filename = NULL;

bool Init(pntr_app* app) {
if (!filename) {
return false;
}

// setup main null0 runtime
if (!null0_engine_init(filename)) {
if (!null0_engine_init(app)) {
return false;
}
return null0_init();
Expand All @@ -37,14 +30,6 @@ void Event(pntr_app* app, pntr_app_event* event) {
}

pntr_app Main(int argc, char* argv[]) {
if (argc != 2) {
printf("Usage: null0 <CART>\n");
} else {
filename = argv[1];
}

printf("Pixel format: %d\n", PNTR_PIXELFORMAT);

#ifdef PNTR_APP_RAYLIB
SetTraceLogLevel(LOG_WARNING);
#endif
Expand Down
12 changes: 9 additions & 3 deletions null0_api/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
add_library(null0_api src/main.c)

if (NULL0_HOST_TYPE_RAYLIB)
target_link_libraries(null0_api raylib pntr pntr_app physfs-static)
target_link_libraries(null0_api raylib pntr pntr_app pntr_physfs physfs-static)
endif()

if (NULL0_HOST_TYPE_SDL)
target_link_libraries(null0_api ${SDL2_LIBRARIES} pntr pntr_app physfs-static)
target_link_libraries(null0_api ${SDL2_LIBRARIES} pntr pntr_app pntr_physfs physfs-static)
endif()

if (NULL0_HOST_TYPE_WEB)
target_link_libraries(null0_api pntr pntr_app physfs-static)
target_link_libraries(null0_api pntr pntr_app pntr_physfs physfs-static)
endif()

if (NULL0_HOST_TYPE_RETRO)
Expand All @@ -24,10 +24,16 @@ if (NULL0_HOST_TYPE_RETRO)
target_link_libraries(null0_api
pntr
pntr_app
pntr_physfs
physfs-static
libretro-audio
)
target_include_directories(null0_api PRIVATE
${libretrocommon_SOURCE_DIR}/include
)

target_compile_definitions(physfs-static PRIVATE
-DPHYSFS_PLATFORM_LIBRETRO_NO_THREADS
-D__LIBRETRO__
)
endif()
12 changes: 5 additions & 7 deletions null0_api/src/null0_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
unsigned char* null0_file_read(char* filename, uint32_t* bytesRead);
bool null0_file_write(char* filename, unsigned char* data, uint32_t byteSize);

#define PNTR_LOAD_FILE null0_file_read
#define PNTR_SAVE_FILE null0_file_write
#define PNTR_PHYSFS_IMPLEMENTATION
#include <pntr_physfs.h>

#define PNTR_ENABLE_MATH
#define PNTR_ENABLE_JPEG
Expand Down Expand Up @@ -83,11 +83,9 @@ int null0_button_map_key(int key) {
}

// call this in your own host's init function to setup basic frame of things
bool null0_engine_init(char* filename) {
if (filename != NULL) {
if (!null0_init_filesystem(filename)) {
return false;
}
bool null0_engine_init(pntr_app* app) {
if (!null0_init_filesystem(app)) {
return false;
}

// default font is 0
Expand Down
82 changes: 47 additions & 35 deletions null0_api/src/null0_api_filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,70 @@
#include <string.h>
#include "physfs.h"

#ifdef PNTR_APP_LIBRETRO
// TODO: Wrap this into a pntr_app_libretro_environ_cb() function.
extern static retro_environment_t environ_cb;
#endif

char** null0_file_list_array;

char* null0_writable_dir;
const char* null0_writable_dir;

// intialize filesystem
bool null0_init_filesystem(char* cart) {
if (!PHYSFS_init("/")) {
bool null0_init_filesystem(pntr_app* app) {
#ifdef PNTR_APP_LIBRETRO
if (!PHYSFS_init(environ_cb)) {
#else
if (!PHYSFS_init(app->argv != NULL ? app->argv[0] : NULL)) {
#endif
printf("Could not init filesystem.\n");
return false;
}
char* cartName = strtok(basename(cart), ".");

if (strlen(cartName) > 127) {
printf("Name is too long.\n");
return false;
}
// Mount the current working directory, so that we're able to load the app->argv[1] file.
PHYSFS_mount(".", NULL, 1);

char pathname[134];
snprintf(pathname, 134, "null0-%s", cartName);
unsigned int size;
void* cart_data = pntr_app_load_arg_file(app, &size);

null0_writable_dir = PHYSFS_getPrefDir("null0", pathname);
if (app->argc > 0) {
// TODO: Find the base directory of the app->argv[0] current file.
null0_writable_dir = app->argv[0];
}
else {
null0_writable_dir = PHYSFS_getBaseDir();
}

if (!PHYSFS_mount(cart, NULL, 1)) {
PHYSFS_deinit();
printf("Could not mount filesystem.\n");
return false;
printf("Stuff: %s\n", app->argv[0]);

if (cart_data == NULL) {
// Mount the directory itself.
if (!PHYSFS_mount(app->argv[0], NULL, 0)) {
PHYSFS_deinit();
printf("Could not mount directory.\n");
return false;
}
}
else {
// Mount the cart data.
int success = PHYSFS_mountMemory(cart_data, size, NULL, "cart.zip", NULL, 0);
pntr_unload_memory(cart_data);
if (success == 0) {
PHYSFS_deinit();
printf("Could not mount cart data.\n");
return false;
}
}

// put null0_writable_dir at end of search-path (so user can overwrite any files)
if (!PHYSFS_mount(null0_writable_dir, NULL, 1)) {
PHYSFS_deinit();
printf("Could not mount write-dir.\n");
return false;
// Don't error out, just report the error.
}

if (!PHYSFS_setWriteDir(null0_writable_dir)) {
PHYSFS_deinit();
printf("Could not set write-dir.\n");
return false;
// Don't error out, just report the error
}

return true;
Expand All @@ -69,25 +94,12 @@ PHYSFS_Stat null0_file_info(char* filename) {

// Read a file from cart
unsigned char* null0_file_read(char* filename, uint32_t* bytesRead) {
PHYSFS_File* f = PHYSFS_openRead(filename);
PHYSFS_Stat i = null0_file_info(filename);

unsigned char* b = (unsigned char*)malloc(i.filesize);
PHYSFS_sint64 br = PHYSFS_readBytes(f, b, i.filesize);
*bytesRead = br;
PHYSFS_close(f);
return b;
return pntr_physfs_load_file((const char*)filename, bytesRead);
}

// Write a file to persistant storage
bool null0_file_write(char* filename, unsigned char* data, uint32_t byteSize) {
PHYSFS_File* f = PHYSFS_openWrite(filename);
PHYSFS_sint64 bytesWritten = PHYSFS_writeBytes(f, data, byteSize);
PHYSFS_close(f);
if (byteSize != bytesWritten) {
return false;
}
return true;
return pntr_physfs_save_file((const char*)filename, data, byteSize);
}

// Write a file to persistant storage, appending to the end
Expand All @@ -108,6 +120,6 @@ char** null0_file_list(char* dir) {
}

// Get the user's writable dir (where file writes or appends go)
char* null0_get_write_dir() {
const char* null0_get_write_dir() {
return null0_writable_dir;
}
2 changes: 1 addition & 1 deletion null0_api/src/null0_api_wamr.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static uint32_t wamr_null0_file_read(wasm_exec_env_t exec_env, char* filename, u

// Get the user's writable dir (where file writes or appends go)
static uint32_t wamr_null0_get_write_dir(wasm_exec_env_t exec_env) {
char* d = null0_get_write_dir();
char* d = (char*)null0_get_write_dir();
int s = strlen(d);
d[s] = 0;
return wasm_runtime_module_dup_data(module_inst, d, s + 1);
Expand Down
1 change: 1 addition & 0 deletions tools/cmake/Findnull0.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ if(HOST_TYPE)
find_package(pntr REQUIRED)
find_package(pntr_app REQUIRED)
find_package(pntr_app_sfx REQUIRED)
find_package(pntr_physfs REQUIRED)
find_package(physfs REQUIRED)

add_subdirectory(null0_api)
Expand Down
4 changes: 3 additions & 1 deletion tools/cmake/Findphysfs.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FetchContent_Declare(physfs
URL https://github.com/icculus/physfs/archive/refs/heads/master.zip
# PhysFS fork with libretro VFS support
# https://github.com/icculus/physfs/pull/83
URL https://github.com/RobLoach/physfs/archive/refs/heads/libretro-support.zip
)
FetchContent_MakeAvailable(physfs)

Expand Down
4 changes: 4 additions & 0 deletions tools/cmake/Findpntr_physfs.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FetchContent_Declare(pntr_physfs
URL https://github.com/RobLoach/pntr_physfs/archive/refs/heads/master.zip
)
FetchContent_MakeAvailable(pntr_physfs)