From 12a1f752459c5e47a380d1ff830b1f8dffcf143a Mon Sep 17 00:00:00 2001 From: kungfooman Date: Sat, 23 Mar 2019 04:05:09 +0100 Subject: [PATCH] embed chakracore into cod2 and refactor api somewhat --- chakracore.cpp | 31 ++++++++++++++++--------------- chakracore.h | 6 +++--- cl_main.cpp | 9 +++++++++ test_chakracore.cpp | 9 ++++++++- 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/chakracore.cpp b/chakracore.cpp index 525d76e..f13544e 100644 --- a/chakracore.cpp +++ b/chakracore.cpp @@ -2,39 +2,40 @@ #include #include -chakracore_init_t dll_chakracore_init = NULL; -chakracore_set_printf_t dll_chakracore_set_printf = NULL; -chakracore_eval_t dll_chakracore_eval = NULL; +chakracore_init_t dll_chakracore_init = NULL; +chakracore_set_printstring_t dll_chakracore_set_printstring = NULL; +chakracore_eval_t dll_chakracore_eval = NULL; -int chakracore_prepare(printf_t printer) { +int chakracore_prepare(printstring_t printer) { HMODULE handle = LoadLibraryA("ChakraCoreInterface.dll"); - dll_chakracore_init = (chakracore_init_t )GetProcAddress(handle, "chakracore_init"); - dll_chakracore_set_printf = (chakracore_set_printf_t)GetProcAddress(handle, "chakracore_set_printf"); - dll_chakracore_eval = (chakracore_eval_t )GetProcAddress(handle, "chakracore_eval"); + dll_chakracore_init = (chakracore_init_t )GetProcAddress(handle, "chakracore_init"); + dll_chakracore_set_printstring = (chakracore_set_printstring_t)GetProcAddress(handle, "chakracore_set_printstring"); + dll_chakracore_eval = (chakracore_eval_t )GetProcAddress(handle, "chakracore_eval"); -#if 1 + #ifdef TEST_CHAKRACORE printf("cci handle=%d\n", handle); - printf("dll_chakracore_init = %d\n", dll_chakracore_init ); - printf("dll_chakracore_set_printf = %d\n", dll_chakracore_set_printf); - printf("dll_chakracore_eval = %d\n", dll_chakracore_eval ); -#endif + printf("dll_chakracore_init = %d\n", dll_chakracore_init ); + printf("dll_chakracore_set_printf = %d\n", dll_chakracore_set_printstring); + printf("dll_chakracore_eval = %d\n", dll_chakracore_eval ); + #endif - if (dll_chakracore_set_printf) { - dll_chakracore_set_printf(printer); + if (dll_chakracore_set_printstring) { + dll_chakracore_set_printstring(printer); } if (dll_chakracore_init) dll_chakracore_init(); - return 1; } int chakracore_eval(const char *code) { if (dll_chakracore_eval == NULL) { + #ifdef TEST_CHAKRACORE printf("dll_chakracore_eval == NULL\n"); + #endif return 0; } return dll_chakracore_eval(code); diff --git a/chakracore.h b/chakracore.h index e7031e5..b5366a2 100644 --- a/chakracore.h +++ b/chakracore.h @@ -2,10 +2,10 @@ -typedef int(*printf_t)(const char *format, ...); +typedef int(__cdecl *printstring_t)(const char *format); typedef int(*chakracore_init_t)(); -typedef int(*chakracore_set_printf_t)(printf_t); +typedef int(*chakracore_set_printstring_t)(printstring_t); typedef int(*chakracore_eval_t)(const char *code); -int chakracore_prepare(printf_t printer); +int chakracore_prepare(printstring_t printer); int chakracore_eval(const char *code); \ No newline at end of file diff --git a/cl_main.cpp b/cl_main.cpp index 6e39849..00e8fea 100644 --- a/cl_main.cpp +++ b/cl_main.cpp @@ -3,6 +3,7 @@ //#pragma comment(lib, "opengl32.lib") #include "imgui.h" #include "cg_public.h" +#include "chakracore.h" dvar_t *con_restricted = (dvar_t*)0x5E132C; int *cls_keyCatchers = (int*)0x96B654; @@ -163,6 +164,11 @@ void CL_Frame() //bool cl_inited = false; +int Com_PrintString(const char *str) { + Com_Printf("%s", str); + return 0; +} + void CL_Init( void ) { void(*o)(void) = (void(*)(void))0x411650; @@ -181,4 +187,7 @@ void CL_Init( void ) //cl_inited = true; CG_InitConsoleCommands(); + + chakracore_prepare(Com_PrintString); + chakracore_eval("console.log(\"^3Loaded ChakraCore\");"); } \ No newline at end of file diff --git a/test_chakracore.cpp b/test_chakracore.cpp index 00687b4..a4c07ce 100644 --- a/test_chakracore.cpp +++ b/test_chakracore.cpp @@ -1,8 +1,15 @@ +#define TEST_CHAKRACORE 1 + #include #include "chakracore.h" +int __cdecl printstring(const char *str) { + printf("%s", str); + return 0; +} + int main() { - chakracore_prepare(printf); + chakracore_prepare(printstring); chakracore_eval("console.log(\"^1hai\");"); chakracore_eval("console.log(1);"); chakracore_eval("console.log(2);");