Make clock hook configurable

This commit is contained in:
Tau 2019-10-14 23:18:18 -04:00
parent 6bdd1f90bc
commit e918795445
13 changed files with 103 additions and 37 deletions

View File

@ -13,7 +13,6 @@
#include "hook/process.h" #include "hook/process.h"
#include "hooklib/clock.h"
#include "hooklib/gfx.h" #include "hooklib/gfx.h"
#include "hooklib/serial.h" #include "hooklib/serial.h"
#include "hooklib/spike.h" #include "hooklib/spike.h"
@ -48,8 +47,6 @@ static DWORD CALLBACK chuni_pre_startup(void)
/* Hook Win32 APIs */ /* Hook Win32 APIs */
clock_read_hook_init();
clock_write_hook_init();
gfx_hook_init(); gfx_hook_init();
serial_hook_init(); serial_hook_init();

View File

@ -13,7 +13,6 @@
#include "hook/process.h" #include "hook/process.h"
#include "hooklib/clock.h"
#include "hooklib/gfx.h" #include "hooklib/gfx.h"
#include "hooklib/serial.h" #include "hooklib/serial.h"
#include "hooklib/spike.h" #include "hooklib/spike.h"
@ -36,7 +35,6 @@ static DWORD CALLBACK diva_pre_startup(void)
/* Hook Win32 APIs */ /* Hook Win32 APIs */
clock_write_hook_init();
serial_hook_init(); serial_hook_init();
/* Initialize emulation hooks */ /* Initialize emulation hooks */

View File

@ -1,4 +0,0 @@
#pragma once
void clock_read_hook_init(void);
void clock_write_hook_init(void);

View File

