Optimize RDat constructor process

This commit is contained in:
JackCarterSmith 2022-09-20 18:08:38 +02:00
parent 167b8b3f27
commit b00005e6d6
Signed by: JackCarterSmith
GPG Key ID: 832E52F4E23F8F24
5 changed files with 137 additions and 51 deletions

View File

@ -11,6 +11,7 @@ cmake_find_package
[options] [options]
boost:zlib=False boost:zlib=False
boost:bzip2=False boost:bzip2=False
boost:without_test=True
[imports] [imports]
bin, *.dll -> ./bin bin, *.dll -> ./bin

View File

@ -7,6 +7,9 @@
* *
*/ */
#include "RDI_Datatypes.h"
#ifndef RDI_H_ #ifndef RDI_H_
#define RDI_H_ #define RDI_H_

28
include/RDI_Datatypes.h Normal file
View File

@ -0,0 +1,28 @@
/**
* @file RDI_Datatypes.h
* @date 20/09/2022
* @author JackCarterSmith
* @copyright GPL-v3.0
* @brief Rogue Data Interface library variables type definitions.
*
*/
#ifndef RDI_DATATYPES_H_
#define RDI_DATATYPES_H_
namespace RDI {
typedef enum e_rdi_result {
RDI_OK,
RDI_ERROR_GENERIC,
RDI_ERROR_PROCESS,
RDI_ERROR_MEMORY,
RDI_ERROR_FILESYSTEM,
RDI_ERROR_UNEXPECTED
} RDI_RESULT;
}
#endif /* RDI_DATATYPES_H_ */

View File

