Skip to content

Commit

Permalink
Support BlocklandLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
qoh committed Mar 13, 2016
1 parent aaf5f8c commit 3eb249e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 33 deletions.
9 changes: 2 additions & 7 deletions BlocklandLua/Torque.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ LookupNamespaceFn LookupNamespace;

//StringTable::insert
initGameFn initGame;
Sim__initFn Sim__init;
StringTableInsertFn StringTableInsert;
Namespace__lookupFn Namespace__lookup;
CodeBlock__execFn CodeBlock__exec;
Expand Down Expand Up @@ -198,15 +197,11 @@ const char* Eval(const char* str)
return Evaluate(str, false, NULL);
}

void torque_pre_init()
{
InitScanner("Blockland.exe");
BLSCAN(Sim__init, "\x56\x33\xF6\x57\x89\x35", "xxxxxx");
}

//Initialize the Torque Interface
bool torque_init()
{
InitScanner("Blockland.exe");

//Printf is required for debug output, so find it first
Printf = (PrintfFn)ScanFunc("\x8B\x4C\x24\x04\x8D\x44\x24\x08\x50\x6A\x00\x6A\x00\xE8\x00\x00\x00\x00\x83\xC4\x0C\xC3", "xxxxxxxxxxxxxx????xxxx");

Expand Down
67 changes: 43 additions & 24 deletions BlocklandLua/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ static int lu_ts_register(lua_State *L)

static int lu_ts_schedule(lua_State *L)
{
unsigned int timeDelta = luaL_checknumber(L, 1);
unsigned int timeDelta = static_cast<unsigned int>(luaL_checknumber(L, 1));
/* SimObject *refObject = Sim__getRootGroup();
SimConsoleEvent *evt = new SimConsoleEvent(argc, func and argv, false);
Expand All @@ -565,10 +565,43 @@ static int lu_ts_schedule(lua_State *L)

static int lu_ts_cancel(lua_State *L)
{
Sim__cancelEvent(luaL_checknumber(L, 1));
Sim__cancelEvent(static_cast<unsigned int>(luaL_checknumber(L, 1)));
return 0;
}

/* MologieDetours::Detour<ShapeNameHudOnRenderFn> *detour_ShapeNameHudOnRender = NULL;
// typedef void(__fastcall *ShapeNameHudOnRenderFn)(SimObject *this_, int, int arg1, int arg2);
// void __stdcall hook_ShapeNameHudOnRender(DWORD* obj, DWORD arg1, DWORD arg2, DWORD arg3)
void __fastcall hook_ShapeNameHudOnRender(DWORD *this_, int dummy, int arg1, int arg2)
{
// detour_ShapeNameHudOnRender->GetOriginalFunction()(obj, arg1, arg2, arg3);
detour_ShapeNameHudOnRender->GetOriginalFunction()(this_, dummy, arg1, arg2);
lua_pushstring(gL, "hook:render2d");
lua_gettable(gL, LUA_REGISTRYINDEX);
if (lua_pcall(gL, 0, 0, 0))
showluaerror(gL);
}
static int lu_ts_hook(lua_State *L)
{
const char *ev = luaL_checkstring(L, 1);
luaL_checktype(L, 2, LUA_TFUNCTION);
if (detour_ShapeNameHudOnRender == NULL)
detour_ShapeNameHudOnRender = new MologieDetours::Detour<ShapeNameHudOnRenderFn>(ShapeNameHudOnRender, (ShapeNameHudOnRenderFn)hook_ShapeNameHudOnRender);
lua_pushvalue(L, 2);
lua_pushstring(L, "hook:");
lua_pushvalue(L, 1);
lua_concat(L, 2);
lua_settable(gL, LUA_REGISTRYINDEX);
return 0;
} */

static int lu_ts_global_index(lua_State *L)
{
lua_pushstring(L, GetGlobalVariable(luaL_checkstring(L, 2)));
Expand Down Expand Up @@ -603,28 +636,26 @@ static luaL_Reg lua_ts_reg[] = {
{"register", lu_ts_register},
{"schedule", lu_ts_schedule},
{"cancel", lu_ts_cancel},
// {"hook", lu_ts_hook},
{NULL, NULL}
};

void init()
bool init()
{
if (!torque_init())
return;
return false;

Printf("Lua Init:");
Printf(" Using %s", LUA_VERSION);

lua_State *L = luaL_newstate();
gL = L;

if (L == NULL)
{
Printf(" Failed to create Lua state");
return;
Printf(" \x03" "Failed to create Lua state");
return false;
}

Printf(" Preparing Lua environment");

luaL_openlibs(L);

// set up ts_object metatable
Expand Down Expand Up @@ -665,30 +696,18 @@ void init()
})
)lua");

Printf(" Installing Lua extensions");

ConsoleFunction(NULL, "luaEval", ts_luaEval, "luaEval(string code, bool silent=false) - Execute a chunk of code as Lua.", 2, 3);
ConsoleFunction(NULL, "luaExec", ts_luaExec, "luaExec(string filename, bool silent=false) - Execute a Lua code file.", 2, 3);
ConsoleFunction(NULL, "luaCall", ts_luaCall, "luaCall(string name, ...) - Call a Lua function.", 2, 20);

Printf("");
}

MologieDetours::Detour<Sim__initFn> *detour_Sim__init = NULL;

void hook_Sim__init(void)
{
init();
return detour_Sim__init->GetOriginalFunction()();
Printf(" Ready for action");
return true;
}

int __stdcall DllMain(HINSTANCE hInstance, unsigned long reason, void *reserved)
{
if (reason == DLL_PROCESS_ATTACH)
{
torque_pre_init();
detour_Sim__init = new MologieDetours::Detour<Sim__initFn>(Sim__init, hook_Sim__init);
}
return init();

return true;
}
Expand Down
4 changes: 2 additions & 2 deletions BlocklandLua/torque.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ BLFUNC_EXTERN(void, , Printf, const char* format, ...);
extern const char *StringTableEntry(const char *str, bool caseSensitive=false);
extern DWORD StringTable;
BLFUNC_EXTERN(bool, , initGame, int argc, const char **argv);
BLFUNC_EXTERN(void, , Sim__init, void);
BLFUNC_EXTERN(Namespace *, , LookupNamespace, const char *ns);
BLFUNC_EXTERN(const char *, __thiscall, StringTableInsert, DWORD stringTablePtr, const char* val, const bool caseSensitive)
BLFUNC_EXTERN(Namespace::Entry *, __thiscall, Namespace__lookup, Namespace *this_, const char *name)
Expand Down Expand Up @@ -155,7 +154,8 @@ const char* Eval(const char* str);
BLFUNC_EXTERN(void, , SetGlobalVariable, const char *name, const char *value);
BLFUNC_EXTERN(char *, , GetGlobalVariable, const char *name);

typedef void(__thiscall* ShapeNameHudOnRenderFn)(DWORD* obj, DWORD arg1, DWORD arg2, DWORD arg3);
typedef void(__thiscall *ShapeNameHudOnRenderFn)(DWORD* obj, DWORD arg1, DWORD arg2, DWORD arg3);
// typedef void(__fastcall *ShapeNameHudOnRenderFn)(SimObject *this_, int, int arg1, int arg2);
static ShapeNameHudOnRenderFn ShapeNameHudOnRender;

void torque_pre_init();
Expand Down

0 comments on commit 3eb249e

Please # to comment.