th135hack.zip/th135hack/main.cpp
#include <stdio.h>
#include <algorithm>
#include <boost/version.hpp>
#include <boost/static_assert.hpp>
#include <boost/filesystem.hpp>
#include <Windows.h>
#include "dll_hack_lib.h"
#include "squirrel.h"
#include "sqstdio.h"
#include "dik_list.h"
#pragma comment(lib, "user32.lib")
unsigned int baseAddr;
SQRESULT (*tostring)(HSQUIRRELVM, SQInteger);
SQRESULT (*dofile)(HSQUIRRELVM, const SQChar *, SQBool, SQBool);
FILE *fp;
const char *TypeToStr(SQObjectType type) {
switch (type) {
case OT_NULL: return "OT_NULL";
case OT_INTEGER: return "OT_INTEGER";
case OT_FLOAT: return "OT_FLOAT";
case OT_BOOL: return "OT_BOOL";
case OT_STRING: return "OT_STRING";
case OT_TABLE: return "OT_TABLE";
case OT_ARRAY: return "OT_ARRAY";
case OT_USERDATA: return "OT_USERDATA";
case OT_CLOSURE: return "OT_CLOSURE";
case OT_NATIVECLOSURE: return "OT_NATIVECLOSURE";
case OT_GENERATOR: return "OT_GENERATOR";
case OT_USERPOINTER: return "OT_USERPOINTER";
case OT_THREAD: return "OT_THREAD";
case OT_FUNCPROTO: return "OT_FUNCPROTO";
case OT_CLASS: return "OT_CLASS";
case OT_INSTANCE: return "OT_INSTANCE";
case OT_WEAKREF: return "OT_WEAKREF";
case OT_OUTER: return "OT_OUTER";
default: return "UNKNOWN";
}
}
void printfunc(HSQUIRRELVM v, const SQChar* s, ...) {
va_list arglist;
va_start(arglist, s);
vprintf(s, arglist);
va_end(arglist);
va_start(arglist, s);
vfprintf(fp, s, arglist);
va_end(arglist);
}
void printf2(const char * const fmt, ...) {
va_list arglist;
va_start(arglist, fmt);
vprintf(fmt, arglist);
va_end(arglist);
va_start(arglist, fmt);
vfprintf(fp, fmt, arglist);
va_end(arglist);
}
#define printf printf2
void printtab(const unsigned int tab) {
for (unsigned int i = 0; i < tab; i++) {
printf(" ");
}
}
void printInstance(const unsigned int tab, HSQUIRRELVM v);
void printInstance(const unsigned int tab, HSQUIRRELVM v, const char * const name);
void printTable(const unsigned int tab, HSQUIRRELVM v);
void printTable(const unsigned int tab, HSQUIRRELVM v, const char * const name);
void printSQObject(const unsigned int tab, HSQUIRRELVM v, int idx) {
const SQObjectType type = sq_gettype(v, idx);
if (type == OT_STRING) {
const char *str;
sq_getstring(v, idx, &str);
printf("%s", str);
} else if (type == OT_INTEGER) {
SQInteger num;
sq_getinteger(v, idx, &num);
printf("%d", num);
} else if (type == OT_NULL) {
printf("null");
} else if (type == OT_TABLE) {
printf("\n");
printTable(tab + 1, v);
} else if (type == OT_INSTANCE) {
printf("\n");
printInstance(tab + 1, v);
} else {
tostring(v, idx);
const char *str;
sq_getstring(v, idx, &str);
printf("%s", str);
sq_pop(v, 1);
}
}
void printInstance(const unsigned int tab, HSQUIRRELVM v) {
if (tab >= 5) {
return;
}
sq_getclass(v, -1);
for (sq_pushnull(v); SQ_SUCCEEDED(sq_next(v, -2)); sq_pop(v, 3)) {
const char *key;
sq_getstring(v, -2, &key);
sq_pushstring(v, key, -1);
sq_get(v, -6);
const SQObjectType type = sq_gettype(v, -1);
printtab(tab);
printf("%s:%s = ", key, TypeToStr(type));
printSQObject(tab, v, -1);
printf("\n");
}
sq_pop(v, 2);
}
void printInstance(const unsigned int tab, HSQUIRRELVM v, const char * const name) {
sq_pushstring(v, name, -1);
sq_get(v, -2);
printInstance(tab, v);
sq_pop(v, 1);
}
void printTable(const unsigned int tab, HSQUIRRELVM v) {
if (tab >= 5) {
return;
}
for (sq_pushnull(v); SQ_SUCCEEDED(sq_next(v, -2)); sq_pop(v, 2)) {
const SQObjectType keyType = sq_gettype(v, -2);
printtab(tab);
if (keyType == OT_STRING) {
const char *key;
sq_getstring(v, -2, &key);
printf("%s", key);
} else if (keyType == OT_INTEGER) {
int key;
sq_getinteger(v, -2, &key);
printf("%d", key);
} else {
printf("Error: key is unknown type %s\n", TypeToStr(keyType));
}
const SQObjectType valueType = sq_gettype(v, -1);
printf(":%s = ", TypeToStr(valueType));
printSQObject(tab, v, -1);
printf("\n");
}
sq_pop(v, 1);
}
void printTable(const unsigned int tab, HSQUIRRELVM v, const char * const name) {
sq_pushstring(v, name, -1);
sq_get(v, -2);
printTable(tab, v);
sq_pop(v, 1);
}
void hook(HSQUIRRELVM v, SQInteger hookType, const SQChar * const sourcename, SQInteger line, const SQChar * const funcname) {
static unsigned int count = 0;
count++;
if ((count % 10000) != 0) {
return;
}
sq_pushroottable(v);
sq_pushstring(v, "actor", -1);
sq_get(v, -2);
const unsigned int size = sq_getsize(v, -1);
if (size == 0) {
sq_pop(v, 2);
return;
}
sq_pushstring(v, "player1", -1);
sq_get(v, -2);
printInstance(0, v);
sq_pop(v, 3);
}
void hookPrintPos(HSQUIRRELVM v, SQInteger hookType, const SQChar * const sourcename, SQInteger line, const SQChar * const funcname) {
if (funcname == NULL || ::strcmp("WorldContactTest", funcname) != 0) {
return;
}
sq_pushroottable(v);
sq_pushstring(v, "actor", -1);
sq_get(v, -2);
const unsigned int size = sq_getsize(v, -1);
if (size < 2) {
sq_pop(v, 2);
return;
}
sq_pushstring(v, "player1", -1);
sq_get(v, -2);
sq_pushstring(v, "x", -1);
if (SQ_FAILED(sq_get(v, -2))) {
printf("error\n");
sq_pop(v, 3);
}
int x;
sq_getinteger(v, -1, &x);
sq_pop(v, 1);
sq_pushstring(v, "y", -1);
if (SQ_FAILED(sq_get(v, -2))) {
printf("error\n");
sq_pop(v, 3);
}
int y;
sq_getinteger(v, -1, &y);
sq_pop(v, 2);
sq_pushstring(v, "player2", -1);
sq_get(v, -2);
sq_pushstring(v, "x", -1);
if (SQ_FAILED(sq_get(v, -2))) {
printf("error\n");
sq_pop(v, 3);
}
int ex;
sq_getinteger(v, -1, &ex);
sq_pop(v, 1);
sq_pushstring(v, "y", -1);
if (SQ_FAILED(sq_get(v, -2))) {
printf("error\n");
sq_pop(v, 3);
}
int ey;
sq_getinteger(v, -1, &ey);
printf("%d:%d %d:%d\n", x, y, ex, ey);
sq_pop(v, 4);
static unsigned int frame = 0;
frame++;
if (x > ex) {
::keybd_event(VK_LEFT, DIK_LEFT, KEYEVENTF_KEYUP, 0);
::keybd_event(VK_RIGHT, DIK_RIGHT, 0, 0);
} else {
::keybd_event(VK_RIGHT, DIK_RIGHT, KEYEVENTF_KEYUP, 0);
::keybd_event(VK_LEFT, DIK_LEFT, 0, 0);
}
if (ex - 70 <= x && x <= ex + 70) {
::keybd_event(0x5A, DIK_Z, ((frame%2) == 0 ? KEYEVENTF_KEYUP : 0), 0);
}
}
void hookPrintPlayer1(HSQUIRRELVM v, SQInteger hookType, const SQChar * const sourcename, SQInteger line, const SQChar * const funcname) {
sq_pushroottable(v);
sq_pushstring(v, "actor", -1);
sq_get(v, -2);
const unsigned int size = sq_getsize(v, -1);
if (size != 2) {
sq_pop(v, 2);
return;
}
sq_pushstring(v, "player1", -1);
sq_get(v, -2);
sq_getclass(v, -1);
for (sq_pushnull(v); SQ_SUCCEEDED(sq_next(v, -2)); sq_pop(v, 6)) {
const SQObjectType keyType = sq_gettype(v, -2);
const char *key;
sq_getstring(v, -2, &key);
printf("%s", key);
sq_pushroottable(v);
sq_pushstring(v, "actor", -1);
sq_get(v, -2);
sq_pushstring(v, "player1", -1);
sq_get(v, -2);
sq_pushstring(v, key, -1);
sq_get(v, -2);
const SQObjectType type = sq_gettype(v, -1);
printf(":%s", TypeToStr(type));
if (type == OT_STRING) {
const char *str;
sq_getstring(v, -1, &str);
printf(" = %s", str);
} else if (type == OT_INTEGER) {
SQInteger i;
sq_getinteger(v, -1, &i);
printf(" = %d", i);
} else if (type == OT_NULL) {
} else {
tostring(v, -1);
const char *str;
sq_getstring(v, -1, &str);
printf(" = %s", str);
sq_pop(v, 1);
}
printf("\n");
}
sq_pop(v, 5);
}
void hookEnableKokoro(HSQUIRRELVM v, SQInteger type, const SQChar * const sourcename, SQInteger line, const SQChar * const funcname) {
static bool first = true;
if (!first) {
return;
}
first = false;
sq_pushroottable(v);
sq_pushstring(v, "save_data", -1);
sq_get(v, -2);
sq_pushstring(v, "enable_kokoro", -1);
sq_pushbool(v, true);
sq_set(v, -3);
sq_pop(v, 2);
}
void hookPrintRootTable(HSQUIRRELVM v, SQInteger type, const SQChar * const sourcename, SQInteger line, const SQChar * const funcname) {
static bool first = true;
if (!first) {
return;
}
if (::GetKeyState(VK_RETURN) == 0) {
return;
}
first = false;
for (sq_pushroottable(v), sq_pushnull(v); SQ_SUCCEEDED(sq_next(v, -2)); sq_pop(v, 2)) {
const char *key;
sq_getstring(v, -2, &key);
const SQObjectType type = sq_gettype(v, -1);
printf("%s:%s", key, TypeToStr(type));
if (type == OT_INTEGER) {
SQInteger num;
sq_getinteger(v, -1, &num);
printf(" = %d", num);
} else if (type == OT_NULL) {
} else {
tostring(v, -1);
const char *str;
sq_getstring(v, -1, &str);
printf(" = %s", str);
sq_pop(v, 1);
}
printf("\n");
if (type == OT_TABLE) {
for (sq_pushnull(v); SQ_SUCCEEDED(sq_next(v, -2)); sq_pop(v, 2)) {
printf("\t");
const SQObjectType keyType = sq_gettype(v, -2);
if (keyType == OT_STRING) {
const char *key;
sq_getstring(v, -2, &key);
printf("%s", key);
} else if (keyType == OT_INTEGER) {
SQInteger key;
sq_getinteger(v, -2, &key);
printf("%d", key);
} else {
printf("break");
}
const SQObjectType type = sq_gettype(v, -1);
printf(":%s\n", TypeToStr(type));
}
sq_pop(v, 1);
}
}
sq_pop(v, 2);
}
void hookPrintStack(HSQUIRRELVM v, SQInteger type, const SQChar * const sourcename, SQInteger line, const SQChar * const funcname) {
if (type != 'c') {
return;
}
printf("%s(", funcname);
for (unsigned int i = 1; i <= sq_gettop(v); i++) {
if (i != 1) {
printf(", ");
}
const SQObjectType type = sq_gettype(v, i);
printf("%s", TypeToStr(type));
if (type == OT_INTEGER) {
SQInteger num;
sq_getinteger(v, i, &num);
printf(" = %d", num);
} else if (type == OT_NULL) {
} else {
tostring(v, i);
const char *str;
sq_getstring(v, -1, &str);
printf(" = %s", str);
sq_pop(v, 1);
}
}
printf(")\n");
}
void hookPrintArgs(HSQUIRRELVM v, SQInteger type, const SQChar * const sourcename, SQInteger line, const SQChar * const funcname) {
if (type != 'c') {
return;
}
printf("%s(", funcname);
const unsigned int level = 0;
for (unsigned int i = 0; ; i++) {
const char * const name = sq_getlocal(v, level, i);
if (name == NULL) {
break;
}
if (i != 0) {
printf(", ");
}
const SQObjectType type = sq_gettype(v, -1);
printf("%s:%s", name, TypeToStr(type));
if (type == OT_INTEGER) {
SQInteger i;
sq_getinteger(v, -1, &i);
printf(" = %d", i);
} else if (type == OT_NULL) {
} else {
tostring(v, -1);
const char *str;
sq_getstring(v, -1, &str);
printf(" = %s", str);
sq_pop(v, 1);
}
sq_pop(v, 1);
}
printf(")\n");
}
void hookPrintStackTrace(HSQUIRRELVM v, SQInteger type, const SQChar * const sourcename, SQInteger line, const SQChar * const funcname) {
if (type != 'c') {
return;
}
unsigned int tab = 0;
for (int i = 10; i >= 0; i--) {
SQStackInfos info;
if (!SQ_SUCCEEDED(sq_stackinfos(v, i, &info))) {
continue;
}
for (unsigned int l = 0; l < tab; l++) {
printf("\t");
}
printf("%s\n", info.funcname);
tab++;
}
}
void main() {
org::click3::DllHackLib::SetupConsole();
fp = ::fopen("log.txt", "a");
baseAddr = reinterpret_cast<unsigned int>(::GetModuleHandleW(NULL));
dofile = reinterpret_cast<SQRESULT (*)(HSQUIRRELVM, const SQChar *, SQBool, SQBool)>(baseAddr + 0x0034AAF0);
tostring = reinterpret_cast<SQRESULT (*)(HSQUIRRELVM, SQInteger)>(baseAddr + 0x00232600);
unsigned int addr = 0;
while(addr == 0) {
Sleep(1000);
addr = *reinterpret_cast<const unsigned int *>(baseAddr + 0x004D7984);
}
HSQUIRRELVM v = reinterpret_cast<HSQUIRRELVM>(addr);
sq_setprintfunc(v, printfunc, printfunc);
sq_setnativedebughook(v, hookPrintPos);
}