Skip to content

Commit

Permalink
feat: convert from dds to iwi
Browse files Browse the repository at this point in the history
  • Loading branch information
Laupetin committed Sep 27, 2024
1 parent 8bd7066 commit a2d70c1
Show file tree
Hide file tree
Showing 4 changed files with 309 additions and 231 deletions.
125 changes: 108 additions & 17 deletions src/ImageConverter/ImageConverter.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#include "ImageConverter.h"

#include "Image/DdsLoader.h"
#include "Image/DdsWriter.h"
#include "Image/IwiLoader.h"
#include "Image/IwiWriter13.h"
#include "Image/IwiWriter27.h"
#include "Image/IwiWriter6.h"
#include "Image/IwiWriter8.h"
#include "Image/Texture.h"
#include "ImageConverterArgs.h"
#include "Utils/StringUtils.h"

#include <assert.h>
#include <filesystem>
#include <format>
#include <fstream>
Expand All @@ -16,19 +22,7 @@ namespace fs = std::filesystem;
namespace image_converter
{
constexpr auto EXTENSION_IWI = ".iwi";

class ImageLoader
{
public:
ImageLoader() = default;
virtual ~ImageLoader() = default;
ImageLoader(const ImageLoader& other) = default;
ImageLoader(ImageLoader&& other) noexcept = default;
ImageLoader& operator=(const ImageLoader& other) = default;
ImageLoader& operator=(ImageLoader&& other) noexcept = default;

// virtual Texture*
};
constexpr auto EXTENSION_DDS = ".dds";

class ImageConverterImpl final : public ImageConverter
{
Expand Down Expand Up @@ -63,13 +57,11 @@ namespace image_converter
utils::MakeStringLowerCase(extension);

if (extension == EXTENSION_IWI)
{
ConvertIwi(filePath);
}
else if (extension == EXTENSION_DDS)
ConvertDds(filePath);
else
{
std::cerr << std::format("Unsupported extension {}\n", extension);
}
}

bool ConvertIwi(const fs::path& iwiPath)
Expand Down Expand Up @@ -99,9 +91,108 @@ namespace image_converter
return true;
}

bool ConvertDds(const fs::path& ddsPath)
{
std::ifstream file(ddsPath, std::ios::in | std::ios::binary);
if (!file.is_open())
{
std::cerr << std::format("Failed to open input file {}\n", ddsPath.string());
return false;
}

const auto texture = dds::LoadDds(file);
if (!texture)
return false;

if (!EnsureIwiWriterIsPresent())
return false;

auto outPath = ddsPath;
outPath.replace_extension(".iwi");

std::ofstream outFile(outPath, std::ios::out | std::ios::binary);
if (!outFile.is_open())
{
std::cerr << std::format("Failed to open output file {}\n", outPath.string());
return false;
}

m_iwi_writer->DumpImage(outFile, texture.get());
return true;
}

bool EnsureIwiWriterIsPresent()
{
if (m_iwi_writer)
return true;

if (m_game_to_convert_to == Game::UNKNOWN && !ShowGameTui())
return false;

switch (m_game_to_convert_to)
{
case Game::IW3:
m_iwi_writer = std::make_unique<iwi6::IwiWriter>();
break;
case Game::IW4:
case Game::IW5:
m_iwi_writer = std::make_unique<iwi8::IwiWriter>();
break;
case Game::T5:
m_iwi_writer = std::make_unique<iwi13::IwiWriter>();
break;
case Game::T6:
m_iwi_writer = std::make_unique<iwi27::IwiWriter>();
break;
default:
assert(false);
return false;
}

return true;
}

bool ShowGameTui()
{
std::cout << "Select the game to convert to:\n";
std::cout << " 1 - Call Of Duty 4: Modern Warfare (IW3)\n";
std::cout << " 2 - Call Of Duty: Modern Warfare 2 (IW4)\n";
std::cout << " 3 - Call Of Duty: Modern Warfare 3 (IW5)\n";
std::cout << " 4 - Call Of Duty: Black Ops (T5)\n";
std::cout << " 5 - Call Of Duty: Black Ops 2 (T6)\n";

unsigned num;
std::cin >> num;

switch (num)
{
case 1:
m_game_to_convert_to = Game::IW3;
break;
case 2:
m_game_to_convert_to = Game::IW4;
break;
case 3:
m_game_to_convert_to = Game::IW5;
break;
case 4:
m_game_to_convert_to = Game::T5;
break;
case 5:
m_game_to_convert_to = Game::T6;
break;
default:
std::cerr << "Invalid input\n";
return false;
}

return true;
}

ImageConverterArgs m_args;
image_converter::Game m_game_to_convert_to;
DdsWriter m_dds_writer;
std::unique_ptr<IImageWriter> m_iwi_writer;
};
} // namespace image_converter

Expand Down
8 changes: 3 additions & 5 deletions src/ObjLoading/Game/IW3/AssetLoaders/AssetLoaderGfxImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ bool AssetLoaderGfxImage::LoadFromRaw(
if (!file.IsOpen())
return false;

const DdsLoader ddsLoader(zone->GetMemory());
auto* texture = ddsLoader.LoadDds(*file.m_stream);

if (texture == nullptr)
const auto texture = dds::LoadDds(*file.m_stream);
if (!texture)
{
std::cout << std::format("Failed to load dds file for image asset \"{}\"\n", assetName);
std::cerr << std::format("Failed to load dds file for image asset \"{}\"\n", assetName);
return false;
}

Expand Down
Loading

0 comments on commit a2d70c1

Please sign in to comment.