From 69c74aa130b37a1d00c615cc8820736f49dd46b6 Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Sun, 3 Sep 2023 18:13:51 -0400 Subject: [PATCH 1/6] assetsys: Add assetsys_mount_data() to mount from an archive data buffer This change attempts to tackle #53 and adds a `assetsys_mount_data()` function to mount data from an archive data buffer. This paves the path to allow disabling STDIO, and cases in which the target already has the file loaded into memory. It splits the `assetsys_mount()` into two different internal functions to save on code duplication. These functions may or may not be named correctly: - `assetsys_internal_create_mount()`: Prepares a new mount for the assetsys instance - `assetsys_internal_mount_zip(): Iterates through the mount system and indexes the files --- assetsys.h | 278 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 172 insertions(+), 106 deletions(-) diff --git a/assetsys.h b/assetsys.h index 341c094..bc548db 100644 --- a/assetsys.h +++ b/assetsys.h @@ -254,6 +254,15 @@ If multiple mounts contains the same file and it is accessible through the same `mount_as` prefix or not), the last mounted data source will be used when loading that file. +assetsys_mount_data +------------------- + + assetsys_error_t assetsys_mount( assetsys_t* sys, void const* data, size_t size, char const* mount_as ) + +Same as assetsys_mount(), but this function mounts a given data buffer represented by an archive file (a standard .zip +file, with or without compression). + + assetsys_dismount ----------------- @@ -5857,6 +5866,163 @@ static void assetsys_internal_recurse_directories( assetsys_t* sys, int const co assetsys_internal_dir_close( &dir ); } +/** + * Mount the given internal mount data into the assetsys instance. + * + * @internal + */ +assetsys_error_t assetsys_internal_mount_zip( assetsys_t* sys, struct assetsys_internal_mount_t* mount ) + { + int count = (int) mz_zip_reader_get_num_files( &mount->zip ); + + for( int i = 0; i < count; ++i ) + { + if( mz_zip_reader_is_file_a_directory( &mount->zip, (mz_uint) i ) ) + { + if( mount->dirs_count >= mount->dirs_capacity ) + { + mount->dirs_capacity *= 2; + struct assetsys_internal_folder_t* new_dirs = (struct assetsys_internal_folder_t*) ASSETSYS_MALLOC( + sys->memctx, sizeof( *(mount->dirs) ) * mount->dirs_capacity ); + memcpy( new_dirs, mount->dirs, sizeof( *(mount->dirs) ) * mount->dirs_count ); + ASSETSYS_FREE( sys->memctx, mount->dirs ); + mount->dirs = new_dirs; + } + + char filename[ 1024 ]; + mz_zip_reader_get_filename( &mount->zip, (mz_uint) i, filename, sizeof( filename ) ); + + struct assetsys_internal_folder_t* as_dir = &mount->dirs[ mount->dirs_count++ ]; + strcpy( sys->temp, assetsys_internal_get_string( sys, mount->mounted_as ) ); + strcat( sys->temp, "/" ); + strcat( sys->temp, filename ); + sys->temp[ strlen( sys->temp ) - 1 ] = '\0'; + as_dir->collated_index = assetsys_internal_register_collated( sys, sys->temp, 0 ); + } + } + + for( int i = 0; i < count; ++i ) + { + if( !mz_zip_reader_is_file_a_directory( &mount->zip, (mz_uint) i ) ) + { + if( mount->files_count >= mount->files_capacity ) + { + mount->files_capacity *= 2; + struct assetsys_internal_file_t* new_files = (struct assetsys_internal_file_t*) ASSETSYS_MALLOC( + sys->memctx, sizeof( *(mount->files) ) * mount->files_capacity ); + memcpy( new_files, mount->files, sizeof( *(mount->files) ) * mount->files_count ); + ASSETSYS_FREE( sys->memctx, mount->files ); + mount->files = new_files; + } + + mz_zip_archive_file_stat stat; + mz_bool result = mz_zip_reader_file_stat( &mount->zip, (mz_uint) i, &stat); + if( !result ) + { + mz_zip_reader_end( &mount->zip ); + ASSETSYS_FREE( sys->memctx, mount->dirs ); + ASSETSYS_FREE( sys->memctx, mount->files ); + return ASSETSYS_ERROR_FAILED_TO_READ_ZIP; + } + + struct assetsys_internal_file_t* file = &mount->files[ mount->files_count++ ]; + strcpy( sys->temp, assetsys_internal_get_string( sys, mount->mounted_as ) ); + strcat( sys->temp, "/" ); + strcat( sys->temp, stat.m_filename ); + file->collated_index = assetsys_internal_register_collated( sys, sys->temp, 1 ); + file->size = (int) stat.m_uncomp_size; + file->zip_index = i; + + char* dir_path = assetsys_internal_dirname( sys->temp ); + ASSETSYS_U64 handle = strpool_inject( &sys->strpool, dir_path, (int) strlen( dir_path ) - 1 ); + int found = 0; + for( int j = 0; j < mount->dirs_count; ++j ) + { + if( handle == sys->collated[ mount->dirs[ j ].collated_index ].path ) + found = 1; + } + if( !found ) + { + struct assetsys_internal_folder_t* as_dir = &mount->dirs[ mount->dirs_count++ ]; + as_dir->collated_index = assetsys_internal_register_collated( sys, + assetsys_internal_get_string( sys, handle ), 0 ); + } + } + } + + return ASSETSYS_SUCCESS; + } + +/** + * Creates an internal mount object for use by assetsys. + * + * @internal + */ +struct assetsys_internal_mount_t* assetsys_internal_create_mount( assetsys_t* sys, enum assetsys_internal_mount_type_t type, char const* path, char const* mount_as ) + { + if( sys->mounts_count >= sys->mounts_capacity ) + { + sys->mounts_capacity *= 2; + struct assetsys_internal_mount_t* new_mounts = (struct assetsys_internal_mount_t*) ASSETSYS_MALLOC( sys->memctx, + sizeof( *sys->mounts ) * sys->mounts_capacity ); + memcpy( new_mounts, sys->mounts, sizeof( *sys->mounts ) * sys->mounts_count ); + ASSETSYS_FREE( sys->memctx, sys->mounts ); + sys->mounts = new_mounts; + } + + struct assetsys_internal_mount_t* mount = &sys->mounts[ sys->mounts_count ]; + + mount->mounted_as = assetsys_internal_add_string( sys, mount_as ? mount_as : "" ); + mount->mount_len = mount_as ? (int) strlen( mount_as ) : 0; + mount->path = assetsys_internal_add_string( sys, path ); + mount->type = type; + + mount->files_count = 0; + mount->files_capacity = 4096; + mount->files = (struct assetsys_internal_file_t*) ASSETSYS_MALLOC( sys->memctx, + sizeof( *(mount->files) ) * mount->files_capacity ); + + mount->dirs_count = 0; + mount->dirs_capacity = 1024; + mount->dirs = (struct assetsys_internal_folder_t*) ASSETSYS_MALLOC( sys->memctx, + sizeof( *(mount->dirs) ) * mount->dirs_capacity ); + + return mount; + } + +assetsys_error_t assetsys_mount_data( assetsys_t* sys, void const* data, size_t size, char const* mount_as) + { + if (!data) return ASSETSYS_ERROR_INVALID_PARAMETER; + if( !mount_as ) return ASSETSYS_ERROR_INVALID_PARAMETER; + if( strchr( mount_as, '\\' ) ) return ASSETSYS_ERROR_INVALID_PATH; + int mount_len = (int) strlen( mount_as ); + if( mount_len == 0 || mount_as[ 0 ] != '/' || ( mount_len > 1 && mount_as[ mount_len - 1 ] == '/' ) ) + return ASSETSYS_ERROR_INVALID_PATH; + + struct assetsys_internal_mount_t* mount = assetsys_internal_create_mount(sys, ASSETSYS_INTERNAL_MOUNT_TYPE_ZIP, "data", mount_as); + + memset( &mount->zip, 0, sizeof( mount->zip ) ); + mount->zip.m_pAlloc = assetsys_internal_mz_alloc; + mount->zip.m_pRealloc = assetsys_internal_mz_realloc; + mount->zip.m_pFree = assetsys_internal_mz_free; + mount->zip.m_pAlloc_opaque = sys->memctx; + mz_bool status = mz_zip_reader_init_mem( &mount->zip, data, size, 0 ); + if( !status ) + { + ASSETSYS_FREE( sys->memctx, mount->dirs ); + ASSETSYS_FREE( sys->memctx, mount->files ); + return ASSETSYS_ERROR_FAILED_TO_READ_ZIP; + } + + assetsys_error_t result = assetsys_internal_mount_zip( sys, mount ); + if( result != ASSETSYS_SUCCESS ) + return result; + + assetsys_internal_collate_directories( sys, mount ); + + ++sys->mounts_count; + return ASSETSYS_SUCCESS; + } assetsys_error_t assetsys_mount( assetsys_t* sys, char const* path, char const* mount_as ) { @@ -5890,38 +6056,12 @@ assetsys_error_t assetsys_mount( assetsys_t* sys, char const* path, char const* return ASSETSYS_ERROR_INVALID_PATH; } - if( sys->mounts_count >= sys->mounts_capacity ) - { - sys->mounts_capacity *= 2; - struct assetsys_internal_mount_t* new_mounts = (struct assetsys_internal_mount_t*) ASSETSYS_MALLOC( sys->memctx, - sizeof( *sys->mounts ) * sys->mounts_capacity ); - memcpy( new_mounts, sys->mounts, sizeof( *sys->mounts ) * sys->mounts_count ); - ASSETSYS_FREE( sys->memctx, sys->mounts ); - sys->mounts = new_mounts; - } + struct assetsys_internal_mount_t* mount = assetsys_internal_create_mount(sys, type, path, mount_as); - struct assetsys_internal_mount_t* mount = &sys->mounts[ sys->mounts_count ]; - - mount->mounted_as = assetsys_internal_add_string( sys, mount_as ? mount_as : "" ); - mount->mount_len = mount_as ? (int) strlen( mount_as ) : 0; - mount->path = assetsys_internal_add_string( sys, path ); - mount->type = type; - - mount->files_count = 0; - mount->files_capacity = 4096; - mount->files = (struct assetsys_internal_file_t*) ASSETSYS_MALLOC( sys->memctx, - sizeof( *(mount->files) ) * mount->files_capacity ); - - mount->dirs_count = 0; - mount->dirs_capacity = 1024; - mount->dirs = (struct assetsys_internal_folder_t*) ASSETSYS_MALLOC( sys->memctx, - sizeof( *(mount->dirs) ) * mount->dirs_capacity ); - - struct assetsys_internal_folder_t* dir = &mount->dirs[ mount->dirs_count++ ]; - dir->collated_index = assetsys_internal_register_collated( sys, mount_as, 0 ); - if( type == ASSETSYS_INTERNAL_MOUNT_TYPE_DIR ) { + struct assetsys_internal_folder_t* dir = &mount->dirs[ mount->dirs_count++ ]; + dir->collated_index = assetsys_internal_register_collated( sys, mount_as, 0 ); assetsys_internal_recurse_directories( sys, dir->collated_index, mount ); } else if( type == ASSETSYS_INTERNAL_MOUNT_TYPE_ZIP ) @@ -5939,84 +6079,10 @@ assetsys_error_t assetsys_mount( assetsys_t* sys, char const* path, char const* return ASSETSYS_ERROR_FAILED_TO_READ_ZIP; } - int count = (int) mz_zip_reader_get_num_files( &mount->zip ); - - for( int i = 0; i < count; ++i ) - { - if( mz_zip_reader_is_file_a_directory( &mount->zip, (mz_uint) i ) ) - { - if( mount->dirs_count >= mount->dirs_capacity ) - { - mount->dirs_capacity *= 2; - struct assetsys_internal_folder_t* new_dirs = (struct assetsys_internal_folder_t*) ASSETSYS_MALLOC( - sys->memctx, sizeof( *(mount->dirs) ) * mount->dirs_capacity ); - memcpy( new_dirs, mount->dirs, sizeof( *(mount->dirs) ) * mount->dirs_count ); - ASSETSYS_FREE( sys->memctx, mount->dirs ); - mount->dirs = new_dirs; - } - - char filename[ 1024 ]; - mz_zip_reader_get_filename( &mount->zip, (mz_uint) i, filename, sizeof( filename ) ); - - struct assetsys_internal_folder_t* as_dir = &mount->dirs[ mount->dirs_count++ ]; - strcpy( sys->temp, assetsys_internal_get_string( sys, mount->mounted_as ) ); - strcat( sys->temp, "/" ); - strcat( sys->temp, filename ); - sys->temp[ strlen( sys->temp ) - 1 ] = '\0'; - as_dir->collated_index = assetsys_internal_register_collated( sys, sys->temp, 0 ); - } - } - - for( int i = 0; i < count; ++i ) - { - if( !mz_zip_reader_is_file_a_directory( &mount->zip, (mz_uint) i ) ) - { - if( mount->files_count >= mount->files_capacity ) - { - mount->files_capacity *= 2; - struct assetsys_internal_file_t* new_files = (struct assetsys_internal_file_t*) ASSETSYS_MALLOC( - sys->memctx, sizeof( *(mount->files) ) * mount->files_capacity ); - memcpy( new_files, mount->files, sizeof( *(mount->files) ) * mount->files_count ); - ASSETSYS_FREE( sys->memctx, mount->files ); - mount->files = new_files; - } - - mz_zip_archive_file_stat stat; - mz_bool result = mz_zip_reader_file_stat( &mount->zip, (mz_uint) i, &stat); - if( !result ) - { - mz_zip_reader_end( &mount->zip ); - ASSETSYS_FREE( sys->memctx, mount->dirs ); - ASSETSYS_FREE( sys->memctx, mount->files ); - return ASSETSYS_ERROR_FAILED_TO_READ_ZIP; - } - - struct assetsys_internal_file_t* file = &mount->files[ mount->files_count++ ]; - strcpy( sys->temp, assetsys_internal_get_string( sys, mount->mounted_as ) ); - strcat( sys->temp, "/" ); - strcat( sys->temp, stat.m_filename ); - file->collated_index = assetsys_internal_register_collated( sys, sys->temp, 1 ); - file->size = (int) stat.m_uncomp_size; - file->zip_index = i; - - char* dir_path = assetsys_internal_dirname( sys->temp ); - ASSETSYS_U64 handle = strpool_inject( &sys->strpool, dir_path, (int) strlen( dir_path ) - 1 ); - int found = 0; - for( int j = 0; j < mount->dirs_count; ++j ) - { - if( handle == sys->collated[ mount->dirs[ j ].collated_index ].path ) - found = 1; - } - if( !found ) - { - struct assetsys_internal_folder_t* as_dir = &mount->dirs[ mount->dirs_count++ ]; - as_dir->collated_index = assetsys_internal_register_collated( sys, - assetsys_internal_get_string( sys, handle ), 0 ); - } - } - } + assetsys_error_t result = assetsys_internal_mount_zip( sys, mount ); + if( result != ASSETSYS_SUCCESS ) + return result; } - assetsys_internal_collate_directories( sys, mount ); From 877be33ab995acd0355772cbbc690db791feb6b7 Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Sun, 3 Sep 2023 18:48:00 -0400 Subject: [PATCH 2/6] assetsys: Add documentation on assetsys_mount_data() --- docs/assetsys.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/assetsys.md b/docs/assetsys.md index 07f4278..a0b2f25 100644 --- a/docs/assetsys.md +++ b/docs/assetsys.md @@ -190,6 +190,13 @@ If multiple mounts contains the same file and it is accessible through the same `mount_as` prefix or not), the last mounted data source will be used when loading that file. +assetsys_mount_data +------------------- + + assetsys_error_t assetsys_mount_data( assetsys_t* sys, void const* data, size_t size, char const* mount_as ) + +Same as assetsys_mount(), but takes a data buffer of an archived .zip file, along with the size of the file. + assetsys_dismount ----------------- From 26dcde8cb2188dc9aa98988e2e2f82ef26db4d69 Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Mon, 4 Sep 2023 00:26:37 -0400 Subject: [PATCH 3/6] assetsys: Rename assetsys_internal_mount_zip to assetsys_internal_mount_files --- assetsys.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/assetsys.h b/assetsys.h index bc548db..2e68f43 100644 --- a/assetsys.h +++ b/assetsys.h @@ -5871,7 +5871,7 @@ static void assetsys_internal_recurse_directories( assetsys_t* sys, int const co * * @internal */ -assetsys_error_t assetsys_internal_mount_zip( assetsys_t* sys, struct assetsys_internal_mount_t* mount ) +assetsys_error_t assetsys_internal_mount_files( assetsys_t* sys, struct assetsys_internal_mount_t* mount ) { int count = (int) mz_zip_reader_get_num_files( &mount->zip ); @@ -6014,7 +6014,7 @@ assetsys_error_t assetsys_mount_data( assetsys_t* sys, void const* data, size_t return ASSETSYS_ERROR_FAILED_TO_READ_ZIP; } - assetsys_error_t result = assetsys_internal_mount_zip( sys, mount ); + assetsys_error_t result = assetsys_internal_mount_files( sys, mount ); if( result != ASSETSYS_SUCCESS ) return result; @@ -6079,7 +6079,7 @@ assetsys_error_t assetsys_mount( assetsys_t* sys, char const* path, char const* return ASSETSYS_ERROR_FAILED_TO_READ_ZIP; } - assetsys_error_t result = assetsys_internal_mount_zip( sys, mount ); + assetsys_error_t result = assetsys_internal_mount_files( sys, mount ); if( result != ASSETSYS_SUCCESS ) return result; } From fe23cb0e31654ae66ac8bdba51aa89938c0ac605 Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Tue, 5 Sep 2023 12:51:24 -0400 Subject: [PATCH 4/6] assetsys: Rename to assetsys_mount_from_memory(), and add a test --- assetsys.h | 85 +++++++++++++++++++++++++++++++++++++++--------- docs/assetsys.md | 6 ++-- 2 files changed, 73 insertions(+), 18 deletions(-) diff --git a/assetsys.h b/assetsys.h index bc548db..65da978 100644 --- a/assetsys.h +++ b/assetsys.h @@ -254,13 +254,12 @@ If multiple mounts contains the same file and it is accessible through the same `mount_as` prefix or not), the last mounted data source will be used when loading that file. -assetsys_mount_data +assetsys_mount_from_memory ------------------- - assetsys_error_t assetsys_mount( assetsys_t* sys, void const* data, size_t size, char const* mount_as ) + assetsys_error_t assetsys_mount_from_memory( assetsys_t* sys, void const* data, size_t size, char const* mount_as ) -Same as assetsys_mount(), but this function mounts a given data buffer represented by an archive file (a standard .zip -file, with or without compression). +Same as `assetsys_mount()`, but takes a data buffer of an archived *.zip* file, along with the size of the file. assetsys_dismount @@ -5366,6 +5365,7 @@ void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char #define _CRT_NONSTDC_NO_DEPRECATE #define _CRT_SECURE_NO_WARNINGS #include +#include // FILE, fopen, fseet, SEEK_END, SEEK_SET, ftell, fread #include "strpool.h" @@ -5986,11 +5986,20 @@ struct assetsys_internal_mount_t* assetsys_internal_create_mount( assetsys_t* sy mount->dirs_capacity = 1024; mount->dirs = (struct assetsys_internal_folder_t*) ASSETSYS_MALLOC( sys->memctx, sizeof( *(mount->dirs) ) * mount->dirs_capacity ); + + if( type == ASSETSYS_INTERNAL_MOUNT_TYPE_ZIP ) + { + memset( &mount->zip, 0, sizeof( mount->zip ) ); + mount->zip.m_pAlloc = assetsys_internal_mz_alloc; + mount->zip.m_pRealloc = assetsys_internal_mz_realloc; + mount->zip.m_pFree = assetsys_internal_mz_free; + mount->zip.m_pAlloc_opaque = sys->memctx; + } return mount; } -assetsys_error_t assetsys_mount_data( assetsys_t* sys, void const* data, size_t size, char const* mount_as) +assetsys_error_t assetsys_mount_from_memory( assetsys_t* sys, void const* data, size_t size, char const* mount_as) { if (!data) return ASSETSYS_ERROR_INVALID_PARAMETER; if( !mount_as ) return ASSETSYS_ERROR_INVALID_PARAMETER; @@ -6001,11 +6010,6 @@ assetsys_error_t assetsys_mount_data( assetsys_t* sys, void const* data, size_t struct assetsys_internal_mount_t* mount = assetsys_internal_create_mount(sys, ASSETSYS_INTERNAL_MOUNT_TYPE_ZIP, "data", mount_as); - memset( &mount->zip, 0, sizeof( mount->zip ) ); - mount->zip.m_pAlloc = assetsys_internal_mz_alloc; - mount->zip.m_pRealloc = assetsys_internal_mz_realloc; - mount->zip.m_pFree = assetsys_internal_mz_free; - mount->zip.m_pAlloc_opaque = sys->memctx; mz_bool status = mz_zip_reader_init_mem( &mount->zip, data, size, 0 ); if( !status ) { @@ -6066,11 +6070,10 @@ assetsys_error_t assetsys_mount( assetsys_t* sys, char const* path, char const* } else if( type == ASSETSYS_INTERNAL_MOUNT_TYPE_ZIP ) { - memset( &mount->zip, 0, sizeof( mount->zip ) ); - mount->zip.m_pAlloc = assetsys_internal_mz_alloc; - mount->zip.m_pRealloc = assetsys_internal_mz_realloc; - mount->zip.m_pFree = assetsys_internal_mz_free; - mount->zip.m_pAlloc_opaque = sys->memctx; +#ifdef MINIZ_NO_STDIO + // If we explicitly disable stdio. + return ASSETSYS_ERROR_FAILED_TO_READ_ZIP; +#else mz_bool status = mz_zip_reader_init_file( &mount->zip, path, 0 ); if( !status ) { @@ -6082,6 +6085,7 @@ assetsys_error_t assetsys_mount( assetsys_t* sys, char const* path, char const* assetsys_error_t result = assetsys_internal_mount_zip( sys, mount ); if( result != ASSETSYS_SUCCESS ) return result; +#endif } assetsys_internal_collate_directories( sys, mount ); @@ -6421,6 +6425,57 @@ void test_assetsys( void ) { assetsys_destroy( assetsys ); TESTFW_TEST_END(); + + TESTFW_TEST_BEGIN( "Test mounting data and loading file" ); + { + // Zip file with a test.txt file containing "Hello, World!" + const char data[] = { + 0x50, 0x4b, 0x03, 0x04, 0x0a, 0x03, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x9e, 0x23, 0x57, 0x84, 0x9e, + 0xe8, 0xb4, 0x0e, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x74, 0x65, + 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x57, 0x6f, 0x72, + 0x6c, 0x64, 0x21, 0x0a, 0x50, 0x4b, 0x01, 0x02, 0x3f, 0x03, 0x0a, 0x03, 0x00, 0x00, 0x00, 0x00, + 0xfc, 0x9e, 0x23, 0x57, 0x84, 0x9e, 0xe8, 0xb4, 0x0e, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, + 0x08, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x80, 0xb4, 0x81, 0x00, 0x00, + 0x00, 0x00, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74, 0x0a, 0x00, 0x20, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x0e, 0xf2, 0x2d, 0xc2, 0xde, 0xd9, 0x01, 0x00, 0x0e, + 0xf2, 0x2d, 0xc2, 0xde, 0xd9, 0x01, 0x00, 0x0e, 0xf2, 0x2d, 0xc2, 0xde, 0xd9, 0x01, 0x50, 0x4b, + 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x5a, 0x00, 0x00, 0x00, 0x34, 0x00, + 0x00, 0x00, 0x00, 0x00 + }; + const size_t data_size = 164; + + // Create the asset system + assetsys_t* assetsys = assetsys_create( 0 ); + TESTFW_EXPECTED( assetsys != NULL ); + + // Mount current working folder as a virtual "/data" path + TESTFW_EXPECTED( assetsys_mount_from_memory( assetsys, data, data_size, "/data" ) == ASSETSYS_SUCCESS ); + + // Load a file + assetsys_file_t file; + TESTFW_EXPECTED( assetsys_file( assetsys, "/data/test.txt", &file ) == ASSETSYS_SUCCESS ); + + // Find the size of the file + int size = assetsys_file_size( assetsys, file ); + TESTFW_EXPECTED( size > 10 ); + + // Load the file + char* content = (char*) malloc( size + 1 ); // extra space for '\0' + int loaded_size = 0; + TESTFW_EXPECTED( assetsys_file_load( assetsys, file, &loaded_size, content, size ) == ASSETSYS_SUCCESS ); + content[ size ] = '\0'; // zero terminate the text file + + TESTFW_EXPECTED( content[0] == 'H' ); + TESTFW_EXPECTED( content[1] == 'e' ); + TESTFW_EXPECTED( content[2] == 'l' ); + TESTFW_EXPECTED( content[3] == 'l' ); + TESTFW_EXPECTED( content[4] == 'o' ); + + // Clean up + free( content ); + assetsys_destroy( assetsys ); + } + TESTFW_TEST_END(); } diff --git a/docs/assetsys.md b/docs/assetsys.md index a0b2f25..a249122 100644 --- a/docs/assetsys.md +++ b/docs/assetsys.md @@ -190,12 +190,12 @@ If multiple mounts contains the same file and it is accessible through the same `mount_as` prefix or not), the last mounted data source will be used when loading that file. -assetsys_mount_data +assetsys_mount_from_memory ------------------- - assetsys_error_t assetsys_mount_data( assetsys_t* sys, void const* data, size_t size, char const* mount_as ) + assetsys_error_t assetsys_mount_from_memory( assetsys_t* sys, void const* data, size_t size, char const* mount_as ) -Same as assetsys_mount(), but takes a data buffer of an archived .zip file, along with the size of the file. +Same as `assetsys_mount()`, but takes a data buffer of an archived *.zip* file, along with the size of the file. assetsys_dismount ----------------- From f39d2369c212a3efbb506f4c43e48a10b4decd86 Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Tue, 5 Sep 2023 13:13:48 -0400 Subject: [PATCH 5/6] assetsys: Have the test use unsigned char Co-authored-by: Mattias Gustavsson --- assetsys.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assetsys.h b/assetsys.h index da381cc..1f360cf 100644 --- a/assetsys.h +++ b/assetsys.h @@ -6429,7 +6429,7 @@ void test_assetsys( void ) { TESTFW_TEST_BEGIN( "Test mounting data and loading file" ); { // Zip file with a test.txt file containing "Hello, World!" - const char data[] = { + const unsigned char data[] = { 0x50, 0x4b, 0x03, 0x04, 0x0a, 0x03, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x9e, 0x23, 0x57, 0x84, 0x9e, 0xe8, 0xb4, 0x0e, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x57, 0x6f, 0x72, From 2c1df818fbb720d5e4d4b76a708da6d79afc251f Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Tue, 5 Sep 2023 13:22:00 -0400 Subject: [PATCH 6/6] assetsys: Update version number, and add contributor name --- assetsys.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/assetsys.h b/assetsys.h index 1f360cf..4a05df5 100644 --- a/assetsys.h +++ b/assetsys.h @@ -3,7 +3,7 @@ Licensing information can be found at the end of the file. ------------------------------------------------------------------------------ -assetsys.h - v1.3 - File system abstraction to read from zip-files, for C/C++. +assetsys.h - v1.4 - File system abstraction to read from zip-files, for C/C++. Do this: #define ASSETSYS_IMPLEMENTATION @@ -6515,8 +6515,10 @@ int main( int argc, char** argv ) { contributors: Randy Gaul (hotloading support) + Rob Loach (assetsys_mount_from_memory) revision history: + 1.4 allow mounting from memory 1.3 allow absolute paths when mounting, update docs for mount as root 1.2 asserts with message, eliminated a frequent small allocation 1.1 changes to support loading assets being re-saved during execution