Skip to content

Commit

Permalink
KZ v1.0.5 GOG support, minor fixes. Need to test.
Browse files Browse the repository at this point in the history
  • Loading branch information
nkrapivin committed Feb 3, 2021
1 parent a52518d commit ed955d1
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 91 deletions.
24 changes: 24 additions & 0 deletions libLassebq/GMLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ lua_State* lS = nullptr;
bool g_AddCollisionEvents = false;
bool g_ThrowErrors = true;
bool g_NoConsole = false;
bool g_IgnoreArgc = false;

int lua_GMLua_ignoreArgc(lua_State* _pL)
{
int Largc = lua_gettop(_pL); // get argument count.
if (Largc != 1)
{
return luaL_error(_pL, __FUNCTION__ " error: Invalid argument count, expected 1, got %d.", Largc);
}

if (lua_isboolean(_pL, 1))
{
g_IgnoreArgc = lua_toboolean(_pL, 1) == 1 ? true : false;
}
else
{
return luaL_error(_pL, __FUNCTION__ " error: Invalid argument type, expected boolean.");
}

return 0;
}

bool isInvalidConstant(const char* n)
{
Expand Down Expand Up @@ -571,6 +592,7 @@ void RegisterFunctions(lua_State* _pL)
lua_register(_pL, "GMLua_getvarb", lua_GMLua_getvarb);
lua_register(_pL, "GMLua_setvarb", lua_GMLua_setvarb);
lua_register(_pL, "GMLua_instToPtr", lua_GMLua_instToPtr);
lua_register(_pL, "GMLua_ignoreArgc", lua_GMLua_ignoreArgc);
}

void RegisterScripts(lua_State* _pL)
Expand Down Expand Up @@ -650,6 +672,8 @@ int RValueToLua(lua_State* _pL, const RValue& result)

