Modern respak used, changed pack and code

This commit is contained in:
2021-05-10 18:50:06 +02:00
parent 6fb53a6435
commit f8f181bd33
3 changed files with 92 additions and 80 deletions

BIN
data.jrf

Binary file not shown.

View File

@@ -1,90 +1,100 @@
#include "fileManager.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "fcntl.h"
#ifdef _WIN32
#include "io.h"
#else
#include "unistd.h"
#endif
#include <SDL2/SDL.h>
#include <fstream>
char *resourceFileName = "data.jrf";
#pragma pack(push,1)
void setResourceFile(char *p_resourceFileName) {
struct DATA_Header {
char magic[4];
Uint32 num_files;
Uint32 index_offset;
};
struct DATA_Info {
Uint32 offset;
Uint32 length;
char name[13];
};
struct DATA_Index {
DATA_Info* file_info;
};
struct DATA_File {
DATA_Header header;
DATA_Index index;
};
#pragma pack(pop)
static const char *resourceFileName = "data.jrf";
static DATA_File *data_file = NULL;
void file_setResourceFile(const char *p_resourceFileName) {
resourceFileName = p_resourceFileName;
}
char *GetBufferFromResource(char *resourcename, int *filesize)
{
//Try to open the resource file in question
int fd = open(resourceFileName, O_RDONLY);
if (fd < 0)
{
perror("Error opening resource file");
static void getDataFile() {
std::ifstream fd( resourceFileName, std::ios::in | std::ios::binary );
if( fd.fail() ) {
perror("No s'ha pogut obrir l'arxiu de recursos");
exit(1);
}
//Make sure we're at the beginning of the file
lseek(fd, 0, SEEK_SET);
data_file = (DATA_File*)malloc( sizeof( DATA_File ) );
//Read the first INT, which will tell us how many files are in this resource
int numfiles;
int resultat = read(fd, &numfiles, sizeof(int));
fd.read( (char*)&data_file->header, sizeof( DATA_Header ) );
#ifdef _WIN32
int final = eof(fd);
#endif
fd.seekg( data_file->header.index_offset );
//Get the pointers to the stored files
int *filestart = (int *) malloc(sizeof(int) * numfiles);
resultat = read(fd, filestart, sizeof(int) * numfiles);
data_file->index.file_info = (DATA_Info*)malloc( data_file->header.num_files * sizeof( DATA_Info ) );
//Loop through the files, looking for the file in question
int filenamesize;
char *buffer;
int i;
for(i=0;i<numfiles;i++)
{
int result = 129;
char *filename;
//Seek to the location
lseek(fd, filestart[i], SEEK_SET);
//Get the filesize value
read(fd, filesize, sizeof(int));
//Get the size of the filename string
read(fd, &filenamesize, sizeof(int));
//Size the buffer and read the filename
filename = (char *) malloc(filenamesize + 1);
result = read(fd, filename, filenamesize);
//Remember to terminate the string properly!
filename[filenamesize] = '\0';
//Compare to the string we're looking for
if (strcmp(filename, resourcename) == 0)
{
//Get the contents of the file
buffer = (char *) malloc(*filesize);
read(fd, buffer, *filesize);
free(filename);
break;
}
//Free the filename buffer
free(filename);
fd.read( (char*)data_file->index.file_info, data_file->header.num_files * sizeof( DATA_Info ) );
fd.close();
}
const char *file_setBufferFromResource(const char *resourcename, int& filesize) {
if( data_file == NULL ) {
getDataFile();
}
//Release memory
free(filestart);
bool found = false;
int count = 0;
while( !found && count < data_file->header.num_files ) {
found = ( strcmp( resourcename, data_file->index.file_info[count].name ) == 0 );
if( !found ) count++;
}
//Close the resource file!
close(fd);
//Did we find the file within the resource that we were looking for?
if (buffer == NULL)
{
printf("Unable to find '%s' in the resource file!\n", resourcename);
if( !found ) {
perror("El recurs no s'ha trobat en l'arxiu de recursos");
exit(1);
}
//Return the buffer
filesize = data_file->index.file_info[count].length;
std::ifstream fd( resourceFileName, std::ios::in | std::ios::binary );
if( fd.fail() ) {
perror("No s'ha pogut obrir l'arxiu de recursos");
exit(1);
}
fd.seekg( data_file->index.file_info[count].offset );
char* buffer = (char*)malloc( filesize );
fd.read( buffer, filesize );
fd.close();
return buffer;
}
void file_quit() {
if( data_file != NULL ) {
free( data_file->index.file_info );
free( data_file );
}
}

View File

@@ -1,5 +1,7 @@
#pragma once
void setResourceFile(char *p_resourceFileName);
void file_setResourceFile(const char *p_resourceFileName);
char *GetBufferFromResource(char *resourcename, int *filesize);
const char *file_getBufferFromResource(const char *resourcename, int& filesize);
void file_quit();