diff --git a/rwengine/src/engine/GameData.cpp b/rwengine/src/engine/GameData.cpp index 93895ff3..b6d5779e 100644 --- a/rwengine/src/engine/GameData.cpp +++ b/rwengine/src/engine/GameData.cpp @@ -292,7 +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->loadFile(scm_h.data.get(), scm_h.length); return scm; } @@ -524,7 +524,7 @@ void GameData::loadIFP(const std::string& name) { if (f.data) { LoaderIFP loader; - if (loader.loadFromMemory(f.data)) { + if (loader.loadFromMemory(f.data.get())) { animations.insert(loader.animations.begin(), loader.animations.end()); } diff --git a/rwengine/src/loaders/LoaderCutsceneDAT.cpp b/rwengine/src/loaders/LoaderCutsceneDAT.cpp index a02a7e9b..eb620705 100644 --- a/rwengine/src/loaders/LoaderCutsceneDAT.cpp +++ b/rwengine/src/loaders/LoaderCutsceneDAT.cpp @@ -13,7 +13,7 @@ #include "platform/FileHandle.hpp" void LoaderCutsceneDAT::load(CutsceneTracks &tracks, const FileContentsInfo& file) { - std::string dataStr(file.data, file.length); + std::string dataStr(file.data.get(), file.length); std::stringstream ss(dataStr); int numZooms = 0; diff --git a/rwengine/src/loaders/LoaderGXT.cpp b/rwengine/src/loaders/LoaderGXT.cpp index 9d31b327..13e75dff 100644 --- a/rwengine/src/loaders/LoaderGXT.cpp +++ b/rwengine/src/loaders/LoaderGXT.cpp @@ -9,7 +9,7 @@ #include void LoaderGXT::load(GameTexts &texts, const FileContentsInfo &file) { - auto data = file.data; + auto data = file.data.get(); data += 4; // TKEY diff --git a/rwlib/source/loaders/LoaderDFF.cpp b/rwlib/source/loaders/LoaderDFF.cpp index 482b20e1..e3a040ff 100644 --- a/rwlib/source/loaders/LoaderDFF.cpp +++ b/rwlib/source/loaders/LoaderDFF.cpp @@ -458,7 +458,7 @@ AtomicPtr LoaderDFF::readAtomic(FrameList &framelist, ClumpPtr LoaderDFF::loadFromMemory(const FileContentsInfo& file) { auto model = std::make_shared(); - RWBStream rootStream(file.data, file.length); + RWBStream rootStream(file.data.get(), file.length); auto rootID = rootStream.getNextChunk(); if (rootID != CHUNK_CLUMP) { diff --git a/rwlib/source/loaders/LoaderIMG.cpp b/rwlib/source/loaders/LoaderIMG.cpp index 208f67d2..f426c27b 100644 --- a/rwlib/source/loaders/LoaderIMG.cpp +++ b/rwlib/source/loaders/LoaderIMG.cpp @@ -49,7 +49,7 @@ bool LoaderIMG::findAssetInfo(const std::string& assetname, return false; } -char* LoaderIMG::loadToMemory(const std::string& assetname) { +std::unique_ptr LoaderIMG::loadToMemory(const std::string& assetname) { LoaderIMGFile assetInfo; bool found = findAssetInfo(assetname, assetInfo); @@ -62,39 +62,37 @@ char* LoaderIMG::loadToMemory(const std::string& assetname) { FILE* fp = fopen(imgName.string().c_str(), "rb"); if (fp) { - char* raw_data = new char[assetInfo.size * 2048]; + auto raw_data = std::make_unique(assetInfo.size * 2048); fseek(fp, assetInfo.offset * 2048, SEEK_SET); - if (fread(raw_data, 2048, assetInfo.size, fp) != assetInfo.size) { + if (fread(raw_data.get(), 2048, assetInfo.size, fp) != assetInfo.size) { RW_ERROR("Error reading asset " << assetInfo.name); } fclose(fp); return raw_data; } else - return 0; + return nullptr; } /// Writes the contents of assetname to filename bool LoaderIMG::saveAsset(const std::string& assetname, const std::string& filename) { - char* raw_data = loadToMemory(assetname); + auto raw_data = loadToMemory(assetname); if (!raw_data) return false; FILE* dumpFile = fopen(filename.c_str(), "wb"); if (dumpFile) { LoaderIMGFile asset; if (findAssetInfo(assetname, asset)) { - fwrite(raw_data, 2048, asset.size, dumpFile); + fwrite(raw_data.get(), 2048, asset.size, dumpFile); printf("=> IMG: Saved %s to disk with filename %s\n", assetname.c_str(), filename.c_str()); } fclose(dumpFile); - delete[] raw_data; return true; } else { - delete[] raw_data; return false; } } diff --git a/rwlib/source/loaders/LoaderIMG.hpp b/rwlib/source/loaders/LoaderIMG.hpp index bb51462c..bfe70453 100644 --- a/rwlib/source/loaders/LoaderIMG.hpp +++ b/rwlib/source/loaders/LoaderIMG.hpp @@ -39,9 +39,8 @@ public: bool load(const rwfs::path& filename); /// Load a file from the archive to memory and pass a pointer to it - /// Warning: Please delete[] the memory in the end. - /// Warning: Returns NULL (0) if by any reason it can't load the file - char* loadToMemory(const std::string& assetname); + /// Warning: Returns nullptr if by any reason it can't load the file + std::unique_ptr loadToMemory(const std::string& assetname); /// Writes the contents of assetname to filename bool saveAsset(const std::string& assetname, const std::string& filename); diff --git a/rwlib/source/loaders/LoaderTXD.cpp b/rwlib/source/loaders/LoaderTXD.cpp index 8c6687c8..e28a73b3 100644 --- a/rwlib/source/loaders/LoaderTXD.cpp +++ b/rwlib/source/loaders/LoaderTXD.cpp @@ -174,7 +174,7 @@ TextureData::Handle createTexture(RW::BSTextureNative& texNative, bool TextureLoader::loadFromMemory(const FileContentsInfo& file, TextureArchive& inTextures) { - auto data = file.data; + auto data = file.data.get(); RW::BinaryStreamSection root(data); /*auto texDict =*/root.readStructure(); diff --git a/rwlib/source/platform/FileHandle.hpp b/rwlib/source/platform/FileHandle.hpp index cfaade72..fcd23f7b 100644 --- a/rwlib/source/platform/FileHandle.hpp +++ b/rwlib/source/platform/FileHandle.hpp @@ -1,20 +1,29 @@ #ifndef _LIBRW_FILEHANDLE_HPP_ #define _LIBRW_FILEHANDLE_HPP_ + #include +#include /** * @brief Contains a pointer to a file's contents. */ struct FileContentsInfo { - char *data; + std::unique_ptr data; size_t length; - FileContentsInfo(char* mem, size_t len) : data(mem), length(len) { + FileContentsInfo(std::unique_ptr mem, size_t len) + : data(std::move(mem)), length(len) { } - ~FileContentsInfo() { - delete[] data; + FileContentsInfo(FileContentsInfo&& info) + : data(std::move(info.data)), length(info.length) { + info.data = nullptr; } + + FileContentsInfo(FileContentsInfo& info) = delete; + FileContentsInfo& operator=(FileContentsInfo& info) = delete; + + ~FileContentsInfo() = default; }; #endif diff --git a/rwlib/source/platform/FileIndex.cpp b/rwlib/source/platform/FileIndex.cpp index 49699f84..355d2f1f 100644 --- a/rwlib/source/platform/FileIndex.cpp +++ b/rwlib/source/platform/FileIndex.cpp @@ -6,7 +6,7 @@ #include #include -#include "FileHandle.hpp" +#include "platform/FileHandle.hpp" #include "loaders/LoaderIMG.hpp" #include "rw/debug.hpp" @@ -66,10 +66,10 @@ FileContentsInfo FileIndex::openFileRaw(const std::string &filePath) const { dfile.seekg(0, std::ios::end); auto length = dfile.tellg(); dfile.seekg(0); - auto data = new char[length]; - dfile.read(data, length); + auto data = std::make_unique(length); + dfile.read(data.get(), length); - return {data, static_cast(length)}; + return {std::move(data), static_cast(length)}; } void FileIndex::indexArchive(const std::string &archive) { @@ -101,7 +101,7 @@ FileContentsInfo FileIndex::openFile(const std::string &filePath) { const auto &indexedData = indexedDataPos->second; - char *data = nullptr; + std::unique_ptr data = nullptr; size_t length = 0; if (indexedData.type == IndexedDataType::ARCHIVE) { @@ -126,9 +126,9 @@ FileContentsInfo FileIndex::openFile(const std::string &filePath) { dfile.seekg(0, std::ios::end); length = dfile.tellg(); dfile.seekg(0); - data = new char[length]; - dfile.read(data, length); + data = std::make_unique(length); + dfile.read(data.get(), length); } - return {data, length}; + return {std::move(data), length}; } diff --git a/rwviewer/views/TextViewer.cpp b/rwviewer/views/TextViewer.cpp index 910f225f..6d9b6ab5 100644 --- a/rwviewer/views/TextViewer.cpp +++ b/rwviewer/views/TextViewer.cpp @@ -8,10 +8,11 @@ #include #include +#include #include #include #include -#include +#include #include #include diff --git a/tests/test_RWBStream.cpp b/tests/test_RWBStream.cpp index 341ceb43..89f1d666 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.get(), d.length); RWBStream::ChunkID id = stream.getNextChunk();