Skip to content

Commit

Permalink
Automatically fixup Steam G1.DAT
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjuk authored and IntelOrca committed Jan 23, 2018
1 parent e4e509c commit ed9b2ca
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
30 changes: 28 additions & 2 deletions src/openloco/graphics/gfx.cpp
@@ -1,4 +1,5 @@
#include <fstream>
#include <iostream>

#include "../interop/interop.hpp"
#include "../environment.h"
Expand All @@ -22,7 +23,7 @@ namespace openloco::gfx
{
auto g1Path = environment::get_path(environment::path_id::g1);

std::ifstream stream(g1Path.make_preferred().u8string().c_str(), std::ios::in | std::ios::binary);
std::ifstream stream(g1Path, std::ios::in | std::ios::binary);
gfx::g1_header_t header;
void * g1Buffer;

Expand All @@ -38,6 +39,16 @@ namespace openloco::gfx
throw std::runtime_error("Reading g1 file header failed.");
}

if (header.num_entries != LOCO_G1_ELEMENT_COUNT)
{
std::cout << "G1 element count doesn't match expected value: ";
std::cout << "Expected " << LOCO_G1_ELEMENT_COUNT << "; Got " << header.num_entries << std::endl;
if (header.num_entries == LOCO_G1_ELEMENT_COUNT_STEAM)
{
std::cout << "Got Steam G1.DAT variant, will fix elements automatically." << std::endl;
}
}

// Read element headers
if (!stream.read((char *)_g1Elements.get(), header.num_entries * sizeof(g1_element_t)))
{
Expand All @@ -52,12 +63,27 @@ namespace openloco::gfx
throw std::runtime_error("Reading g1 elements failed.");
}

stream.close();

// Adjust memory offsets
for (uint32_t i = 0; i < header.num_entries; i++)
{
_g1Elements[i].offset += (int32_t)g1Buffer;
}
stream.close();

// The steam G1.DAT is missing two localised tutorial icons, and a smaller font variant
// This code copies the closest variants into their place, and moves other elements accordingly
if (header.num_entries == LOCO_G1_ELEMENT_COUNT_STEAM)
{
// Extra two tutorial images
memmove(&_g1Elements[3551], &_g1Elements[3549], sizeof(g1_element_t) * (header.num_entries - 3549));
memcpy(&_g1Elements[3549], &_g1Elements[3551], sizeof(g1_element_t));
memcpy(&_g1Elements[3550], &_g1Elements[3551], sizeof(g1_element_t));

// Extra font variant
memcpy(&_g1Elements[3898], &_g1Elements[1788], sizeof(g1_element_t) * 223);

}
}
catch (const std::exception &e)
{
Expand Down
3 changes: 2 additions & 1 deletion src/openloco/graphics/gfx.h
Expand Up @@ -4,7 +4,8 @@
#include "../localisation/stringmgr.h"
#include "../openloco.h"

#define LOCO_G1_ELEMENT_COUNT 0x1A10
#define LOCO_G1_ELEMENT_COUNT 0x101A
#define LOCO_G1_ELEMENT_COUNT_STEAM 0x0F38

namespace openloco::gfx
{
Expand Down

0 comments on commit ed9b2ca

Please sign in to comment.