Skip to content

Commit 39bc786

Browse files
authored
Fix minor issues (#492)
1 parent 9ce091e commit 39bc786

File tree

9 files changed

+71
-65
lines changed

9 files changed

+71
-65
lines changed

cleo_plugins/DebugUtils/ScreenLog.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class ScreenLog
3030
CRGBA fontColor[4] = {
3131
// colors for eLogLevel
3232
CRGBA(0xDD, 0xDD, 0xDD, 0xFF), // None
33-
CRGBA(0xFF, 0x30, 0xEE, 0xFF), // Error
3433
CRGBA(0xFF, 0xEE, 0x30, 0xFF), // User
34+
CRGBA(0xFF, 0x30, 0xEE, 0xFF), // Error
3535
CRGBA(0xDD, 0xDD, 0xDD, 0xFF), // Default
3636
};
3737

cleo_plugins/Input/main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ class Input
5454
// refresh keys info
5555
void CheckKeyboard()
5656
{
57-
// skip if game window is not active
58-
if (GetForegroundWindow() != RsGlobal.ps->window) return;
59-
6057
std::swap(keyStatesCurr, keyStatesPrev);
61-
GetKeyboardState(keyStatesCurr->data());
58+
if (GetForegroundWindow() == RsGlobal.ps->window)
59+
GetKeyboardState(keyStatesCurr->data());
60+
else
61+
keyStatesCurr->fill(0); // no inputs if window inactive
6262
}
6363

6464
static void SendKeyEvent(BYTE vKey, bool down)

cleo_plugins/Math/Math.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ class Math
441441

442442
if (valueCount < 2) // value + result
443443
{
444-
SUSPEND("Insufficient number of arguments", "");
444+
SUSPEND("Insufficient number of arguments");
445445
}
446446
valueCount -= 1; // output param
447447

@@ -466,7 +466,7 @@ class Math
466466

467467
if (valueCount < 2) // value + result
468468
{
469-
SUSPEND("Insufficient number of arguments", "");
469+
SUSPEND("Insufficient number of arguments");
470470
}
471471
valueCount -= 1; // output param
472472

@@ -491,7 +491,7 @@ class Math
491491

492492
if (valueCount < 2) // value + result
493493
{
494-
SUSPEND("Insufficient number of arguments", "");
494+
SUSPEND("Insufficient number of arguments");
495495
}
496496
valueCount -= 1; // output param
497497

cleo_plugins/MemoryOperations/MemoryOperations.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ class MemoryOperations
671671
auto resultType = thread->PeekDataType();
672672
if (!IsVariable(resultType) && !IsVarString(resultType))
673673
{
674-
SUSPEND("Input argument expected to be a variable, got constant", "");
674+
SUSPEND("Input argument expected to be a variable, got constant");
675675
}
676676

677677
auto ptr = CLEO_GetPointerToScriptVariable(thread);

cleo_plugins/Text/ScriptDrawsState.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#pragma once
2+
#include "crc32.h"
3+
#include <CLEO_Utils.h>
24
#include <CTheScripts.h>
35
#include <CTxdStore.h>
4-
#include <CLEO_Utils.h>
5-
#include <CKeyGen.h>
66
#include <array>
77

88
struct ScriptDrawsState
@@ -22,7 +22,7 @@ struct ScriptDrawsState
2222

2323
ScriptDrawsState()
2424
{
25-
scriptTxd.m_hash = CKeyGen::GetUppercaseKey("script");
25+
scriptTxd.m_hash = crc32FromUpcaseString("script");
2626
scriptTxd.m_wParentIndex = -1;
2727
scriptTxd.m_wRefsCount = 0;
2828
scriptTxd.m_pRwDictionary = nullptr;

cleo_plugins/Text/Text.vcxproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ xcopy /Y "$(OutDir)$(TargetName).*" "$(GTA_SA_DIR)\cleo\cleo_plugins\"
126126
</ItemDefinitionGroup>
127127
<ItemGroup>
128128
<ClCompile Include="..\..\third-party\plugin-sdk\plugin_sa\game_sa\CMissionCleanup.cpp" />
129-
<ClCompile Include="..\..\third-party\plugin-sdk\plugin_sa\game_sa\CKeyGen.cpp" />
130129
<ClCompile Include="..\..\third-party\plugin-sdk\plugin_sa\game_sa\CRect.cpp">
131130
<PrecompiledHeader>NotUsing</PrecompiledHeader>
132131
<DisableSpecificWarnings>4073</DisableSpecificWarnings>

cleo_sdk/CLEO_Utils.h

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -96,42 +96,27 @@ namespace CLEO
9696
return (CLEO_GetConfigInt("StrictValidation", 0) == 1) && !IsLegacyScript(script);
9797
}
9898

99-
static std::string StringPrintf(const char* format, ...)
99+
static std::string StringPrintfV(const char* format, va_list args)
100100
{
101-
va_list args;
102-
103-
va_start(args, format);
104101
auto len = std::vsnprintf(nullptr, 0, format, args);
105-
va_end(args);
106102

107103
if (len <= 0) return {}; // empty or encoding error
108104

109105
std::string result;
110106
result.resize(len);
111107

112-
va_start(args, format);
113108
std::vsnprintf(result.data(), result.length() + 1, format, args);
114-
va_end(args);
115109

116110
return std::move(result);
117111
}
118112

119-
static void StringAppendPrintf(std::string& dest, const char* format, ...)
113+
static std::string StringPrintf(const char* format, ...)
120114
{
121115
va_list args;
122-
123116
va_start(args, format);
124-
auto len = std::vsnprintf(nullptr, 0, format, args);
125-
va_end(args);
126-
127-
if (len <= 0) return; // empty or encoding error
128-
129-
size_t oriSize = dest.size();
130-
dest.resize(dest.size() + len);
131-
132-
va_start(args, format);
133-
std::vsnprintf(dest.data() + oriSize, len + 1, format, args);
117+
auto result = StringPrintfV(format, args);
134118
va_end(args);
119+
return result;
135120
}
136121

137122
static void StringAppendNum(std::string& dest, int number, int padLen = 0)
@@ -574,6 +559,30 @@ namespace CLEO
574559
}
575560
}
576561

