Skip to content

Commit

Permalink
Added is_null opcode
Browse files Browse the repository at this point in the history
  • Loading branch information
MiranDMC committed Apr 8, 2024
1 parent d6c4a88 commit eace1a6
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
- new opcode **2701 ([set_bit](https://library.sannybuilder.com/#/sa/math/2701))**
- new opcode **2702 ([clear_bit](https://library.sannybuilder.com/#/sa/math/2702))**
- new opcode **2703 ([toggle_bit](https://library.sannybuilder.com/#/sa/math/2703))**
- new opcode **2704 ([is_null](https://library.sannybuilder.com/#/sa/math/2704))**
- new [MemoryOperations](https://github.com/cleolibrary/CLEO5/tree/master/cleo_plugins/MemoryOperations) plugin
- memory related opcodes moved from CLEO core into separated plugin
- validation of input and output parameters for all opcodes
Expand Down
18 changes: 18 additions & 0 deletions cleo_plugins/Math/Math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Math
CLEO_RegisterOpcode(0x2701, opcode_2701); // set_bit
CLEO_RegisterOpcode(0x2702, opcode_2702); // clear_bit
CLEO_RegisterOpcode(0x2703, opcode_2703); // toggle_bit
CLEO_RegisterOpcode(0x2704, opcode_2704); // is_null
}

//0A8E=3,%3d% = %1d% + %2d% ; int
Expand Down Expand Up @@ -416,4 +417,21 @@ class Math

return OR_CONTINUE;
}

//2704=1, is_null value %1d%
static OpcodeResult WINAPI opcode_2704(CScriptThread* thread)
{
auto paramType = OPCODE_PEEK_PARAM_TYPE();

if(IsImmString(paramType) || IsVarString(paramType))
{
OPCODE_READ_PARAM_STRING_LEN(text, 1); // one character is all we need
OPCODE_CONDITION_RESULT(text[0] == '\0');
return OR_CONTINUE;
}

auto value = OPCODE_READ_PARAM_ANY32();
OPCODE_CONDITION_RESULT(value == 0);
return OR_CONTINUE;
}
} Math;
4 changes: 4 additions & 0 deletions cleo_plugins/Math/Math.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ xcopy /Y "$(OutDir)$(TargetName).*" "$(GTA_SA_DIR)\cleo\cleo_plugins\"
<ItemGroup>
<ClCompile Include="Math.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\cleo_sdk\CLEO.h" />
<ClInclude Include="..\..\cleo_sdk\CLEO_Utils.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
13 changes: 13 additions & 0 deletions cleo_plugins/Math/Math.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,17 @@
<ItemGroup>
<ClCompile Include="Math.cpp" />
</ItemGroup>
<ItemGroup>
<Filter Include="cleo_sdk">
<UniqueIdentifier>{20ddb375-f549-46bb-814d-53e534880d23}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\cleo_sdk\CLEO.h">
<Filter>cleo_sdk</Filter>
</ClInclude>
<ClInclude Include="..\..\cleo_sdk\CLEO_Utils.h">
<Filter>cleo_sdk</Filter>
</ClInclude>
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions cleo_sdk/CLEO_Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace CLEO
OPCODE_CONDITION_RESULT(value) // set result
OPCODE_SKIP_PARAMS(count) // ignore X params
OPCODE_PEEK_PARAM_TYPE() // get param type without advancing the script
// reading opcode input arguments
OPCODE_READ_PARAM_BOOL()
Expand Down Expand Up @@ -458,6 +460,7 @@ namespace CLEO
}

#define OPCODE_SKIP_PARAMS(_count) CLEO_SkipOpcodeParams(thread, _count)
#define OPCODE_PEEK_PARAM_TYPE() thread->PeekDataType()

// macros for reading opcode input params. Performs type validation, throws error and suspends script if user provided invalid argument type
// TOD: add range checks for limited size types?
Expand Down
104 changes: 104 additions & 0 deletions tests/cleo_tests/Math/2704.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{$CLEO .s}
{$INCLUDE_ONCE ../cleo_tester.inc}

script_name '2704'
test("2704 (is_null)", tests)
terminate_this_custom_script

function tests
it("should test null", test1)
it("should test NOT null", test2)
return

function test1
// int
2704: is_null {value} 0
assert_result_true()

0@ = 0
2704: is_null {value} 0@
assert_result_true()

// float
2704: is_null {value} 0.0
assert_result_true()

0@ = 0.0
2704: is_null {value} 0@
assert_result_true()

// short string
2704: is_null {value} ''
assert_result_true()

0@s = ''
2704: is_null {value} 0@s
assert_result_true()

// long string
2704: is_null {value} ""
assert_result_true()

0@v = ""
2704: is_null {value} 0@v
assert_result_true()
end

function test2
// int
2704: is_null {value} 1
assert_result_false()

0@ = 1
2704: is_null {value} 0@
assert_result_false()

2704: is_null {value} 0x00001000
assert_result_false()

2704: is_null {value} -1
assert_result_false()

2704: is_null {value} 1.0
assert_result_false()

// float
0@ = 0.0
2704: is_null {value} 0@
assert_result_true()

2704: is_null {value} 0.000001
assert_result_false()

// short string
2704: is_null {value} 'a'
assert_result_false()

0@s = 'a'
2704: is_null {value} 0@s
assert_result_false()

2704: is_null {value} ' '
assert_result_false()

2704: is_null {value} 'null'
assert_result_false()

// long string
2704: is_null {value} "a"
assert_result_true()

0@v = "a"
2704: is_null {value} 0@v
assert_result_true()

2704: is_null {value} " "
assert_result_false()

2704: is_null {value} "null"
assert_result_false()

2704: is_null {value} "some very long testing string"
assert_result_false()
end
end

0 comments on commit eace1a6

Please sign in to comment.