Skip to content

Commit

Permalink
granny: support custom loading of gr2.lz4 models
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxiao921 committed Jun 6, 2024
1 parent ab4c957 commit 7041566
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ static bool extension_matches(const char *first, const char *second)
// See the binding data.cpp file for the implementation.
std::unordered_map<std::string, std::string> additional_package_files;

std::unordered_map<std::string, std::string> additional_granny_files;

static void hook_fsAppendPathComponent_packages(const char *basePath, const char *pathComponent, char *output /*size: 512*/)
{
big::g_hooking->get_original<hook_fsAppendPathComponent_packages>()(basePath, pathComponent, output);
Expand All @@ -409,22 +411,18 @@ static void hook_fsAppendPathComponent_packages(const char *basePath, const char
break;
}
}
}
}

static int ends_with(const char *str, const char *suffix)
{
if (!str || !suffix)
{
return 0;
}
size_t lenstr = strlen(str);
size_t lensuffix = strlen(suffix);
if (lensuffix > lenstr)
{
return 0;
for (const auto &[filename, full_file_path] : additional_granny_files)
{
if (strstr(pathComponent, filename.c_str()))
{
LOG(DEBUG) << pathComponent << " | " << filename << " | " << full_file_path;

strcpy(output, full_file_path.c_str());
break;
}
}
}
return strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0;
}

static void hook_fsGetFilesWithExtension_packages(PVOID resourceDir, const char *subDirectory, wchar_t *extension, eastl::vector<eastl::string> *out)
Expand All @@ -433,6 +431,7 @@ static void hook_fsGetFilesWithExtension_packages(PVOID resourceDir, const char

bool has_pkg_manifest = false;
bool has_pkg = false;
bool has_gr2_lz4 = false;
for (const auto &xd : *out)
{
if (ends_with(xd.c_str(), ".pkg_manifest"))
Expand All @@ -444,6 +443,11 @@ static void hook_fsGetFilesWithExtension_packages(PVOID resourceDir, const char
{
has_pkg = true;
}

if (ends_with(xd.c_str(), ".gr2.lz4"))
{
has_gr2_lz4 = true;
}
}

bool is_pkg_manifest_only = has_pkg_manifest && !has_pkg;
Expand All @@ -464,6 +468,13 @@ static void hook_fsGetFilesWithExtension_packages(PVOID resourceDir, const char
out->push_back(filename.c_str());
}
}
else if (has_gr2_lz4)
{
for (const auto &[filename, full_file_path] : additional_granny_files)
{
out->push_back(filename.c_str());
}
}
}

extern "C"
Expand Down Expand Up @@ -614,7 +625,7 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID)
static auto ptr_func = ptr.get_call();

static auto hook_ = hooking::detour_hook_helper::add<hook_fsGetFilesWithExtension_packages>(
"fsGetFilesWithExtension for packages",
"fsGetFilesWithExtension for packages and models",
ptr_func);
}
}
Expand All @@ -626,7 +637,7 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID)
static auto fsAppendPathComponent = fsAppendPathComponent_ptr.offset(-0x97).as_func<void(const char *, const char *, char *)>();

static auto hook_once = big::hooking::detour_hook_helper::add<hook_fsAppendPathComponent_packages>(
"hook_fsAppendPathComponent for packages",
"hook_fsAppendPathComponent for packages and models",
fsAppendPathComponent);
}
}
Expand Down Expand Up @@ -677,6 +688,13 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID)

LOG(INFO) << "Adding to package files: " << (char *)entry.path().u8string().c_str();
}
else if (ends_with((char *)entry.path().u8string().c_str(), ".gr2.lz4"))
{
additional_granny_files.emplace((char *)entry.path().filename().u8string().c_str(),
(char *)entry.path().u8string().c_str());

LOG(INFO) << "Adding to granny files: " << (char *)entry.path().u8string().c_str();
}
}

//static auto ptr_for_cave_test =
Expand Down

0 comments on commit 7041566

Please sign in to comment.