1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-22 10:22:52 +01:00

Merge pull request #540 from ShFil119/remove_FileHandle

Remove unneeded FileHandle (aka shared_ptr)
This commit is contained in:
Daniel Evans 2018-08-07 00:31:19 +01:00 committed by GitHub
commit f95427c136
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 70 additions and 60 deletions

View File

@ -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.get(), 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.get())) {
animations.insert(loader.animations.begin(),
loader.animations.end());
}

View File

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

View File

@ -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.get(), file.length);
std::stringstream ss(dataStr);
int numZooms = 0;

View File

@ -7,7 +7,7 @@ struct CutsceneTracks;
class LoaderCutsceneDAT {
public:
void load(CutsceneTracks& tracks, const FileHandle& file);
void load(CutsceneTracks& tracks, const FileContentsInfo& file);
};
#endif

View File

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

View File

@ -6,7 +6,7 @@ class GameTexts;
class LoaderGXT {
public:
void load(GameTexts& texts, const FileHandle& file);
void load(GameTexts& texts, const FileContentsInfo& file);
};
#endif

View File

@ -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<Clump>();
RWBStream rootStream(file->data, file->length);
RWBStream rootStream(file.data.get(), file.length);
auto rootID = rootStream.getNextChunk();
if (rootID != CHUNK_CLUMP) {

View File

@ -31,7 +31,7 @@ public:
using GeometryList = std::vector<GeometryPtr>;
using FrameList = std::vector<ModelFramePtr>;
ClumpPtr loadFromMemory(const FileHandle& file);
ClumpPtr loadFromMemory(const FileContentsInfo& file);
void setTextureLookupCallback(const TextureLookupCallback& tlc) {
texturelookup = tlc;

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

@ -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.get();
RW::BinaryStreamSection root(data);
/*auto texDict =*/root.readStructure<RW::BSTextureDictionary>();

View File

@ -6,7 +6,7 @@
class TextureLoader {
public:
bool loadFromMemory(const FileHandle& file, TextureArchive& inTextures);
bool loadFromMemory(const FileContentsInfo& file, TextureArchive& inTextures);
};
#endif

View File

@ -1,19 +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"
@ -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()) {
@ -66,10 +66,10 @@ FileHandle 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 std::make_shared<FileContentsInfo>(data, length);
return {std::move(data), static_cast<size_t>(length)};
}
void FileIndex::indexArchive(const std::string &archive) {
@ -91,16 +91,17 @@ 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;
char *data = nullptr;
std::unique_ptr<char[]> data = nullptr;
size_t length = 0;
if (indexedData.type == IndexedDataType::ARCHIVE) {
@ -125,13 +126,9 @@ FileHandle 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);
}
if (data == nullptr) {
return nullptr;
}
return std::make_shared<FileContentsInfo>(data, length);
return {std::move(data), length};
}

View File

@ -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:
/**

View File

@ -16,7 +16,6 @@ class Clump;
// Pointer types
using AnimationPtr = std::shared_ptr<Animation>;
using FileHandle = std::shared_ptr<FileContentsInfo>;
using ModelFramePtr = std::shared_ptr<ModelFrame>;
using GeometryPtr = std::shared_ptr<Geometry>;
using AtomicPtr = std::shared_ptr<Atomic>;

View File

@ -3,6 +3,7 @@
#include <engine/Animator.hpp>
#include <fstream>
#include <objects/GameObject.hpp>
#include <platform/FileHandle.hpp>
#include <widgets/ModelFramesWidget.hpp>
#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;
}

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

@ -1,6 +1,7 @@
#include <boost/test/unit_test.hpp>
#include <data/CutsceneData.hpp>
#include <loaders/LoaderCutsceneDAT.hpp>
#include <platform/FileHandle.hpp>
#include "test_Globals.hpp"
BOOST_AUTO_TEST_SUITE(CutsceneTests)

View File

@ -1,4 +1,5 @@
#include <boost/test/unit_test.hpp>
#include <platform/FileHandle.hpp>
#include <platform/FileIndex.hpp>
#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

View File

@ -1,5 +1,6 @@
#include <boost/test/unit_test.hpp>
#include <data/Clump.hpp>
#include <platform/FileHandle.hpp>
#include "test_Globals.hpp"
BOOST_AUTO_TEST_SUITE(LoaderDFFTests)

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();

View File

@ -2,6 +2,7 @@
#include <engine/ScreenText.hpp>
#include <fonts/GameTexts.hpp>
#include <loaders/LoaderGXT.hpp>
#include <platform/FileHandle.hpp>
#include "test_Globals.hpp"
#define T(x) GameStringUtil::fromString(x, FONT_PRICEDOWN)