562+
static void ShowErrorSuspend(const std::string& scriptInfo, const char* format, ...)
563+
{
564+
va_list args;
565+
va_start(args, format);
566+
auto msg = StringPrintfV(format, args);
567+
va_end(args);
568+
569+
ShowError("%s in script %s\nScript suspended.", msg.c_str(), scriptInfo.c_str());
570+
}
571+
572+
static void ShowErrorSuspendCompat(const std::string& scriptInfo, const char* format, ...)
573+
{
574+
va_list args;
575+
va_start(args, format);
576+
auto msg = StringPrintfV(format, args);
577+
va_end(args);
578+
579+
ShowError(
580+
"%s in script %s\nScript suspended."
581+
"\n\nThis error can be ignored in legacy mode by changing script extension to '.cs4'",
582+
msg.c_str(), scriptInfo.c_str()
583+
);
584+
}
585+
577586
static bool PluginCheckCleoVersion()
578587
{
579588
auto ver = CLEO_GetVersion();
@@ -685,24 +694,17 @@ namespace CLEO
685694
CLEO::ShowError(a, __VA_ARGS__); \
686695
}
687696

688-
#define SHOW_ERROR_COMPAT(a, ...) \
689-
{ \
690-
CLEO::ShowError( \
691-
a "\n\nThis error can be ignored in legacy mode by changing script extension to '.cs4'", __VA_ARGS__ \
692-
); \
693-
}
694-
695-
#define SUSPEND(a, ...) \
697+
#define SUSPEND(...) \
696698
{ \
697-
SHOW_ERROR(a " in script %s\nScript suspended.", __VA_ARGS__, ScriptInfoStr(thread).c_str()); \
699+
CLEO::ShowErrorSuspend(ScriptInfoStr(thread), __VA_ARGS__); \
698700
return thread->Suspend(); \
699701
}
700702