bool LuaChkArgs(const int gmargc, int luaargc, lua_State* _pL)
{
if (g_IgnoreArgc) return true;

if (gmargc != luaargc)
{
luaL_error(_pL, __FUNCTION__ " error: invalid argument count, expected %d, got %d.", gmargc, luaargc);
Expand Down
5 changes: 5 additions & 0 deletions libLassebq/GameSpecific.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#include "GameSpecific.h"

#ifdef KZ_105_STEAM
const char* const VerString = "v1.0.5\n[LLBQ]"; // LLBQ -> LibLasseBQ
#endif
#ifdef KZ_105_GOG
const char* const VerString = "v1.0.5\n[LGOG]";
#endif

void ApplyGameSpecificPatches(uintptr_t exeBase)
{
Expand Down
48 changes: 16 additions & 32 deletions libLassebq/KatanaZeroIDs.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

// What addresses to use?
#define KZ_105_STEAM
#define KZ_105_GOG

// Please please please, keep address format like this:
// #define Something_Addr (0xSOMETHINGu)
Expand All @@ -13,27 +13,19 @@
#define YYSetString_Addr (0x014DB840u)
#define YYCreateString_Addr (0x014DAE30u)
#define YYStrDup_Addr (0x014DB8C0u)
#define YYAddString_Addr (0x014DB0F0u)
#define YYFree_Addr (0x0151FFA0u)
#define ARRAY_LVAL_RV_Addr (0x014DBF50u)
#define Variable_Addr (0x01A37CF8u)
#define GMLScript_Addr (0x01A3C6F4u)
#define Run_Room_Addr (0x01CF88D8u)
#define Global_YYObject_Addr (0x01ADE1D8u)
#define Current_Room_Addr (0x01CF8900u)
#define Object_Hash_Addr (0x01AE769Cu)
#define GetCtxStackTop_Addr (0x014D5A80u)
#define DeterminePotRoot_Addr (0x014BF440u)
#define GetEvRecursive_Addr (0x01522300u)
#define InsertEvent_Addr (0x015224A0u)
#define GMMMSetLength_Addr (0x015203B0u)
#define YYSTraceStart_Addr (0x01AD8B90u)
#define VarGetValueDirect_Addr (0x014FAE90u)
#define VarSetValueDirect_Addr (0x014FBA20u)
#define BuiltinVars_Addr (0x01ADE300u)
#define FindRValueSlot_Addr (0x01525AA0u)
#define Current_Event_Addr (0x01CF8930u)
#define Current_Subtype_Addr (0x01CF8934u)
#define YYError_Addr (0x014DB040u)
#define ConstNumb_Addr (0x01D09634u)
#define ConstNames_Addr (0x01D0962Cu)
Expand All @@ -45,35 +37,27 @@
#define RFunctionTable_Addr (0x01CFBAE4u)
#define RFunctionTableL_Addr (0x01CFBAE8u)
#define FREE_RValue__Pre_Addr (0x00001070u)
#define YYSetString_Addr (0x014D2880u)
#define YYCreateString_Addr (0x014D98B0u)
#define YYSetString_Addr (0x014DA4C0u)
#define YYCreateString_Addr (0x014D9AB0u)
#define YYStrDup_Addr (0x014DA540u)
#define YYAddString_Addr (0x014DB0F0u) /* TODO */
#define YYFree_Addr (0x0151EC20u)
#define ARRAY_LVAL_RV_Addr (0x014DBF50u) /* TODO */
#define ARRAY_LVAL_RV_Addr (0x014DABD0u)
#define Variable_Addr (0x01A36CC0u)
#define GMLScript_Addr (0x01A3B680u)
#define Run_Room_Addr (0x01CF7868u)
#define Global_YYObject_Addr (0x01ADD168u)
#define Current_Room_Addr (0x01CF8900u)
#define Object_Hash_Addr (0x01AE769Cu)
#define GetCtxStackTop_Addr (0x014D5A80u)
#define DeterminePotRoot_Addr (0x014BF440u)
#define GetEvRecursive_Addr (0x01522300u)
#define InsertEvent_Addr (0x015224A0u)
#define GMMMSetLength_Addr (0x015203B0u)
#define YYSTraceStart_Addr (0x01AD7B20u) /* done */
#define VarGetValueDirect_Addr (0x014FAE90u)
#define VarSetValueDirect_Addr (0x014FBA20u)
#define BuiltinVars_Addr (0x01ADE300u)
#define FindRValueSlot_Addr (0x01525AA0u)
#define Current_Event_Addr (0x01CF8930u)
#define Current_Subtype_Addr (0x01CF8934u)
#define YYError_Addr (0x014DB040u)
#define ConstNumb_Addr (0x01D09634u)
#define ConstNames_Addr (0x01D0962Cu)
#define ConstValues_Addr (0x01D09630u)
#define MenuVersionString_Addr (0x01233C16u)
#define Object_Hash_Addr (0x01AE662Cu)
#define GetCtxStackTop_Addr (0x014D4700u)
#define DeterminePotRoot_Addr (0x014BE0B0u)
#define GetEvRecursive_Addr (0x01520F80u)
#define InsertEvent_Addr (0x01521120u)
#define YYSTraceStart_Addr (0x01AD7B20u)
#define BuiltinVars_Addr (0x01ADD290u)
#define YYError_Addr (0x014D9CC0u)
#define ConstNumb_Addr (0x01D085C4u)
#define ConstNames_Addr (0x01D085BCu)
#define ConstValues_Addr (0x01D085C0u)
#define MenuVersionString_Addr (0x01232846u)
#endif

#ifdef DITTO_WIN_STM /* The Swords of Ditto Windows Steam */
Expand Down
20 changes: 0 additions & 20 deletions libLassebq/RValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ DetPotRoot DeterminePotentialRoot = nullptr;
FREE_RVal_Pre FREE_RValue__Pre = nullptr;
YYSetStr YYSetString = nullptr;
YYCreStr YYCreateString = nullptr;
YYAddStr YYAddString = nullptr;
YYStrDupT YYStrDup = nullptr;
YYFreeT YYFree = nullptr;
ARRAYLVal ARRAY_LVAL_RValue = nullptr;
Expand Down Expand Up @@ -318,25 +317,6 @@ RValue RValue::operator--(int)
return tmp;
}

RValue& RValue::operator+=(const char* v)
{
switch (kind & MASK_KIND_RVALUE)
{
case VALUE_STRING:
{
// if string then concatenate, if not then convert to string
const char* pFirst = (pRefString != nullptr) ? pRefString->get() : nullptr;
char* pNew = YYAddString(pFirst, v);
YYCreateString(this, pNew);
YYFree(pNew);
break;
} // end block
default: abort();
}

return *this;
}

bool RValue::operator==(const RValue& rhs) const
{
int lhsType = kind & MASK_KIND_RVALUE;
Expand Down
2 changes: 0 additions & 2 deletions libLassebq/RValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ struct RValue;
typedef void(*FREE_RVal_Pre)(RValue* p);
typedef void(*YYSetStr)(RValue* _pVal, const char* _pS);
typedef void(*YYCreStr)(RValue* _pVal, const char* _pS);
typedef char*(*YYAddStr)(const char* _first, const char* _second);
typedef char*(*YYDupStr)(const char* _pStr);
typedef char*(*YYStrDupT)(const char* _pS);
typedef void(*YYFreeT)(void* _pMemory);
typedef RValue*(*ARRAYLVal)(RValue* _pV, int _index);
extern YYSetStr YYSetString;
extern YYCreStr YYCreateString;
extern YYAddStr YYAddString;
extern YYStrDupT YYStrDup;
extern YYFreeT YYFree;
extern FREE_RVal_Pre FREE_RValue__Pre;
Expand Down
58 changes: 30 additions & 28 deletions libLassebq/libLassebq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,11 @@ HMODULE exeBase = nullptr;
CHash<CObjectGM>** g_ObjectHashTable = nullptr;
int g_GMLScriptsSize;
int g_VariablesSize;
int* g_CurrentEvent = nullptr;
int* g_CurrentSubtype = nullptr;

VarGetValDirect Variable_GetValue_Direct = nullptr;
VarSetValDirect Variable_SetValue_Direct = nullptr;
FindRValSlot FindRValueSlot = nullptr;

std::unordered_map<std::string, int> fR;
std::unordered_map<std::string, int> fS;
std::unordered_map<std::string, int> fV;
std::unordered_map<std::string, int> fB;

// <object_index, <event_key, function_pointer>>
std::unordered_map<int, std::unordered_map<unsigned long long, void*>> EventPatchMap;
Expand All @@ -40,19 +35,25 @@ RValue Result(0.0);
YYErrorT YYError = nullptr;
YYObjectBase** g_pGlobal = nullptr;

void lassebq_find_bvars() {
for (int i = 0; i < 500; i++)
{
const RVariableRoutine& rvr = g_BuiltinVars[i];
if (rvr.f_getroutine == nullptr || rvr.f_name == nullptr) break;
fB[rvr.f_name] = i;
}
}

void lassebq_free_result() {
Result.~RValue();
}

bool lassebq_getvar_direct(std::string name, RValue& ret, int array_index = ARRAY_INDEX_NO_INDEX)
{
if (fV.find(name) == fV.end())
{
std::cout << "TRIED TO ACCESS A NON-EXISTING VARIABLE " << name << std::endl;
abort();
}

return Variable_GetValue_Direct(reinterpret_cast<YYObjectBase*>(g_Self), fV[name], array_index, &ret);
int lassebq_get_bvar(const std::string name) {
int ind = fB[name];
if (ind < 0 || ind > 500) abort();
lassebq_free_result();
g_BuiltinVars[ind].f_getroutine(g_Self, ARRAY_INDEX_NO_INDEX, &Result);
return Result.asInt32();
}

void lassebq_callr(std::string id, const RValueList& args = { })
Expand Down Expand Up @@ -132,14 +133,17 @@ void lassebq_lua_GMLRoutine(CInstance* _pSelf, CInstance* _pOther)
{
g_Self = _pSelf;
g_Other = _pOther;
CEvent* ev = GetEventRecursive(_pSelf->m_pObject, *g_CurrentEvent, *g_CurrentSubtype);

int event_type = lassebq_get_bvar("event_type");
int event_numb = lassebq_get_bvar("event_number");
CEvent* ev = GetEventRecursive(_pSelf->m_pObject, event_type, event_numb);
YY_STACKTRACE_FUNC_ENTRY(ev->e_code->i_pName, 0);

char luaFuncName[256]{ '\0' };
// "objectname_eventname"
snprintf(luaFuncName, sizeof(luaFuncName), "%s_%s",
_pSelf->m_pObject->m_pName,
mapOfEvents[lassebq_evKey(*g_CurrentEvent, *g_CurrentSubtype)].c_str()
mapOfEvents[lassebq_evKey(event_type, event_numb)].c_str()
);

YY_STACKTRACE_LINE(1);
Expand Down Expand Up @@ -197,13 +201,15 @@ void lassebq_luaPatch_GMLRoutine(CInstance* _pSelf, CInstance* _pOther)
g_Other = _pOther;

// Execute the original event.
unsigned long long evKey = lassebq_evKey(*g_CurrentEvent, *g_CurrentSubtype);
int event_type = lassebq_get_bvar("event_type");
int event_numb = lassebq_get_bvar("event_number");
unsigned long long evKey = lassebq_evKey(event_type, event_numb);
GML_ObjectEvent origptr = reinterpret_cast<GML_ObjectEvent>
(EventPatchMap[_pSelf->m_pObject->m_ID][evKey]);
origptr(_pSelf, _pOther);

// Execute the lua function.
CEvent* ev = GetEventRecursive(_pSelf->m_pObject, *g_CurrentEvent, *g_CurrentSubtype);
CEvent* ev = GetEventRecursive(_pSelf->m_pObject, event_type, event_numb);
// Why? When the original event returns, it will call Stacktrace's destructor, thus null-ing it
// So we need to make a new instance of the stacktrace :/
YY_STACKTRACE_FUNC_ENTRY(ev->e_code->i_pName, 0);
Expand Down Expand Up @@ -306,7 +312,6 @@ void lassebq_patchObject(CObjectGM* gmObj)
lua_getglobal(lS, buf);
if (lua_isfunction(lS, -1))
{
CObjectGM* cogm = lassebq_find_obj(i);
int type = static_cast<int>(pair.first >> 32ull);
int subtype = static_cast<int>(pair.first & 0xFFFFFFFFull);
CEvent* origev = GetEventRecursive(cogm, type, subtype);
Expand All @@ -317,7 +322,7 @@ void lassebq_patchObject(CObjectGM* gmObj)
|| origev->e_code->i_pFunc->ptr == lassebq_luaPatch_GMLRoutine)
continue;

std::cout << "AN EVENT " << pair.second << " ALREADY EXISTS! brb replacing..." << std::endl;
std::cout << "AN EVENT " << buf << " ALREADY EXISTS! brb replacing..." << std::endl;
EventPatchMap[i][pair.first] = origev->e_code->i_pFunc->ptr; // save the original function.
origev->e_code->i_pFunc->ptr = lassebq_luaPatch_GMLRoutine;
continue;
Expand Down Expand Up @@ -376,6 +381,9 @@ void lassebq_initYYC()
std::cout << "libLassebq by nkrapivindev, built for project " << PROJECT_NAME << std::endl;
std::cout << "This thing is experimental, please report any bugs to nik#5351 on Discord." << std::endl;
std::cout << "Random quote: " << GetRandomQuote() << std::endl;
#ifdef KZ_105_GOG
std::cout << "KZ_105_GOG, Katana Zero v1.0.5 Windows GOG" << std::endl;
#endif
std::cout << std::endl;
if (g_ThrowErrors) std::cout << "Will ignore Lua errors, this is VERY evil and unstable!" << std::endl;
if (g_AddCollisionEvents) std::cout << "Collision event name generation enabled, loading times will be WAY slower." << std::endl;
Expand Down Expand Up @@ -416,15 +424,11 @@ void lassebq_initYYC()
ARRAY_LVAL_RValue = reinterpret_cast<ARRAYLVal>(exeAsUint + ARRAY_LVAL_RV_Addr);
YYSetString = reinterpret_cast<YYSetStr>(exeAsUint + YYSetString_Addr);
YYCreateString = reinterpret_cast<YYCreStr>(exeAsUint + YYCreateString_Addr);
YYAddString = reinterpret_cast<YYAddStr>(exeAsUint + YYAddString_Addr);
YYStrDup = reinterpret_cast<YYStrDupT>(exeAsUint + YYStrDup_Addr);
YYFree = reinterpret_cast<YYFreeT>(exeAsUint + YYFree_Addr);
g_Self = nullptr;
SYYStackTrace::s_pStart = reinterpret_cast<SYYStackTrace**>(exeAsUint + YYSTraceStart_Addr);

g_CurrentEvent = reinterpret_cast<int*>(exeAsUint + Current_Event_Addr);
g_CurrentSubtype = reinterpret_cast<int*>(exeAsUint + Current_Subtype_Addr);

// remove this later
g_ConstNames = reinterpret_cast<const char***>(exeAsUint + ConstNames_Addr);
g_ConstNumb = reinterpret_cast<const int*>(exeAsUint + ConstNumb_Addr);
Expand All @@ -437,10 +441,8 @@ void lassebq_initYYC()
DeterminePotentialRoot = reinterpret_cast<DetPotRoot>(exeAsUint + DeterminePotRoot_Addr);
GetEventRecursive = reinterpret_cast<GetEvRecurs>(exeAsUint + GetEvRecursive_Addr);
InsertEvent = reinterpret_cast<InsertEv>(exeAsUint + InsertEvent_Addr);
Variable_GetValue_Direct = reinterpret_cast<VarGetValDirect>(exeAsUint + VarGetValueDirect_Addr);
Variable_SetValue_Direct = reinterpret_cast<VarSetValDirect>(exeAsUint + VarSetValueDirect_Addr);
FindRValueSlot = reinterpret_cast<FindRValSlot>(exeAsUint + FindRValueSlot_Addr);
g_BuiltinVars = reinterpret_cast<RVariableRoutine*>(exeAsUint + BuiltinVars_Addr);
lassebq_find_bvars();

for (int i = 0; i < *g_RFunctionTableLen; i++)
{
Expand Down
9 changes: 0 additions & 9 deletions libLassebq/libLassebq.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include <list>
#include <memory>

extern int* g_CurrentEvent;
extern int* g_CurrentSubtype;
extern int g_GMLScriptsSize;

// Extension func defines.
Expand Down Expand Up @@ -121,13 +119,6 @@ class YYObjectBase : public CInstanceBase {
virtual RValue& InternalGetYYVarRef(int index) = 0;
};

typedef RValue&(__thiscall *GetYYVarRef)(YYObjectBase* self, int _Index);
typedef bool(*VarGetValDirect)(YYObjectBase *inst, int var_ind, int array_ind, RValue *res);
typedef bool(*VarSetValDirect)(YYObjectBase *inst, int var_ind, int array_ind, RValue *val);
extern VarGetValDirect Variable_GetValue_Direct;
extern VarSetValDirect Variable_SetValue_Direct;
typedef int(*FindRValSlot)(YYObjectBase* object, const char* name);
extern FindRValSlot FindRValueSlot;
extern YYObjectBase** g_pGlobal;

struct YYRECT
Expand Down

0 comments on commit ed955d1

Please sign in to comment.