v0.9.97d
- Ara mini usa Respak2
This commit is contained in:
82
jfile.cpp
82
jfile.cpp
@@ -5,6 +5,9 @@
|
||||
#include "jfile.h"
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -12,33 +15,18 @@
|
||||
#include <pwd.h>
|
||||
#endif
|
||||
|
||||
#define DEFAULT_FILENAME "data.jrf"
|
||||
#define DEFAULT_FILENAME "data.jf2"
|
||||
#define DEFAULT_FOLDER "data/"
|
||||
#define CONFIG_FILENAME "config.txt"
|
||||
|
||||
#pragma pack(push,1)
|
||||
struct DATA_Header {
|
||||
char magic[4];
|
||||
uint32_t num_files;
|
||||
uint32_t index_offset;
|
||||
struct file_t
|
||||
{
|
||||
std::string path;
|
||||
uint32_t size;
|
||||
uint32_t offset;
|
||||
};
|
||||
|
||||
struct DATA_Info {
|
||||
uint32_t offset;
|
||||
uint32_t length;
|
||||
char name[13];
|
||||
};
|
||||
|
||||
struct DATA_Index {
|
||||
DATA_Info* file_info;
|
||||
};
|
||||
|
||||
struct DATA_File {
|
||||
DATA_Header header;
|
||||
DATA_Index index;
|
||||
};
|
||||
|
||||
#pragma pack(pop)
|
||||
std::vector<file_t> toc;
|
||||
|
||||
/* 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 {
|
||||
@@ -47,7 +35,6 @@ struct keyvalue_t {
|
||||
|
||||
char *resource_filename = NULL;
|
||||
char *resource_folder = NULL;
|
||||
DATA_File *data_file = NULL;
|
||||
int file_source = SOURCE_FILE;
|
||||
char scratch[255];
|
||||
static std::string config_folder;
|
||||
@@ -72,18 +59,31 @@ void file_setsource(const int src) {
|
||||
|
||||
bool file_getdictionary() {
|
||||
if (resource_filename == NULL) file_setresourcefilename(DEFAULT_FILENAME);
|
||||
FILE* f = fopen(resource_filename, "rb");
|
||||
if (f) {
|
||||
data_file = (DATA_File*)malloc(sizeof(DATA_File));
|
||||
fread((char*)&data_file->header, sizeof(DATA_Header), 1, f);
|
||||
fseek(f, data_file->header.index_offset, SEEK_SET);
|
||||
data_file->index.file_info = (DATA_Info*)malloc(data_file->header.num_files * sizeof(DATA_Info));
|
||||
fread((char*)data_file->index.file_info, data_file->header.num_files * sizeof(DATA_Info), 1, f);
|
||||
fclose(f);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
|
||||
std::ifstream fi (resource_filename, std::ios::binary);
|
||||
if (!fi.is_open()) return false;
|
||||
char header[4];
|
||||
fi.read(header, 4);
|
||||
uint32_t num_files, toc_offset;
|
||||
fi.read((char*)&num_files, 4);
|
||||
fi.read((char*)&toc_offset, 4);
|
||||
fi.seekg(toc_offset);
|
||||
|
||||
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) {
|
||||
@@ -94,7 +94,7 @@ char *file_getfilenamewithfolder(const char* filename) {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -103,8 +103,8 @@ FILE *file_getfilepointer(const char *resourcename, int& filesize, const bool bi
|
||||
if (file_source==SOURCE_FILE) {
|
||||
bool found = false;
|
||||
uint32_t count = 0;
|
||||
while( !found && count < data_file->header.num_files ) {
|
||||
found = ( strcmp( resourcename, data_file->index.file_info[count].name ) == 0 );
|
||||
while( !found && count < toc.size() ) {
|
||||
found = ( std::string(resourcename) == toc[count].path );
|
||||
if( !found ) count++;
|
||||
}
|
||||
|
||||
@@ -113,20 +113,16 @@ FILE *file_getfilepointer(const char *resourcename, int& filesize, const bool bi
|
||||
exit(1);
|
||||
}
|
||||
|
||||
filesize = data_file->index.file_info[count].length;
|
||||
filesize = toc[count].size;
|
||||
|
||||
f = fopen(resource_filename, binary?"rb":"r");
|
||||
if (not f) {
|
||||
perror("No s'ha pogut obrir l'arxiu de recursos");
|
||||
exit(1);
|
||||
}
|
||||
fseek(f, data_file->index.file_info[count].offset, SEEK_SET);
|
||||
fseek(f, toc[count].offset, SEEK_SET);
|
||||
} else {
|
||||
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);
|
||||
filesize = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
|
||||
Reference in New Issue
Block a user