From 494bb6246e8d20dd289e57c56312c9e6bf5d9217 Mon Sep 17 00:00:00 2001 From: JackCarterSmith Date: Fri, 16 Sep 2022 19:56:43 +0200 Subject: [PATCH] Added data struct template --- include/RDI.h | 45 +++++++++++++++++++++++------ src/RDI.cpp | 14 +++++++-- src/RDat.cpp | 4 ++- src/RDat.h | 30 ++++++++++--------- src/bundle_struct.h | 47 ++++++++++++++++++++++++++++++ src/data_struct.h | 70 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 184 insertions(+), 26 deletions(-) create mode 100644 src/bundle_struct.h create mode 100644 src/data_struct.h diff --git a/include/RDI.h b/include/RDI.h index 760c340..7eb34e9 100644 --- a/include/RDI.h +++ b/include/RDI.h @@ -25,15 +25,15 @@ #if defined(RDI_DLL) # if defined(WIN32) # if defined(RDI_DLLBUILD) -# define RDI_EXTERN extern RDI_ABI_EXPORT +# define RDI_EXTERN RDI_ABI_EXPORT # else -# define RDI_EXTERN extern RDI_ABI_IMPORT +# define RDI_EXTERN RDI_ABI_IMPORT # endif # endif #endif #ifndef RDI_EXTERN -# define RDI_EXTERN extern +# define RDI_EXTERN #endif /////////////////////////////////////////////////////////////////////////////// @@ -42,11 +42,40 @@ namespace RDI { - /** - * @brief Get the current library version. - * @return Char array of the version, escape char included. - */ - RDI_EXTERN const char* getVersion( void ); + class RDI_EXTERN RDI { + private: + std::string libVersion; + std::string workingDir = "."; + + public: + RDI(); + ~RDI(); + + /** + * @brief Get the current library version. + * @return String of the version. + */ + inline std::string getLibVersion() { + return libVersion; + } + + /** + * @brief Get the current working directory of the library instance. + * @return Path string of the current working directory. + */ + std::string getWorkingDirectory() { + return workingDir; + } + + /** + * @brief Set the current working directory of the library instance. + * @param[in] newPath Path string without final slash. + */ + void setWorkingDirectory( std::string newPath ) { + workingDir = newPath; + } + + }; /** * @brief Search for Rogue data file, try to open it and map it in memory. diff --git a/src/RDI.cpp b/src/RDI.cpp index b31210d..10fdc63 100644 --- a/src/RDI.cpp +++ b/src/RDI.cpp @@ -22,12 +22,20 @@ using namespace std; using RDI::RDat; +namespace RDI { + + RDI::RDI() { + this->libVersion = PRG_VERSION; + } + +} + + + + /* * Libs interface */ -inline const char* RDI::getVersion( void ) { - return PRG_VERSION; -} unsigned short OpenRogueDat( void ) { return 0; diff --git a/src/RDat.cpp b/src/RDat.cpp index 35d13bc..c6946dc 100644 --- a/src/RDat.cpp +++ b/src/RDat.cpp @@ -7,8 +7,10 @@ * */ +#include #include #include +#include "data_struct.h" #include "RDat.h" using namespace std; @@ -27,7 +29,7 @@ RDat::RDat( string fPath ) { rdf.open(datFile, ios::in | ios::binary); if (rdf.is_open()) { size = rdf.tellg(); - rDatPtr = new char[size]; + rDatPtr = (MEMFILE)malloc(size); rdf.seekg(0, ios::beg); rdf.read(rDatPtr, size); rdf.close(); diff --git a/src/RDat.h b/src/RDat.h index 72d29bf..e894825 100644 --- a/src/RDat.h +++ b/src/RDat.h @@ -7,27 +7,29 @@ * */ -#ifndef SRC_RDAT_H_ -#define SRC_RDAT_H_ +#ifndef RDAT_H_ +#define RDAT_H_ + namespace RDI { -typedef char* MEMFILE; + typedef char* MEMFILE; -class RDat { -private: - MEMFILE rDatPtr = nullptr; -public: - RDat( std::string fPath ); - virtual ~RDat(); + class RDat { + private: + MEMFILE rDatPtr = nullptr; - MEMFILE getRDat() { - return rDatPtr; - } + public: + RDat( std::string fPath ); + virtual ~RDat(); -}; + MEMFILE getRDat() { + return rDatPtr; + } + + }; } /* namespace RDI */ -#endif /* SRC_RDAT_H_ */ +#endif /* RDAT_H_ */ diff --git a/src/bundle_struct.h b/src/bundle_struct.h new file mode 100644 index 0000000..22b52bb --- /dev/null +++ b/src/bundle_struct.h @@ -0,0 +1,47 @@ +/** + * @file bundle_struct.h + * @date 16/09/2022 + * @author JackCarterSmith + * @copyright GPL-v3.0 + * @brief Bundle (000/001) file mapping definition. + * + */ + +#ifndef BUNDLE_STRUCT_H_ +#define BUNDLE_STRUCT_H_ + +#ifndef PACK +# if defined(_MSC_VER) +# define PACK +# elif defined(__GNUC__) +# define PACK __attribute__((packed)) +# endif +#endif + + +namespace RDI { + + //////////////////////////////////////////////////////////////////////////// + // Declaration of Memory Mapped Structure + // Caution: the place of variable is important for correct mapping! + //////////////////////////////////////////////////////////////////////////// + +#if defined(_MSC_VER) +#pragma pack(push, 1) +#endif + + typedef struct PACK file_header { + unsigned int datas_offset; + } T_FILE_HEADER; + + typedef struct PACK dat_section { + unsigned int file_headers_offset; + } T_DAT_SECTION; + +#if defined(_MSC_VER) +#pragma pack(pop) +#endif + +} + +#endif /* BUNDLE_STRUCT_H_ */ diff --git a/src/data_struct.h b/src/data_struct.h new file mode 100644 index 0000000..e979278 --- /dev/null +++ b/src/data_struct.h @@ -0,0 +1,70 @@ +/** + * @file data_struct.h + * @date 16/09/2022 + * @author JackCarterSmith + * @copyright GPL-v3.0 + * @brief Data file mapping definition. + * + */ + +#ifndef DATA_STRUCT_H_ +#define DATA_STRUCT_H_ + +#ifndef PACK +# if defined(_MSC_VER) +# define PACK +# elif defined(__GNUC__) +# define PACK __attribute__((packed)) +# endif +#endif + + +namespace RDI { + + //////////////////////////////////////////////////////////////////////////// + // Declaration of Memory Mapped Structure + // Caution: the place of variable is important for correct mapping! + //////////////////////////////////////////////////////////////////////////// + +#if defined(_MSC_VER) +#pragma pack(push, 1) +#endif + + /* + * DATA.DAT content type + */ + typedef struct PACK file_header { + unsigned int datas_offset; + unsigned int datas_size; // If file is directory, equal to sum of it's file. + unsigned int reserved0; // 0xFFFFFFFF + unsigned short flags; + unsigned short dir_entries_size; // If directory = sum of files entry size + itself, 0 if standard file. + char name[16]; // File extension is UPPERCASE, separated of file name by '_'. + } T_FILE_HEADER; + + typedef struct PACK dat_section { + unsigned int file_headers_offset; + unsigned int file_headers_size; + unsigned char files_datas; // Should be used as start pointer for memcpy. + } T_DAT_SECTION; + + + /* + * DATA.HDR content type + * Entries are in a row, test for EOF. + */ + typedef struct PACK hdr_entry { + char section_name[16]; + unsigned int reserved0; // 12B of zeros + unsigned int reserved1; + unsigned int reserved2; + unsigned int section_offset; + } T_HDR_ENTRY; + +#if defined(_MSC_VER) +#pragma pack(pop) +#endif + +} + +#endif /* DATA_STRUCT_H_ */