RDI/src/RDI.cpp

175 lines
4.6 KiB
C++

/**
* @file RDI.cpp
* @date 04/02/2023
* @author JackCarterSmith
* @copyright GPL-v3.0
* @brief Rogue Data Interface library main entry abstraction file.
*
*/
#if !(__cplusplus < 201703L) && !(_MSVC_LANG < 201703L)
#error "This code run on C++17 or upper."
#endif
#if defined(RDI_DLL)
# define RDI_DLLBUILD
#endif
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <stdexcept>
#include <RSPModel.h> // Only for version getting, no processing.
#include <RSPTerrain.h> // Only for version getting, no processing.
#include "config.h"
#include "Erso.hpp"
#include "DatFileEntry.hpp"
#include "Krennic.hpp"
#include <RDI_FileHandlers.hpp>
#include "RDI.hpp"
using namespace RDI::DatFile;
/*
* Internal modules instance
*/
static RDI::Erso *ErsoModule = nullptr;
static RDI::Krennic *KrennicModule = nullptr;
/*
* Libs interface
*/
const std::string RDI::RDI_getLibVersion() noexcept { return PRG_VERSION; }
void RDI::RDI_Init( std::string roguePath ) {
#ifndef NDEBUG
std::cout << std::endl << "[RDI][DBG] Running RDI v" << std::string(RDI::RDI_getLibVersion()) << std::endl;
std::cout << "[RDI][DBG] - RSPModelLib v" << std::string(RSPModel_getVersion()) << std::endl;
std::cout << "[RDI][DBG] - RSPTerrainLib v" << std::string(RSPTerrain_getVersion()) << std::endl;
std::cout << "[RDI][DBG] - RSPTextureLib v" << "N/A" << std::endl;
#endif
// Create new instance of Erso module
// Load and extract datas files from DATA.DAT
if (ErsoModule == nullptr) {
#ifndef NDEBUG
std::cout << "[RDI][DBG] Loading Erso module..." << std::endl;
#endif
ErsoModule = new RDI::Erso(roguePath);
}
// Create new instance of Krennic module
// Process datas from Erso and build new dataset to be used outside this library
if (ErsoModule != nullptr && KrennicModule == nullptr) {
#ifndef NDEBUG
std::cout << "[RDI][DBG] Loading Krennic module..." << std::endl;
#endif
KrennicModule = new RDI::Krennic(ErsoModule);
}
}
const unsigned char RDI::RDI_getSectionCount() {
if (ErsoModule == nullptr) return 0;
else return ErsoModule->getDataSectionCount();
}
const std::string RDI::RDI_getSectionName( unsigned char id ) {
if (ErsoModule == nullptr) return "";
else return ErsoModule->getDataSectionName(id);
}
const unsigned int RDI::RDI_getSectionOffset( unsigned char id ) {
if (ErsoModule == nullptr) return 0;
else return ErsoModule->getDataSectionOffset(id);
}
const unsigned int RDI::RDI_getDirectoryElementCount( std::string path ) {
DatFileEntryDirectory* result = nullptr;
if (path.empty()) return 0;
result = dynamic_cast<DatFileEntryDirectory *>(ErsoModule->getElement(boost::filesystem::path(path)));
if (result == nullptr) return 0;
else return result->getSize();
}
const std::vector<std::string> RDI::RDI_getDirectoryElements( std::string path ) {
DatFileEntryDirectory* de = nullptr;
std::vector<std::string> elementsNameArray;
elementsNameArray.clear();
if (!path.empty()) {
de = dynamic_cast<DatFileEntryDirectory *>(ErsoModule->getElement(boost::filesystem::path(path)));
if (de != nullptr && de->isDirectory()) {
for (DatFileEntry* de2 : de->getFiles())
elementsNameArray.push_back(de2->getName());
}
}
return elementsNameArray;
}
const bool RDI::RDI_isElementDirectory( std::string path ) {
DatFileEntry* result = nullptr;
if (path.empty()) return false;
result = ErsoModule->getElement(boost::filesystem::path(path));
if (result == nullptr) return false;
else return (result->isDirectory() || result->isRootDirectory());
}
/* -------------------------------------------------------------------------- */
const std::vector<std::string> RDI::RDI_getLevelsName( void ) noexcept {
return KrennicModule->getLevelsList();
}
const std::vector<std::string> RDI::RDI_getModelsName( void ) noexcept {
return KrennicModule->getModelsList();
}
const std::vector<std::string> RDI::RDI_getTexturesName( void ) noexcept {
return KrennicModule->getTexturesList();
}
const std::vector<std::string> RDI::RDI_getMusicsName( void ) noexcept {
return KrennicModule->getMusicsList();
}
RDI::LVL* RDI::RDI_getLevel( std::string levelName ) {
try {
return KrennicModule->getLevel(levelName);
}
catch (std::invalid_argument const &ex) { // Can't find level, incorrect name?
throw std::invalid_argument(ex);
}
}
RDI::HOB* RDI::RDI_getModel( std::string modelName ) {
try {
return KrennicModule->getModel(modelName);
}
catch (std::invalid_argument const &ex) { // Can't find model, incorrect name?
throw std::invalid_argument(ex);
}
}
/* -------------------------------------------------------------------------- */
void RDI::RDI_CleanUp(){
if (KrennicModule) delete KrennicModule;
if (ErsoModule) delete ErsoModule;
}