wip hispeed_auto

This commit is contained in:
CrazyRedMachine 2023-08-22 00:46:48 +02:00
parent adc18fcc35
commit 9e5bf9cb7e
3 changed files with 169 additions and 0 deletions

View File

@ -43,6 +43,13 @@
<!-- Display offset adjust value on score result screen (requires hidden_is_offset, won't be sent over network) -->
<show_offset __type="bool">0</show_offset>
<!-- Hi-speed -->
<!-- Auto set hi-speed to match previously set BPM (0: off, 1: target lower bpm for soflan, 2: target higher bpm for soflan) -->
<hispeed_auto __type="u8">0</hispeed_auto>
<!-- Default target BPM, 0 to disable (requires hispeed_auto) -->
<!-- Note: target is still updated when manually changing hi-speed (except soflan and "?" songs) -->
<hispeed_default_bpm __type="u16">0</hispeed_default_bpm>
<!-- Result screen display patches -->
<!-- Display details on result screen by default (no need to press yellow button) -->
<show_details __type="bool">0</show_details>

View File

@ -43,6 +43,8 @@ struct popnhax_config {
uint8_t debounce;
bool enhanced_polling_stats;
int8_t enhanced_polling_priority;
uint8_t hispeed_auto;
uint16_t hispeed_default_bpm;
};
#endif

View File

@ -164,6 +164,10 @@ PSMAP_MEMBER_REQ(PSMAP_PROPERTY_TYPE_BOOL, struct popnhax_config, enhanced_polli
"/popnhax/enhanced_polling_stats")
PSMAP_MEMBER_REQ(PSMAP_PROPERTY_TYPE_S8, struct popnhax_config, enhanced_polling_priority,
"/popnhax/enhanced_polling_priority")
PSMAP_MEMBER_REQ(PSMAP_PROPERTY_TYPE_U8, struct popnhax_config, hispeed_auto,
"/popnhax/hispeed_auto")
PSMAP_MEMBER_REQ(PSMAP_PROPERTY_TYPE_U16, struct popnhax_config, hispeed_default_bpm,
"/popnhax/hispeed_default_bpm")
PSMAP_END
enum BufferIndexes {
@ -3974,6 +3978,156 @@ static bool patch_enhanced_polling_stats()
return true;
}
uint32_t g_hispeed_addr = 0;
uint16_t g_target_bpm = 0;
bool g_mystery_bpm = 0;
uint16_t g_low_bpm = 0;
uint16_t g_hi_bpm = 0;
uint32_t g_hispeed = 0;
bool g_set_hispeed_addr = false;
void (*real_set_hispeed)();
void hook_set_hispeed()
{
__asm("mov %0, eax\n":"=r"(g_hispeed_addr): :);
real_set_hispeed();
}
void (*real_read_hispeed)();
void hook_read_hispeed()
{
/*
on hook ici pour calculer [ round(target / (bpm/10)) = hispeed ]
on ecrit notre hispeed dans edi pour que ça s'affiche, et dans g_hispeed_addr pour que ça soit pris en compte
*/
__asm("mov %0, word ptr [ebp+0xA1A]\n":"=a"(g_low_bpm): :);
__asm("mov %0, word ptr [ebp+0xA1C]\n":"=a"(g_hi_bpm): :);
__asm("mov %0, byte ptr [ebp+0xA1E]\n":"=a"(g_mystery_bpm): :);
g_hispeed = g_target_bpm / g_hi_bpm * 10;
__asm("mov edi, dword ptr[%0]\n"::"m"(g_hispeed));
__asm("mov eax, dword ptr[%0]\n"::"m"(g_hispeed_addr));
__asm("mov dword ptr[eax], edi\n");
real_read_hispeed();
}
void (*real_increase_hispeed)();
void hook_increase_hispeed()
{
if ( g_mystery_bpm || g_low_bpm != g_hi_bpm )
__asm("jmp leave_increase_hispeed\n");
__asm("push eax\n");
__asm("push edx\n");
__asm("push ecx\n");
uint32_t *hispeed = 0;
__asm("mov %0, edi\n":"=a"(hispeed): :);
uint32_t newspeed = *hispeed;
newspeed++;
if (newspeed >0x64) newspeed = 0x0A;
g_target_bpm = g_hi_bpm*newspeed/10;
__asm("pop ecx\n");
__asm("pop edx\n");
__asm("pop eax\n");
__asm("leave_increase_hispeed:\n");
real_increase_hispeed();
}
void (*real_decrease_hispeed)();
void hook_decrease_hispeed()
{
if ( g_mystery_bpm || g_low_bpm != g_hi_bpm )
__asm("jmp leave_decrease_hispeed\n");
__asm("push eax\n");
__asm("push edx\n");
__asm("push ecx\n");
uint32_t *hispeed = 0;
__asm("mov %0, edi\n":"=a"(hispeed): :);
uint32_t newspeed = *hispeed;
newspeed--;
if (newspeed <0x0A) newspeed = 0x64;
g_target_bpm = g_hi_bpm*newspeed/10;
__asm("pop ecx\n");
__asm("pop edx\n");
__asm("pop eax\n");
__asm("leave_decrease_hispeed:\n");
real_decrease_hispeed();
}
bool patch_hispeed_auto(uint16_t default_bpm)
{
DWORD dllSize = 0;
char *data = getDllData(g_game_dll_fn, &dllSize);
g_target_bpm = default_bpm;
/* retrieve hi-speed address */
{
int64_t pattern_offset = search(data, dllSize, "\x66\x89\x0C\x07\x0F\xB6\x45\x04", 8, 0);
if (pattern_offset == -1) {
LOG("popnhax: auto hi-speed: cannot find hi-speed address\n");
return false;
}
uint64_t patch_addr = (int64_t)data + pattern_offset + 0x04;
MH_CreateHook((LPVOID)(patch_addr), (LPVOID)hook_set_hispeed,
(void **)&real_set_hispeed);
}
/* write new hispeed according to target bpm */
{
int64_t pattern_offset = search(data, dllSize, "\x98\x50\x66\x8B\x85\x1A\x0A\x00\x00\x8B\xCF", 11, 0);
if (pattern_offset == -1) {
LOG("popnhax: auto hi-speed: cannot find hi-speed apply address\n");
return false;
}
uint64_t patch_addr = (int64_t)data + pattern_offset - 0x07;
MH_CreateHook((LPVOID)(patch_addr), (LPVOID)hook_read_hispeed,
(void **)&real_read_hispeed);
}
/* update target bpm on hispeed increase */
{
int64_t pattern_offset = search(data, dllSize, "\x66\xFF\x07\x0F\xB7\x07\x66\x83\xF8\x64", 10, 0);
if (pattern_offset == -1) {
LOG("popnhax: auto hi-speed: cannot find hi-speed increase\n");
return false;
}
uint64_t patch_addr = (int64_t)data + pattern_offset;
MH_CreateHook((LPVOID)(patch_addr), (LPVOID)hook_increase_hispeed,
(void **)&real_increase_hispeed);
}
/* update target bpm on hispeed decrease */
{
int64_t pattern_offset = search(data, dllSize, "\x66\xFF\x0F\x0F\xB7\x07\x66\x83\xF8\x0A", 10, 0);
if (pattern_offset == -1) {
LOG("popnhax: auto hi-speed: cannot find hi-speed decrease\n");
return false;
}
uint64_t patch_addr = (int64_t)data + pattern_offset;
MH_CreateHook((LPVOID)(patch_addr), (LPVOID)hook_decrease_hispeed,
(void **)&real_decrease_hispeed);
}
LOG("popnhax: auto hi-speed enabled (%hu bpm)\n", g_target_bpm);
return true;
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
switch (ul_reason_for_call) {
@ -4255,6 +4409,12 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv
patch_enhanced_polling_stats();
}
}
if (config.hispeed_auto)
{
patch_hispeed_auto(config.hispeed_default_bpm);
}
#if DEBUG == 1
patch_get_time();
#endif