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

@ -15,8 +15,8 @@ std::string AssetDumperAddonMapEnts::GetFileNameForAsset(Zone* zone, XAssetInfo<
return asset->m_name;
}
void AssetDumperAddonMapEnts::DumpAsset(Zone* zone, XAssetInfo<AddonMapEnts>* asset, FileAPI::File* out)
void AssetDumperAddonMapEnts::DumpAsset(Zone* zone, XAssetInfo<AddonMapEnts>* asset, std::ostream& stream)
{
const auto* addonMapEnts = asset->Asset();
out->Write(addonMapEnts->entityString, 1, std::max(addonMapEnts->numEntityChars - 1, 0));
stream.write(addonMapEnts->entityString, std::max(addonMapEnts->numEntityChars - 1, 0));
}

View File

@ -10,6 +10,6 @@ namespace IW4
protected:
bool ShouldDump(XAssetInfo<AddonMapEnts>* asset) override;
std::string GetFileNameForAsset(Zone* zone, XAssetInfo<AddonMapEnts>* asset) override;
void DumpAsset(Zone* zone, XAssetInfo<AddonMapEnts>* asset, FileAPI::File* out) override;
void DumpAsset(Zone* zone, XAssetInfo<AddonMapEnts>* asset, std::ostream& stream) override;
};
}

View File

@ -42,8 +42,8 @@ std::string AssetDumperGfxImage::GetFileNameForAsset(Zone* zone, XAssetInfo<GfxI
return "images/" + asset->m_name + m_writer->GetFileExtension();
}
void AssetDumperGfxImage::DumpAsset(Zone* zone, XAssetInfo<GfxImage>* asset, FileAPI::File* out)
void AssetDumperGfxImage::DumpAsset(Zone* zone, XAssetInfo<GfxImage>* asset, std::ostream& stream)
{
const auto* image = asset->Asset();
m_writer->DumpImage(out, image->texture.texture);
m_writer->DumpImage(stream, image->texture.texture);
}

View File

@ -13,7 +13,7 @@ namespace IW4
protected:
bool ShouldDump(XAssetInfo<GfxImage>* asset) override;
std::string GetFileNameForAsset(Zone* zone, XAssetInfo<GfxImage>* asset) override;
void DumpAsset(Zone* zone, XAssetInfo<GfxImage>* asset, FileAPI::File* out) override;
void DumpAsset(Zone* zone, XAssetInfo<GfxImage>* asset, std::ostream& stream) override;
public:
AssetDumperGfxImage();

View File

