1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-22 02:12:45 +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) {
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());
}

View File

@ -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;

View File

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

View File

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

View File

@ -49,7 +49,7 @@ bool LoaderIMG::findAssetInfo(const std::string& assetname,
return false;
}
char* LoaderIMG::loadToMemory(const std::string& assetname) {
std::unique_ptr<char[]> 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<char[]>(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;
}
}

View File

@ -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<char[]> loadToMemory(const std::string& assetname);
/// Writes the contents of assetname to 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,
TextureArchive& inTextures) {
auto data = file.data;
auto data = file.data.get();
RW::BinaryStreamSection root(data);
/*auto texDict =*/root.readStructure<RW::BSTextureDictionary>();

View File

@ -1,20 +1,29 @@
#ifndef _LIBRW_FILEHANDLE_HPP_
#define _LIBRW_FILEHANDLE_HPP_
#include <cstddef>
#include <memory>
/**
* @brief Contains a pointer to a file's contents.
*/
struct FileContentsInfo {
char *data;
std::unique_ptr<char[]> data;
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() {
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

View File

@ -6,7 +6,7 @@
#include <iterator>
#include <sstream>
#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<char[]>(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) {
@ -101,7 +101,7 @@ FileContentsInfo FileIndex::openFile(const std::string &filePath) {
const auto &indexedData = indexedDataPos->second;
char *data = nullptr;
std::unique_ptr<char[]> 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<char[]>(length);
dfile.read(data.get(), length);
}
return {data, length};
return {std::move(data), length};
}

View File

@ -8,10 +8,11 @@
#include <iomanip>
#include <iostream>
#include <boost/filesystem.hpp>
#include <fonts/GameTexts.hpp>
#include <loaders/LoaderGXT.hpp>
#include <models/TextModel.hpp>
#include <boost/filesystem.hpp>
#include <platform/FileHandle.hpp>
#include <render/GameRenderer.hpp>
#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");
RWBStream stream(d.data, d.length);
RWBStream stream(d.data.get(), d.length);
RWBStream::ChunkID id = stream.getNextChunk();