Skip to content

Commit

Permalink
Add LooseFileLoader mod
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Apr 2, 2024
1 parent ffccfa7 commit 2f4d786
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 0 deletions.
24 changes: 24 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2211,6 +2211,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.cpp"
"src/mods/Hooks.cpp"
"src/mods/IntegrityCheckBypass.cpp"
"src/mods/LooseFileLoader.cpp"
"src/mods/ManualFlashlight.cpp"
"src/mods/PluginLoader.cpp"
"src/mods/REFrameworkConfig.cpp"
Expand Down Expand Up @@ -2260,6 +2261,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.hpp"
"src/mods/Hooks.hpp"
"src/mods/IntegrityCheckBypass.hpp"
"src/mods/LooseFileLoader.hpp"
"src/mods/ManualFlashlight.hpp"
"src/mods/PluginLoader.hpp"
"src/mods/REFrameworkConfig.hpp"
Expand Down Expand Up @@ -2414,6 +2416,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.cpp"
"src/mods/Hooks.cpp"
"src/mods/IntegrityCheckBypass.cpp"
"src/mods/LooseFileLoader.cpp"
"src/mods/ManualFlashlight.cpp"
"src/mods/PluginLoader.cpp"
"src/mods/REFrameworkConfig.cpp"
Expand Down Expand Up @@ -2463,6 +2466,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.hpp"
"src/mods/Hooks.hpp"
"src/mods/IntegrityCheckBypass.hpp"
"src/mods/LooseFileLoader.hpp"
"src/mods/ManualFlashlight.hpp"
"src/mods/PluginLoader.hpp"
"src/mods/REFrameworkConfig.hpp"
Expand Down Expand Up @@ -4262,6 +4266,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.cpp"
"src/mods/Hooks.cpp"
"src/mods/IntegrityCheckBypass.cpp"
"src/mods/LooseFileLoader.cpp"
"src/mods/ManualFlashlight.cpp"
"src/mods/PluginLoader.cpp"
"src/mods/REFrameworkConfig.cpp"
Expand Down Expand Up @@ -4311,6 +4316,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.hpp"
"src/mods/Hooks.hpp"
"src/mods/IntegrityCheckBypass.hpp"
"src/mods/LooseFileLoader.hpp"
"src/mods/ManualFlashlight.hpp"
"src/mods/PluginLoader.hpp"
"src/mods/REFrameworkConfig.hpp"
Expand Down Expand Up @@ -4465,6 +4471,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.cpp"
"src/mods/Hooks.cpp"
"src/mods/IntegrityCheckBypass.cpp"
"src/mods/LooseFileLoader.cpp"
"src/mods/ManualFlashlight.cpp"
"src/mods/PluginLoader.cpp"
"src/mods/REFrameworkConfig.cpp"
Expand Down Expand Up @@ -4514,6 +4521,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.hpp"
"src/mods/Hooks.hpp"
"src/mods/IntegrityCheckBypass.hpp"
"src/mods/LooseFileLoader.hpp"
"src/mods/ManualFlashlight.hpp"
"src/mods/PluginLoader.hpp"
"src/mods/REFrameworkConfig.hpp"
Expand Down Expand Up @@ -4668,6 +4676,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.cpp"
"src/mods/Hooks.cpp"
"src/mods/IntegrityCheckBypass.cpp"
"src/mods/LooseFileLoader.cpp"
"src/mods/ManualFlashlight.cpp"
"src/mods/PluginLoader.cpp"
"src/mods/REFrameworkConfig.cpp"
Expand Down Expand Up @@ -4717,6 +4726,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.hpp"
"src/mods/Hooks.hpp"
"src/mods/IntegrityCheckBypass.hpp"
"src/mods/LooseFileLoader.hpp"
"src/mods/ManualFlashlight.hpp"
"src/mods/PluginLoader.hpp"
"src/mods/REFrameworkConfig.hpp"
Expand Down Expand Up @@ -7340,6 +7350,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.cpp"
"src/mods/Hooks.cpp"
"src/mods/IntegrityCheckBypass.cpp"
"src/mods/LooseFileLoader.cpp"
"src/mods/ManualFlashlight.cpp"
"src/mods/PluginLoader.cpp"
"src/mods/REFrameworkConfig.cpp"
Expand Down Expand Up @@ -7389,6 +7400,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.hpp"
"src/mods/Hooks.hpp"
"src/mods/IntegrityCheckBypass.hpp"
"src/mods/LooseFileLoader.hpp"
"src/mods/ManualFlashlight.hpp"
"src/mods/PluginLoader.hpp"
"src/mods/REFrameworkConfig.hpp"
Expand Down Expand Up @@ -7543,6 +7555,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.cpp"
"src/mods/Hooks.cpp"
"src/mods/IntegrityCheckBypass.cpp"
"src/mods/LooseFileLoader.cpp"
"src/mods/ManualFlashlight.cpp"
"src/mods/PluginLoader.cpp"
"src/mods/REFrameworkConfig.cpp"
Expand Down Expand Up @@ -7592,6 +7605,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.hpp"
"src/mods/Hooks.hpp"
"src/mods/IntegrityCheckBypass.hpp"
"src/mods/LooseFileLoader.hpp"
"src/mods/ManualFlashlight.hpp"
"src/mods/PluginLoader.hpp"
"src/mods/REFrameworkConfig.hpp"
Expand Down Expand Up @@ -8570,6 +8584,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.cpp"
"src/mods/Hooks.cpp"
"src/mods/IntegrityCheckBypass.cpp"
"src/mods/LooseFileLoader.cpp"
"src/mods/ManualFlashlight.cpp"
"src/mods/PluginLoader.cpp"
"src/mods/REFrameworkConfig.cpp"
Expand Down Expand Up @@ -8619,6 +8634,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.hpp"
"src/mods/Hooks.hpp"
"src/mods/IntegrityCheckBypass.hpp"
"src/mods/LooseFileLoader.hpp"
"src/mods/ManualFlashlight.hpp"
"src/mods/PluginLoader.hpp"
"src/mods/REFrameworkConfig.hpp"
Expand Down Expand Up @@ -9595,6 +9611,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.cpp"
"src/mods/Hooks.cpp"
"src/mods/IntegrityCheckBypass.cpp"
"src/mods/LooseFileLoader.cpp"
"src/mods/ManualFlashlight.cpp"
"src/mods/PluginLoader.cpp"
"src/mods/REFrameworkConfig.cpp"
Expand Down Expand Up @@ -9644,6 +9661,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.hpp"
"src/mods/Hooks.hpp"
"src/mods/IntegrityCheckBypass.hpp"
"src/mods/LooseFileLoader.hpp"
"src/mods/ManualFlashlight.hpp"
"src/mods/PluginLoader.hpp"
"src/mods/REFrameworkConfig.hpp"
Expand Down Expand Up @@ -10622,6 +10640,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.cpp"
"src/mods/Hooks.cpp"
"src/mods/IntegrityCheckBypass.cpp"
"src/mods/LooseFileLoader.cpp"
"src/mods/ManualFlashlight.cpp"
"src/mods/PluginLoader.cpp"
"src/mods/REFrameworkConfig.cpp"
Expand Down Expand Up @@ -10671,6 +10690,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.hpp"
"src/mods/Hooks.hpp"
"src/mods/IntegrityCheckBypass.hpp"
"src/mods/LooseFileLoader.hpp"
"src/mods/ManualFlashlight.hpp"
"src/mods/PluginLoader.hpp"
"src/mods/REFrameworkConfig.hpp"
Expand Down Expand Up @@ -11649,6 +11669,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.cpp"
"src/mods/Hooks.cpp"
"src/mods/IntegrityCheckBypass.cpp"
"src/mods/LooseFileLoader.cpp"
"src/mods/ManualFlashlight.cpp"
"src/mods/PluginLoader.cpp"
"src/mods/REFrameworkConfig.cpp"
Expand Down Expand Up @@ -11698,6 +11719,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.hpp"
"src/mods/Hooks.hpp"
"src/mods/IntegrityCheckBypass.hpp"
"src/mods/LooseFileLoader.hpp"
"src/mods/ManualFlashlight.hpp"
"src/mods/PluginLoader.hpp"
"src/mods/REFrameworkConfig.hpp"
Expand Down Expand Up @@ -12676,6 +12698,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.cpp"
"src/mods/Hooks.cpp"
"src/mods/IntegrityCheckBypass.cpp"
"src/mods/LooseFileLoader.cpp"
"src/mods/ManualFlashlight.cpp"
"src/mods/PluginLoader.cpp"
"src/mods/REFrameworkConfig.cpp"
Expand Down Expand Up @@ -12725,6 +12748,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/mods/Graphics.hpp"
"src/mods/Hooks.hpp"
"src/mods/IntegrityCheckBypass.hpp"
"src/mods/LooseFileLoader.hpp"
"src/mods/ManualFlashlight.hpp"
"src/mods/PluginLoader.hpp"
"src/mods/REFrameworkConfig.hpp"
Expand Down
2 changes: 2 additions & 0 deletions src/Mods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "mods/Scene.hpp"
#include "mods/ScriptRunner.hpp"
#include "mods/VR.hpp"
#include "mods/LooseFileLoader.hpp"
#include "mods/vr/games/RE8VR.hpp"

