2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-06-30 08:11:49 +00:00

Replace FileAPI with c++ streams and std::filesystem

This commit is contained in:
Jan
2021-03-03 14:04:35 +01:00
parent b6b0a57232
commit 1cd06668e0
96 changed files with 1355 additions and 1061 deletions

View File

@ -2,28 +2,28 @@
const std::string AbstractZoneDefWriter::META_DATA_KEY_GAME = "game";
AbstractZoneDefWriter::AbstractZoneDefWriter(Zone* zone, FileAPI::IFile* file)
AbstractZoneDefWriter::AbstractZoneDefWriter(Zone* zone, std::ostream& stream)
: m_zone(zone),
m_stream(stream)
{
m_zone = zone;
m_file = file;
}
void AbstractZoneDefWriter::EmptyLine() const
{
m_file->Printf("\n");
m_stream << "\n";
}
void AbstractZoneDefWriter::WriteComment(const std::string& comment) const
{
m_file->Printf("// %s\n", comment.c_str());
m_stream << "// " << comment << "\n";
}
void AbstractZoneDefWriter::WriteMetaData(const std::string& metaDataKey, const std::string& metaDataValue) const
{
m_file->Printf(">%s,%s\n", metaDataKey.c_str(), metaDataValue.c_str());
m_stream << ">" << metaDataKey << "," << metaDataValue << "\n";
}
void AbstractZoneDefWriter::WriteEntry(const std::string& entryKey, const std::string& entryValue) const
{
m_file->Printf("%s,%s\n", entryKey.c_str(), entryValue.c_str());
}
m_stream << entryKey << "," << entryValue << "\n";
}

View File

@ -1,12 +1,14 @@
#pragma once
#include "Utils/FileAPI.h"
#include <ostream>
#include "Zone/Zone.h"
class AbstractZoneDefWriter
{
protected:
Zone* m_zone;
FileAPI::IFile* m_file;
std::ostream& m_stream;
static const std::string META_DATA_KEY_GAME;
@ -15,7 +17,7 @@ protected:
void WriteMetaData(const std::string& metaDataKey, const std::string& metaDataValue) const;
void WriteEntry(const std::string& entryKey, const std::string& entryValue) const;
AbstractZoneDefWriter(Zone* zone, FileAPI::IFile* file);
AbstractZoneDefWriter(Zone* zone, std::ostream& stream);
public:
virtual void WriteZoneDef() = 0;
@ -25,5 +27,5 @@ class IZoneDefWriter
{
public:
virtual bool CanHandleZone(Zone* zone) const = 0;
virtual void WriteZoneDef(Zone* zone, FileAPI::IFile* file) const = 0;
virtual void WriteZoneDef(Zone* zone, std::ostream& stream) const = 0;
};

View File

@ -42,8 +42,8 @@ namespace IW4
}
public:
ZoneDefWriterInternal(Zone* zone, FileAPI::IFile* file)
: AbstractZoneDefWriter(zone, file)
ZoneDefWriterInternal(Zone* zone, std::ostream& stream)
: AbstractZoneDefWriter(zone, stream)
{
}
@ -63,8 +63,8 @@ bool ZoneDefWriter::CanHandleZone(Zone* zone) const
return zone->m_game == &g_GameIW4;
}
void ZoneDefWriter::WriteZoneDef(Zone* zone, FileAPI::IFile* file) const
void ZoneDefWriter::WriteZoneDef(Zone* zone, std::ostream& stream) const
{
ZoneDefWriterInternal writer(zone, file);
ZoneDefWriterInternal writer(zone, stream);
writer.WriteZoneDef();
}

View File

@ -8,6 +8,6 @@ namespace IW4
{
public:
bool CanHandleZone(Zone* zone) const override;
void WriteZoneDef(Zone* zone, FileAPI::IFile* file) const override;
void WriteZoneDef(Zone* zone, std::ostream& stream) const override;
};
}

View File

@ -80,8 +80,8 @@ namespace T6
}
public:
ZoneDefWriterInternal(Zone* zone, FileAPI::IFile* file)
: AbstractZoneDefWriter(zone, file)
ZoneDefWriterInternal(Zone* zone, std::ostream& stream)
: AbstractZoneDefWriter(zone, stream)
{
}
@ -117,8 +117,8 @@ bool ZoneDefWriter::CanHandleZone(Zone* zone) const
return zone->m_game == &g_GameT6;
}
void ZoneDefWriter::WriteZoneDef(Zone* zone, FileAPI::IFile* file) const
void ZoneDefWriter::WriteZoneDef(Zone* zone, std::ostream& stream) const
{
ZoneDefWriterInternal writer(zone, file);
ZoneDefWriterInternal writer(zone, stream);
writer.WriteZoneDef();
}

View File

@ -8,6 +8,6 @@ namespace T6
{
public:
bool CanHandleZone(Zone* zone) const override;
void WriteZoneDef(Zone* zone, FileAPI::IFile* file) const override;
void WriteZoneDef(Zone* zone, std::ostream& stream) const override;
};
}

View File

