Skip to content

Commit

Permalink
emergency backup commit cause everything breaks if you breathe on it now
Browse files Browse the repository at this point in the history
  • Loading branch information
muhopensores committed Aug 19, 2024
1 parent 41c34b6 commit f794174
Show file tree
Hide file tree
Showing 34 changed files with 2,712 additions and 1,260 deletions.
3 changes: 3 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ AlignConsecutiveAssignments: 'true'
AlignOperands: 'true'
BreakBeforeBraces: Attach
PointerAlignment: Left
IndentWidth: 4
AccessModifierOffset: -4
ColumnLimit: 0
...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ build86/
lsp/
compile_commands.json
.clangd/
.cache/
src/Config.hpp
22 changes: 3 additions & 19 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ include_directories(${CMAKE_SOURCE_DIR}/dependencies/glm)
include_directories(${CMAKE_SOURCE_DIR}/dependencies/rpclib/include)
include_directories(${CMAKE_SOURCE_DIR}/dependencies/SDL2/include)
include_directories(${CMAKE_SOURCE_DIR}/dependencies/DebugDraw)
include_directories(${CMAKE_SOURCE_DIR}/dependencies/meshoptimizer/src)
#temp
include_directories(${CMAKE_SOURCE_DIR}/dependencies/WickedEngine)

include_directories($ENV{DXSDK_DIR}/Include)
link_directories("$ENV{DXSDK_DIR}/Lib/x86")
Expand All @@ -33,25 +36,6 @@ endforeach(OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES)
# Disable exceptions
# string(REGEX REPLACE "/EHsc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")

find_package(Git)
if (Git_FOUND)
execute_process(COMMAND
"${GIT_EXECUTABLE}" log -1 --pretty=\"%h\"
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
OUTPUT_VARIABLE GIT_COMMIT_HASH
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)

execute_process(COMMAND
"${GIT_EXECUTABLE}" log -1 --date short --pretty=\"%ad\"
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
OUTPUT_VARIABLE GIT_COMMITTER_DATE
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_HASH ${GIT_COMMIT_HASH})
set(GIT_DATE ${GIT_COMMITTER_DATE})
else()
set(GIT_HASH "install_git_exe_bro")
set(GIT_DATE "really")
endif()