@ -14,9 +14,9 @@ std::string AssetDumperLoadedSound::GetFileNameForAsset(Zone* zone, XAssetInfo<L
return "sound/" + asset->m_name;
}
void AssetDumperLoadedSound::DumpWavPcm(Zone* zone, const LoadedSound* asset, FileAPI::File* out)
void AssetDumperLoadedSound::DumpWavPcm(Zone* zone, const LoadedSound* asset, std::ostream& stream)
{
const uint32_t riffMasterChunkSize = sizeof WAV_CHUNK_ID_RIFF
const auto riffMasterChunkSize = sizeof WAV_CHUNK_ID_RIFF
+ sizeof uint32_t
+ sizeof WAV_WAVE_ID
+ sizeof WavChunkHeader
@ -24,16 +24,16 @@ void AssetDumperLoadedSound::DumpWavPcm(Zone* zone, const LoadedSound* asset, Fi
+ sizeof WavChunkHeader
+ sizeof asset->sound.info.data_len;
out->Write(&WAV_CHUNK_ID_RIFF, sizeof WAV_CHUNK_ID_RIFF, 1);
out->Write(&riffMasterChunkSize, sizeof riffMasterChunkSize, 1);
out->Write(&WAV_WAVE_ID, sizeof WAV_WAVE_ID, 1);
stream.write(reinterpret_cast<const char*>(&WAV_CHUNK_ID_RIFF), sizeof WAV_CHUNK_ID_RIFF);
stream.write(reinterpret_cast<const char*>(&riffMasterChunkSize), sizeof riffMasterChunkSize);
stream.write(reinterpret_cast<const char*>(&WAV_WAVE_ID), sizeof WAV_WAVE_ID);
const WavChunkHeader formatChunkHeader
{
WAV_CHUNK_ID_FMT,
sizeof WavFormatChunkPcm
};
out->Write(&formatChunkHeader, sizeof formatChunkHeader, 1);
stream.write(reinterpret_cast<const char*>(&formatChunkHeader), sizeof formatChunkHeader);
WavFormatChunkPcm formatChunk
{
@ -44,24 +44,24 @@ void AssetDumperLoadedSound::DumpWavPcm(Zone* zone, const LoadedSound* asset, Fi
static_cast<uint16_t>(asset->sound.info.block_size),
static_cast<uint16_t>(asset->sound.info.bits)
};
out->Write(&formatChunk, sizeof formatChunk, 1);
stream.write(reinterpret_cast<const char*>(&formatChunk), sizeof formatChunk);
const WavChunkHeader dataChunkHeader
{
WAV_CHUNK_ID_DATA,
asset->sound.info.data_len
};
out->Write(&dataChunkHeader, sizeof dataChunkHeader, 1);
out->Write(asset->sound.data, 1, asset->sound.info.data_len);
stream.write(reinterpret_cast<const char*>(&dataChunkHeader), sizeof dataChunkHeader);
stream.write(asset->sound.data, asset->sound.info.data_len);
}
void AssetDumperLoadedSound::DumpAsset(Zone* zone, XAssetInfo<LoadedSound>* asset, FileAPI::File* out)
void AssetDumperLoadedSound::DumpAsset(Zone* zone, XAssetInfo<LoadedSound>* asset, std::ostream& stream)
{
const auto* loadedSound = asset->Asset();
switch (static_cast<WavFormat>(loadedSound->sound.info.format))
{
case WavFormat::PCM:
DumpWavPcm(zone, loadedSound, out);
DumpWavPcm(zone, loadedSound, stream);
break;
default:

View File

@ -7,10 +7,10 @@ namespace IW4
{
class AssetDumperLoadedSound final : public AbstractAssetDumper<LoadedSound>
{
static void DumpWavPcm(Zone* zone, const LoadedSound* asset, FileAPI::File* out);
static void DumpWavPcm(Zone* zone, const LoadedSound* asset, std::ostream& stream);
protected:
bool ShouldDump(XAssetInfo<LoadedSound>* asset) override;
std::string GetFileNameForAsset(Zone* zone, XAssetInfo<LoadedSound>* asset) override;
void DumpAsset(Zone* zone, XAssetInfo<LoadedSound>* asset, FileAPI::File* out) override;
void DumpAsset(Zone* zone, XAssetInfo<LoadedSound>* asset, std::ostream& stream) override;
};
}

View File

@ -1,25 +1,35 @@
#include "AssetDumperLocalizeEntry.h"
#include <fstream>
#include <filesystem>
#include "Dumping/Localize/LocalizeCommon.h"
#include "Dumping/Localize/StringFileDumper.h"
using namespace IW4;
namespace fs = std::filesystem;
void AssetDumperLocalizeEntry::DumpPool(Zone* zone, AssetPool<LocalizeEntry>* pool, const std::string& basePath)
{
if (pool->m_asset_lookup.empty())
return;
const std::string language = LocalizeCommon::GetNameOfLanguage(zone->m_language);
const std::string stringsPath = utils::Path::Combine(basePath, language + "/localizedstrings");
const auto language = LocalizeCommon::GetNameOfLanguage(zone->m_language);
fs::path stringsPath(basePath);
stringsPath.append(language);
stringsPath.append("/localizedstrings");
FileAPI::DirectoryCreate(stringsPath);
create_directories(stringsPath);
FileAPI::File stringFile = FileAPI::Open(utils::Path::Combine(stringsPath, zone->m_name + ".str"), FileAPI::Mode::MODE_WRITE);
auto stringFilePath(stringsPath);
stringFilePath.append(zone->m_name);
stringFilePath.append(".str");
if (stringFile.IsOpen())
std::ofstream stringFile(stringFilePath, std::fstream::out | std::ofstream::binary);
if (stringFile.is_open())
{
StringFileDumper stringFileDumper(zone, &stringFile);
StringFileDumper stringFileDumper(zone, stringFile);
stringFileDumper.SetLanguageName(language);
@ -28,14 +38,14 @@ void AssetDumperLocalizeEntry::DumpPool(Zone* zone, AssetPool<LocalizeEntry>* po
stringFileDumper.SetNotes("");
for (auto localizeEntry : *pool)
for (auto* localizeEntry : *pool)
{
stringFileDumper.WriteLocalizeEntry(localizeEntry->m_name, localizeEntry->Asset()->value);
}
stringFileDumper.Finalize();
stringFile.Close();
stringFile.close();
}
else
{

View File

@ -14,7 +14,7 @@ std::string AssetDumperRawFile::GetFileNameForAsset(Zone* zone, XAssetInfo<RawFi
return asset->m_name;
}
void AssetDumperRawFile::DumpAsset(Zone* zone, XAssetInfo<RawFile>* asset, FileAPI::File* out)
void AssetDumperRawFile::DumpAsset(Zone* zone, XAssetInfo<RawFile>* asset, std::ostream& stream)
{
const auto* rawFile = asset->Asset();
if (rawFile->compressedLen > 0)
@ -52,13 +52,13 @@ void AssetDumperRawFile::DumpAsset(Zone* zone, XAssetInfo<RawFile>* asset, FileA
return;
}
out->Write(buffer, 1, sizeof buffer - zs.avail_out);
stream.write(reinterpret_cast<char*>(buffer), sizeof buffer - zs.avail_out);
}
inflateEnd(&zs);
}
else if (rawFile->len > 0)
{
out->Write(rawFile->data.buffer, 1, rawFile->len);
stream.write(rawFile->data.buffer, rawFile->len);
}
}

View File

@ -10,6 +10,6 @@ namespace IW4
protected:
bool ShouldDump(XAssetInfo<RawFile>* asset) override;
std::string GetFileNameForAsset(Zone* zone, XAssetInfo<RawFile>* asset) override;
void DumpAsset(Zone* zone, XAssetInfo<RawFile>* asset, FileAPI::File* out) override;
void DumpAsset(Zone* zone, XAssetInfo<RawFile>* asset, std::ostream& stream) override;
};
}

View File

@ -14,14 +14,14 @@ std::string AssetDumperStringTable::GetFileNameForAsset(Zone* zone, XAssetInfo<S
return asset->m_name;
}
void AssetDumperStringTable::DumpAsset(Zone* zone, XAssetInfo<StringTable>* asset, FileAPI::File* out)
void AssetDumperStringTable::DumpAsset(Zone* zone, XAssetInfo<StringTable>* asset, std::ostream& stream)
{
const auto* stringTable = asset->Asset();
CsvWriter csv(out);
CsvWriter csv(stream);
for (int row = 0; row < stringTable->rowCount; row++)
for (auto row = 0; row < stringTable->rowCount; row++)
{
for (int column = 0; column < stringTable->columnCount; column++)
for (auto column = 0; column < stringTable->columnCount; column++)
{
const auto* cell = &stringTable->values[column + row * stringTable->columnCount];
csv.WriteColumn(cell->string);

View File

@ -10,6 +10,6 @@ namespace IW4
protected:
bool ShouldDump(XAssetInfo<StringTable>* asset) override;
std::string GetFileNameForAsset(Zone* zone, XAssetInfo<StringTable>* asset) override;
void DumpAsset(Zone* zone, XAssetInfo<StringTable>* asset, FileAPI::File* out) override;
void DumpAsset(Zone* zone, XAssetInfo<StringTable>* asset, std::ostream& stream) override;
};
}

View File

@ -240,7 +240,7 @@ std::string AssetDumperVehicle::GetFileNameForAsset(Zone* zone, XAssetInfo<Vehic
return "vehicles/" + asset->m_name;
}
void AssetDumperVehicle::DumpAsset(Zone* zone, XAssetInfo<VehicleDef>* asset, FileAPI::File* out)
void AssetDumperVehicle::DumpAsset(Zone* zone, XAssetInfo<VehicleDef>* asset, std::ostream& stream)
{
InfoStringFromVehicleConverter converter(asset->Asset(), vehicle_fields, _countof(vehicle_fields), [asset](const scr_string_t scrStr) -> std::string
{
@ -253,5 +253,5 @@ void AssetDumperVehicle::DumpAsset(Zone* zone, XAssetInfo<VehicleDef>* asset, Fi
const auto infoString = converter.Convert();
const auto stringValue = infoString.ToString("VEHICLEFILE");
out->Write(stringValue.c_str(), 1, stringValue.length());
stream.write(stringValue.c_str(), stringValue.size());
}

View File

@ -13,6 +13,6 @@ namespace IW4
protected:
bool ShouldDump(XAssetInfo<VehicleDef>* asset) override;
std::string GetFileNameForAsset(Zone* zone, XAssetInfo<VehicleDef>* asset) override;
void DumpAsset(Zone* zone, XAssetInfo<VehicleDef>* asset, FileAPI::File* out) override;
void DumpAsset(Zone* zone, XAssetInfo<VehicleDef>* asset, std::ostream& stream) override;
};
}

View File

@ -1162,7 +1162,7 @@ std::string AssetDumperWeapon::GetFileNameForAsset(Zone* zone, XAssetInfo<Weapon
return "weapons/" + asset->m_name;
}
void AssetDumperWeapon::DumpAsset(Zone* zone, XAssetInfo<WeaponCompleteDef>* asset, FileAPI::File* out)
void AssetDumperWeapon::DumpAsset(Zone* zone, XAssetInfo<WeaponCompleteDef>* asset, std::ostream& stream)
{
auto* fullDef = new WeaponFullDef;
memset(fullDef, 0, sizeof WeaponFullDef);
@ -1179,7 +1179,7 @@ void AssetDumperWeapon::DumpAsset(Zone* zone, XAssetInfo<WeaponCompleteDef>* ass
const auto infoString = converter.Convert();
const auto stringValue = infoString.ToString("WEAPONFILE");
out->Write(stringValue.c_str(), 1, stringValue.length());
stream.write(stringValue.c_str(), stringValue.size());
delete fullDef;
}

View File

@ -14,6 +14,6 @@ namespace IW4
protected:
bool ShouldDump(XAssetInfo<WeaponCompleteDef>* asset) override;
std::string GetFileNameForAsset(Zone* zone, XAssetInfo<WeaponCompleteDef>* asset) override;
void DumpAsset(Zone* zone, XAssetInfo<WeaponCompleteDef>* asset, FileAPI::File* out) override;
void DumpAsset(Zone* zone, XAssetInfo<WeaponCompleteDef>* asset, std::ostream& stream) override;
};
}