Skip to content

Commit

Permalink
Merge branch 'praydog:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyhodge authored May 18, 2024
2 parents a08ca2e + 82cbd4b commit ae09a28
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 6 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ FetchContent_Declare(
)
FetchContent_MakeAvailable(directxtk12)

message(STATUS "Fetching safetyhook (4faf792c2d66cbf06f2c942561d053aafd79006b)...")
message(STATUS "Fetching safetyhook (b046e123dc69821f2c375161e0adef3c6d9c9db4)...")
FetchContent_Declare(
safetyhook
GIT_REPOSITORY
https://github.com/cursey/safetyhook
GIT_TAG
4faf792c2d66cbf06f2c942561d053aafd79006b
b046e123dc69821f2c375161e0adef3c6d9c9db4
)
FetchContent_MakeAvailable(safetyhook)

Expand Down
2 changes: 1 addition & 1 deletion cmake.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ condition = "build-framework-dependencies"

[fetch-content.safetyhook]
git = "https://github.com/cursey/safetyhook"
tag = "4faf792c2d66cbf06f2c942561d053aafd79006b"
tag = "b046e123dc69821f2c375161e0adef3c6d9c9db4"

[target.imgui]
type = "static"
Expand Down
4 changes: 3 additions & 1 deletion include/reframework/API.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#endif

#define REFRAMEWORK_PLUGIN_VERSION_MAJOR 1
#define REFRAMEWORK_PLUGIN_VERSION_MINOR 9
#define REFRAMEWORK_PLUGIN_VERSION_MINOR 10
#define REFRAMEWORK_PLUGIN_VERSION_PATCH 0

#define REFRAMEWORK_RENDERER_D3D11 0
Expand Down Expand Up @@ -254,6 +254,8 @@ typedef struct {

void* (*get_init_data)(REFrameworkFieldHandle);
void* (*get_data_raw)(REFrameworkFieldHandle, void* obj, bool is_value_type);

unsigned int (*get_index)(REFrameworkFieldHandle);
} REFrameworkTDBField;

typedef struct {
Expand Down
5 changes: 5 additions & 0 deletions include/reframework/API.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,11 @@ class API {
return fn(*this, obj, is_value_type);
}

uint32_t get_index() const {
static const auto fn = API::s_instance->sdk()->field->get_index;
return fn(*this);
}

template <typename T> T& get_data(void* object = nullptr, bool is_value_type = false) const { return *(T*)get_data_raw(object, is_value_type); }
};

Expand Down
20 changes: 19 additions & 1 deletion shared/sdk/REContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,25 @@ namespace sdk {
}
}
if (!method_inside_invoke_tbl) {
spdlog::info("[VM::update_pointers] Unable to find method inside invoke table.");
spdlog::info("[VM::update_pointers] Unable to find method inside invoke table. Trying fallback scan...");
const auto anchor = utility::scan(mod, "8D 56 FF 48 8B CF E8 ? ? ? ?");

if (!anchor) {
spdlog::info("[VM::update_pointers] Unable to find anchor for invoke table.");
return;
}

const auto lea_rdx = utility::scan_reverse(*anchor, 0x100, "48 8D 15 ? ? ? ?");

if (!lea_rdx) {
spdlog::info("[VM::update_pointers] Unable to find lea rdx for invoke table.");
return;
}

s_invoke_tbl = (sdk::InvokeMethod*)utility::resolve_displacement(*lea_rdx).value_or(0);

spdlog::info("[VM::update_pointers] s_invoke_tbl: {:x}", (uintptr_t)s_invoke_tbl);

return;
}

Expand Down
6 changes: 6 additions & 0 deletions shared/sdk/RETypeDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,12 @@ void* REField::get_data_raw(void* object, bool is_value_type) const {

return nullptr;
}

uint32_t sdk::REField::get_index() const {
auto tdb = RETypeDB::get();

return (uint32_t)(((uintptr_t)this - (uintptr_t)tdb->fields) / sizeof(sdk::REField));
}
} // namespace sdk

// methods
Expand Down
1 change: 1 addition & 0 deletions shared/sdk/RETypeDB.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,7 @@ struct REField : public sdk::REField_ {
bool is_literal() const;

void* get_data_raw(void* object = nullptr, bool is_value_type = false) const;
uint32_t get_index() const;

template <typename T> T& get_data(void* object = nullptr, bool is_value_type = false) const { return *(T*)get_data_raw(object); }
};
Expand Down
15 changes: 14 additions & 1 deletion src/mods/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ void Graphics::on_draw_ui() {
}

