Skip to content

Commit

Permalink
Remove replace_extension since it can sometimes corrupt string contents
Browse files Browse the repository at this point in the history
  • Loading branch information
zero318 committed Jun 21, 2024
1 parent ee7d775 commit 7bbf78b
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions thcrap_tasofro/src/th135.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,32 @@ bool th135_init_fr(Th135File *fr, std::filesystem::path& path)
path = path.lexically_relative(std::filesystem::current_path());
}

if (th135_init_fr_inner(fr, (const char*)path.generic_u8string().c_str())) {
#if !CPP20
std::string path_str = path.generic_u8string();
#else
std::u8string path_str = path.generic_u8string();
#endif

if (th135_init_fr_inner(fr, (const char*)path_str.c_str())) {
return true;
}

// If the game loads a DDS file and we have no corresponding DDS file,
// try to replace it with a PNG file (the game will deal with it)
if (path.extension() == ".dds") {
path.replace_extension(".png");
const char* path_str = (const char*)path.generic_u8string().c_str();
size_t final_dot = path_str.find_last_of('.');
if (
final_dot != decltype(path_str)::npos &&
!path_str.compare(final_dot, 4, "dds\0"sv)
) {
path_str.replace(path_str.length() - strlen("dds"), strlen("dds"), "png"sv);

const char* path_ptr = (const char*)path_str.c_str();

if unexpected(runconfig_dat_dump_get()) {
register_utf8_filename(path_str);
register_utf8_filename(path_ptr);
}

return th135_init_fr_inner(fr, path_str);
return th135_init_fr_inner(fr, path_ptr);
}

return false;
Expand Down

0 comments on commit 7bbf78b

Please sign in to comment.