diff --git a/rwengine/src/engine/GameData.cpp b/rwengine/src/engine/GameData.cpp index e95f54df..93895ff3 100644 --- a/rwengine/src/engine/GameData.cpp +++ b/rwengine/src/engine/GameData.cpp @@ -292,8 +292,7 @@ void GameData::loadHandling(const std::string& path) { SCMFile* GameData::loadSCM(const std::string& path) { auto scm_h = index.openFileRaw(path); SCMFile* scm = new SCMFile; - scm->loadFile(scm_h->data, scm_h->length); - scm_h.reset(); + scm->loadFile(scm_h.data, scm_h.length); return scm; } @@ -371,7 +370,7 @@ void GameData::loadTXD(const std::string& name) { TextureArchive GameData::loadTextureArchive(const std::string& name) { /// @todo refactor loadTXD to use correct file locations auto file = index.openFile(name); - if (!file) { + if (!file.data) { logger->error("Data", "Failed to open txd: " + name); return {}; } @@ -397,7 +396,7 @@ void GameData::getNameAndLod(std::string& name, int& lod) { ClumpPtr GameData::loadClump(const std::string& name) { auto file = index.openFile(name); - if (!file) { + if (!file.data) { logger->error("Data", "Failed to load model " + name); return nullptr; } @@ -420,7 +419,7 @@ ClumpPtr GameData::loadClump(const std::string& name, const std::string& texture void GameData::loadModelFile(const std::string& name) { auto file = index.openFileRaw(name); - if (!file) { + if (!file.data) { logger->log("Data", Logger::Error, "Failed to load model file " + name); return; } @@ -486,7 +485,7 @@ bool GameData::loadModel(ModelID model) { loadTXD(slotname + ".txd"); auto file = index.openFile(name + ".dff"); - if (!file) { + if (!file.data) { logger->error("Data", "Failed to load model for " + std::to_string(model) + " [" + name + "]"); return false; @@ -523,9 +522,9 @@ bool GameData::loadModel(ModelID model) { void GameData::loadIFP(const std::string& name) { auto f = index.openFile(name); - if (f) { + if (f.data) { LoaderIFP loader; - if (loader.loadFromMemory(f->data)) { + if (loader.loadFromMemory(f.data)) { animations.insert(loader.animations.begin(), loader.animations.end()); } diff --git a/rwengine/src/engine/GameWorld.cpp b/rwengine/src/engine/GameWorld.cpp index c6db19d7..ece8e574 100644 --- a/rwengine/src/engine/GameWorld.cpp +++ b/rwengine/src/engine/GameWorld.cpp @@ -29,6 +29,8 @@ #include "objects/PickupObject.hpp" #include "objects/VehicleObject.hpp" +#include "platform/FileHandle.hpp" + #include "render/ViewCamera.hpp" #ifdef RW_WINDOWS @@ -695,7 +697,7 @@ void GameWorld::loadCutscene(const std::string& name) { CutsceneData* cutscene = new CutsceneData; - if (datfile) { + if (datfile.data) { LoaderCutsceneDAT loaderdat; loaderdat.load(cutscene->tracks, datfile); } diff --git a/rwengine/src/loaders/LoaderCutsceneDAT.cpp b/rwengine/src/loaders/LoaderCutsceneDAT.cpp index e2795859..a02a7e9b 100644 --- a/rwengine/src/loaders/LoaderCutsceneDAT.cpp +++ b/rwengine/src/loaders/LoaderCutsceneDAT.cpp @@ -12,8 +12,8 @@ #include "data/CutsceneData.hpp" #include "platform/FileHandle.hpp" -void LoaderCutsceneDAT::load(CutsceneTracks &tracks, const FileHandle& file) { - std::string dataStr(file->data, file->length); +void LoaderCutsceneDAT::load(CutsceneTracks &tracks, const FileContentsInfo& file) { + std::string dataStr(file.data, file.length); std::stringstream ss(dataStr); int numZooms = 0; diff --git a/rwengine/src/loaders/LoaderCutsceneDAT.hpp b/rwengine/src/loaders/LoaderCutsceneDAT.hpp index ce4e2e55..3965bf7e 100644 --- a/rwengine/src/loaders/LoaderCutsceneDAT.hpp +++ b/rwengine/src/loaders/LoaderCutsceneDAT.hpp @@ -7,7 +7,7 @@ struct CutsceneTracks; class LoaderCutsceneDAT { public: - void load(CutsceneTracks& tracks, const FileHandle& file); + void load(CutsceneTracks& tracks, const FileContentsInfo& file); }; #endif diff --git a/rwengine/src/loaders/LoaderGXT.cpp b/rwengine/src/loaders/LoaderGXT.cpp index 4ddf2f82..9d31b327 100644 --- a/rwengine/src/loaders/LoaderGXT.cpp +++ b/rwengine/src/loaders/LoaderGXT.cpp @@ -8,8 +8,8 @@ #include #include -void LoaderGXT::load(GameTexts &texts, const FileHandle &file) { - auto data = file->data; +void LoaderGXT::load(GameTexts &texts, const FileContentsInfo &file) { + auto data = file.data; data += 4; // TKEY diff --git a/rwengine/src/loaders/LoaderGXT.hpp b/rwengine/src/loaders/LoaderGXT.hpp index d1c170a7..e03fbcd1 100644 --- a/rwengine/src/loaders/LoaderGXT.hpp +++ b/rwengine/src/loaders/LoaderGXT.hpp @@ -6,7 +6,7 @@ class GameTexts; class LoaderGXT { public: - void load(GameTexts& texts, const FileHandle& file); + void load(GameTexts& texts, const FileContentsInfo& file); }; #endif diff --git a/rwlib/source/loaders/LoaderDFF.cpp b/rwlib/source/loaders/LoaderDFF.cpp index ad5fa7ac..482b20e1 100644 --- a/rwlib/source/loaders/LoaderDFF.cpp +++ b/rwlib/source/loaders/LoaderDFF.cpp @@ -455,10 +455,10 @@ AtomicPtr LoaderDFF::readAtomic(FrameList &framelist, return atomic; } -ClumpPtr LoaderDFF::loadFromMemory(const FileHandle& file) { +ClumpPtr LoaderDFF::loadFromMemory(const FileContentsInfo& file) { auto model = std::make_shared(); - RWBStream rootStream(file->data, file->length); + RWBStream rootStream(file.data, file.length); auto rootID = rootStream.getNextChunk(); if (rootID != CHUNK_CLUMP) { diff --git a/rwlib/source/loaders/LoaderDFF.hpp b/rwlib/source/loaders/LoaderDFF.hpp index 66d23926..56b41278 100644 --- a/rwlib/source/loaders/LoaderDFF.hpp +++ b/rwlib/source/loaders/LoaderDFF.hpp @@ -31,7 +31,7 @@ public: using GeometryList = std::vector; using FrameList = std::vector; - ClumpPtr loadFromMemory(const FileHandle& file); + ClumpPtr loadFromMemory(const FileContentsInfo& file); void setTextureLookupCallback(const TextureLookupCallback& tlc) { texturelookup = tlc; diff --git a/rwlib/source/loaders/LoaderTXD.cpp b/rwlib/source/loaders/LoaderTXD.cpp index 5183e4c7..8c6687c8 100644 --- a/rwlib/source/loaders/LoaderTXD.cpp +++ b/rwlib/source/loaders/LoaderTXD.cpp @@ -172,9 +172,9 @@ TextureData::Handle createTexture(RW::BSTextureNative& texNative, transparent); } -bool TextureLoader::loadFromMemory(const FileHandle& file, +bool TextureLoader::loadFromMemory(const FileContentsInfo& file, TextureArchive& inTextures) { - auto data = file->data; + auto data = file.data; RW::BinaryStreamSection root(data); /*auto texDict =*/root.readStructure(); diff --git a/rwlib/source/loaders/LoaderTXD.hpp b/rwlib/source/loaders/LoaderTXD.hpp index da6fe1c0..a23a4bc3 100644 --- a/rwlib/source/loaders/LoaderTXD.hpp +++ b/rwlib/source/loaders/LoaderTXD.hpp @@ -6,7 +6,7 @@ class TextureLoader { public: - bool loadFromMemory(const FileHandle& file, TextureArchive& inTextures); + bool loadFromMemory(const FileContentsInfo& file, TextureArchive& inTextures); }; #endif diff --git a/rwlib/source/platform/FileHandle.hpp b/rwlib/source/platform/FileHandle.hpp index aae88aa6..cfaade72 100644 --- a/rwlib/source/platform/FileHandle.hpp +++ b/rwlib/source/platform/FileHandle.hpp @@ -1,5 +1,6 @@ #ifndef _LIBRW_FILEHANDLE_HPP_ #define _LIBRW_FILEHANDLE_HPP_ +#include /** * @brief Contains a pointer to a file's contents. diff --git a/rwlib/source/platform/FileIndex.cpp b/rwlib/source/platform/FileIndex.cpp index 5da3a8a2..49699f84 100644 --- a/rwlib/source/platform/FileIndex.cpp +++ b/rwlib/source/platform/FileIndex.cpp @@ -50,7 +50,7 @@ rwfs::path FileIndex::findFilePath(const std::string &filePath) const { return getIndexedDataAt(filePath)->path; } -FileHandle FileIndex::openFileRaw(const std::string &filePath) const { +FileContentsInfo FileIndex::openFileRaw(const std::string &filePath) const { const auto *indexData = getIndexedDataAt(filePath); std::ifstream dfile(indexData->path, std::ios::binary); if (!dfile.is_open()) { @@ -69,7 +69,7 @@ FileHandle FileIndex::openFileRaw(const std::string &filePath) const { auto data = new char[length]; dfile.read(data, length); - return std::make_shared(data, length); + return {data, static_cast(length)}; } void FileIndex::indexArchive(const std::string &archive) { @@ -91,11 +91,12 @@ void FileIndex::indexArchive(const std::string &archive) { } } -FileHandle FileIndex::openFile(const std::string &filePath) { +FileContentsInfo FileIndex::openFile(const std::string &filePath) { auto cleanFilePath = normalizeFilePath(filePath); auto indexedDataPos = indexedData_.find(cleanFilePath); + if (indexedDataPos == indexedData_.end()) { - return nullptr; + return {nullptr, 0}; } const auto &indexedData = indexedDataPos->second; @@ -129,9 +130,5 @@ FileHandle FileIndex::openFile(const std::string &filePath) { dfile.read(data, length); } - if (data == nullptr) { - return nullptr; - } - - return std::make_shared(data, length); + return {data, length}; } diff --git a/rwlib/source/platform/FileIndex.hpp b/rwlib/source/platform/FileIndex.hpp index 9770dc0d..790bf284 100644 --- a/rwlib/source/platform/FileIndex.hpp +++ b/rwlib/source/platform/FileIndex.hpp @@ -38,7 +38,7 @@ public: * @return FileHandle to the file * @throws if this FileIndex has not indexed the path */ - FileHandle openFileRaw(const std::string &filePath) const; + FileContentsInfo openFileRaw(const std::string &filePath) const; /** * Adds the files contained within the given Archive file to the @@ -54,7 +54,7 @@ public: * @param filePath name of the file to open * @return FileHandle to the file, nullptr if this FileINdexed has not indexed the path */ - FileHandle openFile(const std::string &filePath); + FileContentsInfo openFile(const std::string &filePath); private: /** diff --git a/rwlib/source/rw/forward.hpp b/rwlib/source/rw/forward.hpp index 2c5cf958..b2dd834e 100644 --- a/rwlib/source/rw/forward.hpp +++ b/rwlib/source/rw/forward.hpp @@ -16,7 +16,6 @@ class Clump; // Pointer types using AnimationPtr = std::shared_ptr; -using FileHandle = std::shared_ptr; using ModelFramePtr = std::shared_ptr; using GeometryPtr = std::shared_ptr; using AtomicPtr = std::shared_ptr; diff --git a/rwviewer/views/ModelViewer.cpp b/rwviewer/views/ModelViewer.cpp index 7b8ecfb7..6241583f 100644 --- a/rwviewer/views/ModelViewer.cpp +++ b/rwviewer/views/ModelViewer.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include "ViewerWidget.hpp" @@ -47,7 +48,7 @@ void ModelViewer::showObject(uint16_t object) { }); auto file = world()->data->index.openFile(modelName); - if (!file) { + if (!file.data) { RW_ERROR("Couldn't load " << modelName); return; } diff --git a/tests/test_Cutscene.cpp b/tests/test_Cutscene.cpp index 0b0f5cbc..14b4399b 100644 --- a/tests/test_Cutscene.cpp +++ b/tests/test_Cutscene.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include "test_Globals.hpp" BOOST_AUTO_TEST_SUITE(CutsceneTests) diff --git a/tests/test_FileIndex.cpp b/tests/test_FileIndex.cpp index 7546335d..8a23c8cc 100644 --- a/tests/test_FileIndex.cpp +++ b/tests/test_FileIndex.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "test_Globals.hpp" @@ -50,7 +51,7 @@ BOOST_AUTO_TEST_CASE(test_openFile) { index.indexTree(Global::getGamePath() + "/data"); auto handle = index.openFile("cullzone.dat"); - BOOST_CHECK(handle != nullptr); + BOOST_CHECK(handle.data != nullptr); } BOOST_AUTO_TEST_CASE(test_indexArchive) { @@ -59,14 +60,14 @@ BOOST_AUTO_TEST_CASE(test_indexArchive) { { auto handle = index.openFile("landstal.dff"); - BOOST_CHECK(handle == nullptr); + BOOST_CHECK(handle.data == nullptr); } index.indexArchive("models/gta3.img"); { auto handle = index.openFile("landstal.dff"); - BOOST_CHECK(handle != nullptr); + BOOST_CHECK(handle.data != nullptr); } } #endif diff --git a/tests/test_LoaderDFF.cpp b/tests/test_LoaderDFF.cpp index 8c9f4617..a16693ee 100644 --- a/tests/test_LoaderDFF.cpp +++ b/tests/test_LoaderDFF.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "test_Globals.hpp" BOOST_AUTO_TEST_SUITE(LoaderDFFTests) diff --git a/tests/test_RWBStream.cpp b/tests/test_RWBStream.cpp index 6d5ecbb8..341ceb43 100644 --- a/tests/test_RWBStream.cpp +++ b/tests/test_RWBStream.cpp @@ -10,7 +10,7 @@ BOOST_AUTO_TEST_CASE(iterate_stream_test) { { auto d = Global::get().e->data->index.openFile("landstal.dff"); - RWBStream stream(d->data, d->length); + RWBStream stream(d.data, d.length); RWBStream::ChunkID id = stream.getNextChunk(); diff --git a/tests/test_Text.cpp b/tests/test_Text.cpp index 062febe5..9fc281b3 100644 --- a/tests/test_Text.cpp +++ b/tests/test_Text.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "test_Globals.hpp" #define T(x) GameStringUtil::fromString(x, FONT_PRICEDOWN)