support 64 bit hashes (#27)

* use image.OverrideFormat()

* 1.6.4

* import std

* uninitialised member variable

* debug statement for 64 bit hash testing

* nopetynope

* uint32_t

* support for 64 bit hashes, but rip compute time :/

* refactor weird comments and constructors

* 1.7.0.0 because of 64 bit hashes

* only check crc64 if at least one mod with crc64 is loaded

* do some dumb shit to deal with incorrect bmps somehow (needs work)

* remove image interpretation for bmps

* create TpfConvert.exe to convert tpf files easily

* attempt

* set up vcpkg and install d3dx though that

* missing env variable?

* Win32 architecture

* update ci to tag TpfConvert.exe and d3dx9.dll as well (required to run TpfConvert)

* documentation
This commit is contained in:
DubbleClick
2024-09-28 12:15:04 +02:00
committed by GitHub
parent 866455567d
commit fd9d1af477
24 changed files with 728 additions and 123 deletions
+39
View File
@@ -0,0 +1,39 @@
add_executable(TpfConvert)
# Find all .ixx files in the TpfConvert folder
file(GLOB TPF_SOURCES "src/*.ixx")
# Add the .ixx files to the TpfConvert target
target_sources(TpfConvert PRIVATE ${TPF_SOURCES})
set_target_properties(TpfConvert PROPERTIES
CXX_STANDARD 23
CXX_STANDARD_REQUIRED ON
)
target_compile_definitions(TpfConvert PRIVATE
"NOMINMAX"
"_WIN32_WINNT=_WIN32_WINNT_WIN7"
"WIN32_LEAN_AND_MEAN"
"VC_EXTRALEAN"
)
if(DEFINED ENV{VCPKG_ROOT})
message(STATUS "VCPKG_ROOT: $ENV{VCPKG_ROOT}")
else()
message(STATUS "VCPKG_ROOT is not set")
endif()
if(DEFINED CMAKE_TOOLCHAIN_FILE)
message(STATUS "CMAKE_TOOLCHAIN_FILE: ${CMAKE_TOOLCHAIN_FILE}")
else()
message(STATUS "CMAKE_TOOLCHAIN_FILE is not set")
endif()
find_package(dxsdk-d3dx CONFIG REQUIRED)
target_link_libraries(TpfConvert PRIVATE
libzippp
Microsoft::D3DX9
d3d9
)
+179
View File
@@ -0,0 +1,179 @@
export module ModfileLoader;
import std;
import <libzippp.h>;
import ModfileLoader.TpfReader;
export using HashType = uint64_t;
export struct TexEntry {
std::vector<uint8_t> data{};
HashType crc_hash = 0; // hash value
std::string ext{};
};
namespace {
HashType GetCrcFromFilename(const std::string& filename) {
const static std::regex re(R"(0x[0-9a-f]{4,16})", std::regex::optimize | std::regex::icase);
std::smatch match;
if (!std::regex_search(filename, match, re)) {
return 0;
}
uint64_t crc64_hash = 0;
const auto number_str = match.str();
try {
crc64_hash = std::stoull(number_str, nullptr, 16);
}
catch (const std::invalid_argument&) {
std::print(stderr, "Failed to parse {} as a hash\n", filename);
return 0;
}
catch (const std::out_of_range&) {
std::print(stderr, "Out of range while parsing {} as a hash\n", filename);
return 0;
}
return crc64_hash;
}
}
export class ModfileLoader {
std::filesystem::path file_name;
const std::string TPF_PASSWORD{
0x73, 0x2A, 0x63, 0x7D, 0x5F, 0x0A, static_cast<char>(0xA6), static_cast<char>(0xBD),
0x7D, 0x65, 0x7E, 0x67, 0x61, 0x2A, 0x7F, 0x7F,
0x74, 0x61, 0x67, 0x5B, 0x60, 0x70, 0x45, 0x74,
0x5C, 0x22, 0x74, 0x5D, 0x6E, 0x6A, 0x73, 0x41,
0x77, 0x6E, 0x46, 0x47, 0x77, 0x49, 0x0C, 0x4B,
0x46, 0x6F
};
public:
ModfileLoader(const std::filesystem::path& fileName);
std::vector<TexEntry> GetContents() const;
private:
std::vector<TexEntry> GetTpfContents() const;
std::vector<TexEntry> GetFileContents() const;
static void LoadEntries(libzippp::ZipArchive& archive, std::vector<TexEntry>& entries);
};
ModfileLoader::ModfileLoader(const std::filesystem::path& fileName)
{
file_name = std::filesystem::absolute(fileName);
}
std::vector<TexEntry> ModfileLoader::GetContents() const
{
try {
return file_name.wstring().ends_with(L".tpf") ? GetTpfContents() : GetFileContents();
}
catch (const std::exception&) {
std::print(stderr, "Failed to open mod file: {}\n", file_name.string().c_str());
}
return {};
}
std::vector<TexEntry> ModfileLoader::GetTpfContents() const
{
std::vector<TexEntry> entries;
auto tpf_reader = TpfReader(file_name);
const auto buffer = tpf_reader.ReadToEnd();
const auto zip_archive = libzippp::ZipArchive::fromBuffer(buffer.data(), buffer.size(), false, TPF_PASSWORD);
if (!zip_archive) {
std::print(stderr, "Failed to open tpf file: {} - {} uint8_ts!\n", file_name.string(), buffer.size());
return {};
}
zip_archive->setErrorHandlerCallback(
[](const std::string& message, const std::string& strerror, int zip_error_code, int system_error_code) -> void {
std::print(stderr, "GetTpfContents: {} {} {} {}\n", message, strerror, zip_error_code, system_error_code);
});
zip_archive->open();
LoadEntries(*zip_archive, entries);
zip_archive->close();
libzippp::ZipArchive::free(zip_archive);
return entries;
}
std::vector<TexEntry> ModfileLoader::GetFileContents() const
{
std::vector<TexEntry> entries;
libzippp::ZipArchive zip_archive(file_name.string());
zip_archive.open();
LoadEntries(zip_archive, entries);
zip_archive.close();
return entries;
}
void ParseSimpleArchive(const libzippp::ZipArchive& archive, std::vector<TexEntry>& entries)
{
for (const auto& entry : archive.getEntries()) {
if (!entry.isFile())
continue;
const auto crc_hash = GetCrcFromFilename(entry.getName());
if (!crc_hash)
continue;
const auto data_ptr = static_cast<uint8_t*>(entry.readAsBinary());
const auto size = entry.getSize();
std::vector vec(data_ptr, data_ptr + size);
std::filesystem::path tex_name(entry.getName());
entries.emplace_back(std::move(vec), crc_hash, tex_name.extension().string());
delete[] data_ptr;
}
}
void ParseTexmodArchive(std::vector<std::string>& lines, libzippp::ZipArchive& archive, std::vector<TexEntry>& entries)
{
for (const auto& line : lines) {
// 0xC57D73F7|GW.EXE_0xC57D73F7.tga\r\n
// match[1] | match[2]
const static auto address_file_regex = std::regex(R"(^[\\/.]*([^|]+)\|([^\r\n]+))", std::regex::optimize);
std::smatch match;
if (!std::regex_search(line, match, address_file_regex))
continue;
const auto address_string = match[1].str();
const auto file_path = match[2].str();
const auto crc_hash = GetCrcFromFilename(address_string);
if (!crc_hash)
continue;
const auto entry = archive.getEntry(file_path);
if (entry.isNull() || !entry.isFile())
continue;
const auto data_ptr = static_cast<uint8_t*>(entry.readAsBinary());
const auto size = static_cast<size_t>(entry.getSize());
std::vector vec(data_ptr, data_ptr + size);
const auto tex_name = std::filesystem::path(entry.getName());
entries.emplace_back(std::move(vec), crc_hash, tex_name.extension().string());
delete[] data_ptr;
}
}
void ModfileLoader::LoadEntries(libzippp::ZipArchive& archive, std::vector<TexEntry>& entries)
{
const auto def_file = archive.getEntry("texmod.def");
if (def_file.isNull() || !def_file.isFile()) {
ParseSimpleArchive(archive, entries);
}
else {
const auto def = def_file.readAsText();
std::istringstream iss(def);
std::vector<std::string> lines;
std::string line;
while (std::getline(iss, line)) {
lines.push_back(line);
}
ParseTexmodArchive(lines, archive, entries);
}
}
@@ -0,0 +1,58 @@
export module ModfileLoader.TpfReader;
import std;
export class TpfReader {
public:
TpfReader(const std::filesystem::path& path)
{
file_stream = std::ifstream(path, std::ios::binary);
if (!file_stream.seekg(0, std::ios::end).good() || !file_stream.seekg(0, std::ios::beg).good()) {
throw std::invalid_argument("Provided stream needs to have SEEK set to True");
}
}
~TpfReader()
{
file_stream.close();
}
std::vector<char> ReadToEnd()
{
file_stream.seekg(0, std::ios::end);
line_length = file_stream.tellg();
file_stream.seekg(0, std::ios::beg); // Go to the beginning of the stream
std::vector<char> data(line_length);
file_stream.read(data.data(), line_length);
for (auto i = 0; i < data.size(); i++) {
data[i] = XOR(data[i], i);
}
for (int i = data.size() - 1; i > 0 && data[i] != 0; i--) {
data[i] = 0;
}
// in the other zip libraries, these had to be cut off, with libzip we need to zero them out
// cutting them off makes the archive invalid
//data.resize(last_zero);
return data;
}
private:
std::ifstream file_stream;
long line_length = 0;
[[nodiscard]] char XOR(const char b, const long position) const
{
if (position > line_length - 4) {
return b ^ TPF_XOREven;
}
return position % 2 == 0 ? b ^ TPF_XOREven : b ^ TPF_XOROdd;
}
static constexpr char TPF_XOROdd = 0x3F;
static constexpr char TPF_XOREven = static_cast<char>(0xA4);
};
+144
View File
@@ -0,0 +1,144 @@
#include <corecrt_wstdio.h>
#include <Windows.h>
#include <d3dx9.h>
#include <d3d9.h>
import std;
import ModfileLoader;
import <libzippp.h>;
struct TexEntry;
namespace {
IDirect3D9* pD3D = nullptr;
IDirect3DDevice9* pDevice = nullptr;
bool InitializeDirect3D()
{
pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if (pD3D == nullptr) {
std::print(stderr, "Failed to create Direct3D9 object\n");
return false;
}
D3DPRESENT_PARAMETERS d3dpp = {};
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = GetDesktopWindow();
if (FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, GetDesktopWindow(),
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice))) {
std::print(stderr, "Failed to create Direct3D9 device\n");
pD3D->Release();
return false;
}
return true;
}
void CleanupDirect3D()
{
if (pDevice) pDevice->Release();
if (pD3D) pD3D->Release();
}
std::pair<std::string, std::vector<uint8_t>> SaveAsDDS(const TexEntry& entry)
{
IDirect3DTexture9* pTexture = nullptr;
ID3DXBuffer* pBuffer = nullptr;
HRESULT hr = D3DXCreateTextureFromFileInMemoryEx(pDevice,
entry.data.data(), entry.data.size(),
D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0,
D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_FILTER_NONE,
D3DX_FILTER_NONE, 0, NULL, NULL, &pTexture);
if (FAILED(hr)) {
std::print(stderr, "Failed to create texture from memory\n");
return {};
}
hr = D3DXSaveTextureToFileInMemory(&pBuffer, D3DXIFF_DDS, pTexture, NULL);
if (FAILED(hr)) {
std::print(stderr, "Failed to save texture to DDS memory buffer\n");
pTexture->Release();
return {};
}
std::string dds_filename = std::format("0x{:x}.dds", entry.crc_hash);
std::vector<uint8_t> dds_data(pBuffer->GetBufferSize());
std::memcpy(dds_data.data(), pBuffer->GetBufferPointer(), pBuffer->GetBufferSize());
pTexture->Release();
pBuffer->Release();
return {dds_filename, dds_data};
}
std::filesystem::path GetExecutablePath()
{
char buffer[MAX_PATH];
GetModuleFileNameA(NULL, buffer, MAX_PATH);
return std::filesystem::path(buffer).parent_path();
}
}
int main(int argc, char* argv[])
{
const std::filesystem::path path = argc > 1 ? argv[1] : GetExecutablePath() / "plugins";
if (!InitializeDirect3D()) {
return -1;
}
if (!std::filesystem::exists(path)) {
return 1;
}
for (const auto& modfile : std::filesystem::directory_iterator(path)) {
if (modfile.is_regular_file() && (modfile.path().extension() == ".tpf" || modfile.path().extension() == ".zip")) {
const auto mod_path = modfile.path();
if (mod_path.extension() == ".zip" && mod_path.stem().string().ends_with("_")) {
std::print("Skipping previous TpfConvert output: {}\n", mod_path.filename().string());
continue;
}
else {
std::print("Processing: {}\n", mod_path.filename().string());
}
const auto zip_filename = mod_path.parent_path() / (mod_path.stem().string() + "_.zip");
if (std::filesystem::exists(zip_filename)) {
std::print("{} was already processed\n", mod_path.filename().string());
continue;
}
libzippp::ZipArchive zip_archive(zip_filename.string());
ModfileLoader loader(mod_path);
std::vector<std::pair<std::string, std::vector<uint8_t>>> data_entries;
const auto entries = loader.GetContents();
zip_archive.open(libzippp::ZipArchive::Write);
for (const auto& entry : entries) {
auto [dds_filename, dds_data] = SaveAsDDS(entry);
if (!dds_filename.empty()) {
data_entries.emplace_back(dds_filename, std::move(dds_data));
}
}
for (const auto& [filename, data] : data_entries) {
const auto success = zip_archive.addData(filename, data.data(), data.size());
if (!success) {
std::print(stderr, "Failed to add data to ZIP: {}\n", filename);
}
}
zip_archive.close();
std::print("Saved to ZIP: {}\n", zip_filename.string());
}
}
CleanupDirect3D();
return 0;
}