- Ara mini usa Respak2
This commit is contained in:
2024-11-26 16:15:31 +01:00
parent 377f0a238b
commit 802f32f1b8
2 changed files with 40 additions and 44 deletions

View File

@@ -5,6 +5,9 @@
#include "jfile.h" #include "jfile.h"
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <iostream>
#include <fstream>
#include <filesystem>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -12,33 +15,18 @@
#include <pwd.h> #include <pwd.h>
#endif #endif
#define DEFAULT_FILENAME "data.jrf" #define DEFAULT_FILENAME "data.jf2"
#define DEFAULT_FOLDER "data/" #define DEFAULT_FOLDER "data/"
#define CONFIG_FILENAME "config.txt" #define CONFIG_FILENAME "config.txt"
#pragma pack(push,1) struct file_t
struct DATA_Header { {
char magic[4]; std::string path;
uint32_t num_files; uint32_t size;
uint32_t index_offset;
};
struct DATA_Info {
uint32_t offset; uint32_t offset;
uint32_t length;
char name[13];
}; };
struct DATA_Index { std::vector<file_t> toc;
DATA_Info* file_info;
};
struct DATA_File {
DATA_Header header;
DATA_Index index;
};
#pragma pack(pop)
/* El std::map me fa coses rares, vaig a usar un good old std::vector amb una estructura key,value propia i au, que sempre funciona */ /* El std::map me fa coses rares, vaig a usar un good old std::vector amb una estructura key,value propia i au, que sempre funciona */
struct keyvalue_t { struct keyvalue_t {
@@ -47,7 +35,6 @@ struct keyvalue_t {
char *resource_filename = NULL; char *resource_filename = NULL;
char *resource_folder = NULL; char *resource_folder = NULL;
DATA_File *data_file = NULL;
int file_source = SOURCE_FILE; int file_source = SOURCE_FILE;
char scratch[255]; char scratch[255];
static std::string config_folder; static std::string config_folder;
@@ -72,18 +59,31 @@ void file_setsource(const int src) {
bool file_getdictionary() { bool file_getdictionary() {
if (resource_filename == NULL) file_setresourcefilename(DEFAULT_FILENAME); if (resource_filename == NULL) file_setresourcefilename(DEFAULT_FILENAME);
FILE* f = fopen(resource_filename, "rb");
if (f) { std::ifstream fi (resource_filename, std::ios::binary);
data_file = (DATA_File*)malloc(sizeof(DATA_File)); if (!fi.is_open()) return false;
fread((char*)&data_file->header, sizeof(DATA_Header), 1, f); char header[4];
fseek(f, data_file->header.index_offset, SEEK_SET); fi.read(header, 4);
data_file->index.file_info = (DATA_Info*)malloc(data_file->header.num_files * sizeof(DATA_Info)); uint32_t num_files, toc_offset;
fread((char*)data_file->index.file_info, data_file->header.num_files * sizeof(DATA_Info), 1, f); fi.read((char*)&num_files, 4);
fclose(f); fi.read((char*)&toc_offset, 4);
return true; fi.seekg(toc_offset);
} else {
return false; for (uint32_t i=0; i<num_files; ++i)
{
uint32_t file_offset, file_size;
fi.read( (char*)&file_offset, 4 );
fi.read( (char*)&file_size, 4 );
uint8_t path_size;
fi.read( (char*)&path_size, 1 );
char file_name[path_size+1];
fi.read( file_name, path_size );
file_name[path_size] = 0;
std::string filename = file_name;
toc.push_back({filename, file_size, file_offset});
} }
fi.close();
return true;
} }
char *file_getfilenamewithfolder(const char* filename) { char *file_getfilenamewithfolder(const char* filename) {
@@ -94,7 +94,7 @@ char *file_getfilenamewithfolder(const char* filename) {
FILE *file_getfilepointer(const char *resourcename, int& filesize, const bool binary) { FILE *file_getfilepointer(const char *resourcename, int& filesize, const bool binary) {
if (file_source==SOURCE_FILE and not data_file) { if (file_source==SOURCE_FILE and toc.size()==0) {
if (not file_getdictionary()) file_setsource(SOURCE_FOLDER); if (not file_getdictionary()) file_setsource(SOURCE_FOLDER);
} }
@@ -103,8 +103,8 @@ FILE *file_getfilepointer(const char *resourcename, int& filesize, const bool bi
if (file_source==SOURCE_FILE) { if (file_source==SOURCE_FILE) {
bool found = false; bool found = false;
uint32_t count = 0; uint32_t count = 0;
while( !found && count < data_file->header.num_files ) { while( !found && count < toc.size() ) {
found = ( strcmp( resourcename, data_file->index.file_info[count].name ) == 0 ); found = ( std::string(resourcename) == toc[count].path );
if( !found ) count++; if( !found ) count++;
} }
@@ -113,20 +113,16 @@ FILE *file_getfilepointer(const char *resourcename, int& filesize, const bool bi
exit(1); exit(1);
} }
filesize = data_file->index.file_info[count].length; filesize = toc[count].size;
f = fopen(resource_filename, binary?"rb":"r"); f = fopen(resource_filename, binary?"rb":"r");
if (not f) { if (not f) {
perror("No s'ha pogut obrir l'arxiu de recursos"); perror("No s'ha pogut obrir l'arxiu de recursos");
exit(1); exit(1);
} }
fseek(f, data_file->index.file_info[count].offset, SEEK_SET); fseek(f, toc[count].offset, SEEK_SET);
} else { } else {
f = fopen(file_getfilenamewithfolder(resourcename), binary?"rb":"r"); f = fopen(file_getfilenamewithfolder(resourcename), binary?"rb":"r");
if (not f) {
printf("ERROR: No s'ha pogut obrir l'arxiu '%s'\n",resourcename);
exit(1);
}
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
filesize = ftell(f); filesize = ftell(f);
fseek(f, 0, SEEK_SET); fseek(f, 0, SEEK_SET);

View File

@@ -1,3 +1,3 @@
#pragma once #pragma once
#define MINI_VERSION "0.9.96d" #define MINI_VERSION "0.9.97d"