@ -7,8 +7,6 @@ hooklib_lib = static_library(
capnhook.get_variable('hook_dep'), capnhook.get_variable('hook_dep'),
], ],
sources : [ sources : [
'clock.c',
'clock.h',
'dll.c', 'dll.c',
'dll.h', 'dll.h',
'dns.c', 'dns.c',

View File

@ -9,7 +9,6 @@
#include "hook/process.h" #include "hook/process.h"
#include "hooklib/clock.h"
#include "hooklib/gfx.h" #include "hooklib/gfx.h"
#include "hooklib/serial.h" #include "hooklib/serial.h"
#include "hooklib/spike.h" #include "hooklib/spike.h"
@ -35,7 +34,6 @@ static DWORD CALLBACK idz_pre_startup(void)
/* Hook Win32 APIs */ /* Hook Win32 APIs */
clock_write_hook_init();
serial_hook_init(); serial_hook_init();
/* Initialize emulation hooks */ /* Initialize emulation hooks */

View File

@ -5,9 +5,9 @@
#include "hook/process.h" #include "hook/process.h"
#include "hooklib/clock.h"
#include "hooklib/spike.h" #include "hooklib/spike.h"
#include "platform/clock.h"
#include "platform/config.h" #include "platform/config.h"
#include "platform/nusec.h" #include "platform/nusec.h"
@ -17,17 +17,17 @@ static process_entry_t app_startup;
static DWORD CALLBACK app_pre_startup(void) static DWORD CALLBACK app_pre_startup(void)
{ {
struct nusec_config nusec_cfg; struct clock_config clock_cfg;
struct ds_config ds_cfg; struct ds_config ds_cfg;
struct nusec_config nusec_cfg;
dprintf("--- Begin %s ---\n", __func__); dprintf("--- Begin %s ---\n", __func__);
nusec_config_load(&nusec_cfg, L".\\segatools.ini"); clock_config_load(&clock_cfg, L".\\segatools.ini");
ds_config_load(&ds_cfg, L".\\segatools.ini"); ds_config_load(&ds_cfg, L".\\segatools.ini");
nusec_config_load(&nusec_cfg, L".\\segatools.ini");
// TODO make use of clock read hook configurable clock_hook_init(&clock_cfg);
clock_read_hook_init();
clock_write_hook_init();
nusec_hook_init(&nusec_cfg, "SSSS", "AAV0"); nusec_hook_init(&nusec_cfg, "SSSS", "AAV0");
ds_hook_init(&ds_cfg); ds_hook_init(&ds_cfg);

View File

@ -6,7 +6,6 @@
#include "hook/process.h" #include "hook/process.h"
#include "hooklib/clock.h"
#include "hooklib/serial.h" #include "hooklib/serial.h"
#include "hooklib/spike.h" #include "hooklib/spike.h"
@ -31,7 +30,6 @@ static DWORD CALLBACK mu3_pre_startup(void)
/* Hook Win32 APIs */ /* Hook Win32 APIs */
clock_write_hook_init();
serial_hook_init(); serial_hook_init();
/* Initialize emulation hooks */ /* Initialize emulation hooks */

View File

@ -1,9 +1,13 @@
#include <windows.h> #include <windows.h>
#include <assert.h>
#include <stdint.h> #include <stdint.h>
#include "hook/table.h" #include "hook/table.h"
#include "platform/clock.h"
#include "platform/config.h"
#include "util/dprintf.h" #include "util/dprintf.h"
static void WINAPI my_GetSystemTimeAsFileTime(FILETIME *out); static void WINAPI my_GetSystemTimeAsFileTime(FILETIME *out);
@ -16,13 +20,18 @@ static BOOL WINAPI my_SetTimeZoneInformation(TIME_ZONE_INFORMATION *tzinfo);
static BOOL (WINAPI * next_GetSystemTimeAsFileTime)(FILETIME *out); static BOOL (WINAPI * next_GetSystemTimeAsFileTime)(FILETIME *out);
static int64_t clock_current_day; static int64_t clock_current_day;
static bool clock_time_warp;
static const struct hook_symbol clock_read_hook_syms[] = { static const struct hook_symbol clock_base_hook_syms[] = {
{ {
.name = "GetSystemTimeAsFileTime", .name = "GetSystemTimeAsFileTime",
.patch = my_GetSystemTimeAsFileTime, .patch = my_GetSystemTimeAsFileTime,
.link = (void **) &next_GetSystemTimeAsFileTime, .link = (void **) &next_GetSystemTimeAsFileTime,
}, { }
};
static const struct hook_symbol clock_read_hook_syms[] = {
{
.name = "GetLocalTime", .name = "GetLocalTime",
.patch = my_GetLocalTime, .patch = my_GetLocalTime,
}, { }, {
@ -65,6 +74,12 @@ static void WINAPI my_GetSystemTimeAsFileTime(FILETIME *out)
int64_t fake_jiffies_biased; int64_t fake_jiffies_biased;
int64_t fake_jiffies; int64_t fake_jiffies;
if (!clock_time_warp) {
next_GetSystemTimeAsFileTime(out);
return;
}
if (out == NULL) { if (out == NULL) {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
@ -207,20 +222,39 @@ static BOOL WINAPI my_SetTimeZoneInformation(TIME_ZONE_INFORMATION *in)
return TRUE; return TRUE;
} }
void clock_read_hook_init(void) HRESULT clock_hook_init(const struct clock_config *cfg)
{ {
hook_table_apply( assert(cfg != NULL);
NULL,
"kernel32.dll",
clock_read_hook_syms,
_countof(clock_read_hook_syms));
}
void clock_write_hook_init(void) clock_time_warp = cfg->timewarp;
{
hook_table_apply( if (cfg->timezone || cfg->timewarp || !cfg->writeable) {
NULL, /* All the clock hooks require the core GSTAFT hook to be installed */
"kernel32.dll", /* Note the ! up there btw. */
clock_write_hook_syms,
_countof(clock_write_hook_syms)); hook_table_apply(
NULL,
"kernel32.dll",
clock_base_hook_syms,
_countof(clock_base_hook_syms));
}
if (cfg->timezone) {
hook_table_apply(
NULL,
"kernel32.dll",
clock_read_hook_syms,
_countof(clock_read_hook_syms));
}
if (!cfg->writeable) {
/* Install hook if this config parameter is FALSE! */
hook_table_apply(
NULL,
"kernel32.dll",
clock_write_hook_syms,
_countof(clock_write_hook_syms));
}
return S_OK;
} }

7
platform/clock.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
#include <windows.h>
#include "platform/config.h"
HRESULT clock_hook_init(const struct clock_config *cfg);

View File

@ -17,6 +17,7 @@ void alls_config_load(struct alls_config *cfg, const wchar_t *filename)
assert(filename != NULL); assert(filename != NULL);
amvideo_config_load(&cfg->amvideo, filename); amvideo_config_load(&cfg->amvideo, filename);
clock_config_load(&cfg->clock, filename);
dns_config_load(&cfg->dns, filename); dns_config_load(&cfg->dns, filename);
hwmon_config_load(&cfg->hwmon, filename); hwmon_config_load(&cfg->hwmon, filename);
misc_config_load(&cfg->misc, filename); misc_config_load(&cfg->misc, filename);
@ -31,6 +32,7 @@ void nu_config_load(struct nu_config *cfg, const wchar_t *filename)
assert(filename != NULL); assert(filename != NULL);
amvideo_config_load(&cfg->amvideo, filename); amvideo_config_load(&cfg->amvideo, filename);
clock_config_load(&cfg->clock, filename);
dns_config_load(&cfg->dns, filename); dns_config_load(&cfg->dns, filename);
hwmon_config_load(&cfg->hwmon, filename); hwmon_config_load(&cfg->hwmon, filename);
misc_config_load(&cfg->misc, filename); misc_config_load(&cfg->misc, filename);
@ -46,6 +48,20 @@ void amvideo_config_load(struct amvideo_config *cfg, const wchar_t *filename)
cfg->enable = GetPrivateProfileIntW(L"amvideo", L"enable", 1, filename); cfg->enable = GetPrivateProfileIntW(L"amvideo", L"enable", 1, filename);
} }
void clock_config_load(struct clock_config *cfg, const wchar_t *filename)
{
assert(cfg != NULL);
assert(filename != NULL);
cfg->timezone = GetPrivateProfileIntW(L"clock", L"timezone", 1, filename);
cfg->timewarp = GetPrivateProfileIntW(L"clock", L"timewarp", 0, filename);
cfg->writeable = GetPrivateProfileIntW(
L"clock",
L"writeable",
0,
filename);
}
void dns_config_load(struct dns_config *cfg, const wchar_t *filename) void dns_config_load(struct dns_config *cfg, const wchar_t *filename)
{ {
wchar_t default_[128]; wchar_t default_[128];

View File

@ -10,6 +10,12 @@ struct amvideo_config {
bool enable; bool enable;
}; };
struct clock_config {
bool timezone;
bool timewarp;
bool writeable;
};
struct dns_config { struct dns_config {
bool enable; bool enable;
wchar_t router[128]; wchar_t router[128];
@ -51,6 +57,7 @@ struct vfs_config {
struct nu_config { struct nu_config {
struct amvideo_config amvideo; struct amvideo_config amvideo;
struct clock_config clock;
struct dns_config dns; struct dns_config dns;
struct hwmon_config hwmon; struct hwmon_config hwmon;
struct misc_config misc; struct misc_config misc;
@ -60,6 +67,7 @@ struct nu_config {
struct alls_config { struct alls_config {
struct amvideo_config amvideo; struct amvideo_config amvideo;
struct clock_config clock;
struct dns_config dns; struct dns_config dns;
struct hwmon_config hwmon; struct hwmon_config hwmon;
struct misc_config misc; struct misc_config misc;
@ -72,6 +80,7 @@ void alls_config_load(struct alls_config *cfg, const wchar_t *filename);
void nu_config_load(struct nu_config *cfg, const wchar_t *filename); void nu_config_load(struct nu_config *cfg, const wchar_t *filename);
void amvideo_config_load(struct amvideo_config *cfg, const wchar_t *filename); void amvideo_config_load(struct amvideo_config *cfg, const wchar_t *filename);
void clock_config_load(struct clock_config *cfg, const wchar_t *filename);
void dns_config_load(struct dns_config *cfg, const wchar_t *filename); void dns_config_load(struct dns_config *cfg, const wchar_t *filename);
void hwmon_config_load(struct hwmon_config *cfg, const wchar_t *filename); void hwmon_config_load(struct hwmon_config *cfg, const wchar_t *filename);
void misc_config_load(struct misc_config *cfg, const wchar_t *filename); void misc_config_load(struct misc_config *cfg, const wchar_t *filename);

View File

@ -9,6 +9,8 @@ platform_lib = static_library(
sources : [ sources : [
'amvideo.c', 'amvideo.c',
'amvideo.h', 'amvideo.h',
'clock.c',
'clock.h',
'config.c', 'config.c',
'config.h', 'config.h',
'dns.c', 'dns.c',

View File

@ -3,6 +3,7 @@
#include <assert.h> #include <assert.h>
#include "platform/amvideo.h" #include "platform/amvideo.h"
#include "platform/clock.h"
#include "platform/config.h" #include "platform/config.h"
#include "platform/dns.h" #include "platform/dns.h"
#include "platform/hwmon.h" #include "platform/hwmon.h"
@ -31,6 +32,12 @@ HRESULT platform_hook_init_alls(
return hr; return hr;
} }
hr = clock_hook_init(&cfg->clock);
if (FAILED(hr)) {
return hr;
}
hr = dns_platform_hook_init(&cfg->dns); hr = dns_platform_hook_init(&cfg->dns);
if (FAILED(hr)) { if (FAILED(hr)) {
@ -85,6 +92,12 @@ HRESULT platform_hook_init_nu(
return hr; return hr;
} }
hr = clock_hook_init(&cfg->clock);
if (FAILED(hr)) {
return hr;
}
hr = dns_platform_hook_init(&cfg->dns); hr = dns_platform_hook_init(&cfg->dns);
if (FAILED(hr)) { if (FAILED(hr)) {