int npk_add(lua_State* L) { npkg* pkg = _npkg::_pkgs[lua_tointeger(L, -3)]; const char* file = lua_tostring(L, -2); const char* ent = lua_tostring(L, -1); NPK_ENTITY entity = 0; if(pkg && file && ent) if(!isDirectory(file)) npk_package_add_file(pkg->pkg, file, ent, &entity); return 0; }
MPackageEnt MPackageManagerNPK::addFileToPackage(const char* filename, MPackage package, const char* entityName) { #ifdef M_PACKAGE_WRITABLE NPK_ENTITY entity = 0; if(filename && package && entityName) { npk_package_add_file(((MPackageNPK*)package)->package, filename, entityName, &entity); } return entity; #endif return 0; }
void adddir(npkg* pkg, const char* _dir, const char* root) { DIR* dir = opendir(_dir); NPK_ENTITY entity; struct dirent* ent; if(dir == NULL) return; while((ent = readdir(dir)) != NULL) { if(strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue; char src[0xff]; snprintf(src, 0xff, "%s/%s", _dir, ent->d_name); const char* dest = src; if(strstr(dest, root) == dest) dest = dest + strlen(root); while(*dest == '/') dest++; if(isDirectory(src)) adddir(pkg, src, root); else npk_package_add_file(pkg->pkg, src, dest, &entity); } }
int libnpk_streamable( int argc, char * argv [] ) { int teakey[4] = {1,2,3,4}; NPK_PACKAGE pack; NPK_ENTITY entity; // create a pack CHECK( NPK_SUCCESS == npk_package_alloc( &pack, teakey ) ); CHECK( NPK_SUCCESS == npk_package_add_file( pack, "sample.txt", "sample.txt", &entity ) ); CHECK( NPK_SUCCESS == npk_package_add_file( pack, "sample.txt", "tea.txt", &entity ) ); CHECK( NPK_SUCCESS == npk_entity_set_flag( entity, NPK_ENTITY_ENCRYPT_TEA ) ); CHECK( NPK_SUCCESS == npk_package_add_file( pack, "sample.txt", "xxtea.txt", &entity ) ); CHECK( NPK_SUCCESS == npk_entity_set_flag( entity, NPK_ENTITY_ENCRYPT_XXTEA ) ); CHECK( NPK_SUCCESS == npk_package_add_file( pack, "sample.txt", "zip.txt", &entity ) ); CHECK( NPK_SUCCESS == npk_entity_set_flag( entity, NPK_ENTITY_COMPRESS_ZLIB ) ); CHECK( NPK_SUCCESS == npk_package_save( pack, "foo.npk", true ) ); npk_package_close( pack ); // simulate download int rh = open( "foo.npk", O_RDONLY | O_BINARY ); size_t filesize = npk_seek( rh, 0, SEEK_END ); npk_seek( rh, 0, SEEK_SET ); int wh = open( "foo_2.npk", O_CREAT | O_RDWR | O_TRUNC | O_BINARY, S_IREAD | S_IWRITE ); // validation std::string entityNames[4] = { "sample.txt", "tea.txt", "xxtea.txt", "zip.txt" }; pack = 0; int i = 0; size_t offset = 0; char buf[255]; while( offset < filesize ) { size_t r = rand()%16; if( r + offset > filesize ) r = filesize - offset; read( rh, &buf, sizeof(char)*r ); write( wh, buf, sizeof(char)*r ); printf( "offset %ld, reading %ld byte(s).\n", offset, r ); offset += r; if( pack == 0 ) { pack = npk_package_open( "foo_2.npk", teakey ); if( pack != 0 ) printf( " package loaded.\n" ); } else { NPK_ENTITY entity = npk_package_get_entity( pack, entityNames[i].c_str() ); CHECK( entity != NULL ); NPK_SIZE size = npk_entity_get_size( entity ); if( npk_entity_is_ready( entity ) ) { printf( " entity %s ready.\n", entityNames[i].c_str() ); void* buf = malloc( size ); CHECK( npk_entity_read( entity, buf ) ); CHECK_EQUAL_STR_WITH_FILE( (const char*)buf, "sample.txt" ); free( buf ); ++i; } } } npk_package_close( pack ); close( wh ); close( rh ); return 0; }