1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-07 03:12:36 +01:00

Convert data of FileContentsInfo to unique_ptr

I've removed copy ctor and copy assign operator.
We use here unique_ptr so copying should not happen.
This commit is contained in:
Filip Gawin 2018-08-05 18:28:59 +02:00
parent 27333efd8c
commit f3a718611e
11 changed files with 38 additions and 31 deletions

View File

@ -292,7 +292,7 @@ void GameData::loadHandling(const std::string& path) {
SCMFile* GameData::loadSCM(const std::string& path) { SCMFile* GameData::loadSCM(const std::string& path) {
auto scm_h = index.openFileRaw(path); auto scm_h = index.openFileRaw(path);
SCMFile* scm = new SCMFile; SCMFile* scm = new SCMFile;
scm->loadFile(scm_h.data, scm_h.length); scm->loadFile(scm_h.data.get(), scm_h.length);
return scm; return scm;
} }
@ -524,7 +524,7 @@ void GameData::loadIFP(const std::string& name) {
if (f.data) { if (f.data) {
LoaderIFP loader; LoaderIFP loader;
if (loader.loadFromMemory(f.data)) { if (loader.loadFromMemory(f.data.get())) {
animations.insert(loader.animations.begin(), animations.insert(loader.animations.begin(),
loader.animations.end()); loader.animations.end());
} }

View File

@ -13,7 +13,7 @@
#include "platform/FileHandle.hpp" #include "platform/FileHandle.hpp"
void LoaderCutsceneDAT::load(CutsceneTracks &tracks, const FileContentsInfo& file) { 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); std::stringstream ss(dataStr);
int numZooms = 0; int numZooms = 0;

View File

@ -9,7 +9,7 @@
#include <platform/FileHandle.hpp> #include <platform/FileHandle.hpp>
void LoaderGXT::load(GameTexts &texts, const FileContentsInfo &file) { void LoaderGXT::load(GameTexts &texts, const FileContentsInfo &file) {
auto data = file.data; auto data = file.data.get();
data += 4; // TKEY data += 4; // TKEY

View File

@ -458,7 +458,7 @@ AtomicPtr LoaderDFF::readAtomic(FrameList &framelist,
ClumpPtr LoaderDFF::loadFromMemory(const FileContentsInfo& file) { ClumpPtr LoaderDFF::loadFromMemory(const FileContentsInfo& file) {
auto model = std::make_shared<Clump>(); auto model = std::make_shared<Clump>();
RWBStream rootStream(file.data, file.length); RWBStream rootStream(file.data.get(), file.length);
auto rootID = rootStream.getNextChunk(); auto rootID = rootStream.getNextChunk();
if (rootID != CHUNK_CLUMP) { if (rootID != CHUNK_CLUMP) {

View File

@ -49,7 +49,7 @@ bool LoaderIMG::findAssetInfo(const std::string& assetname,
return false; return false;
} }
char* LoaderIMG::loadToMemory(const std::string& assetname) { std::unique_ptr<char[]> LoaderIMG::loadToMemory(const std::string& assetname) {
LoaderIMGFile assetInfo; LoaderIMGFile assetInfo;
bool found = findAssetInfo(assetname, 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"); FILE* fp = fopen(imgName.string().c_str(), "rb");
if (fp) { if (fp) {
char* raw_data = new char[assetInfo.size * 2048]; auto raw_data = std::make_unique<char[]>(assetInfo.size * 2048);
fseek(fp, assetInfo.offset * 2048, SEEK_SET); 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); RW_ERROR("Error reading asset " << assetInfo.name);
} }
fclose(fp); fclose(fp);
return raw_data; return raw_data;
} else } else
return 0; return nullptr;
} }
/// Writes the contents of assetname to filename /// Writes the contents of assetname to filename
bool LoaderIMG::saveAsset(const std::string& assetname, bool LoaderIMG::saveAsset(const std::string& assetname,
const std::string& filename) { const std::string& filename) {
char* raw_data = loadToMemory(assetname); auto raw_data = loadToMemory(assetname);
if (!raw_data) return false; if (!raw_data) return false;
FILE* dumpFile = fopen(filename.c_str(), "wb"); FILE* dumpFile = fopen(filename.c_str(), "wb");
if (dumpFile) { if (dumpFile) {
LoaderIMGFile asset; LoaderIMGFile asset;
if (findAssetInfo(assetname, 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", printf("=> IMG: Saved %s to disk with filename %s\n",
assetname.c_str(), filename.c_str()); assetname.c_str(), filename.c_str());
} }
fclose(dumpFile); fclose(dumpFile);
delete[] raw_data;
return true; return true;
} else { } else {
delete[] raw_data;
return false; return false;
} }
} }

View File

@ -39,9 +39,8 @@ public:
bool load(const rwfs::path& filename); bool load(const rwfs::path& filename);
/// Load a file from the archive to memory and pass a pointer to it /// Load a file from the archive to memory and pass a pointer to it
/// Warning: Please delete[] the memory in the end. /// Warning: Returns nullptr if by any reason it can't load the file
/// Warning: Returns NULL (0) if by any reason it can't load the file std::unique_ptr<char[]> loadToMemory(const std::string& assetname);
char* loadToMemory(const std::string& assetname);
/// Writes the contents of assetname to filename /// Writes the contents of assetname to filename
bool saveAsset(const std::string& assetname, const std::string& filename); bool saveAsset(const std::string& assetname, const std::string& filename);

View File

@ -174,7 +174,7 @@ TextureData::Handle createTexture(RW::BSTextureNative& texNative,
bool TextureLoader::loadFromMemory(const FileContentsInfo& file, bool TextureLoader::loadFromMemory(const FileContentsInfo& file,
TextureArchive& inTextures) { TextureArchive& inTextures) {
auto data = file.data; auto data = file.data.get();
RW::BinaryStreamSection root(data); RW::BinaryStreamSection root(data);
/*auto texDict =*/root.readStructure<RW::BSTextureDictionary>(); /*auto texDict =*/root.readStructure<RW::BSTextureDictionary>();

View File

@ -1,20 +1,29 @@
#ifndef _LIBRW_FILEHANDLE_HPP_ #ifndef _LIBRW_FILEHANDLE_HPP_
#define _LIBRW_FILEHANDLE_HPP_ #define _LIBRW_FILEHANDLE_HPP_
#include <cstddef> #include <cstddef>
#include <memory>
/** /**
* @brief Contains a pointer to a file's contents. * @brief Contains a pointer to a file's contents.
*/ */
struct FileContentsInfo { struct FileContentsInfo {
char *data; std::unique_ptr<char[]> data;
size_t length; size_t length;
FileContentsInfo(char* mem, size_t len) : data(mem), length(len) { FileContentsInfo(std::unique_ptr<char[]> mem, size_t len)
: data(std::move(mem)), length(len) {
} }
~FileContentsInfo() { FileContentsInfo(FileContentsInfo&& info)
delete[] data; : data(std::move(info.data)), length(info.length) {
info.data = nullptr;
} }
FileContentsInfo(FileContentsInfo& info) = delete;
FileContentsInfo& operator=(FileContentsInfo& info) = delete;
~FileContentsInfo() = default;
}; };
#endif #endif

View File

@ -6,7 +6,7 @@
#include <iterator> #include <iterator>
#include <sstream> #include <sstream>
#include "FileHandle.hpp" #include "platform/FileHandle.hpp"
#include "loaders/LoaderIMG.hpp" #include "loaders/LoaderIMG.hpp"
#include "rw/debug.hpp" #include "rw/debug.hpp"
@ -66,10 +66,10 @@ FileContentsInfo FileIndex::openFileRaw(const std::string &filePath) const {
dfile.seekg(0, std::ios::end); dfile.seekg(0, std::ios::end);
auto length = dfile.tellg(); auto length = dfile.tellg();
dfile.seekg(0); dfile.seekg(0);
auto data = new char[length]; auto data = std::make_unique<char[]>(length);
dfile.read(data, length); dfile.read(data.get(), length);
return {data, static_cast<size_t>(length)}; return {std::move(data), static_cast<size_t>(length)};
} }
void FileIndex::indexArchive(const std::string &archive) { void FileIndex::indexArchive(const std::string &archive) {
@ -101,7 +101,7 @@ FileContentsInfo FileIndex::openFile(const std::string &filePath) {
const auto &indexedData = indexedDataPos->second; const auto &indexedData = indexedDataPos->second;
char *data = nullptr; std::unique_ptr<char[]> data = nullptr;
size_t length = 0; size_t length = 0;
if (indexedData.type == IndexedDataType::ARCHIVE) { if (indexedData.type == IndexedDataType::ARCHIVE) {
@ -126,9 +126,9 @@ FileContentsInfo FileIndex::openFile(const std::string &filePath) {
dfile.seekg(0, std::ios::end); dfile.seekg(0, std::ios::end);
length = dfile.tellg(); length = dfile.tellg();
dfile.seekg(0); dfile.seekg(0);
data = new char[length]; data = std::make_unique<char[]>(length);
dfile.read(data, length); dfile.read(data.get(), length);
} }
return {data, length}; return {std::move(data), length};
} }

View File

@ -8,10 +8,11 @@
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
#include <boost/filesystem.hpp>
#include <fonts/GameTexts.hpp> #include <fonts/GameTexts.hpp>
#include <loaders/LoaderGXT.hpp> #include <loaders/LoaderGXT.hpp>
#include <models/TextModel.hpp> #include <models/TextModel.hpp>
#include <boost/filesystem.hpp> #include <platform/FileHandle.hpp>
#include <render/GameRenderer.hpp> #include <render/GameRenderer.hpp>
#include <QGroupBox> #include <QGroupBox>

View File

@ -10,7 +10,7 @@ BOOST_AUTO_TEST_CASE(iterate_stream_test) {
{ {
auto d = Global::get().e->data->index.openFile("landstal.dff"); 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(); RWBStream::ChunkID id = stream.getNextChunk();