@ -3,13 +3,13 @@
#include <set>
#include <regex>
#include <filesystem>
#include <fstream>
#include "Utils/ClassUtils.h"
#include "Utils/Arguments/ArgumentParser.h"
#include "ZoneLoading.h"
#include "ObjWriting.h"
#include "ContentLister/ContentPrinter.h"
#include "Utils/PathUtils.h"
#include "Utils/FileAPI.h"
#include "ObjLoading.h"
#include "SearchPath/SearchPaths.h"
#include "SearchPath/SearchPathFilesystem.h"
@ -19,6 +19,9 @@
#include "Game/IW4/ZoneDefWriterIW4.h"
#include "Game/T6/ZoneDefWriterT6.h"
#include "Utils/ObjFileStream.h"
namespace fs = std::filesystem;
const IZoneDefWriter* const ZONE_DEF_WRITERS[]
{
@ -33,7 +36,7 @@ class Unlinker::Impl
SearchPathFilesystem* m_last_zone_search_path;
std::set<std::string> m_absolute_search_paths;
bool ShouldLoadObj() const
_NODISCARD bool ShouldLoadObj() const
{
return m_args.m_task != UnlinkerArgs::ProcessingTask::LIST;
}
@ -80,7 +83,7 @@ class Unlinker::Impl
SearchPaths GetSearchPathsForZone(const std::string& zonePath)
{
SearchPaths searchPathsForZone;
const std::string absoluteZoneDirectory = absolute(std::filesystem::path(zonePath).remove_filename()).string();
const auto absoluteZoneDirectory = fs::absolute(std::filesystem::path(zonePath).remove_filename()).string();
if (m_last_zone_search_path != nullptr && m_last_zone_search_path->GetPath() == absoluteZoneDirectory)
{
@ -115,19 +118,19 @@ class Unlinker::Impl
{
for (const auto& path : m_args.m_user_search_paths)
{
std::string absolutePath = std::filesystem::absolute(path).string();
auto absolutePath = fs::absolute(path);
if (!FileAPI::DirectoryExists(absolutePath))
if (!fs::is_directory(absolutePath))
{
printf("Could not find directory of search path: \"%s\"\n", path.c_str());
return false;
}
auto* searchPath = new SearchPathFilesystem(absolutePath);
LoadSearchPath(searchPath);
m_search_paths.CommitSearchPath(searchPath);
auto searchPath = std::make_unique<SearchPathFilesystem>(absolutePath.string());
LoadSearchPath(searchPath.get());
m_search_paths.CommitSearchPath(std::move(searchPath));
m_absolute_search_paths.insert(absolutePath);
m_absolute_search_paths.insert(absolutePath.string());
}
if (m_args.m_verbose)
@ -161,23 +164,26 @@ class Unlinker::Impl
}
else if (m_args.m_task == UnlinkerArgs::ProcessingTask::DUMP)
{
const std::string outputFolderPath = m_args.GetOutputFolderPathForZone(zone);
FileAPI::DirectoryCreate(outputFolderPath);
const auto outputFolderPath = m_args.GetOutputFolderPathForZone(zone);
fs::create_directories(outputFolderPath);
const std::string zoneDefinitionFileFolder = utils::Path::Combine(outputFolderPath, "zone_source");
FileAPI::DirectoryCreate(zoneDefinitionFileFolder);
fs::path zoneDefinitionFileFolder(outputFolderPath);
zoneDefinitionFileFolder.append("zone_source");
fs::create_directories(zoneDefinitionFileFolder);
FileAPI::File zoneDefinitionFile = FileAPI::Open(
utils::Path::Combine(zoneDefinitionFileFolder, zone->m_name + ".zone"),
FileAPI::Mode::MODE_WRITE);
auto zoneDefinitionFilePath(zoneDefinitionFileFolder);
zoneDefinitionFilePath.append(zone->m_name);
zoneDefinitionFilePath.replace_extension(".zone");
if (zoneDefinitionFile.IsOpen())
std::ofstream zoneDefinitionFile(zoneDefinitionFilePath, std::fstream::out | std::fstream::binary);
if (zoneDefinitionFile.is_open())
{
for (auto zoneDefWriter : ZONE_DEF_WRITERS)
for (const auto* zoneDefWriter : ZONE_DEF_WRITERS)
{
if (zoneDefWriter->CanHandleZone(zone))
{
zoneDefWriter->WriteZoneDef(zone, &zoneDefinitionFile);
zoneDefWriter->WriteZoneDef(zone, zoneDefinitionFile);
break;
}
}
@ -189,7 +195,7 @@ class Unlinker::Impl
return false;
}
zoneDefinitionFile.Close();
zoneDefinitionFile.close();
}
return true;
@ -214,18 +220,18 @@ public:
for (const auto& zonePath : m_args.m_zones_to_load)
{
if (!FileAPI::FileExists(zonePath))
if (!fs::is_regular_file(zonePath))
{
printf("Could not find file \"%s\".\n", zonePath.c_str());
continue;
}
std::string absoluteZoneDirectory = absolute(std::filesystem::path(zonePath).remove_filename()).string();
auto absoluteZoneDirectory = absolute(std::filesystem::path(zonePath).remove_filename()).string();
SearchPaths searchPathsForZone = GetSearchPathsForZone(absoluteZoneDirectory);
auto searchPathsForZone = GetSearchPathsForZone(absoluteZoneDirectory);
searchPathsForZone.IncludeSearchPath(&m_search_paths);
Zone* zone = ZoneLoading::LoadZone(zonePath);
auto* zone = ZoneLoading::LoadZone(zonePath);
if (zone == nullptr)
{
printf("Failed to load zone \"%s\".\n", zonePath.c_str());