set(LOG_FILENAME "${CMAKE_PROJECT_NAME}_framework_log.txt")
set(LOG_ENTRY "${CMAKE_PROJECT_NAME} ModFramework entry")
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
wip
**Building
to build this you need to install legacy DirectX SDK headers from https://www.microsoft.com/en-US/download/details.aspx?id=6812
otherwise just run a coresponding batch file to generate solution and build it using cmake.
3 changes: 2 additions & 1 deletion dependencies/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ add_library(imgui STATIC

target_compile_definitions(imgui PUBLIC IMGUI_USER_CONFIG="../src/fw-imgui/imgui_imconfig.hpp")

add_subdirectory(DebugDraw)
add_subdirectory(DebugDraw)
add_subdirectory(meshoptimizer)
10 changes: 8 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
set(MODS_SRC
mods/SimpleMod.hpp
mods/RendererReplace.hpp
mods/RendererReplace.cpp
mods/CustomAlolcator.hpp
mods/CustomAlolcator.cpp
mods/EnemySpawnRate.hpp
Expand Down Expand Up @@ -43,7 +45,8 @@ set(MODS_SRC
mods/EnemyStepCooldown.cpp
mods/EnemyStates.hpp
mods/EnemyStates.cpp

mods/AreaJump.hpp
mods/AreaJump.cpp
)

set(HOOKS_SRC
Expand Down Expand Up @@ -102,6 +105,8 @@ set(UTILITY_SRC
utility/Thread.hpp
utility/Thread.cpp
utility/SDLGamepad.hpp
#utility/tgc.h
#utility/tgc.c
)

set(FRAMEWORK_SRC
Expand Down Expand Up @@ -137,7 +142,8 @@ add_library(${CMAKE_PROJECT_NAME} SHARED
)

#target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC imgui minhook glm_static shlwapi D3dx9 d3d9 SDL2main SDL2 rpc)
target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC imgui minhook glm_static shlwapi D3dx9 d3d9 SDL2main SDL2)
#target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC imgui minhook glm_static shlwapi D3dx9 d3d9 SDL2main SDL2 WickedEngine_Windows d3d12 dxgi)
target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC imgui minhook glm_static shlwapi D3dx9 d3d9 SDL2main SDL2 meshoptimizer)
target_compile_features(${CMAKE_PROJECT_NAME} PUBLIC cxx_std_17)

include_directories(${CMAKE_PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR})
Expand Down
207 changes: 159 additions & 48 deletions src/Main.cpp
Original file line number Diff line number Diff line change
@@ -1,73 +1,184 @@
#include <thread>
#include <chrono>
#include <windows.h>
#include <thread>
#include <Windows.h>

#include "ModFramework.hpp"
#include "mods/CustomAlolcator.hpp"
#include "utility/ExceptionHandler.hpp"
#include "mods/RendererReplace.hpp"

static HMODULE g_dinput;
static HMODULE g_styleswitcher;

//static Mod* g_renderer_replace{ nullptr };
#pragma comment(lib, "mincore")
#if 1
extern "C" {
// DirectInput8Create wrapper for dinput8.dll
__declspec(dllexport) HRESULT WINAPI direct_input8_create(HINSTANCE hinst, DWORD dw_version, const IID& riidltf, LPVOID* ppv_out, LPUNKNOWN punk_outer) {
// This needs to be done because when we include dinput.h in DInputHook,
// It is a redefinition, so we assign an export by not using the original name
#pragma comment(linker, "/EXPORT:DirectInput8Create=_direct_input8_create@20")
return ((decltype(direct_input8_create)*)GetProcAddress(g_dinput, "DirectInput8Create"))(hinst, dw_version, riidltf, ppv_out, punk_outer);
}
// DirectInput8Create wrapper for dinput8.dll
__declspec(dllexport) HRESULT WINAPI direct_input8_create(HINSTANCE hinst, DWORD dw_version, const IID& riidltf, LPVOID* ppv_out, LPUNKNOWN punk_outer) {
// This needs to be done because when we include dinput.h in DInputHook,
// It is a redefinition, so we assign an export by not using the original name
#pragma comment(linker, "/EXPORT:DirectInput8Create=_direct_input8_create@20")
return ((decltype(direct_input8_create)*)GetProcAddress(g_dinput, "DirectInput8Create"))(hinst, dw_version, riidltf, ppv_out, punk_outer);
}
}
#endif

void failed() {
MessageBox(0, "DMC3 ModFramework: Unable to load the original version.dll. Please report this to the developer.", "ModFramework", 0);
ExitProcess(0);
static constexpr uint64_t round_to_pow2_size(uint32_t minimum_size, uint32_t pow2_size) {
uint64_t result = (minimum_size + pow2_size - 1) & ~(pow2_size - 1);
return result;
}

void startup_thread() {
void failed() {
MessageBox(0, "DMC3 ModFramework: Unable to load the original version.dll. Please report this to the developer.", "ModFramework", 0);
ExitProcess(0);
}

reframework::setup_exception_handler();
void WINAPI startup_thread() {
#ifndef NDEBUG
AllocConsole();
HANDLE handleOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD consoleMode;
GetConsoleMode( handleOut , &consoleMode);
consoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
consoleMode |= DISABLE_NEWLINE_AUTO_RETURN;
SetConsoleMode( handleOut , consoleMode );

freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
AllocConsole();
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD console_mode;
GetConsoleMode(handle_out, &console_mode);
console_mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
console_mode |= DISABLE_NEWLINE_AUTO_RETURN;
SetConsoleMode(handle_out, console_mode);

FILE* f_inp;
FILE* f_out;

freopen_s(&f_inp, "CONIN$", "r", stdin);
freopen_s(&f_out, "CONOUT$", "w", stdout);
freopen_s(&f_out, "CONOUT$", "w", stderr);

#endif

#if 1
wchar_t buffer[MAX_PATH]{ 0 };
if (GetSystemDirectoryW(buffer, MAX_PATH) != 0) {
// Load the original dinput8.dll
if ((g_dinput = LoadLibraryW((std::wstring{ buffer } + L"\\dinput8.dll").c_str())) == NULL) {
failed();
}
g_framework = std::make_unique<ModFramework>();
}
else {
failed();
}
wchar_t buffer[MAX_PATH]{0};
if (GetSystemDirectoryW(buffer, MAX_PATH) != 0) {
// Load the original dinput8.dll
if ((g_dinput = LoadLibraryW((std::wstring{buffer} + L"\\dinput8.dll").c_str())) == NULL) {
failed();
}
//g_framework = std::make_unique<ModFramework>();
#ifdef NDEBUG
reframework::setup_exception_handler();
#endif
} else {
failed();
}
g_framework = std::make_unique<ModFramework>();
#else
g_framework = std::make_unique<ModFramework>();
#endif
}

HANDLE filemapping {};
PULONG_PTR page_array = NULL;
ULONG_PTR number_of_pages = NULL;

static BOOL EnableAWE() {
HANDLE hToken;
TOKEN_PRIVILEGES tp;

// Open the process token with TOKEN_ADJUST_PRIVILEGES and TOKEN_QUERY
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
return FALSE;
}

tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

// Lookup the LUID for SeLockMemoryPrivilege
if (!LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &(tp.Privileges[0].Luid))) {
CloseHandle(hToken);
return FALSE;
}

// Adjust the token privileges
if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL)) {
CloseHandle(hToken);
return FALSE;
}

CloseHandle(hToken);
return TRUE;
}

