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

@ -1,10 +1,10 @@
#pragma once
#include "IAssetDumper.h"
#include "Utils/FileAPI.h"
#include "Utils/PathUtils.h"
#include <cstdio>
#include <fstream>
#include <filesystem>
#include <iostream>
template<class T>
class AbstractAssetDumper : public IAssetDumper<T>
@ -12,7 +12,7 @@ class AbstractAssetDumper : public IAssetDumper<T>
protected:
virtual bool ShouldDump(XAssetInfo<T>* asset) = 0;
virtual std::string GetFileNameForAsset(Zone* zone, XAssetInfo<T>* asset) = 0;
virtual void DumpAsset(Zone* zone, XAssetInfo<T>* asset, FileAPI::File* out) = 0;
virtual void DumpAsset(Zone* zone, XAssetInfo<T>* asset, std::ostream& stream) = 0;
public:
void DumpPool(Zone* zone, AssetPool<T>* pool, const std::string& basePath) override
@ -25,21 +25,23 @@ public:
continue;
}
std::string assetFilePath = utils::Path::Combine(basePath, GetFileNameForAsset(zone, assetInfo));
std::filesystem::path assetFilePath(basePath);
assetFilePath.append(GetFileNameForAsset(zone, assetInfo));
FileAPI::DirectoryCreate(utils::Path::GetDirectory(assetFilePath));
auto assetFileFolder(assetFilePath);
assetFileFolder.replace_filename("");
create_directories(assetFileFolder);
auto file = FileAPI::Open(assetFilePath, FileAPI::Mode::MODE_WRITE);
if(file.IsOpen())
std::ofstream file(assetFilePath, std::fstream::out | std::fstream::binary);
if(file.is_open())
{
DumpAsset(zone, assetInfo, &file);
DumpAsset(zone, assetInfo, file);
file.Close();
file.close();
}
else
{
printf("Failed to open file '%s' to dump asset '%s'\n", assetFilePath.c_str(), assetInfo->m_name.c_str());
std::cout << "Failed to open file '" << assetFilePath.string() << "' to dump asset '" << assetInfo->m_name.c_str() << "'\n";
}
}
}

View File

@ -2,23 +2,21 @@
#include <sstream>
const std::string CsvWriter::LINE_BREAK = "\n";
CsvWriter::CsvWriter(FileAPI::IFile* file)
CsvWriter::CsvWriter(std::ostream& stream)
: m_stream(stream),
m_column_count(0),
m_current_column(0),
m_first_row(true)
{
m_file = file;
m_first_row = true;
m_current_column = 0;
m_column_count = 0;
}
void CsvWriter::WriteColumn(const std::string& value)
{
if (m_current_column++ > 0)
m_file->Printf(",");
m_stream << ",";
bool containsSeparator = false;
bool containsQuote = false;
auto containsSeparator = false;
auto containsQuote = false;
for (const auto& c : value)
{
if (c == '"')
@ -35,7 +33,7 @@ void CsvWriter::WriteColumn(const std::string& value)
{
std::ostringstream str;
for(const auto& c : value)
for (const auto& c : value)
{
if (c == '"')
str << "\"\"";
@ -43,15 +41,15 @@ void CsvWriter::WriteColumn(const std::string& value)
str << c;
}
m_file->Printf("\"%s\"", str.str().c_str());
m_stream << "\"" << str.str() << "\"";
}
else if (containsSeparator)
{
m_file->Printf("\"%s\"", value.c_str());
m_stream << "\"" << value << "\"";
}
else
{
m_file->Printf("%s", value.c_str());
m_stream << value;
}
}
@ -64,13 +62,13 @@ void CsvWriter::NextRow()
}
else
{
while(m_current_column < m_column_count)
while (m_current_column < m_column_count)
{
m_file->Printf(",");
m_stream << ",";
m_current_column++;
}
}
m_file->Printf("\n");
m_stream << "\n";
m_current_column = 0;
}

View File

