Skip to content

Commit

Permalink
embed chakracore into cod2 and refactor api somewhat
Browse files Browse the repository at this point in the history
  • Loading branch information
kungfooman committed Mar 23, 2019
1 parent d563b97 commit 12a1f75
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
31 changes: 16 additions & 15 deletions chakracore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,40 @@
#include <stdio.h>
#include <Windows.h>

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);
Expand Down
6 changes: 3 additions & 3 deletions chakracore.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
9 changes: 9 additions & 0 deletions cl_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -181,4 +187,7 @@ void CL_Init( void )
//cl_inited = true;

CG_InitConsoleCommands();

chakracore_prepare(Com_PrintString);
chakracore_eval("console.log(\"^3Loaded ChakraCore\");");
}
9 changes: 8 additions & 1 deletion test_chakracore.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
#define TEST_CHAKRACORE 1

#include <stdio.h>
#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);");
Expand Down

0 comments on commit 12a1f75

Please sign in to comment.