Modern respak used, changed pack and code
This commit is contained in:
148
fileManager.cpp
148
fileManager.cpp
@@ -1,90 +1,100 @@
|
|||||||
#include "fileManager.h"
|
#include "fileManager.h"
|
||||||
#include "stdio.h"
|
#include <SDL2/SDL.h>
|
||||||
#include "stdlib.h"
|
#include <fstream>
|
||||||
#include "string.h"
|
|
||||||
#include "fcntl.h"
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include "io.h"
|
|
||||||
#else
|
|
||||||
#include "unistd.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
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;
|
resourceFileName = p_resourceFileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *GetBufferFromResource(char *resourcename, int *filesize)
|
static void getDataFile() {
|
||||||
{
|
std::ifstream fd( resourceFileName, std::ios::in | std::ios::binary );
|
||||||
//Try to open the resource file in question
|
|
||||||
int fd = open(resourceFileName, O_RDONLY);
|
if( fd.fail() ) {
|
||||||
if (fd < 0)
|
perror("No s'ha pogut obrir l'arxiu de recursos");
|
||||||
{
|
|
||||||
perror("Error opening resource file");
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Make sure we're at the beginning of the file
|
data_file = (DATA_File*)malloc( sizeof( DATA_File ) );
|
||||||
lseek(fd, 0, SEEK_SET);
|
|
||||||
|
|
||||||
//Read the first INT, which will tell us how many files are in this resource
|
fd.read( (char*)&data_file->header, sizeof( DATA_Header ) );
|
||||||
int numfiles;
|
|
||||||
int resultat = read(fd, &numfiles, sizeof(int));
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
fd.seekg( data_file->header.index_offset );
|
||||||
int final = eof(fd);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//Get the pointers to the stored files
|
data_file->index.file_info = (DATA_Info*)malloc( data_file->header.num_files * sizeof( DATA_Info ) );
|
||||||
int *filestart = (int *) malloc(sizeof(int) * numfiles);
|
|
||||||
resultat = read(fd, filestart, sizeof(int) * numfiles);
|
|
||||||
|
|
||||||
//Loop through the files, looking for the file in question
|
fd.read( (char*)data_file->index.file_info, data_file->header.num_files * sizeof( DATA_Info ) );
|
||||||
int filenamesize;
|
|
||||||
char *buffer;
|
fd.close();
|
||||||
int i;
|
}
|
||||||
for(i=0;i<numfiles;i++)
|
|
||||||
{
|
const char *file_setBufferFromResource(const char *resourcename, int& filesize) {
|
||||||
int result = 129;
|
|
||||||
char *filename;
|
if( data_file == NULL ) {
|
||||||
//Seek to the location
|
getDataFile();
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Release memory
|
bool found = false;
|
||||||
free(filestart);
|
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!
|
if( !found ) {
|
||||||
close(fd);
|
perror("El recurs no s'ha trobat en l'arxiu de recursos");
|
||||||
|
|
||||||
//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);
|
|
||||||
exit(1);
|
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;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void file_quit() {
|
||||||
|
if( data_file != NULL ) {
|
||||||
|
free( data_file->index.file_info );
|
||||||
|
free( data_file );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#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();
|
||||||
|
|||||||
Reference in New Issue
Block a user