mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-10 23:08:05 -05:00
Replace FileAPI with c++ streams and std::filesystem
This commit is contained in:
@ -1,16 +1,17 @@
|
||||
#include "LoadingFileStream.h"
|
||||
|
||||
LoadingFileStream::LoadingFileStream(FileAPI::File* file)
|
||||
LoadingFileStream::LoadingFileStream(std::istream& stream)
|
||||
: m_stream(stream)
|
||||
{
|
||||
m_file = file;
|
||||
}
|
||||
|
||||
size_t LoadingFileStream::Load(void* buffer, const size_t length)
|
||||
{
|
||||
return m_file->Read(buffer, 1, length);
|
||||
m_stream.read(static_cast<char*>(buffer), length);
|
||||
return static_cast<size_t>(m_stream.gcount());
|
||||
}
|
||||
|
||||
int64_t LoadingFileStream::Pos()
|
||||
{
|
||||
return m_file->Pos();
|
||||
return m_stream.tellg();
|
||||
}
|
@ -1,13 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <istream>
|
||||
|
||||
#include "ILoadingStream.h"
|
||||
#include "Utils/FileAPI.h"
|
||||
|
||||
class LoadingFileStream final : public ILoadingStream
|
||||
{
|
||||
FileAPI::File* m_file;
|
||||
std::istream& m_stream;
|
||||
|
||||
public:
|
||||
explicit LoadingFileStream(FileAPI::File* file);
|
||||
explicit LoadingFileStream(std::istream& stream);
|
||||
|
||||
size_t Load(void* buffer, size_t length) override;
|
||||
int64_t Pos() override;
|
||||
|
@ -1,5 +1,7 @@
|
||||
#include "StepDumpData.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
StepDumpData::StepDumpData(const unsigned int dumpCount)
|
||||
{
|
||||
m_dump_count = dumpCount;
|
||||
@ -10,7 +12,7 @@ void StepDumpData::PerformStep(ZoneLoader* zoneLoader, ILoadingStream* stream)
|
||||
uint8_t tempBuffer[128];
|
||||
unsigned int dumpedBytes = 0;
|
||||
|
||||
FileAPI::File tempFile = FileAPI::Open("dump.dat", FileAPI::Mode::MODE_WRITE);
|
||||
std::ofstream tempFile("dump.dat", std::fstream::out | std::fstream::binary);
|
||||
|
||||
while (dumpedBytes < m_dump_count)
|
||||
{
|
||||
@ -25,14 +27,14 @@ void StepDumpData::PerformStep(ZoneLoader* zoneLoader, ILoadingStream* stream)
|
||||
toDump = sizeof(tempBuffer);
|
||||
}
|
||||
|
||||
const size_t loadedSize = stream->Load(tempBuffer, toDump);
|
||||
const auto loadedSize = stream->Load(tempBuffer, toDump);
|
||||
dumpedBytes += loadedSize;
|
||||
|
||||
if (loadedSize == 0)
|
||||
break;
|
||||
|
||||
tempFile.Write(tempBuffer, 1, loadedSize);
|
||||
tempFile.write(reinterpret_cast<char*>(tempBuffer), loadedSize);
|
||||
}
|
||||
|
||||
tempFile.Close();
|
||||
tempFile.close();
|
||||
}
|
@ -12,13 +12,13 @@ ZoneLoader::ZoneLoader(Zone* zone)
|
||||
|
||||
ZoneLoader::~ZoneLoader()
|
||||
{
|
||||
for(auto step : m_steps)
|
||||
for(auto* step : m_steps)
|
||||
{
|
||||
delete step;
|
||||
}
|
||||
m_steps.clear();
|
||||
|
||||
for(auto processor : m_processors)
|
||||
for(auto* processor : m_processors)
|
||||
{
|
||||
delete processor;
|
||||
}
|
||||
@ -27,9 +27,9 @@ ZoneLoader::~ZoneLoader()
|
||||
|
||||
ILoadingStream* ZoneLoader::BuildLoadingChain(ILoadingStream* rootStream)
|
||||
{
|
||||
ILoadingStream* currentStream = rootStream;
|
||||
auto* currentStream = rootStream;
|
||||
|
||||
for(auto processor : m_processors)
|
||||
for(auto* processor : m_processors)
|
||||
{
|
||||
processor->SetBaseStream(currentStream);
|
||||
|
||||
@ -74,14 +74,14 @@ void ZoneLoader::RemoveStreamProcessor(StreamProcessor* streamProcessor)
|
||||
}
|
||||
}
|
||||
|
||||
Zone* ZoneLoader::LoadZone(FileAPI::File* file)
|
||||
Zone* ZoneLoader::LoadZone(std::istream& stream)
|
||||
{
|
||||
LoadingFileStream fileStream(file);
|
||||
ILoadingStream* endStream = BuildLoadingChain(&fileStream);
|
||||
LoadingFileStream fileStream(stream);
|
||||
auto* endStream = BuildLoadingChain(&fileStream);
|
||||
|
||||
try
|
||||
{
|
||||
for(auto step : m_steps)
|
||||
for(auto* step : m_steps)
|
||||
{
|
||||
step->PerformStep(this, endStream);
|
||||
|
||||
|
@ -3,10 +3,10 @@
|
||||
#include "ILoadingStep.h"
|
||||
#include "Zone/Zone.h"
|
||||
#include "Zone/XBlock.h"
|
||||
#include "Utils/FileAPI.h"
|
||||
#include "StreamProcessor.h"
|
||||
|
||||
#include <vector>
|
||||
#include <istream>
|
||||
|
||||
class ILoadingStep;
|
||||
|
||||
@ -33,5 +33,5 @@ public:
|
||||
|
||||
void RemoveStreamProcessor(StreamProcessor* streamProcessor);
|
||||
|
||||
Zone* LoadZone(FileAPI::File* file);
|
||||
Zone* LoadZone(std::istream& stream);
|
||||
};
|
||||
|
@ -1,8 +1,14 @@
|
||||
#include "ZoneLoading.h"
|
||||
|
||||
#include "Utils/PathUtils.h"
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#include "Game/IW4/ZoneLoaderFactoryIW4.h"
|
||||
#include "Game/T6/ZoneLoaderFactoryT6.h"
|
||||
#include "Utils/ObjFileStream.h"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
IZoneLoaderFactory* ZoneLoaderFactories[]
|
||||
{
|
||||
@ -12,17 +18,22 @@ IZoneLoaderFactory* ZoneLoaderFactories[]
|
||||
|
||||
Zone* ZoneLoading::LoadZone(const std::string& path)
|
||||
{
|
||||
std::string zoneName = utils::Path::GetFilenameWithoutExtension(path);
|
||||
FileAPI::File file = FileAPI::Open(path, FileAPI::Mode::MODE_READ);
|
||||
auto zoneName = fs::path(path).filename().replace_extension("").string();
|
||||
std::ifstream file(path, std::fstream::in | std::fstream::binary);
|
||||
|
||||
if(!file.IsOpen())
|
||||
if(!file.is_open())
|
||||
{
|
||||
printf("Could not open file '%s'.\n", path.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ZoneHeader header{};
|
||||
file.Read(&header, sizeof(ZoneHeader), 1);
|
||||
file.read(reinterpret_cast<char*>(&header), sizeof header);
|
||||
if(file.gcount() != sizeof header)
|
||||
{
|
||||
std::cout << "Failed to read zone header from file '" << path << "'.\n";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ZoneLoader* zoneLoader = nullptr;
|
||||
for(auto* factory : ZoneLoaderFactories)
|
||||
@ -39,8 +50,9 @@ Zone* ZoneLoading::LoadZone(const std::string& path)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Zone* loadedZone = zoneLoader->LoadZone(&file);
|
||||
auto* loadedZone = zoneLoader->LoadZone(file);
|
||||
delete zoneLoader;
|
||||
|
||||
file.Close();
|
||||
file.close();
|
||||
return loadedZone;
|
||||
}
|
Reference in New Issue
Block a user