From 5a5ffee8193f46e3a0a6a99c53555559eff4e24d Mon Sep 17 00:00:00 2001 From: Kevin Trocolli Date: Fri, 18 Nov 2022 16:37:48 -0500 Subject: [PATCH] mercury: fix coin counter (thanks Raki!) --- hooklib/reg.c | 94 +++++++++++++++++++++++++++++++++++++++++++ mercuryhook/io4.c | 14 ++++++- mercuryio/mercuryio.h | 1 + 3 files changed, 108 insertions(+), 1 deletion(-) diff --git a/hooklib/reg.c b/hooklib/reg.c index 6028e8e..cc3c584 100644 --- a/hooklib/reg.c +++ b/hooklib/reg.c @@ -89,6 +89,16 @@ static LSTATUS WINAPI hook_RegSetValueExW( const void *bytes, uint32_t nbytes); +static LSTATUS WINAPI hook_RegGetValueW( + HKEY hkey, + LPCWSTR lpSubKey, + LPCWSTR lpValue, + uint32_t flags, + uint32_t *type, + void *pData, + uint32_t *numData +); + /* Link pointers */ static LSTATUS (WINAPI *next_RegOpenKeyExW)( @@ -135,6 +145,16 @@ static LSTATUS (WINAPI *next_RegSetValueExW)( const void *bytes, uint32_t nbytes); +static LSTATUS (WINAPI *next_RegGetValueW)( + HKEY hkey, + LPCWSTR lpSubKey, + LPCWSTR lpValue, + uint32_t flags, + uint32_t *type, + void *pData, + uint32_t *numData +); + static const struct hook_symbol reg_hook_syms[] = { { .name = "RegOpenKeyExW", @@ -160,6 +180,10 @@ static const struct hook_symbol reg_hook_syms[] = { .name = "RegSetValueExW", .patch = hook_RegSetValueExW, .link = (void **) &next_RegSetValueExW, + }, { + .name = "RegGetValueW", + .patch = hook_RegGetValueW, + .link = (void **) &next_RegGetValueW, } }; @@ -184,6 +208,12 @@ HRESULT reg_hook_push_key( reg_hook_init(); + /*dprintf("Pushing reg key %ls:\n", name); + + for (int i = 0; i < nvals; i++) { + dprintf("\t%ls\n", vals[i].name); + } */ + EnterCriticalSection(®_hook_lock); new_mem = realloc( @@ -222,6 +252,7 @@ static void reg_hook_init(void) reg_hook_initted = true; InitializeCriticalSection(®_hook_lock); + dprintf("Reg hook init\n"); hook_table_apply( NULL, @@ -727,6 +758,69 @@ static LSTATUS WINAPI hook_RegSetValueExW( return err; } +static LSTATUS WINAPI hook_RegGetValueW( + HKEY handle, + LPCWSTR subkey, + LPCWSTR name, + uint32_t flags, + uint32_t *type, + void *pData, + uint32_t *numData) +{ + struct reg_hook_key *key; + HKEY tmp = NULL; + const struct reg_hook_val *val; + LSTATUS err; + + EnterCriticalSection(®_hook_lock); + //dprintf("Registry: RegGetValueW lookup for %ls\\%ls\n", subkey, name); + + if (subkey == NULL) { + key = reg_hook_match_key_locked(handle); + } else { + err = hook_RegOpenKeyExW(handle, subkey, flags, 1, &tmp); + key = reg_hook_match_key_locked(tmp); + } + + //dprintf("Registry: RegGetValueW key is %ls", key->name); + + if (key == NULL) { + LeaveCriticalSection(®_hook_lock); + dprintf("Registry: RegGetValueW Failed to find %ls\\%ls, passing on\n", subkey, name); + + return next_RegGetValueW( + handle, + subkey, + name, + flags, + type, + pData, + numData); + } + + val = reg_hook_match_val_locked(key, name); + + if (val != NULL) { + //dprintf("Registry: RegGetValueW found %ls\\%ls!\n", subkey, name); + + if (val->read != NULL) { + val->read(pData, numData); + + if (tmp != NULL) { + hook_RegCloseKey(tmp); + } + + LeaveCriticalSection(®_hook_lock); + err = ERROR_SUCCESS; + return err; + } + } + + LeaveCriticalSection(®_hook_lock); + err = ERROR_NOT_FOUND; + return err; +} + HRESULT reg_hook_read_bin( void *bytes, uint32_t *nbytes, diff --git a/mercuryhook/io4.c b/mercuryhook/io4.c index 092a037..df22656 100644 --- a/mercuryhook/io4.c +++ b/mercuryhook/io4.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "board/io4.h" @@ -10,6 +11,9 @@ #include "util/dprintf.h" +bool mercury_io_coin = false; +uint16_t mercury_io_coins = 0; + static HRESULT mercury_io4_poll(void *ctx, struct io4_state *state); static const struct io4_ops mercury_io4_ops = { @@ -64,8 +68,16 @@ static HRESULT mercury_io4_poll(void *ctx, struct io4_state *state) } if (opbtn & MERCURY_IO_OPBTN_COIN) { - state->chutes[0] += 1 << 8; // FIXME: Inserts 1 credit on press, then 15 on release... + if (!mercury_io_coin) { + mercury_io_coin = true; + mercury_io_coins++; + } } + else { + mercury_io_coin = false; + } + + state->chutes[0] = 128 + 256 * mercury_io_coins; if (gamebtn & MERCURY_IO_GAMEBTN_VOL_UP) { state->buttons[0] |= 1 << 1; diff --git a/mercuryio/mercuryio.h b/mercuryio/mercuryio.h index 1031698..4d029ca 100644 --- a/mercuryio/mercuryio.h +++ b/mercuryio/mercuryio.h @@ -3,6 +3,7 @@ #include #include +#include #include "mercuryhook/elisabeth.h"