@ -1,18 +1,19 @@
#pragma once
#include "Utils/FileAPI.h"
#include <string>
#include <ostream>
class CsvWriter
{
static constexpr char SEPARATOR = ',';
static const std::string LINE_BREAK;
FileAPI::IFile* m_file;
std::ostream& m_stream;
unsigned m_column_count;
unsigned m_current_column;
bool m_first_row;
public:
explicit CsvWriter(FileAPI::IFile* file);
explicit CsvWriter(std::ostream& stream);
void WriteColumn(const std::string& value);
void NextRow();

View File

@ -1,16 +1,12 @@
#include "StringFileDumper.h"
#include <regex>
StringFileDumper::StringFileDumper(Zone* zone, FileAPI::File* file)
StringFileDumper::StringFileDumper(Zone* zone, std::ostream& stream)
: m_zone(zone),
m_stream(stream),
m_language_caps("ENGLISH"),
m_wrote_header(false)
{
m_zone = zone;
m_file = file;
m_config_file = "";
m_notes = "";
m_language_caps = "ENGLISH";
m_wrote_header = false;
}
void StringFileDumper::SetLanguageName(std::string language)
@ -31,11 +27,11 @@ void StringFileDumper::SetNotes(std::string notes)
void StringFileDumper::WriteHeader()
{
m_file->Printf("// Dumped from fastfile \"%s\".\n", m_zone->m_name.c_str());
m_file->Printf("// In their original format the strings might have been separated in multiple files.\n");
m_file->Printf("VERSION \"1\"\n");
m_file->Printf("CONFIG \"%s\"\n", m_config_file.c_str());
m_file->Printf("FILENOTES \"%s\"\n", m_notes.c_str());
m_stream << "// Dumped from fastfile \"" << m_zone->m_name << "\".\n";
m_stream << "// In their original format the strings might have been separated in multiple files.\n";
m_stream << "VERSION \"1\"\n";
m_stream << "CONFIG \"" << m_config_file << "\"\n";
m_stream << "FILENOTES \"" << m_notes << "\"\n";
m_wrote_header = true;
}
@ -45,13 +41,13 @@ void StringFileDumper::WriteLocalizeEntry(const std::string& reference, const st
if (!m_wrote_header)
WriteHeader();
m_file->Printf("\n");
m_file->Printf("REFERENCE %s\n", reference.c_str());
m_stream << "\n";
m_stream << "REFERENCE " << reference <<"\n";
const std::string escapedValue = std::regex_replace(value, std::regex("\n"), "\\n");
const auto escapedValue = std::regex_replace(value, std::regex("\n"), "\\n");
const std::string valueSpacing = std::string(15 - m_language_caps.length(), ' ');
m_file->Printf("LANG_%s%s\"%s\"\n", m_language_caps.c_str(), valueSpacing.c_str(), escapedValue.c_str());
const auto valueSpacing = std::string(15 - m_language_caps.length(), ' ');
m_stream << "LANG_" << m_language_caps << valueSpacing << "\"" << escapedValue << "\"\n";
}
void StringFileDumper::Finalize()
@ -59,5 +55,5 @@ void StringFileDumper::Finalize()
if (!m_wrote_header)
WriteHeader();
m_file->Printf("\nENDMARKER");
}
m_stream << "\nENDMARKER";
}

View File

@ -1,12 +1,13 @@
#pragma once
#include <ostream>
#include "Zone/Zone.h"
#include "Utils/FileAPI.h"
class StringFileDumper
{
Zone* m_zone;
FileAPI::File* m_file;
std::ostream& m_stream;
std::string m_config_file;
std::string m_notes;
@ -17,7 +18,7 @@ class StringFileDumper
void WriteHeader();
public:
StringFileDumper(Zone* zone, FileAPI::File* file);
StringFileDumper(Zone* zone, std::ostream& stream);
void SetConfigFile(std::string configFile);
void SetNotes(std::string notes);

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

View File

@ -241,8 +241,8 @@ class AssetDumperFontIconInternal
}
public:
explicit AssetDumperFontIconInternal(FileAPI::IFile* file)
: m_csv(file)
explicit AssetDumperFontIconInternal(std::ostream& stream)
: m_csv(stream)
{
}
@ -265,8 +265,8 @@ std::string AssetDumperFontIcon::GetFileNameForAsset(Zone* zone, XAssetInfo<Font
return asset->m_name;
}
void AssetDumperFontIcon::DumpAsset(Zone* zone, XAssetInfo<FontIcon>* asset, FileAPI::File* out)
void AssetDumperFontIcon::DumpAsset(Zone* zone, XAssetInfo<FontIcon>* asset, std::ostream& stream)
{
AssetDumperFontIconInternal dumper(out);
AssetDumperFontIconInternal dumper(stream);
dumper.DumpFontIcon(asset->Asset());
}

View File