701-
#define SUSPEND_COMPAT(a, ...) \
703+
#define SUSPEND_COMPAT(...) \
702704
{ \
703705
if (IsStrictValidation(thread)) \
704706
{ \
705-
SHOW_ERROR_COMPAT(a " in script %s\nScript suspended.", __VA_ARGS__, ScriptInfoStr(thread).c_str()); \
707+
CLEO::ShowErrorSuspendCompat(ScriptInfoStr(thread), __VA_ARGS__); \
706708
return thread->Suspend(); \
707709
} \
708710
}
@@ -1075,15 +1077,15 @@ namespace CLEO
10751077
char* _varName##Ok = CLEO_ReadParamsFormatted(thread, _buff_format_##_varName, _varName, sizeof(_varName)); \
10761078
if (_varName##Ok == nullptr) \
10771079
{ \
1078-
SUSPEND("Invalid formatted string", ""); \
1080+
SUSPEND("Invalid formatted string"); \
10791081
}
10801082

10811083
#define OPCODE_READ_PARAMS_FORMATTED(_format, _varName) \
10821084
char _varName[2 * MAX_STR_LEN + 1]; \
10831085
char* _varName##Ok = CLEO_ReadParamsFormatted(thread, _format, _varName, sizeof(_varName)); \
10841086
if (_varName##Ok == nullptr) \
10851087
{ \
1086-
SUSPEND("Invalid formatted string", ""); \
1088+
SUSPEND("Invalid formatted string"); \
10871089
}
10881090

10891091
#define OPCODE_READ_PARAM_FILEPATH(_varName) \

source/CConfigManager.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,17 @@ namespace CLEO
3333
if (value.empty()) return defaultValue;
3434

3535
char* end;
36-
const auto base = (value[0] == '0' && value[1] == 'x') ? 16 : 10;
37-
const auto result = strtol(value.c_str(), &end, base);
36+
int result;
37+
38+
if (value[0] == '0' && value[1] == 'x')
39+
{
40+
result = (int)strtoul(value.c_str(), &end, 16);
41+
}
42+
else
43+
{
44+
result = strtol(value.c_str(), &end, 10);
45+
}
46+
3847
return (end == value.c_str()) ? defaultValue : result;
3948
}
4049

source/CCustomOpcodeSystem.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,10 @@ namespace CLEO
4040
// check if last opcode ended correctly
4141
if (thread->bIsProcessing && !IsLegacyScript(thread))
4242
{
43-
SHOW_ERROR_COMPAT(
44-
"Unexpected opcode [%04X]!\n"
45-
"Called in script %s\n"
46-
"Some runtimes, e.g. SAMP, silently ignore script errors. Check the correctness of this script.",
47-
opcode, ScriptInfoStr(thread).c_str()
48-
)
43+
ShowErrorSuspendCompat(
44+
ScriptInfoStr(thread).c_str(),
45+
"Unexpected opcode [%04X], likely caused by an error that was silently ignored (e.g. in SAMP),", opcode
46+
);
4947
return OnOpcodeFinished(thread, thread->Suspend());
5048
}
5149

@@ -59,11 +57,9 @@ namespace CLEO
5957
if ((BYTE*)lastOpcodePtr == endPos ||
6058
(BYTE*)lastOpcodePtr == (endPos - 1)) // consider script can end with incomplete opcode
6159
{
62-
SHOW_ERROR_COMPAT(
63-
"Code execution past script end in script %s\n"
64-
"This usually happens when [004E] command is missing.\n"
65-
"Script suspended.",
66-
ScriptInfoStr(thread).c_str()
60+
ShowErrorSuspendCompat(
61+
ScriptInfoStr(thread).c_str(), "Script execution continued beyond its end, likely due to a missing "
62+
"TERMINATE_THIS_SCRIPT (004E) instruction"
6763
);
6864
return OnOpcodeFinished(thread, thread->Suspend());
6965
}
@@ -819,7 +815,7 @@ namespace CLEO
819815

820816
if (thread->SP == 0)
821817
{
822-
SUSPEND("`return` used without preceding `gosub` call", "");
818+
SUSPEND("`return` used without preceding `gosub` call");
823819
}
824820

825821
return CallNativeOpcode(thread, 0x0051); // call game's original
@@ -968,7 +964,7 @@ namespace CLEO
968964

969965
if (thread->SP == 0)
970966
{
971-
SUSPEND("`return_if_false` used without preceding `gosub` call", "");
967+
SUSPEND("`return_if_false` used without preceding `gosub` call");
972968
}
973969

974970
thread->SetIp(thread->PopStack());
@@ -1012,7 +1008,7 @@ namespace CLEO
10121008
}
10131009
else
10141010
{
1015-
SUSPEND("Invalid type of first argument in opcode [0AB1]", "");
1011+
SUSPEND("Invalid type of first argument in opcode [0AB1]");
10161012
}
10171013

10181014
ScmFunction* scmFunc = new ScmFunction(thread, callIP);
@@ -1150,7 +1146,7 @@ namespace CLEO
11501146
auto paramType = thread->PeekDataType();
11511147
if (!IsImmInteger(paramType))
11521148
{
1153-
SUSPEND("Invalid type of first argument in opcode [0AB2]", "");
1149+
SUSPEND("Invalid type of first argument in opcode [0AB2]");
11541150
}
11551151
DWORD declaredParamCount = CLEO_GetIntOpcodeParam(thread);
11561152

@@ -1225,7 +1221,7 @@ namespace CLEO
12251221
ScmFunction* scmFunc = ScmFunction::Get(cs->GetScmFunction());
12261222
if (scmFunc == nullptr)
12271223
{
1228-
SUSPEND("Quering argument count without preceding CLEO function call", "");
1224+
SUSPEND("Quering argument count without preceding CLEO function call");
12291225
}
12301226

12311227
OPCODE_WRITE_PARAM_INT(scmFunc->callArgCount);
@@ -1239,7 +1235,7 @@ namespace CLEO
12391235
auto argCount = OPCODE_PEEK_VARARG_COUNT();
12401236
if (argCount < 1)
12411237
{
1242-
SUSPEND("Opcode [2002] missing condition result argument", "");
1238+
SUSPEND("Opcode [2002] missing condition result argument");
12431239
}
12441240

12451241
auto result = OPCODE_READ_PARAM_BOOL();
@@ -1256,7 +1252,7 @@ namespace CLEO
12561252
auto argCount = OPCODE_PEEK_VARARG_COUNT();
12571253
if (argCount != 0) // argument(s) not supported yet
12581254
{
1259-
SUSPEND("Too many arguments in opcode [2003]", "");
1255+
SUSPEND("Too many arguments in opcode [2003]");
12601256
}
12611257

12621258
OPCODE_CONDITION_RESULT(false);

0 commit comments

Comments
 (0)