Optimize RDat constructor process
This commit is contained in:
parent
167b8b3f27
commit
b00005e6d6
@ -11,6 +11,7 @@ cmake_find_package
|
||||
[options]
|
||||
boost:zlib=False
|
||||
boost:bzip2=False
|
||||
boost:without_test=True
|
||||
|
||||
[imports]
|
||||
bin, *.dll -> ./bin
|
@ -7,6 +7,9 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include "RDI_Datatypes.h"
|
||||
|
||||
|
||||
#ifndef RDI_H_
|
||||
#define RDI_H_
|
||||
|
||||
|
28
include/RDI_Datatypes.h
Normal file
28
include/RDI_Datatypes.h
Normal 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_ */
|
114
src/RDat.cpp
114
src/RDat.cpp
@ -15,6 +15,7 @@
|
||||
#include <RSPModel.h>
|
||||
#include <RSPTerrain.h>
|
||||
#include <RSPTexture.h>
|
||||
#include <RDI_Datatypes.h>
|
||||
#include "data_struct.h"
|
||||
#include "RDat.h"
|
||||
|
||||
@ -28,15 +29,42 @@ using boost::filesystem::file_size;
|
||||
|
||||
namespace RDI {
|
||||
|
||||
struct dataSection {
|
||||
std::string name = "";
|
||||
unsigned int offset = 0;
|
||||
};
|
||||
|
||||
RDat::RDat( string fPath ) {
|
||||
unsigned int i, size;
|
||||
path fp(fPath);
|
||||
std::fstream rdf;
|
||||
RDI_RESULT errCode = RDI_OK;
|
||||
|
||||
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 (boost::filesystem::is_directory(fp)) fp.append("DATA.HDR");
|
||||
@ -45,47 +73,61 @@ namespace RDI {
|
||||
if (fp.extension() == ".DAT") fp.replace_extension(".HDR");
|
||||
|
||||
// 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()) {
|
||||
size = file_size(fp);
|
||||
rDatPtr = (MEMFILE)malloc(size);
|
||||
rdf.read(rDatPtr, size);
|
||||
rdf.close();
|
||||
|
||||
cDatSection = size / sizeof(T_HDR_ENTRY);
|
||||
cDatSection = file_size(fp) / sizeof(T_HDR_ENTRY);
|
||||
pDatSection = new vector<struct dataSection>(cDatSection);
|
||||
for ( i = 0; i < pDatSection->size(); i++ ) {
|
||||
pDatSection->at(i).name.append(((T_HDR_ENTRY*)(rDatPtr + i * sizeof(T_HDR_ENTRY)))->section_name);
|
||||
pDatSection->at(i).offset = ((T_HDR_ENTRY*)(rDatPtr + i * sizeof(T_HDR_ENTRY)))->section_offset;
|
||||
// Store header infos into structure to make easier access later.
|
||||
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");
|
||||
//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.
|
||||
rdf.open(fp.string(), ios::in | ios::binary);
|
||||
return RDI_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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()) {
|
||||
size = file_size(fp);
|
||||
rDatPtr = (MEMFILE)realloc(rDatPtr, size);
|
||||
rdf.read(rDatPtr, size);
|
||||
fPtr = (MEMFILE)malloc(size);
|
||||
rdf.read(fPtr, size);
|
||||
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() {
|
||||
free(rDatPtr);
|
||||
delete pDatSection;
|
||||
return fPtr;
|
||||
}
|
||||
|
||||
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 */
|
||||
|
26
src/RDat.h
26
src/RDat.h
@ -7,7 +7,9 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <vector>
|
||||
#include "RDI_Datatypes.h"
|
||||
|
||||
|
||||
#ifndef RDAT_H_
|
||||
@ -17,14 +19,12 @@ namespace RDI {
|
||||
|
||||
typedef char* MEMFILE;
|
||||
|
||||
struct dataSection {
|
||||
std::string name = "";
|
||||
unsigned int offset = 0;
|
||||
};
|
||||
|
||||
class RDat {
|
||||
private:
|
||||
std::string workingDir = ".";
|
||||
|
||||
unsigned char cDatSection = 0;
|
||||
std::vector<struct dataSection> *pDatSection = nullptr;
|
||||
MEMFILE rDatPtr = nullptr;
|
||||
|
||||
public:
|
||||
RDat( std::string fPath );
|
||||
~RDat();
|
||||
@ -40,6 +40,18 @@ namespace RDI {
|
||||
std::string getDataSectionName( 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 */
|
||||
|
Loading…
x
Reference in New Issue
Block a user