if (interception_node_open) {
if (ImGui::InputText(std::format("Replace Shader", i).c_str(), intercepted.replace_with_name.data(), intercepted.replace_with_name.size())) {
intercepted.replace_with_hash = sdk::murmur_hash::calc32_as_utf8(intercepted.replace_with_name.data());
}

for (auto& replacement : intercepted.replacement_shaders) {
i++;
ImGui::PushID(std::format("Shader {}", i).c_str());
Expand Down Expand Up @@ -1110,7 +1114,7 @@ sdk::renderer::PipelineState* Graphics::find_pipeline_state_hook(void* shader_re
auto& graphics = Graphics::get();

const auto og = graphics->m_find_pipeline_state_hook->get_original<decltype(find_pipeline_state_hook)>();
const auto result = og(shader_resource, murmur_hash, unk);
auto result = og(shader_resource, murmur_hash, unk);

if (!graphics->m_shader_playground->value()) {
return result;
Expand All @@ -1135,6 +1139,15 @@ sdk::renderer::PipelineState* Graphics::find_pipeline_state_hook(void* shader_re
if (intercepted_shader == nullptr) {
return result;
}

if (intercepted_shader->replace_with_hash != 0 || std::string_view{intercepted_shader->replace_with_name} == "None") {
const auto replacement = og(shader_resource, intercepted_shader->replace_with_hash, unk);

if (replacement != nullptr) {
result = replacement;
}
}

uint32_t i = 1;

for (auto& replacement_shader : intercepted_shader->replacement_shaders) {
Expand Down
8 changes: 8 additions & 0 deletions src/mods/Graphics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ class Graphics : public Mod {
}();

uint32_t hash{ 0 };

std::string replace_with_name = []() {
std::string result{};
result.resize(1024);
return result;
}();

uint32_t replace_with_hash{ 0 };
};

std::array<InterceptedShader, 8> m_intercepted_shaders{};
Expand Down
2 changes: 2 additions & 0 deletions src/mods/PluginLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ REFrameworkTDBField g_tdb_field_data {

[](REFrameworkFieldHandle field) { return REFIELD(field)->get_init_data(); },
[](REFrameworkFieldHandle field, void* obj, bool is_value_type) { return REFIELD(field)->get_data_raw(obj, is_value_type); },

[](REFrameworkFieldHandle field) { return REFIELD(field)->get_index(); },
};

REFrameworkTDBProperty g_tdb_property_data {
Expand Down
52 changes: 52 additions & 0 deletions src/mods/ScriptRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,58 @@ void ScriptRunner::hook_battle_rule() {
}

void ScriptRunner::on_frame() {
if (!m_scene_okay) try {
if (!m_checked_scene_once) {
m_checked_scene_once = true;
m_scene_check_time = std::chrono::system_clock::now();
}

// Just bail out of this if 5 seconds have passed and we still haven't found the scene or scene manager.
if (std::chrono::system_clock::now() - m_scene_check_time > std::chrono::seconds(5)) {
m_scene_okay = true;
spdlog::warn("[ScriptRunner] Scene or scene manager not found after 5 seconds. Loading scripts anyways...");
return;
}

const auto scene_manager_t = sdk::find_type_definition("via.SceneManager");
if (scene_manager_t == nullptr) {
return;
}

const auto get_CurrentScene = scene_manager_t->get_method("get_CurrentScene");

if (get_CurrentScene == nullptr) {
return;
}

const auto scene_manager = sdk::get_native_singleton("via.SceneManager");

if (scene_manager == nullptr) {
return;
}

const auto context = sdk::get_thread_context();

if (context == nullptr) {
return;
}

const auto scene = get_CurrentScene->call_safe<void*>(context, scene_manager);

if (scene == nullptr) {
return;
}

m_scene_okay = true;
spdlog::info("[ScriptRunner] Scene and scene manager found. Loading scripts...");
} catch (const std::exception& e) {
spdlog::error("[ScriptRunner] Error while checking for scene: {}", e.what());
return;
} catch (...) {
spdlog::error("[ScriptRunner] Unknown error while checking for scene.");
return;
}

std::scoped_lock _{m_access_mutex};

hook_battle_rule();
Expand Down
3 changes: 3 additions & 0 deletions src/mods/ScriptRunner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ class ScriptRunner : public Mod {
std::shared_mutex m_script_error_mutex{};
std::chrono::system_clock::time_point m_last_script_error_time{};

std::chrono::system_clock::time_point m_scene_check_time{};
bool m_checked_scene_once{false};
bool m_scene_okay{false};
bool m_console_spawned{false};
bool m_needs_first_reset{true};
bool m_last_online_match_state{false};
Expand Down

0 comments on commit ae09a28

Please sign in to comment.