@ -15,6 +15,7 @@
#include <RSPModel.h> #include <RSPModel.h>
#include <RSPTerrain.h> #include <RSPTerrain.h>
#include <RSPTexture.h> #include <RSPTexture.h>
#include <RDI_Datatypes.h>
#include "data_struct.h" #include "data_struct.h"
#include "RDat.h" #include "RDat.h"
@ -28,15 +29,42 @@ using boost::filesystem::file_size;
namespace RDI { namespace RDI {
struct dataSection {
std::string name = "";
unsigned int offset = 0;
};
RDat::RDat( string fPath ) { RDat::RDat( string fPath ) {
unsigned int i, size; RDI_RESULT errCode = RDI_OK;
path fp(fPath);
std::fstream rdf; this->workingDir = fPath;
// Process header file and dump data file in memory.
errCode = DumpLegacyFiles();
if (errCode == RDI_ERROR_FILESYSTEM) {
std::cout << "Data files (DATA.DAT/DATA.HDR) not found! Interrupt." << std::endl;
return;
} else if (errCode == RDI_ERROR_MEMORY) {
std::cout << "Memory allocation or file access failed! Interrupt." << std::endl;
return;
}
// Process data file tree and filter file subtype into list.
ProcessFilesTree();
}
RDat::~RDat() {
free(rDatPtr);
delete pDatSection;
}
string RDat::getDataSectionName( unsigned char id ) { return pDatSection->at(id).name; }
unsigned int RDat::getDataSectionOffset( unsigned char id ) { return pDatSection->at(id).offset; }
/**
* Helper function to search for legacy DATA.DAT/HDR and dump it in memory.
*
* @return Error status.
*/
RDI_RESULT RDat::DumpLegacyFiles() {
unsigned int i;
path fp(workingDir);
MEMFILE pTmpFile;
// If input as a directory, assume it's contain DATA.DAT/HDR. // If input as a directory, assume it's contain DATA.DAT/HDR.
if (boost::filesystem::is_directory(fp)) fp.append("DATA.HDR"); if (boost::filesystem::is_directory(fp)) fp.append("DATA.HDR");
@ -45,47 +73,61 @@ namespace RDI {
if (fp.extension() == ".DAT") fp.replace_extension(".HDR"); if (fp.extension() == ".DAT") fp.replace_extension(".HDR");
// Open and parse data header file. // Open and parse data header file.
rdf.open(fp.string(), ios::in | ios::binary); //if (boost::filesystem::exists(fp)) return RDI_ERROR_FILESYSTEM;
pTmpFile = MallocFile(fp);
if (!pTmpFile) return RDI_ERROR_MEMORY;
if (rdf.is_open()) { cDatSection = file_size(fp) / sizeof(T_HDR_ENTRY);
size = file_size(fp);
rDatPtr = (MEMFILE)malloc(size);
rdf.read(rDatPtr, size);
rdf.close();
cDatSection = size / sizeof(T_HDR_ENTRY);
pDatSection = new vector<struct dataSection>(cDatSection); pDatSection = new vector<struct dataSection>(cDatSection);
for ( i = 0; i < pDatSection->size(); i++ ) { for ( i = 0; i < pDatSection->size(); i++ ) {
pDatSection->at(i).name.append(((T_HDR_ENTRY*)(rDatPtr + i * sizeof(T_HDR_ENTRY)))->section_name); // Store header infos into structure to make easier access later.
pDatSection->at(i).offset = ((T_HDR_ENTRY*)(rDatPtr + i * sizeof(T_HDR_ENTRY)))->section_offset; pDatSection->at(i).name.append(((T_HDR_ENTRY*)(pTmpFile + i * sizeof(T_HDR_ENTRY)))->section_name);
pDatSection->at(i).offset = ((T_HDR_ENTRY*)(pTmpFile + i * sizeof(T_HDR_ENTRY)))->section_offset;
} }
free(pTmpFile);
// Dump data file and store it's pointer into class member.
fp.replace_extension(".DAT"); fp.replace_extension(".DAT");
//if (boost::filesystem::exists(fp)) return RDI_ERROR_FILESYSTEM;
rDatPtr = MallocFile(fp);
if (!rDatPtr) return RDI_ERROR_MEMORY;
// Open and parse file tree in datas file. return RDI_OK;
rdf.open(fp.string(), ios::in | ios::binary); }
/**
* Dump file into to newly allocated memory.
* @param filePath File path.
*
* @return Start memory pointer of the file.
*/
MEMFILE RDat::MallocFile( path filePath ) {
const unsigned int size = file_size(filePath);
std::fstream rdf;
MEMFILE fPtr = NULL;
rdf.open(filePath.string(), ios::in | ios::binary);
if (rdf.is_open()) { if (rdf.is_open()) {
size = file_size(fp); fPtr = (MEMFILE)malloc(size);
rDatPtr = (MEMFILE)realloc(rDatPtr, size); rdf.read(fPtr, size);
rdf.read(rDatPtr, size);
rdf.close(); rdf.close();
//TODO: Process files lists.
} else {
std::cout << "Datas file (DAT) not found!" << std::endl << "Data files should have same exact name." << std::endl;
}
} else {
std::cout << "Header file (HDR) not found!" << std::endl << "Data files should have same exact name." << std::endl;
}
} }
RDat::~RDat() { return fPtr;
free(rDatPtr);
delete pDatSection;
} }
std::string RDat::getDataSectionName( unsigned char id ) { return pDatSection->at(id).name; } /**
unsigned int RDat::getDataSectionOffset( unsigned char id ) { return pDatSection->at(id).offset; } * Helper function to process files content for further use in this library.
*
* @return Error status.
*/
RDI_RESULT RDat::ProcessFilesTree() {
return RDI_OK;
}
} /* namespace RDI */ } /* namespace RDI */

View File

@ -7,7 +7,9 @@
* *
*/ */
#include <boost/filesystem.hpp>
#include <vector> #include <vector>
#include "RDI_Datatypes.h"
#ifndef RDAT_H_ #ifndef RDAT_H_
@ -17,14 +19,12 @@ namespace RDI {
typedef char* MEMFILE; typedef char* MEMFILE;
struct dataSection {
std::string name = "";
unsigned int offset = 0;
};
class RDat { class RDat {
private:
std::string workingDir = ".";
unsigned char cDatSection = 0;
std::vector<struct dataSection> *pDatSection = nullptr;
MEMFILE rDatPtr = nullptr;
public: public:
RDat( std::string fPath ); RDat( std::string fPath );
~RDat(); ~RDat();
@ -40,6 +40,18 @@ namespace RDI {
std::string getDataSectionName( unsigned char id ); std::string getDataSectionName( unsigned char id );
unsigned int getDataSectionOffset( unsigned char id ); unsigned int getDataSectionOffset( unsigned char id );
private:
std::string workingDir = ".";
unsigned char cDatSection = 0;
std::vector<struct dataSection> *pDatSection = nullptr;
MEMFILE rDatPtr = nullptr;
/* File processing methods */
RDI_RESULT DumpLegacyFiles();
MEMFILE MallocFile( boost::filesystem::path filePath );
RDI_RESULT ProcessFilesTree();
}; };
} /* namespace RDI */ } /* namespace RDI */