@ -10,6 +10,6 @@ namespace T6
protected:
bool ShouldDump(XAssetInfo<FontIcon>* asset) override;
std::string GetFileNameForAsset(Zone* zone, XAssetInfo<FontIcon>* asset) override;
void DumpAsset(Zone* zone, XAssetInfo<FontIcon>* asset, FileAPI::File* out) override;
void DumpAsset(Zone* zone, XAssetInfo<FontIcon>* 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 T6
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

@ -1,25 +1,35 @@
#include "AssetDumperLocalizeEntry.h"
#include <fstream>
#include <filesystem>
#include "Dumping/Localize/LocalizeCommon.h"
#include "Dumping/Localize/StringFileDumper.h"
using namespace T6;
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

@ -138,7 +138,7 @@ std::string AssetDumperPhysConstraints::GetFileNameForAsset(Zone* zone, XAssetIn
return "physconstraints/" + asset->m_name;
}
void AssetDumperPhysConstraints::DumpAsset(Zone* zone, XAssetInfo<PhysConstraints>* asset, FileAPI::File* out)
void AssetDumperPhysConstraints::DumpAsset(Zone* zone, XAssetInfo<PhysConstraints>* asset, std::ostream& stream)
{
assert(asset->Asset()->count <= 4);
@ -153,5 +153,5 @@ void AssetDumperPhysConstraints::DumpAsset(Zone* zone, XAssetInfo<PhysConstraint
const auto infoString = converter.Convert();
const auto stringValue = infoString.ToString("PHYSCONSTRAINTS");
out->Write(stringValue.c_str(), 1, stringValue.length());
stream.write(stringValue.c_str(), stringValue.size());
}

View File

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

View File

@ -86,7 +86,7 @@ std::string AssetDumperPhysPreset::GetFileNameForAsset(Zone* zone, XAssetInfo<Ph
return "physic/" + asset->m_name;
}
void AssetDumperPhysPreset::DumpAsset(Zone* zone, XAssetInfo<PhysPreset>* asset, FileAPI::File* out)
void AssetDumperPhysPreset::DumpAsset(Zone* zone, XAssetInfo<PhysPreset>* asset, std::ostream& stream)
{
auto* physPresetInfo = new PhysPresetInfo;
CopyToPhysPresetInfo(asset->Asset(), physPresetInfo);
@ -102,7 +102,7 @@ void AssetDumperPhysPreset::DumpAsset(Zone* zone, XAssetInfo<PhysPreset>* asset,
const auto infoString = converter.Convert();
const auto stringValue = infoString.ToString("PHYSIC");
out->Write(stringValue.c_str(), 1, stringValue.length());
stream.write(stringValue.c_str(), stringValue.size());
delete physPresetInfo;
}

View File

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

View File

@ -12,8 +12,8 @@ std::string AssetDumperQdb::GetFileNameForAsset(Zone* zone, XAssetInfo<Qdb>* ass
return asset->m_name;
}
void AssetDumperQdb::DumpAsset(Zone* zone, XAssetInfo<Qdb>* asset, FileAPI::File* out)
void AssetDumperQdb::DumpAsset(Zone* zone, XAssetInfo<Qdb>* asset, std::ostream& stream)
{
const auto* qdb = asset->Asset();
out->Write(qdb->buffer, 1, qdb->len);
stream.write(qdb->buffer, qdb->len);
}

View File

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

View File

@ -12,8 +12,8 @@ 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();
out->Write(rawFile->buffer, 1, rawFile->len);
stream.write(rawFile->buffer, rawFile->len);
}

View File

@ -10,6 +10,6 @@ namespace T6
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

@ -12,8 +12,8 @@ std::string AssetDumperScriptParseTree::GetFileNameForAsset(Zone* zone, XAssetIn
return asset->m_name;
}
void AssetDumperScriptParseTree::DumpAsset(Zone* zone, XAssetInfo<ScriptParseTree>* asset, FileAPI::File* out)
void AssetDumperScriptParseTree::DumpAsset(Zone* zone, XAssetInfo<ScriptParseTree>* asset, std::ostream& stream)
{
const auto* scriptParseTree = asset->Asset();
out->Write(scriptParseTree->buffer, 1, scriptParseTree->len);
stream.write(scriptParseTree->buffer, scriptParseTree->len);
}

View File

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

View File

@ -12,8 +12,8 @@ std::string AssetDumperSlug::GetFileNameForAsset(Zone* zone, XAssetInfo<Slug>* a
return asset->m_name;
}
void AssetDumperSlug::DumpAsset(Zone* zone, XAssetInfo<Slug>* asset, FileAPI::File* out)
void AssetDumperSlug::DumpAsset(Zone* zone, XAssetInfo<Slug>* asset, std::ostream& stream)
{
const auto* slug = asset->Asset();
out->Write(slug->buffer, 1, slug->len);
stream.write(slug->buffer, slug->len);
}

View File

@ -10,6 +10,6 @@ namespace T6
protected:
bool ShouldDump(XAssetInfo<Slug>* asset) override;
std::string GetFileNameForAsset(Zone* zone, XAssetInfo<Slug>* asset) override;
void DumpAsset(Zone* zone, XAssetInfo<Slug>* asset, FileAPI::File* out) override;
void DumpAsset(Zone* zone, XAssetInfo<Slug>* 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 T6
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

@ -85,7 +85,7 @@ std::string AssetDumperTracer::GetFileNameForAsset(Zone* zone, XAssetInfo<Tracer
return "tracer/" + asset->m_name;
}
void AssetDumperTracer::DumpAsset(Zone* zone, XAssetInfo<TracerDef>* asset, FileAPI::File* out)
void AssetDumperTracer::DumpAsset(Zone* zone, XAssetInfo<TracerDef>* asset, std::ostream& stream)
{
InfoStringFromTracerConverter converter(asset->Asset(), tracer_fields, _countof(tracer_fields), [asset](const scr_string_t scrStr) -> std::string
{
@ -98,7 +98,7 @@ void AssetDumperTracer::DumpAsset(Zone* zone, XAssetInfo<TracerDef>* asset, File
const auto infoString = converter.Convert();
const auto stringValue = infoString.ToString("TRACER");
out->Write(stringValue.c_str(), 1, stringValue.length());
stream.write(stringValue.c_str(), stringValue.size());
}
//void AssetDumperTracer::CheckFields()

View File

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

View File

@ -680,7 +680,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
{
@ -693,5 +693,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

@ -12,6 +12,6 @@ namespace T6
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

@ -1620,7 +1620,7 @@ std::string AssetDumperWeapon::GetFileNameForAsset(Zone* zone, XAssetInfo<Weapon
return "weapons/" + asset->m_name;
}
void AssetDumperWeapon::DumpAsset(Zone* zone, XAssetInfo<WeaponVariantDef>* asset, FileAPI::File* out)
void AssetDumperWeapon::DumpAsset(Zone* zone, XAssetInfo<WeaponVariantDef>* asset, std::ostream& stream)
{
auto* fullDef = new WeaponFullDef;
memset(fullDef, 0, sizeof WeaponFullDef);
@ -1637,7 +1637,7 @@ void AssetDumperWeapon::DumpAsset(Zone* zone, XAssetInfo<WeaponVariantDef>* asse
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 T6
protected:
bool ShouldDump(XAssetInfo<WeaponVariantDef>* asset) override;
std::string GetFileNameForAsset(Zone* zone, XAssetInfo<WeaponVariantDef>* asset) override;
void DumpAsset(Zone* zone, XAssetInfo<WeaponVariantDef>* asset, FileAPI::File* out) override;
void DumpAsset(Zone* zone, XAssetInfo<WeaponVariantDef>* asset, std::ostream& stream) override;
};
}

View File

@ -183,7 +183,7 @@ std::string AssetDumperZBarrier::GetFileNameForAsset(Zone* zone, XAssetInfo<ZBar
return "zbarrier/" + asset->m_name;
}
void AssetDumperZBarrier::DumpAsset(Zone* zone, XAssetInfo<ZBarrierDef>* asset, FileAPI::File* out)
void AssetDumperZBarrier::DumpAsset(Zone* zone, XAssetInfo<ZBarrierDef>* asset, std::ostream& stream)
{
InfoStringFromZBarrierConverter converter(asset->Asset(), zbarrier_fields, _countof(zbarrier_fields), [asset](const scr_string_t scrStr) -> std::string
{
@ -196,7 +196,7 @@ void AssetDumperZBarrier::DumpAsset(Zone* zone, XAssetInfo<ZBarrierDef>* asset,
const auto infoString = converter.Convert();
const auto stringValue = infoString.ToString("ZBARRIER");
out->Write(stringValue.c_str(), 1, stringValue.length());
stream.write(stringValue.c_str(), stringValue.size());
}
//void AssetDumperZBarrier::CheckFields()

View File

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

View File

@ -15,7 +15,7 @@ const std::map<ImageFormatId, ImageFormatId> DDS_CONVERSION_TABLE
class DdsWriterInternal
{
FileAPI::IFile* m_file;
std::ostream& m_stream;
Texture* m_texture;
std::unique_ptr<Texture> m_converted_texture;
bool m_use_dx10_extension;
@ -190,8 +190,8 @@ public:
return ".dds";
}
DdsWriterInternal(FileAPI::IFile* file, Texture* texture)
: m_file(file),
DdsWriterInternal(std::ostream& stream, Texture* texture)
: m_stream(stream),
m_texture(texture),
m_use_dx10_extension(false)
{
@ -204,24 +204,24 @@ public:
DDS_HEADER header{};
PopulateDdsHeader(header);
constexpr uint32_t magic = MakeFourCc('D', 'D', 'S', ' ');
constexpr auto magic = MakeFourCc('D', 'D', 'S', ' ');
m_file->Write(&magic, sizeof magic, 1);
m_file->Write(&header, sizeof header, 1);
m_stream.write(reinterpret_cast<const char*>(&magic), sizeof magic);
m_stream.write(reinterpret_cast<const char*>(&header), sizeof header);
if (m_use_dx10_extension)
{
DDS_HEADER_DXT10 dxt10{};
PopulateDxt10Header(dxt10);
m_file->Write(&dxt10, sizeof dxt10, 1);
m_stream.write(reinterpret_cast<const char*>(&dxt10), sizeof dxt10);
}
const int mipCount = m_texture->HasMipMaps() ? m_texture->GetMipMapCount() : 1;
const auto mipCount = m_texture->HasMipMaps() ? m_texture->GetMipMapCount() : 1;
for (auto mipLevel = 0; mipLevel < mipCount; mipLevel++)
{
const auto* buffer = m_texture->GetBufferForMipLevel(mipLevel);
const auto mipLevelSize = m_texture->GetSizeOfMipLevel(mipLevel) * m_texture->GetFaceCount();
m_file->Write(buffer, 1, mipLevelSize);
m_stream.write(reinterpret_cast<const char*>(buffer), mipLevelSize);
}
}
};
@ -239,8 +239,8 @@ std::string DdsWriter::GetFileExtension()
return DdsWriterInternal::GetFileExtension();
}
void DdsWriter::DumpImage(FileAPI::IFile* file, Texture* texture)
void DdsWriter::DumpImage(std::ostream& stream, Texture* texture)
{
DdsWriterInternal internal(file, texture);
DdsWriterInternal internal(stream, texture);
internal.DumpImage();
}

View File

@ -8,5 +8,5 @@ public:
bool SupportsImageFormat(const ImageFormat * imageFormat) override;
std::string GetFileExtension() override;
void DumpImage(FileAPI::IFile* file, Texture* texture) override;
void DumpImage(std::ostream& stream, Texture* texture) override;
};

View File

@ -1,9 +1,10 @@
#pragma once
#include "Image/Texture.h"
#include "Utils/FileAPI.h"
#include <ostream>
#include <string>
#include "Image/Texture.h"
class IImageWriter
{
public:
@ -11,5 +12,5 @@ public:
virtual bool SupportsImageFormat(const ImageFormat* imageFormat) = 0;
virtual std::string GetFileExtension() = 0;
virtual void DumpImage(FileAPI::IFile* file, Texture* texture) = 0;
virtual void DumpImage(std::ostream& stream, Texture* texture) = 0;
};

View File

@ -1,5 +1,6 @@
#include "IwiWriter27.h"
#include <cassert>
#include <ostream>
using namespace iwi27;
@ -52,7 +53,7 @@ std::string IwiWriter::GetFileExtension()
return ".iwi";
}
void IwiWriter::WriteVersion(FileAPI::IFile* file)
void IwiWriter::WriteVersion(std::ostream& stream)
{
IwiVersion version{};
version.tag[0] = 'I';
@ -60,7 +61,7 @@ void IwiWriter::WriteVersion(FileAPI::IFile* file)
version.tag[2] = 'i';
version.version = 27;
file->Write(&version, sizeof IwiVersion, 1);
stream.write(reinterpret_cast<char*>(&version), sizeof IwiVersion);
}
void IwiWriter::FillHeader2D(IwiHeader* header, Texture2D* texture)
@ -86,12 +87,11 @@ void IwiWriter::FillHeader3D(IwiHeader* header, Texture3D* texture)
header->flags |= IMG_FLAG_VOLMAP;
}
void IwiWriter::DumpImage(FileAPI::IFile* file, Texture* texture)
void IwiWriter::DumpImage(std::ostream& stream, Texture* texture)
{
assert(file != nullptr);
assert(texture != nullptr);
WriteVersion(file);
WriteVersion(stream);
IwiHeader header{};
header.flags = 0;
@ -102,39 +102,44 @@ void IwiWriter::DumpImage(FileAPI::IFile* file, Texture* texture)
if (!texture->HasMipMaps())
header.flags |= IMG_FLAG_NOMIPMAPS;
for (signed char& i : header.maxGlossForMip)
for (auto& i : header.maxGlossForMip)
i = 0;
size_t currentFileSize = sizeof IwiVersion + sizeof IwiHeader;
auto currentFileSize = sizeof IwiVersion + sizeof IwiHeader;
const int textureMipCount = texture->HasMipMaps() ? texture->GetMipMapCount() : 1;
for (int currentMipLevel = textureMipCount - 1; currentMipLevel >= 0; currentMipLevel--)
const auto textureMipCount = texture->HasMipMaps() ? texture->GetMipMapCount() : 1;
for (auto currentMipLevel = textureMipCount - 1; currentMipLevel >= 0; currentMipLevel--)
{
const size_t mipLevelSize = texture->GetSizeOfMipLevel(currentMipLevel) * texture->GetFaceCount();
const auto mipLevelSize = texture->GetSizeOfMipLevel(currentMipLevel) * texture->GetFaceCount();
currentFileSize += mipLevelSize;
if(currentMipLevel < static_cast<int>(_countof(iwi27::IwiHeader::fileSizeForPicmip)))
if (currentMipLevel < static_cast<int>(_countof(iwi27::IwiHeader::fileSizeForPicmip)))
header.fileSizeForPicmip[currentMipLevel] = currentFileSize;
}
if(auto* texture2D = dynamic_cast<Texture2D*>(texture))
if (auto* texture2D = dynamic_cast<Texture2D*>(texture))
{
FillHeader2D(&header, texture2D);
}
else if(auto* textureCube = dynamic_cast<TextureCube*>(texture))
else if (auto* textureCube = dynamic_cast<TextureCube*>(texture))
{
FillHeaderCube(&header, textureCube);
}
else if(auto* texture3D = dynamic_cast<Texture3D*>(texture))
else if (auto* texture3D = dynamic_cast<Texture3D*>(texture))
{
FillHeader3D(&header, texture3D);
}
file->Write(&header, sizeof IwiHeader, 1);
for (int currentMipLevel = textureMipCount - 1; currentMipLevel >= 0; currentMipLevel--)
else
{
const size_t mipLevelSize = texture->GetSizeOfMipLevel(currentMipLevel) * texture->GetFaceCount();
file->Write(texture->GetBufferForMipLevel(currentMipLevel), 1, mipLevelSize);
assert(false);
return;
}
stream.write(reinterpret_cast<char*>(&header), sizeof IwiHeader);
for (auto currentMipLevel = textureMipCount - 1; currentMipLevel >= 0; currentMipLevel--)
{
const auto mipLevelSize = texture->GetSizeOfMipLevel(currentMipLevel) * texture->GetFaceCount();
stream.write(reinterpret_cast<char*>(texture->GetBufferForMipLevel(currentMipLevel)), mipLevelSize);
}
}

View File

@ -9,7 +9,7 @@ namespace iwi27
{
static IwiFormat GetIwiFormatForImageFormat(const ImageFormat* imageFormat);
static void WriteVersion(FileAPI::IFile* file);
static void WriteVersion(std::ostream& stream);
static void FillHeader2D(IwiHeader* header, Texture2D* texture);
static void FillHeaderCube(IwiHeader* header, TextureCube* texture);
static void FillHeader3D(IwiHeader* header, Texture3D* texture);
@ -25,6 +25,6 @@ namespace iwi27
bool SupportsImageFormat(const ImageFormat* imageFormat) override;
std::string GetFileExtension() override;
void DumpImage(FileAPI::IFile* file, Texture* texture) override;
void DumpImage(std::ostream& stream, Texture* texture) override;
};
}

View File

@ -40,7 +40,7 @@ IwiFormat IwiWriter::GetIwiFormatForImageFormat(const ImageFormat* imageFormat)
}
}
void IwiWriter::WriteVersion(FileAPI::IFile* file)
void IwiWriter::WriteVersion(std::ostream& stream)
{
IwiVersion version{};
version.tag[0] = 'I';
@ -48,7 +48,7 @@ void IwiWriter::WriteVersion(FileAPI::IFile* file)
version.tag[2] = 'i';
version.version = 8;
file->Write(&version, sizeof IwiVersion, 1);
stream.write(reinterpret_cast<char*>(&version), sizeof IwiVersion);
}
void IwiWriter::FillHeader2D(IwiHeader* header, Texture2D* texture)
@ -84,12 +84,11 @@ std::string IwiWriter::GetFileExtension()
return ".iwi";
}
void IwiWriter::DumpImage(FileAPI::IFile* file, Texture* texture)
void IwiWriter::DumpImage(std::ostream& stream, Texture* texture)
{
assert(file != nullptr);
assert(texture != nullptr);
WriteVersion(file);
WriteVersion(stream);
IwiHeader header{};
header.flags = 0;
@ -99,12 +98,12 @@ void IwiWriter::DumpImage(FileAPI::IFile* file, Texture* texture)
if (!texture->HasMipMaps())
header.flags |= IMG_FLAG_NOMIPMAPS;
size_t currentFileSize = sizeof IwiVersion + sizeof IwiHeader;
auto currentFileSize = sizeof IwiVersion + sizeof IwiHeader;
const int textureMipCount = texture->HasMipMaps() ? texture->GetMipMapCount() : 1;
for (int currentMipLevel = textureMipCount - 1; currentMipLevel >= 0; currentMipLevel--)
const auto textureMipCount = texture->HasMipMaps() ? texture->GetMipMapCount() : 1;
for (auto currentMipLevel = textureMipCount - 1; currentMipLevel >= 0; currentMipLevel--)
{
const size_t mipLevelSize = texture->GetSizeOfMipLevel(currentMipLevel) * texture->GetFaceCount();
const auto mipLevelSize = texture->GetSizeOfMipLevel(currentMipLevel) * texture->GetFaceCount();
currentFileSize += mipLevelSize;
if (currentMipLevel < static_cast<int>(_countof(iwi27::IwiHeader::fileSizeForPicmip)))
@ -123,12 +122,17 @@ void IwiWriter::DumpImage(FileAPI::IFile* file, Texture* texture)
{
FillHeader3D(&header, texture3D);
}
file->Write(&header, sizeof iwi27::IwiHeader, 1);
for (int currentMipLevel = textureMipCount - 1; currentMipLevel >= 0; currentMipLevel--)
else
{
const size_t mipLevelSize = texture->GetSizeOfMipLevel(currentMipLevel) * texture->GetFaceCount();
file->Write(texture->GetBufferForMipLevel(currentMipLevel), 1, mipLevelSize);
assert(false);
return;
}
stream.write(reinterpret_cast<char*>(&header), sizeof IwiHeader);
for (auto currentMipLevel = textureMipCount - 1; currentMipLevel >= 0; currentMipLevel--)
{
const auto mipLevelSize = texture->GetSizeOfMipLevel(currentMipLevel) * texture->GetFaceCount();
stream.write(reinterpret_cast<char*>(texture->GetBufferForMipLevel(currentMipLevel)), mipLevelSize);
}
}

View File

@ -9,7 +9,7 @@ namespace iwi8
{
static IwiFormat GetIwiFormatForImageFormat(const ImageFormat* imageFormat);
static void WriteVersion(FileAPI::IFile* file);
static void WriteVersion(std::ostream& stream);
static void FillHeader2D(IwiHeader* header, Texture2D* texture);
static void FillHeaderCube(IwiHeader* header, TextureCube* texture);
static void FillHeader3D(IwiHeader* header, Texture3D* texture);
@ -25,6 +25,6 @@ namespace iwi8
bool SupportsImageFormat(const ImageFormat* imageFormat) override;
std::string GetFileExtension() override;
void DumpImage(FileAPI::IFile* file, Texture* texture) override;
void DumpImage(std::ostream& stream, Texture* texture) override;
};
}