BOOL APIENTRY DllMain(HMODULE handle, DWORD reason, LPVOID reserved) {
if (reason == DLL_PROCESS_ATTACH) {
g_styleswitcher = LoadLibrary("StyleSwitcher.dll");
if (reason == DLL_PROCESS_ATTACH) {
#ifndef NDEBUG
MessageBox(NULL, "Debug attach opportunity", "DMC3", MB_ICONINFORMATION);
MessageBox(NULL, "Debug attach opportunity", "DMC3", MB_ICONINFORMATION);
#endif
CreateThread(nullptr, 0, (LPTHREAD_START_ROUTINE)startup_thread, nullptr, 0, nullptr);
}
if (reason == DLL_PROCESS_DETACH) {
FreeLibrary(g_styleswitcher);
}
return TRUE;
}

SYSTEM_INFO info;
MEMORY_BASIC_INFORMATION mbi{};
VirtualQuery((LPCVOID)0x00C36980, &mbi, sizeof(mbi));
GetSystemInfo(&info);
DWORD memory_size = 0x8200000;
#if 0
number_of_pages = memory_size / info.dwPageSize;
page_array = new ULONG_PTR[number_of_pages];
if(!EnableAWE()) {
fprintf(stderr, "Failed to EnableSEPrivelege\n");
DWORD err = GetLastError();
}
if (!AllocateUserPhysicalPages(GetCurrentProcess(), &number_of_pages, page_array)) {
fprintf(stderr, "Failed to AllocateUserPhysicalPage\n");
DWORD err = GetLastError();
}
auto lpMemReserved = VirtualAlloc((void*) 0x00c36980,
memory_size,
MEM_RESERVE | MEM_PHYSICAL,
PAGE_READWRITE );
if (!lpMemReserved) {
return TRUE;
}
PVOID virtual_address = (PVOID)0x00C36980;
if (!MapUserPhysicalPages(lpMemReserved, number_of_pages, page_array)) {
fprintf(stderr, "Failed to MapUserPhysicalPages(0x%X)\n", (DWORD)virtual_address);
DWORD err = GetLastError();
}

virtual_address = (PVOID)0x2537000;
if (!MapUserPhysicalPages(virtual_address, number_of_pages, page_array)) {
fprintf(stderr, "Failed to MapUserPhysicalPages(0x%X)\n", (DWORD)virtual_address);
DWORD err = GetLastError();
}
#endif


#if 0
uint64_t data_size = round_to_pow2_size(0x8200000, info.dwAllocationGranularity);
filemapping = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE,
(DWORD)(data_size >> 32), (DWORD)(data_size & 0xffffffff), 0);

if (filemapping != INVALID_HANDLE_VALUE) {
if(filemapping == nullptr) { goto continuers; }
uint64_t dmc3_offset_p2 = round_to_pow2_size(0x00C36980, info.dwAllocationGranularity);
uint32_t dmc3offset = (dmc3_offset_p2 & 0xffffffff);
if (!MapViewOfFile3(filemapping, 0, (void*)(dmc3offset),
0, round_to_pow2_size(0x1432AC0, info.dwAllocationGranularity), MEM_REPLACE_PLACEHOLDER, PAGE_READWRITE, 0, 0)) {
DWORD er = GetLastError();
printf("eror with MapViewOfFile3(filemapping, 0, (void*)0x00C36980, 0, 0x1432AC0, MEM_REPLACE_PLACEHOLDER, PAGE_READWRITE, 0, 0)\n");
}
#if 0
if (!MapViewOfFile3(filemapping, 0, (void*)0x2537000,
0, data_size, MEM_REPLACE_PLACEHOLDER, PAGE_READWRITE, 0, 0)) {
DWORD er = GetLastError();
printf("eror with MapViewOfFile3(filemapping, 0, (void*)0x2537000, 0, data_size, MEM_REPLACE_PLACEHOLDER, PAGE_READWRITE, 0, 0)\n");
}
#endif
}
#endif
g_styleswitcher = LoadLibraryA("StyleSwitcher.dll");

//g_renderer_replace = new RendererReplace();
//g_renderer_replace->on_initialize();
CreateThread(nullptr, 0, (LPTHREAD_START_ROUTINE)startup_thread, nullptr, 0, nullptr);
}
if (reason == DLL_PROCESS_DETACH) {

FreeLibrary(g_styleswitcher);
FreeLibrary(g_dinput);
}
return TRUE;
}
Loading

0 comments on commit f794174

Please sign in to comment.