Last active
December 14, 2025 01:51
-
-
Save ThomasJClark/66372e69f69507bcdac3f37a1b78704a to your computer and use it in GitHub Desktop.
ELDEN RING Nightreign HKS hot reloader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| cmake_minimum_required(VERSION 3.28.1) | |
| set(CMAKE_GENERATOR_PLATFORM x64) | |
| set(CMAKE_CXX_STANDARD 23) | |
| set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) | |
| add_definitions(-D_ITERATOR_DEBUG_LEVEL=0) | |
| project(nrhkshotloader VERSION "1.0.0" LANGUAGES CXX) | |
| include(FetchContent) | |
| set(ER_NIGHTREIGN ON) | |
| FetchContent_Declare(elden-x | |
| GIT_REPOSITORY https://github.com/ThomasJClark/elden-x.git | |
| GIT_TAG c1e1e84fc6b4d8601bbd5d6ed0e7cb8a092e9ce9) | |
| FetchContent_MakeAvailable(elden-x) | |
| target_compile_definitions(elden-x PUBLIC -DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_OFF) | |
| add_library(${PROJECT_NAME} SHARED nrhkshotloader.cpp) | |
| set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME ${PROJECT_NAME}) | |
| target_link_libraries(${PROJECT_NAME} PRIVATE elden-x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ============================================================================== | |
| Copyright (c) 2025 Tom Clark | |
| ============================================================================== | |
| Permission is hereby granted, free of charge, to any person obtaining a copy | |
| of this software and associated documentation files (the "Software"), to deal | |
| in the Software without restriction, including without limitation the rights | |
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| copies of the Software, and to permit persons to whom the Software is | |
| furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in all | |
| copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| SOFTWARE. | |
| ============================================================================== | |
| Pattern16 | |
| Copyright (c) Dasaav | |
| ============================================================================== | |
| Permission is hereby granted, free of charge, to any person obtaining a copy | |
| of this software and associated documentation files (the "Software"), to deal | |
| in the Software without restriction, including without limitation the rights | |
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| copies of the Software, and to permit persons to whom the Software is | |
| furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in all | |
| copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| SOFTWARE. | |
| ============================================================================== | |
| MinHook - The Minimalistic API Hooking Library for x64/x86 | |
| Copyright (C) 2009-2017 Tsuda Kageyu. | |
| All rights reserved. | |
| ============================================================================== | |
| Redistribution and use in source and binary forms, with or without | |
| modification, are permitted provided that the following conditions | |
| are met: | |
| 1. Redistributions of source code must retain the above copyright | |
| notice, this list of conditions and the following disclaimer. | |
| 2. Redistributions in binary form must reproduce the above copyright | |
| notice, this list of conditions and the following disclaimer in the | |
| documentation and/or other materials provided with the distribution. | |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
| TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |
| PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER | |
| OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| ============================================================================== | |
| Hacker Disassembler Engine 64 C | |
| Copyright (c) 2008-2009, Vyacheslav Patkov. | |
| All rights reserved. | |
| ============================================================================== | |
| Redistribution and use in source and binary forms, with or without | |
| modification, are permitted provided that the following conditions | |
| are met: | |
| 1. Redistributions of source code must retain the above copyright | |
| notice, this list of conditions and the following disclaimer. | |
| 2. Redistributions in binary form must reproduce the above copyright | |
| notice, this list of conditions and the following disclaimer in the | |
| documentation and/or other materials provided with the distribution. | |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
| TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |
| PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR | |
| CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <elden-x/chr/world_chr_man.hpp> | |
| #include <elden-x/singletons.hpp> | |
| #include <elden-x/task.hpp> | |
| #include <elden-x/utils/modutils.hpp> | |
| #define WIN32_LEAN_AND_MEAN | |
| #include <windows.h> | |
| #include <filesystem> | |
| #include <memory> | |
| #include <print> | |
| #include <string> | |
| #include <thread> | |
| #include <vector> | |
| using namespace std; | |
| namespace fs = filesystem; | |
| /** | |
| * Get a character ID (e.g. "c0000") from a file path (e.g. "c0000.hks", "c0000.anibnd.dcx") | |
| */ | |
| static optional<wstring_view> get_chr_name(const wstring_view filename) { | |
| if (filename.size() >= 5 && filename[0] == L'c' && | |
| ranges::all_of(&filename[1], &filename[4], iswdigit)) { | |
| return filename.substr(0, 5); | |
| } | |
| return {}; | |
| } | |
| /** | |
| * Task that checks for updated files and adds the associated chrs to the debug reload list | |
| */ | |
| class hotloader : public er::CS::CSEzTask { | |
| struct dir_watch_context { | |
| fs::path path; | |
| HANDLE dir_handle; | |
| HANDLE event_handle; | |
| OVERLAPPED overlapped; | |
| uint8_t buffer[1024]; | |
| }; | |
| vector<unique_ptr<dir_watch_context>> dir_watch_contexts; | |
| public: | |
| /** | |
| * Start watching the given folder | |
| */ | |
| void watch_dir(const fs::path &path) { | |
| if (ranges::any_of(dir_watch_contexts, | |
| [&](auto &context) { return context->path == path; })) { | |
| return; | |
| } | |
| auto context = make_unique<dir_watch_context>(); | |
| context->dir_handle = | |
| CreateFileW(path.c_str(), FILE_LIST_DIRECTORY, | |
| FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, | |
| OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, nullptr); | |
| context->event_handle = CreateEvent(nullptr, true, false, nullptr); | |
| context->overlapped.hEvent = context->event_handle; | |
| ReadDirectoryChangesW(context->dir_handle, context->buffer, sizeof(context->buffer), true, | |
| FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE, nullptr, | |
| &context->overlapped, nullptr); | |
| context->path = path; | |
| dir_watch_contexts.push_back(move(context)); | |
| // println("Watching {}", path.string()); | |
| } | |
| void execute(er::FD4::task_data *data, | |
| er::FD4::task_group group, | |
| er::FD4::task_runner runner) override { | |
| auto world_chr_man = er::CS::WorldChrMan::instance(); | |
| if (!world_chr_man) { | |
| return; | |
| } | |
| for (auto &context : dir_watch_contexts) { | |
| if (WaitForSingleObject(context->event_handle, 0) == WAIT_OBJECT_0) { | |
| unsigned long bytes_returned = 0; | |
| GetOverlappedResult(context->dir_handle, &context->overlapped, &bytes_returned, | |
| false); | |
| auto fni_address = reinterpret_cast<uintptr_t>(context->buffer); | |
| do { | |
| auto fni = reinterpret_cast<FILE_NOTIFY_INFORMATION *>(fni_address); | |
| auto filename = | |
| wstring_view{fni->FileName, fni->FileNameLength / sizeof(fni->FileName[0])}; | |
| if (auto chr_name = get_chr_name(filename)) { | |
| world_chr_man->debug_reload.reload_list.emplace_back(*chr_name); | |
| world_chr_man->debug_reload.unk20 = 10.f; | |
| // println("Reloading {}", string{chr_name->begin(), chr_name->end()}); | |
| } | |
| if (fni->NextEntryOffset == 0) { | |
| break; | |
| } | |
| fni_address += fni->NextEntryOffset; | |
| } while (true); | |
| CloseHandle(context->event_handle); | |
| context->event_handle = CreateEventW(nullptr, true, false, nullptr); | |
| ZeroMemory(&context->overlapped, sizeof(context->overlapped)); | |
| context->overlapped.hEvent = context->event_handle; | |
| ReadDirectoryChangesW(context->dir_handle, context->buffer, sizeof(context->buffer), | |
| true, | |
| FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE, | |
| nullptr, &context->overlapped, nullptr); | |
| ZeroMemory(context->buffer, sizeof(context->buffer)); | |
| } | |
| } | |
| } | |
| }; | |
| static auto mod_task = hotloader{}; | |
| /** | |
| * Hook CreateFileW and keep track of all folders that chr-specific files were loaded from, so we | |
| * can watch for updates | |
| */ | |
| static void *(*create_file_w_trampoline)( | |
| const wchar_t *, uint32_t, uint32_t, LPSECURITY_ATTRIBUTES, uint32_t, uint32_t, void *); | |
| static void *create_file_w_hook(const wchar_t *file_name_c_str, | |
| uint32_t desired_access, | |
| uint32_t share_mode, | |
| LPSECURITY_ATTRIBUTES security_attributes, | |
| uint32_t creation_disposition, | |
| uint32_t flags_and_attributes, | |
| void *template_file) { | |
| auto handle = | |
| create_file_w_trampoline(file_name_c_str, desired_access, share_mode, security_attributes, | |
| creation_disposition, flags_and_attributes, template_file); | |
| if (handle != INVALID_HANDLE_VALUE) { | |
| auto path = fs::path{file_name_c_str}; | |
| if (get_chr_name(path.filename().wstring())) { | |
| mod_task.watch_dir(path.parent_path()); | |
| } | |
| } | |
| return handle; | |
| } | |
| bool WINAPI DllMain(HINSTANCE hinst, unsigned int reason, void *reserved) { | |
| if (reason == DLL_PROCESS_ATTACH) { | |
| DisableThreadLibraryCalls(hinst); | |
| // AllocConsole(); | |
| // freopen("CONOUT$", "w", stdout); | |
| // freopen("CONOUT$", "w", stderr); | |
| static auto mod_thread = thread{[]() { | |
| modutils::initialize(); | |
| er::FD4::find_singletons(); | |
| modutils::hook({.address = CreateFileW}, create_file_w_hook, create_file_w_trampoline); | |
| modutils::enable_hooks(); | |
| er::CS::CSTask::instance()->register_task(er::FD4::task_group::FrameBegin, mod_task); | |
| }}; | |
| } | |
| return true; | |
| } |
Author
What's this line for?
world_chr_man->debug_reload.unk20 = 10.f;
Author
What's this line for?
world_chr_man->debug_reload.unk20 = 10.f;
No idea! My understanding of this data structure is based on what DSAnimStudio does to reload entities. This includes a line of assembly which assigns the float value 10.0 to a field in WorldChrMan at offset 0x1E670, immediately after the reload_list.
mov [rcx+0001E670],41200000This might be some sort of timer for something. I think this structure was originally documented in some debug menu cheat table by horkrux for one of the older games.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on a similar reload entity feature in DSAnimStudio for previous Fromsoft games, which I think may have been based on debug menu stuff by horkrux