#include "Mods.hpp"
Expand All @@ -27,6 +28,7 @@ Mods::Mods() {

#ifndef BAREBONES
m_mods.emplace_back(Hooks::get());
m_mods.emplace_back(LooseFileLoader::get());

m_mods.emplace_back(VR::get());

Expand Down
163 changes: 163 additions & 0 deletions src/mods/LooseFileLoader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#include <sdk/RETypeDB.hpp>
#include <utility/Scan.hpp>
#include <utility/Module.hpp>

#include "LooseFileLoader.hpp"

LooseFileLoader* g_loose_file_loader{nullptr};

std::shared_ptr<LooseFileLoader>& LooseFileLoader::get() {
static auto instance = std::shared_ptr<LooseFileLoader>(new LooseFileLoader());
return instance;
}

std::optional<std::string> LooseFileLoader::on_initialize() {
g_loose_file_loader = this;
return Mod::on_initialize();
}

void LooseFileLoader::on_frame() {
if (!m_attempted_hook && m_enabled->value()) {
hook();
}
}

void LooseFileLoader::on_config_load(const utility::Config& cfg) {
for (IModValue& option : m_options) {
option.config_load(cfg);
}

/*if (!m_attempted_hook && m_enabled->value()) {
hook();
}*/
}

void LooseFileLoader::on_config_save(utility::Config& cfg) {
for (IModValue& option : m_options) {
option.config_save(cfg);
}
}

void LooseFileLoader::on_draw_ui() {
if (!ImGui::CollapsingHeader(get_name().data())) {
return;
}

if (m_attempted_hook && !m_hook_success) {
ImGui::TextWrapped("Failed to hook successfully. This mod will not work.");
return;
}

if (m_enabled->draw("Enable Loose File Loader")) {
}

if (m_hook_success) {
ImGui::TextWrapped("Files encountered: %d", m_files_encountered);
ImGui::TextWrapped("Loose files loaded: %d", m_loose_files_loaded);
}
}

void LooseFileLoader::hook() {
if (m_attempted_hook) {
return;
}

spdlog::info("[LooseFileLoader] Attempting to find path_to_hash");

m_attempted_hook = true;

const auto tdb = sdk::RETypeDB::get();

if (tdb == nullptr) {
spdlog::error("[LooseFileLoader] Failed to get type database");
return;
}

const auto via_io_file = tdb->find_type("via.io.file");

if (via_io_file == nullptr) {
spdlog::error("[LooseFileLoader] Failed to find via.io.file");
return;
}

// "exist" and "exists" are 2 different functions. one searches in pak and other doesnt.
const auto exists_fn = via_io_file->get_method("exists(System.String)");

if (exists_fn == nullptr) {
spdlog::error("[LooseFileLoader] Failed to find via.io.file.exists(System.String)");
return;
}

const auto exists_ptr = exists_fn->get_function();

if (exists_ptr == nullptr) {
spdlog::error("[LooseFileLoader] Failed to get function pointer for via.io.file.exists(System.String)");
return;
}

std::optional<uintptr_t> candidate{};
const auto game_module = utility::get_executable();
const auto game_module_size = utility::get_module_size(game_module).value_or(0);
const auto game_module_end = (uintptr_t)game_module + game_module_size;

utility::exhaustive_decode((uint8_t*)exists_ptr, 500, [&](utility::ExhaustionContext& ctx) -> utility::ExhaustionResult {
if (candidate) {
return utility::ExhaustionResult::BREAK;
}

// We do not care about the address if it is not in the game module, e.g. inside of kernel32.dll
if (ctx.addr < (uintptr_t)game_module || ctx.addr > game_module_end) {
return utility::ExhaustionResult::BREAK;
}

// This means we have just entered call or something
if (ctx.branch_start == ctx.addr) {
if (auto bs = utility::find_string_reference_in_path(ctx.branch_start, L"\\", false); bs.has_value()) {
spdlog::info("[LooseFileLoader] Found a backslash reference at {:x}", bs->addr);

// Now check if murmur hash constant is in the path
if (utility::find_pattern_in_path((uint8_t*)ctx.branch_start, 500, true, "? ? 6B CA EB 85")) {
candidate = ctx.branch_start;
spdlog::info("[LooseFileLoader] Found path_to_hash candidate at {:x}", *candidate);
return utility::ExhaustionResult::BREAK;
}
}
}

return utility::ExhaustionResult::CONTINUE;
});

if (!candidate) {
spdlog::error("[LooseFileLoader] Failed to find path_to_hash candidate, cannot continue");
return;
}

m_path_to_hash_hook = std::make_unique<FunctionHook>(candidate.value(), (uintptr_t)&path_to_hash_hook);

if (!m_path_to_hash_hook->create()) {
spdlog::error("[LooseFileLoader] Failed to hook path_to_hash");
return;
}

m_hook_success = true;
}

uint32_t LooseFileLoader::path_to_hash_hook(const wchar_t* path) {
++g_loose_file_loader->m_files_encountered;
const auto enabled = g_loose_file_loader->m_enabled->value();

if (enabled && path != nullptr && path[0] != L'\0') {
//spdlog::info("[LooseFileLoader] path_to_hash_hook called with path: {}", utility::narrow(path));

if (std::filesystem::exists(path)) {
++g_loose_file_loader->m_loose_files_loaded;
return 4294967296;
}
}


const auto og = g_loose_file_loader->m_path_to_hash_hook->get_original<decltype(path_to_hash_hook)>();
const auto result = og(path);

return result;
}
Loading

0 comments on commit 2f4d786

Please sign in to comment.