1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-15 06:52:34 +02:00

Remove unneed FileHandle (aka shared_ptr)

This commit is contained in:
Filip Gawin 2018-07-06 22:05:43 +02:00
parent 0743ac9681
commit 27333efd8c
20 changed files with 41 additions and 38 deletions

View File

@ -292,8 +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, scm_h.length);
scm_h.reset();
return scm; return scm;
} }
@ -371,7 +370,7 @@ void GameData::loadTXD(const std::string& name) {
TextureArchive GameData::loadTextureArchive(const std::string& name) { TextureArchive GameData::loadTextureArchive(const std::string& name) {
/// @todo refactor loadTXD to use correct file locations /// @todo refactor loadTXD to use correct file locations
auto file = index.openFile(name); auto file = index.openFile(name);
if (!file) { if (!file.data) {
logger->error("Data", "Failed to open txd: " + name); logger->error("Data", "Failed to open txd: " + name);
return {}; return {};
} }
@ -397,7 +396,7 @@ void GameData::getNameAndLod(std::string& name, int& lod) {
ClumpPtr GameData::loadClump(const std::string& name) { ClumpPtr GameData::loadClump(const std::string& name) {
auto file = index.openFile(name); auto file = index.openFile(name);
if (!file) { if (!file.data) {
logger->error("Data", "Failed to load model " + name); logger->error("Data", "Failed to load model " + name);
return nullptr; return nullptr;
} }
@ -420,7 +419,7 @@ ClumpPtr GameData::loadClump(const std::string& name, const std::string& texture
void GameData::loadModelFile(const std::string& name) { void GameData::loadModelFile(const std::string& name) {
auto file = index.openFileRaw(name); auto file = index.openFileRaw(name);
if (!file) { if (!file.data) {
logger->log("Data", Logger::Error, "Failed to load model file " + name); logger->log("Data", Logger::Error, "Failed to load model file " + name);
return; return;
} }
@ -486,7 +485,7 @@ bool GameData::loadModel(ModelID model) {
loadTXD(slotname + ".txd"); loadTXD(slotname + ".txd");
auto file = index.openFile(name + ".dff"); auto file = index.openFile(name + ".dff");
if (!file) { if (!file.data) {
logger->error("Data", "Failed to load model for " + logger->error("Data", "Failed to load model for " +
std::to_string(model) + " [" + name + "]"); std::to_string(model) + " [" + name + "]");
return false; return false;
@ -523,9 +522,9 @@ bool GameData::loadModel(ModelID model) {
void GameData::loadIFP(const std::string& name) { void GameData::loadIFP(const std::string& name) {
auto f = index.openFile(name); auto f = index.openFile(name);
if (f) { if (f.data) {
LoaderIFP loader; LoaderIFP loader;
if (loader.loadFromMemory(f->data)) { if (loader.loadFromMemory(f.data)) {
animations.insert(loader.animations.begin(), animations.insert(loader.animations.begin(),
loader.animations.end()); loader.animations.end());
} }

View File

@ -29,6 +29,8 @@
#include "objects/PickupObject.hpp" #include "objects/PickupObject.hpp"
#include "objects/VehicleObject.hpp" #include "objects/VehicleObject.hpp"
#include "platform/FileHandle.hpp"
#include "render/ViewCamera.hpp" #include "render/ViewCamera.hpp"
#ifdef RW_WINDOWS #ifdef RW_WINDOWS
@ -695,7 +697,7 @@ void GameWorld::loadCutscene(const std::string& name) {
CutsceneData* cutscene = new CutsceneData; CutsceneData* cutscene = new CutsceneData;
if (datfile) { if (datfile.data) {
LoaderCutsceneDAT loaderdat; LoaderCutsceneDAT loaderdat;
loaderdat.load(cutscene->tracks, datfile); loaderdat.load(cutscene->tracks, datfile);
} }

View File

@ -12,8 +12,8 @@
#include "data/CutsceneData.hpp" #include "data/CutsceneData.hpp"
#include "platform/FileHandle.hpp" #include "platform/FileHandle.hpp"
void LoaderCutsceneDAT::load(CutsceneTracks &tracks, const FileHandle& file) { void LoaderCutsceneDAT::load(CutsceneTracks &tracks, const FileContentsInfo& file) {
std::string dataStr(file->data, file->length); std::string dataStr(file.data, file.length);
std::stringstream ss(dataStr); std::stringstream ss(dataStr);
int numZooms = 0; int numZooms = 0;

View File

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

View File

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

View File

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

View File

@ -455,10 +455,10 @@ AtomicPtr LoaderDFF::readAtomic(FrameList &framelist,
return atomic; return atomic;
} }
ClumpPtr LoaderDFF::loadFromMemory(const FileHandle& 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, file.length);
auto rootID = rootStream.getNextChunk(); auto rootID = rootStream.getNextChunk();
if (rootID != CHUNK_CLUMP) { if (rootID != CHUNK_CLUMP) {

View File

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

View File

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

View File

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

View File

@ -1,5 +1,6 @@
#ifndef _LIBRW_FILEHANDLE_HPP_ #ifndef _LIBRW_FILEHANDLE_HPP_
#define _LIBRW_FILEHANDLE_HPP_ #define _LIBRW_FILEHANDLE_HPP_
#include <cstddef>
/** /**
* @brief Contains a pointer to a file's contents. * @brief Contains a pointer to a file's contents.

View File

@ -50,7 +50,7 @@ rwfs::path FileIndex::findFilePath(const std::string &filePath) const {
return getIndexedDataAt(filePath)->path; 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); const auto *indexData = getIndexedDataAt(filePath);
std::ifstream dfile(indexData->path, std::ios::binary); std::ifstream dfile(indexData->path, std::ios::binary);
if (!dfile.is_open()) { if (!dfile.is_open()) {
@ -69,7 +69,7 @@ FileHandle FileIndex::openFileRaw(const std::string &filePath) const {
auto data = new char[length]; auto data = new char[length];
dfile.read(data, length); dfile.read(data, length);
return std::make_shared<FileContentsInfo>(data, length); return {data, static_cast<size_t>(length)};
} }
void FileIndex::indexArchive(const std::string &archive) { 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 cleanFilePath = normalizeFilePath(filePath);
auto indexedDataPos = indexedData_.find(cleanFilePath); auto indexedDataPos = indexedData_.find(cleanFilePath);
if (indexedDataPos == indexedData_.end()) { if (indexedDataPos == indexedData_.end()) {
return nullptr; return {nullptr, 0};
} }
const auto &indexedData = indexedDataPos->second; const auto &indexedData = indexedDataPos->second;
@ -129,9 +130,5 @@ FileHandle FileIndex::openFile(const std::string &filePath) {
dfile.read(data, length); dfile.read(data, length);
} }
if (data == nullptr) { return {data, length};
return nullptr;
}
return std::make_shared<FileContentsInfo>(data, length);
} }

View File

@ -38,7 +38,7 @@ public:
* @return FileHandle to the file * @return FileHandle to the file
* @throws if this FileIndex has not indexed the path * @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 * Adds the files contained within the given Archive file to the
@ -54,7 +54,7 @@ public:
* @param filePath name of the file to open * @param filePath name of the file to open
* @return FileHandle to the file, nullptr if this FileINdexed has not indexed the path * @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: private:
/** /**

View File

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

View File

@ -3,6 +3,7 @@
#include <engine/Animator.hpp> #include <engine/Animator.hpp>
#include <fstream> #include <fstream>
#include <objects/GameObject.hpp> #include <objects/GameObject.hpp>
#include <platform/FileHandle.hpp>
#include <widgets/ModelFramesWidget.hpp> #include <widgets/ModelFramesWidget.hpp>
#include "ViewerWidget.hpp" #include "ViewerWidget.hpp"
@ -47,7 +48,7 @@ void ModelViewer::showObject(uint16_t object) {
}); });
auto file = world()->data->index.openFile(modelName); auto file = world()->data->index.openFile(modelName);
if (!file) { if (!file.data) {
RW_ERROR("Couldn't load " << modelName); RW_ERROR("Couldn't load " << modelName);
return; return;
} }

View File

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

View File

@ -1,4 +1,5 @@
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
#include <platform/FileHandle.hpp>
#include <platform/FileIndex.hpp> #include <platform/FileIndex.hpp>
#include "test_Globals.hpp" #include "test_Globals.hpp"
@ -50,7 +51,7 @@ BOOST_AUTO_TEST_CASE(test_openFile) {
index.indexTree(Global::getGamePath() + "/data"); index.indexTree(Global::getGamePath() + "/data");
auto handle = index.openFile("cullzone.dat"); auto handle = index.openFile("cullzone.dat");
BOOST_CHECK(handle != nullptr); BOOST_CHECK(handle.data != nullptr);
} }
BOOST_AUTO_TEST_CASE(test_indexArchive) { BOOST_AUTO_TEST_CASE(test_indexArchive) {
@ -59,14 +60,14 @@ BOOST_AUTO_TEST_CASE(test_indexArchive) {
{ {
auto handle = index.openFile("landstal.dff"); auto handle = index.openFile("landstal.dff");
BOOST_CHECK(handle == nullptr); BOOST_CHECK(handle.data == nullptr);
} }
index.indexArchive("models/gta3.img"); index.indexArchive("models/gta3.img");
{ {
auto handle = index.openFile("landstal.dff"); auto handle = index.openFile("landstal.dff");
BOOST_CHECK(handle != nullptr); BOOST_CHECK(handle.data != nullptr);
} }
} }
#endif #endif

View File

@ -1,5 +1,6 @@
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
#include <data/Clump.hpp> #include <data/Clump.hpp>
#include <platform/FileHandle.hpp>
#include "test_Globals.hpp" #include "test_Globals.hpp"
BOOST_AUTO_TEST_SUITE(LoaderDFFTests) 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"); 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(); RWBStream::ChunkID id = stream.getNextChunk();

View File

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