From 3fc4b36ed692336c59428d212b0eb6ec21365919 Mon Sep 17 00:00:00 2001 From: MiranDMC Date: Tue, 27 Feb 2024 02:50:08 +0100 Subject: [PATCH] add_dynamic_GXT_entry opcode bug fix (#73) * add_dynamic_GXT_entry opcode bug fix * fixup! add_dynamic_GXT_entry opcode bug fix --- cleo_sdk/CLEO.h | 2 +- cleo_sdk/CLEO_Utils.h | 2 +- source/CCustomOpcodeSystem.cpp | 6 +++--- source/CCustomOpcodeSystem.h | 2 +- source/CScriptEngine.h | 3 ++- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cleo_sdk/CLEO.h b/cleo_sdk/CLEO.h index 0d10c0d7..ec121020 100644 --- a/cleo_sdk/CLEO.h +++ b/cleo_sdk/CLEO.h @@ -490,7 +490,7 @@ SCRIPT_VAR* WINAPI CLEO_GetPointerToScriptVariable(CRunningScript* thread); // g void WINAPI CLEO_RetrieveOpcodeParams(CRunningScript* thread, int count); // read multiple params. Stored in opcodeParams array DWORD WINAPI CLEO_GetIntOpcodeParam(CRunningScript* thread); float WINAPI CLEO_GetFloatOpcodeParam(CRunningScript* thread); -LPSTR WINAPI CLEO_ReadStringOpcodeParam(CRunningScript* thread, char* buf = nullptr, int bufSize = 0); // returns nullptr on fail +LPSTR WINAPI CLEO_ReadStringOpcodeParam(CRunningScript* thread, char* buf = nullptr, int bufSize = 0); // returns null terminated string, nullptr on fail LPSTR WINAPI CLEO_ReadStringPointerOpcodeParam(CRunningScript* thread, char* buf = nullptr, int bufSize = 0); // exactly same as CLEO_ReadStringOpcodeParam void WINAPI CLEO_ReadStringParamWriteBuffer(CRunningScript* thread, char** outBuf, int* outBufSize, DWORD* outNeedsTerminator); // get info about the string opcode param, so it can be written latter. If outNeedsTerminator is not 0 then whole bufSize can be used as text characters. Advances script to next param char* WINAPI CLEO_ReadParamsFormatted(CRunningScript* thread, const char* format, char* buf = nullptr, int bufSize = 0); // consumes all var-arg params and terminator diff --git a/cleo_sdk/CLEO_Utils.h b/cleo_sdk/CLEO_Utils.h index 78a81036..a4198ded 100644 --- a/cleo_sdk/CLEO_Utils.h +++ b/cleo_sdk/CLEO_Utils.h @@ -32,7 +32,7 @@ namespace CLEO OPCODE_READ_PARAM_UINT() OPCODE_READ_PARAM_FLOAT() OPCODE_READ_PARAM_STRING() // returns char* to internal buffer. It might be overwritten by another string read! - OPCODE_READ_PARAM_STRING_BUFF(_buffer, _bufferSize) + OPCODE_READ_PARAM_STRING_BUFF(_buffer, _bufferSize) // always null terminated OPCODE_READ_PARAM_FILEPATH() // returns char* to internal buffer. It might be overwritten by another string read! OPCODE_READ_PARAM_PTR() // read and validate memory address argument OPCODE_READ_PARAM_OBJECT_HANDLE() diff --git a/source/CCustomOpcodeSystem.cpp b/source/CCustomOpcodeSystem.cpp index 3954ea45..ccaba9d7 100644 --- a/source/CCustomOpcodeSystem.cpp +++ b/source/CCustomOpcodeSystem.cpp @@ -399,7 +399,7 @@ namespace CLEO return thread; } - // read string parameter according to convention on strings + // read string parameter according to convention on strings. Always null terminated char* ReadStringParam(CRunningScript *thread, char* buf, DWORD bufSize) { static char internal_buf[MAX_STR_LEN]; @@ -1551,8 +1551,8 @@ namespace CLEO //0ADF=2,add_dynamic_GXT_entry %1d% text %2d% OpcodeResult __stdcall opcode_0ADF(CRunningScript *thread) { - char gxtLabel[8] = { 0 }; // 7 + terminator character - auto gxt = OPCODE_READ_PARAM_STRING_BUFF(gxtLabel, 7); + char gxtBuff[8]; // 7 + terminator character + auto gxt = OPCODE_READ_PARAM_STRING_BUFF(gxtBuff, sizeof(gxtBuff)); auto txt = OPCODE_READ_PARAM_STRING(); GetInstance().TextManager.AddFxt(gxt, txt); diff --git a/source/CCustomOpcodeSystem.h b/source/CCustomOpcodeSystem.h index 8e56a616..705fdb11 100644 --- a/source/CCustomOpcodeSystem.h +++ b/source/CCustomOpcodeSystem.h @@ -67,7 +67,7 @@ namespace CLEO bool needTerminator = false; }; - char* ReadStringParam(CRunningScript* thread, char* buf = nullptr, DWORD bufSize = 0); + char* ReadStringParam(CRunningScript* thread, char* buf = nullptr, DWORD bufSize = 0); // null terminated StringParamBufferInfo GetStringParamWriteBuffer(CRunningScript* thread); // consumes the param int ReadFormattedString(CRunningScript* thread, char* buf, DWORD bufSize, const char* format); diff --git a/source/CScriptEngine.h b/source/CScriptEngine.h index 6364440b..05df03b3 100644 --- a/source/CScriptEngine.h +++ b/source/CScriptEngine.h @@ -156,7 +156,8 @@ namespace CLEO extern SCRIPT_VAR * (__thiscall * GetScriptParamPointer1)(CRunningScript *); extern SCRIPT_VAR * (__thiscall * GetScriptParamPointer2)(CRunningScript *, int __unused__); - char* __fastcall GetScriptStringParam(CRunningScript* thread, int dummy, char* buff, int buffLen); + // reimplemented hook of original game's procedure. Null terminator ommited if not enought space in the buffer! + char* __fastcall GetScriptStringParam(CRunningScript* thread, int dummy, char* buff, int buffLen); inline SCRIPT_VAR * GetScriptParamPointer(CRunningScript *thread) {