1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-10-19 01:17:18 +02:00

42 Commits

Author SHA1 Message Date
Sandu Liviu Catalin
8d15f4b6e9 Add a basic ID pool.
Some checks reported errors
continuous-integration/drone/push Build is pending
continuous-integration/drone Build is passing
continuous-integration/drone/tag Build was killed
2023-03-05 19:51:09 +02:00
Sandu Liviu Catalin
c0d142ab34 Update sqmod.h
Some checks reported errors
continuous-integration/drone/push Build was killed
2023-03-05 17:12:29 +02:00
Sandu Liviu Catalin
f77ec9482f Update Main.cpp 2023-03-05 17:11:38 +02:00
Sandu Liviu Catalin
4fc1e892f7 Update Core.cpp 2023-03-05 17:11:36 +02:00
Sandu Liviu Catalin
015047a935 Update Core.cpp 2023-03-05 17:00:39 +02:00
Sandu Liviu Catalin
bc1fc1d245 Allow external native plug-ins to interact wit the script.
Very primitive mechanism but better than nothing.
2023-03-05 15:38:59 +02:00
Sandu Liviu Catalin
2a069f3040 Update CMakeLists.txt 2023-03-05 15:37:30 +02:00
Sandu Liviu Catalin
d295828545 Add GetJSON method to buffer. 2023-03-05 15:37:24 +02:00
Sandu Liviu Catalin
483ac37bdb Update CMakeLists.txt
Some checks reported errors
continuous-integration/drone/push Build was killed
2022-10-04 02:29:05 +03:00
Sandu Liviu Catalin
78dc76e6b4 Object to typename helper. 2022-10-03 18:32:06 +03:00
Sandu Liviu Catalin
e29070af49 Update Official.cpp 2022-10-03 18:31:48 +03:00
Sandu Liviu Catalin
1f25b3ea60 Update sqratUtil.h 2022-10-03 18:31:36 +03:00
Sandu Liviu Catalin
52cfa235be Update JSON.hpp
Some checks reported errors
continuous-integration/drone/push Build was killed
2022-10-02 21:47:20 +03:00
Sandu Liviu Catalin
ec7f1183d8 Basic hooking into json parsing.
Also allow customization of the fallback meta-method.
2022-10-02 21:24:12 +03:00
Sandu Liviu Catalin
a788e059a5 Implement _tojson meta-method for base types. 2022-09-18 15:02:39 +03:00
Sandu Liviu Catalin
5f20ffc4de Remove the manual comma. 2022-09-17 23:49:37 +03:00
Sandu Liviu Catalin
08106156c4 Don't add comma for the root container. 2022-09-17 23:42:48 +03:00
Sandu Liviu Catalin
60119ff8fa Include comma after separator braces. 2022-09-17 23:41:43 +03:00
Sandu Liviu Catalin
f4720ae77a Implement custom types in JSON. 2022-09-17 23:25:16 +03:00
Sandu Liviu Catalin
c9fb257f48 Update ThreadPool.hpp 2022-09-15 23:03:50 +03:00
Sandu Liviu Catalin
475a428366 Update Data.hpp 2022-09-15 23:03:44 +03:00
Sandu Liviu Catalin
42ac0e32b7 Implement JSON serialization.
Just the basics.
2022-09-15 23:03:38 +03:00
Sandu Liviu Catalin
3e75e36cdf Add auto-close option for web-socket client.
Some checks reported errors
continuous-integration/drone/push Build was killed
2022-08-14 21:00:57 +03:00
Sandu Liviu Catalin
ac7d18f297 Allow chaining in traversal methods for buffer type. 2022-08-14 21:00:30 +03:00
Sandu Liviu Catalin
39473a68f4 Fix formatting in buffer type. 2022-08-14 21:00:10 +03:00
Sandu Liviu Catalin
5a57bf2fbf Prevent double closing event in web-sockets.
Some checks reported errors
continuous-integration/drone/push Build was killed
2022-08-14 20:29:32 +03:00
Sandu Liviu Catalin
15e85f1394 Update Net.hpp 2022-08-14 20:11:53 +03:00
Sandu Liviu Catalin
e8fa9e0259 Update sqratLightObj.h 2022-08-14 18:53:36 +03:00
Sandu Liviu Catalin
c551390999 Helper Base32/64 methods on SqBufer type. 2022-08-14 18:53:28 +03:00
Sandu Liviu Catalin
804a5abb29 Update Official.cpp
Some checks reported errors
continuous-integration/drone/push Build was killed
2022-07-23 23:17:31 +03:00
Sandu Liviu Catalin
68551e4466 Allow either player type for announcing in legacy mode. 2022-07-23 23:16:53 +03:00
Sandu Liviu Catalin
4618577ae4 Implement .Legacy member for native entity types. 2022-07-23 22:09:30 +03:00
Sandu Liviu Catalin
2f428962c8 Update Official.cpp 2022-07-23 21:55:46 +03:00
Sandu Liviu Catalin
34a78dc166 More work on async statements for SQLite. 2022-07-23 21:41:10 +03:00
Sandu Liviu Catalin
49df7b75ee Asynchronous statements implementation for SQLite.
Extend worker threads to allow tasks to re-queue themselves during completion.
2022-07-23 19:27:40 +03:00
Sandu Liviu Catalin
0d927f5d72 Remove log. 2022-07-23 17:50:58 +03:00
Sandu Liviu Catalin
44243aadd2 Fix removal of non alpha-numeric characters from strings. 2022-07-23 17:42:01 +03:00
Sandu Liviu Catalin
aa3952fd45 Update SqBase.hpp
Some checks reported errors
continuous-integration/drone/push Build was killed
2022-07-21 23:45:09 +03:00
Sandu Liviu Catalin
ebe60ebf4b Update Data.cpp
Some checks reported errors
continuous-integration/drone/push Build was killed
2022-07-19 20:03:00 +03:00
Sandu Liviu Catalin
fa9c3a5821 Check for a valid session pointer. 2022-07-19 19:43:30 +03:00
Sandu Liviu Catalin
f238588abe More work on async sql.
Current implementation only provides execution for sqlite queries in worker threads. Statements and MySQL is on the TODO list.
2022-07-19 19:29:32 +03:00
Sandu Liviu Catalin
39524098f1 Extend worker pool.
Task exceptions are caught and they can provide custom information to identify the task that failed.
Take ownership of the task instance sooner.
2022-07-19 19:28:13 +03:00
67 changed files with 3359 additions and 383 deletions

View File

@@ -3,7 +3,7 @@ project(SqMod)
# This plug-in only works on 64-bit
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
message(FATAL_ERROR "SqMod does not support 32-but platforms anymore.")
message(FATAL_ERROR "SqMod does not support 32-bit platforms anymore.")
endif()
# Tell CMake where to find our scripts
@@ -18,7 +18,7 @@ option(ENABLE_BUILTIN_MYSQL_C "Enable built-in MySQL connector library" OFF)
#option(FORCE_32BIT_BIN "Create a 32-bit executable binary if the compiler defaults to 64-bit." OFF)
# This option should only be available in certain conditions
if(WIN32 AND MINGW)
option(COPY_DEPENDENCIES "Copy deppendent DLLs into the deps folder." OFF)
option(COPY_DEPENDENCIES "Copy dependent DLLs into the deps folder." OFF)
endif()
# C++14 is mandatory

View File

@@ -4,6 +4,7 @@
#include "Base/DynArg.hpp"
#include "Core/Buffer.hpp"
#include "Core/Utility.hpp"
#include "Library/JSON.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@@ -300,6 +301,21 @@ String AABB::ToString() const
return fmt::format("{},{},{},{},{},{}", min.x, min.y, min.z, max.x, max.y, max.z);
}
// ------------------------------------------------------------------------------------------------
void AABB::ToJSON(CtxJSON & ctx) const
{
if (ctx.mObjectOverArray)
{
fmt::format_to(std::back_inserter(ctx.mOutput), "{{min:{{x:{},y:{},z:{}}},max:{{x:{},y:{},z:{}}},",
min.x, min.y, min.z, max.x, max.y, max.z);
}
else
{
fmt::format_to(std::back_inserter(ctx.mOutput), "{{min:[{},{},{}],max:[{},{},{}]}},",
min.x, min.y, min.z, max.x, max.y, max.z);
}
}
// ------------------------------------------------------------------------------------------------
void AABB::SetStr(SQChar delim, StackStrF & values)
{
@@ -793,6 +809,7 @@ void Register_AABB(HSQUIRRELVM vm)
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &AABB::ToString)
.Func(_SC("_tojson"), &AABB::ToJSON)
// Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >)

View File

@@ -304,6 +304,11 @@ struct AABB
*/
SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a JSON string.
*/
void ToJSON(CtxJSON & ctx) const;
/* --------------------------------------------------------------------------------------------
* Set the values extracted from the specified string using the specified delimiter.
*/

View File

@@ -3,6 +3,7 @@
#include "Base/DynArg.hpp"
#include "Core/Buffer.hpp"
#include "Core/Utility.hpp"
#include "Library/JSON.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
@@ -360,6 +361,19 @@ String Circle::ToString() const
return fmt::format("{},{},{}", pos.x, pos.y, rad);
}
// ------------------------------------------------------------------------------------------------
void Circle::ToJSON(CtxJSON & ctx) const
{
if (ctx.mObjectOverArray)
{
fmt::format_to(std::back_inserter(ctx.mOutput), "{{x:{},y:{},r:{}}},", pos.x, pos.y, rad);
}
else
{
fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{},{}],", pos.x, pos.y, rad);
}
}
// ------------------------------------------------------------------------------------------------
void Circle::SetRadius(Value nr)
{
@@ -552,6 +566,7 @@ void Register_Circle(HSQUIRRELVM vm)
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Circle >, SQFloat, SQInteger, bool, std::nullptr_t, Circle >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Circle::ToString)
.Func(_SC("_toJSON"), &Circle::ToJSON)
// Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Circle >, SQFloat, SQInteger, bool, std::nullptr_t, Circle >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Circle >, SQFloat, SQInteger, bool, std::nullptr_t, Circle >)

View File

@@ -341,6 +341,11 @@ struct Circle
*/
SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a JSON string.
*/
void ToJSON(CtxJSON & ctx) const;
/* --------------------------------------------------------------------------------------------
* Set the specified radius.
*/

View File

@@ -4,6 +4,7 @@
#include "Base/DynArg.hpp"
#include "Core/Buffer.hpp"
#include "Core/Utility.hpp"
#include "Library/JSON.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
@@ -481,6 +482,19 @@ String Color3::ToString() const
return fmt::format("{},{},{}", r, g, b);
}
// ------------------------------------------------------------------------------------------------
void Color3::ToJSON(CtxJSON & ctx) const
{
if (ctx.mObjectOverArray)
{
fmt::format_to(std::back_inserter(ctx.mOutput), "{{r:{},g:{},b:{}}},", r, g, b);
}
else
{
fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{},{}],", r, g, b);
}
}
// ------------------------------------------------------------------------------------------------
void Color3::SetScalar(Value ns)
{
@@ -741,6 +755,7 @@ void Register_Color3(HSQUIRRELVM vm)
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Color3 >, SQFloat, SQInteger, bool, std::nullptr_t, Color3 >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Color3::ToString)
.Func(_SC("_tojson"), &Color3::ToJSON)
// Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Color3 >, SQFloat, SQInteger, bool, std::nullptr_t, Color3 >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Color3 >, SQFloat, SQInteger, bool, std::nullptr_t, Color3 >)

View File

@@ -405,6 +405,11 @@ struct Color3
*/
SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a JSON string.
*/
void ToJSON(CtxJSON & ctx) const;
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/

View File

@@ -4,6 +4,7 @@
#include "Base/DynArg.hpp"
#include "Core/Buffer.hpp"
#include "Core/Utility.hpp"
#include "Library/JSON.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
@@ -506,6 +507,19 @@ String Color4::ToString() const
return fmt::format("{},{},{},{}", r, g, b, a);
}
// ------------------------------------------------------------------------------------------------
void Color4::ToJSON(CtxJSON & ctx) const
{
if (ctx.mObjectOverArray)
{
fmt::format_to(std::back_inserter(ctx.mOutput), "{{r:{},g:{},b:{},a:{}}},", r, g, b, a);
}
else
{
fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{},{},{}],", r, g, b, a);
}
}
// ------------------------------------------------------------------------------------------------
void Color4::SetScalar(Value ns)
{
@@ -781,6 +795,7 @@ void Register_Color4(HSQUIRRELVM vm)
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Color4 >, SQFloat, SQInteger, bool, std::nullptr_t, Color4 >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Color4::ToString)
.Func(_SC("_tojson"), &Color4::ToJSON)
// Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Color4 >, SQFloat, SQInteger, bool, std::nullptr_t, Color4 >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Color4 >, SQFloat, SQInteger, bool, std::nullptr_t, Color4 >)

View File

@@ -405,6 +405,11 @@ struct Color4
*/
SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a JSON string.
*/
void ToJSON(CtxJSON & ctx) const;
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/

View File

@@ -5,6 +5,7 @@
#include "Base/DynArg.hpp"
#include "Core/Buffer.hpp"
#include "Core/Utility.hpp"
#include "Library/JSON.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
@@ -349,6 +350,19 @@ String Quaternion::ToString() const
return fmt::format("{},{},{},{}", x, y, z, w);
}
// ------------------------------------------------------------------------------------------------
void Quaternion::ToJSON(CtxJSON & ctx) const
{
if (ctx.mObjectOverArray)
{
fmt::format_to(std::back_inserter(ctx.mOutput), "{{x:{},y:{},z:{},w:{}}},", x, y, z, w);
}
else
{
fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{},{},{}],", x, y, z, w);
}
}
// ------------------------------------------------------------------------------------------------
void Quaternion::SetScalar(Value ns)
{
@@ -775,6 +789,7 @@ void Register_Quaternion(HSQUIRRELVM vm)
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Quaternion >, SQFloat, SQInteger, bool, std::nullptr_t, Quaternion >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Quaternion::ToString)
.Func(_SC("_tojson"), &Quaternion::ToJSON)
// Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Quaternion >, SQFloat, SQInteger, bool, std::nullptr_t, Quaternion >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Quaternion >, SQFloat, SQInteger, bool, std::nullptr_t, Quaternion >)

View File

@@ -296,6 +296,11 @@ struct Quaternion
*/
SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a JSON string.
*/
void ToJSON(CtxJSON & ctx) const;
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/

View File

@@ -6,6 +6,9 @@
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
struct CtxJSON;
/* ------------------------------------------------------------------------------------------------
* Helper constants used by the bas types.
*/

View File

@@ -3,6 +3,7 @@
#include "Base/DynArg.hpp"
#include "Core/Buffer.hpp"
#include "Core/Utility.hpp"
#include "Library/JSON.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
@@ -360,6 +361,19 @@ String Sphere::ToString() const
return fmt::format("{},{},{},{}", pos.x, pos.y, pos.z, rad);
}
// ------------------------------------------------------------------------------------------------
void Sphere::ToJSON(CtxJSON & ctx) const
{
if (ctx.mObjectOverArray)
{
fmt::format_to(std::back_inserter(ctx.mOutput), "{{x:{},y:{},z:{},r:{}}},", pos.x, pos.y, pos.z, rad);
}
else
{
fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{},{},{}],", pos.x, pos.y, pos.z, rad);
}
}
// ------------------------------------------------------------------------------------------------
void Sphere::SetRadius(Value nr)
{
@@ -527,6 +541,7 @@ void Register_Sphere(HSQUIRRELVM vm)
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Sphere >, SQFloat, SQInteger, bool, std::nullptr_t, Sphere >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Sphere::ToString)
.Func(_SC("_tojson"), &Sphere::ToJSON)
// Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Sphere >, SQFloat, SQInteger, bool, std::nullptr_t, Sphere >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Sphere >, SQFloat, SQInteger, bool, std::nullptr_t, Sphere >)

View File

@@ -341,6 +341,11 @@ struct Sphere
*/
SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a JSON string.
*/
void ToJSON(CtxJSON & ctx) const;
/* --------------------------------------------------------------------------------------------
* Set the specified radius.
*/

View File

@@ -4,6 +4,7 @@
#include "Base/DynArg.hpp"
#include "Core/Buffer.hpp"
#include "Core/Utility.hpp"
#include "Library/JSON.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
@@ -295,6 +296,19 @@ String Vector2::ToString() const
return fmt::format("{},{}", x, y);
}
// ------------------------------------------------------------------------------------------------
void Vector2::ToJSON(CtxJSON & ctx) const
{
if (ctx.mObjectOverArray)
{
fmt::format_to(std::back_inserter(ctx.mOutput), "{{x:{},y:{}}},", x, y);
}
else
{
fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{}],", x, y);
}
}
// ------------------------------------------------------------------------------------------------
void Vector2::SetScalar(Value ns)
{
@@ -428,6 +442,7 @@ void Register_Vector2(HSQUIRRELVM vm)
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Vector2 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2 >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Vector2::ToString)
.Func(_SC("_tojson"), &Vector2::ToJSON)
// Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Vector2 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2 >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Vector2 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2 >)

View File

@@ -285,6 +285,11 @@ struct Vector2
*/
SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a JSON string.
*/
void ToJSON(CtxJSON & ctx) const;
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/

View File

@@ -4,6 +4,7 @@
#include "Base/DynArg.hpp"
#include "Core/Buffer.hpp"
#include "Core/Utility.hpp"
#include "Library/JSON.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
@@ -441,6 +442,19 @@ String Vector2i::ToString() const
return fmt::format("{},{}", x, y);
}
// ------------------------------------------------------------------------------------------------
void Vector2i::ToJSON(CtxJSON & ctx) const
{
if (ctx.mObjectOverArray)
{
fmt::format_to(std::back_inserter(ctx.mOutput), "{{x:{},y:{}}},", x, y);
}
else
{
fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{}],", x, y);
}
}
// ------------------------------------------------------------------------------------------------
void Vector2i::SetScalar(Value ns)
{
@@ -574,6 +588,7 @@ void Register_Vector2i(HSQUIRRELVM vm)
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Vector2i >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2i >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Vector2i::ToString)
.Func(_SC("_tojson"), &Vector2i::ToJSON)
// Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Vector2i >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2i >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Vector2i >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2i >)

View File

@@ -390,6 +390,11 @@ struct Vector2i
*/
SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a JSON string.
*/
void ToJSON(CtxJSON & ctx) const;
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/

View File

@@ -5,6 +5,7 @@
#include "Base/DynArg.hpp"
#include "Core/Buffer.hpp"
#include "Core/Utility.hpp"
#include "Library/JSON.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
@@ -331,6 +332,19 @@ String Vector3::ToString() const
return fmt::format("{},{},{}", x, y, z);
}
// ------------------------------------------------------------------------------------------------
void Vector3::ToJSON(CtxJSON & ctx) const
{
if (ctx.mObjectOverArray)
{
fmt::format_to(std::back_inserter(ctx.mOutput), "{{x:{},y:{},z:{}}},", x, y, z);
}
else
{
fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{},{}],", x, y, z);
}
}
// ------------------------------------------------------------------------------------------------
void Vector3::SetScalar(Value ns)
{
@@ -696,6 +710,7 @@ void Register_Vector3(HSQUIRRELVM vm)
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Vector3 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector3 >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Vector3::ToString)
.Func(_SC("_tojson"), &Vector3::ToJSON)
// Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Vector3 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector3 >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Vector3 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector3 >)

View File

@@ -297,6 +297,11 @@ struct Vector3
*/
SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a JSON string.
*/
void ToJSON(CtxJSON & ctx) const;
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/

View File

@@ -5,6 +5,7 @@
#include "Base/DynArg.hpp"
#include "Core/Buffer.hpp"
#include "Core/Utility.hpp"
#include "Library/JSON.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
@@ -344,6 +345,19 @@ String Vector4::ToString() const
return fmt::format("{},{},{},{}", x, y, z, w);
}
// ------------------------------------------------------------------------------------------------
void Vector4::ToJSON(CtxJSON & ctx) const
{
if (ctx.mObjectOverArray)
{
fmt::format_to(std::back_inserter(ctx.mOutput), "{{x:{},y:{},z:{},w:{}}},", x, y, z, w);
}
else
{
fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{},{},{}],", x, y, z, w);
}
}
// ------------------------------------------------------------------------------------------------
void Vector4::SetScalar(Value ns)
{
@@ -526,6 +540,7 @@ void Register_Vector4(HSQUIRRELVM vm)
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Vector4 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector4 >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Vector4::ToString)
.Func(_SC("_tojson"), &Vector4::ToJSON)
// Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Vector4 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector4 >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Vector4 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector4 >)

View File

@@ -295,6 +295,11 @@ struct Vector4
*/
SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a JSON string.
*/
void ToJSON(CtxJSON & ctx) const;
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/

View File

@@ -1,5 +1,7 @@
# Create the Squirrel module
add_library(SqModule MODULE SqBase.hpp Main.cpp
# SDK
SDK/sqmod.h
# VCMP
VCMP/vcmp.h
VCMP/vcmp20.h
@@ -114,6 +116,7 @@ add_library(SqModule MODULE SqBase.hpp Main.cpp
Core.cpp Core.hpp
Logger.cpp Logger.hpp
Register.cpp
Exports.cpp
)
# Various definitions required by the plug-in
target_compile_definitions(SqModule PRIVATE SCRAT_USE_EXCEPTIONS=1)
@@ -201,6 +204,7 @@ else(WIN32)
endif(WIN32)
# Include current directory in the search path
target_include_directories(SqModule PRIVATE ${CMAKE_CURRENT_LIST_DIR})
target_include_directories(SqModule PRIVATE ${CMAKE_CURRENT_LIST_DIR}/SDK)
target_include_directories(SqModule PRIVATE ${CMAKE_CURRENT_LIST_DIR}/VCMP)
target_include_directories(SqModule PRIVATE ${CMAKE_CURRENT_LIST_DIR}/Sqrat)
# Include PCRE directory in the header search path
@@ -222,12 +226,12 @@ if(WIN32 AND MINGW AND COPY_DEPENDENCIES)
endif()
# Make sure the deps folder exists
file(MAKE_DIRECTORY "${PROJECT_SOURCE_DIR}/bin/deps")
# Copy dependencies into the plug-ins folder (only so it can be distributed with the DLL)
# Copy dependencies into the deps folder (only so it can be distributed with the DLL)
file(COPY "${MINGW_BIN_PATH}/zlib1.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
file(COPY "${MINGW_BIN_PATH}/libpq.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
file(COPY "${MINGW_BIN_PATH}/libzstd.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
file(COPY "${MINGW_BIN_PATH}/libpsl-5.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
file(COPY "${MINGW_BIN_PATH}/libffi-7.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
file(COPY "${MINGW_BIN_PATH}/libffi-8.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
file(COPY "${MINGW_BIN_PATH}/libcurl-4.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
file(COPY "${MINGW_BIN_PATH}/libssh2-1.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
file(COPY "${MINGW_BIN_PATH}/libidn2-0.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
@@ -245,17 +249,17 @@ if(WIN32 AND MINGW AND COPY_DEPENDENCIES)
file(COPY "${MINGW_BIN_PATH}/libp11-kit-0.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
file(COPY "${MINGW_BIN_PATH}/libbrotlidec.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
file(COPY "${MINGW_BIN_PATH}/libbrotlicommon.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
file(COPY "${MINGW_BIN_PATH}/libunistring-2.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
file(COPY "${MINGW_BIN_PATH}/libunistring-5.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
file(COPY "${MINGW_BIN_PATH}/libnghttp2-14.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
file(COPY "${MINGW_BIN_PATH}/libwinpthread-1.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
file(COPY "${MINGW_BIN_PATH}/libstdc++-6.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
if(CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT FORCE_32BIT_BIN)
file(COPY "${MINGW_BIN_PATH}/libgcc_s_seh-1.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
file(COPY "${MINGW_BIN_PATH}/libssl-1_1-x64.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
file(COPY "${MINGW_BIN_PATH}/libcrypto-1_1-x64.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
file(COPY "${MINGW_BIN_PATH}/libssl-3-x64.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
file(COPY "${MINGW_BIN_PATH}/libcrypto-3-x64.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
else()
file(COPY "${MINGW_BIN_PATH}/libssl-1_1.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
file(COPY "${MINGW_BIN_PATH}/libcrypto-1_1.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
file(COPY "${MINGW_BIN_PATH}/libssl-3.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
file(COPY "${MINGW_BIN_PATH}/libcrypto-3.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")
endif()
if(POCO_UNBUNDLED)
file(COPY "${MINGW_BIN_PATH}/libexpat-1.dll" DESTINATION "${PROJECT_SOURCE_DIR}/bin/deps")

View File

@@ -161,6 +161,7 @@ Core::Core() noexcept
, m_Scripts()
, m_PendingScripts()
, m_Options()
, m_ExtCommands{nullptr, nullptr, nullptr, nullptr}
, m_Blips()
, m_Checkpoints()
, m_KeyBinds()
@@ -441,9 +442,9 @@ bool Core::Execute()
m_LockPostLoadSignal = false;
m_LockUnloadSignal = false;
//cLogDbg(m_Verbosity >= 1, "Signaling outside plug-ins to register their API");
cLogDbg(m_Verbosity >= 1, "Signaling outside plug-ins that the API is being registered");
// Tell modules to do their monkey business
//_Func->SendPluginCommand(0xDEADBABE, "");
_Func->SendPluginCommand(SQMOD_LOAD_CMD, SQMOD_HOST_NAME);
// Load pending scripts while we're in the bounds of the allowed recursiveness
for (unsigned levels = 0; !m_PendingScripts.empty() && (levels < 8); ++levels)
@@ -511,9 +512,9 @@ void Core::Terminate(bool shutdown)
// Clear the callbacks
ResetSignalPair(mOnUnload);
//cLogDbg(m_Verbosity >= 1, "Signaling outside plug-ins to release their resources");
cLogDbg(m_Verbosity >= 1, "Signaling outside plug-ins to release their resources");
// Tell modules to do their monkey business
//_Func->SendPluginCommand(0xDEADC0DE, "");
_Func->SendPluginCommand(SQMOD_TERMINATE_CMD, SQMOD_HOST_NAME);
}
cLogDbg(m_Verbosity >= 1, "Clearing the entity containers");
@@ -596,9 +597,9 @@ void Core::Terminate(bool shutdown)
HSQUIRRELVM sq_vm = m_VM;
m_VM = nullptr;
//cLogDbg(m_Verbosity >= 1, "Signaling outside plug-ins the virtual machine is closing");
cLogDbg(m_Verbosity >= 1, "Signaling outside plug-ins the virtual machine is closing");
// Tell modules to do their monkey business
//_Func->SendPluginCommand(0xBAAAAAAD, "");
_Func->SendPluginCommand(SQMOD_CLOSING_CMD, SQMOD_HOST_NAME);
// Release any callbacks from the logger
Logger::Get().Release();
cLogDbg(m_Verbosity >= 2, "Closing Virtual Machine");
@@ -629,9 +630,9 @@ void Core::Terminate(bool shutdown)
// Destroy the VM context, if any
delete ctx;
//cLogDbg(m_Verbosity >= 1, "Signaling outside plug-ins to release the virtual machine");
cLogDbg(m_Verbosity >= 1, "Signaling outside plug-ins the virtual machine was closed");
// Tell modules to do their monkey business
//_Func->SendPluginCommand(0xDEADBEAF, "");
_Func->SendPluginCommand(SQMOD_RELEASED_CMD, SQMOD_HOST_NAME);
}
OutputMessage("Squirrel plug-in was successfully terminated");
@@ -904,6 +905,81 @@ String Core::FetchCodeLine(const SQChar * src, SQInteger line, bool trim)
return script->FetchLine(line, trim);
}
// ------------------------------------------------------------------------------------------------
int32_t Core::RegisterExtCommand(ExtPluginCommand_t fn)
{
ExtPluginCommand_t * slot = nullptr;
// Find a free slot or matching function pointer in the pool
for (size_t i = 0; i < m_ExtCommands.max_size(); ++i)
{
// Is this slot available and are we still looking for a slot?
if (m_ExtCommands[i] == nullptr && slot == nullptr)
{
slot = &m_ExtCommands[i]; // Found a slot
}
// We keep looking for duplicates even if we found the slot
else if (m_ExtCommands[i] == fn)
{
return 0; // Already registered
}
}
// Do we have a free slot?
if (slot != nullptr)
{
*slot = fn; // Use this slot
return 1; // Successfully registered
}
// No space in the pool
return -1;
}
// ------------------------------------------------------------------------------------------------
int32_t Core::UnregisterExtCommand(ExtPluginCommand_t fn)
{
// Find the matching function pointer
for (size_t i = 0; i < m_ExtCommands.max_size(); ++i)
{
// Is this the same pointer?
if (m_ExtCommands[i] != nullptr && m_ExtCommands[i] == fn)
{
// Forget about it
m_ExtCommands[i] = nullptr;
return 1; // Successfully unregistered
}
}
// No space
return -1;
}
// ------------------------------------------------------------------------------------------------
int32_t Core::SendExtCommand(int32_t target, int32_t req, int32_t tag, const uint8_t * data, size_t size)
{
int32_t count = 0;
// Send the command to all registered function pointers
for (size_t i = 0; i < m_ExtCommands.max_size(); ++i)
{
if (m_ExtCommands[i] != nullptr)
{
const int32_t r = m_ExtCommands[i](target, req, tag, data, size);
// Command processed
++count;
// Command failed?
if (r < 0)
{
LogErr("External command failed (%i): target(%i), req(%i), tag(%i), data(%p), size (%zu)",
r, target, req, tag, data, size);
}
// Command consumed?
else if (r > 0)
{
break; // This function pointer requested exclusive access over this command
}
}
}
// Return how many function pointers received this command
return count;
}
// ------------------------------------------------------------------------------------------------
bool Core::DoScripts(Scripts::iterator itr, Scripts::iterator end)
{
@@ -2371,6 +2447,8 @@ void Core::InitEvents()
InitSignalPair(mOnServerOption, m_Events, "ServerOption");
InitSignalPair(mOnScriptReload, m_Events, "ScriptReload");
InitSignalPair(mOnScriptLoaded, m_Events, "ScriptLoaded");
InitSignalPair(mOnExtCommandReply, m_Events, "ExtCommandReply");
InitSignalPair(mOnExtCommandEvent, m_Events, "ExtCommandEvent");
}
// ------------------------------------------------------------------------------------------------
void Core::DropEvents()
@@ -2516,6 +2594,8 @@ void Core::DropEvents()
ResetSignalPair(mOnServerOption);
ResetSignalPair(mOnScriptReload);
ResetSignalPair(mOnScriptLoaded);
ResetSignalPair(mOnExtCommandReply);
ResetSignalPair(mOnExtCommandEvent);
m_Events.Release();
}
@@ -2861,6 +2941,31 @@ static LightObj & SqGetClientDataBuffer()
return Core::Get().GetClientDataBuffer();
}
// ------------------------------------------------------------------------------------------------
static SQInteger SqSendExtCommand(int32_t target, int32_t req, int32_t tag, SqBuffer & buffer)
{
// Default to an empty/null buffer
const uint8_t * data = nullptr;
size_t size = 0;
// Does the buffer actually point to anything?
if (buffer.GetRef())
{
data = buffer.GetRef()->Begin< uint8_t >();
size = buffer.GetRef()->PositionAs< size_t >();
}
// Forward the request
return Core::Get().SendExtCommand(target, req, tag, data, size);
}
// ------------------------------------------------------------------------------------------------
static SQInteger SqSendExtCommandStr(int32_t target, int32_t req, int32_t tag, StackStrF & str)
{
// Forward the request
return Core::Get().SendExtCommand(target, req, tag,
reinterpret_cast< const uint8_t * >(str.mPtr),
str.mLen <= 0 ? 0 : static_cast< size_t >(str.mLen));
}
// ================================================================================================
void Register_Core(HSQUIRRELVM vm)
{
@@ -2910,6 +3015,8 @@ void Register_Core(HSQUIRRELVM vm)
.Func(_SC("DestroyPickup"), &SqDelPickup)
.Func(_SC("DestroyVehicle"), &SqDelVehicle)
.Func(_SC("ClientDataBuffer"), &SqGetClientDataBuffer)
.Func(_SC("SendExtCommand"), &SqSendExtCommand)
.FmtFunc(_SC("SendExtCommandStr"), &SqSendExtCommandStr)
.Func(_SC("OnPreLoad"), &SqGetPreLoadEvent)
.Func(_SC("OnPostLoad"), &SqGetPostLoadEvent)
.Func(_SC("OnUnload"), &SqGetUnloadEvent)

View File

@@ -9,6 +9,9 @@
// ------------------------------------------------------------------------------------------------
#include <unordered_map>
// ------------------------------------------------------------------------------------------------
#include "SDK/sqmod.h"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@@ -56,6 +59,8 @@ public:
typedef std::vector< ScriptSrc > Scripts; // List of loaded scripts.
// --------------------------------------------------------------------------------------------
typedef std::unordered_map< String, String > Options; // List of custom options.
// --------------------------------------------------------------------------------------------
typedef std::array< ExtPluginCommand_t, 4 > ExtCommands; // 4 external command parsers should be enough.
private:
@@ -68,6 +73,7 @@ private:
Scripts m_Scripts; // Loaded scripts objects.
Scripts m_PendingScripts; // Pending scripts objects.
Options m_Options; // Custom configuration options.
ExtCommands m_ExtCommands; // External command parsers pointers.
// --------------------------------------------------------------------------------------------
Blips m_Blips; // Blips pool.
@@ -399,6 +405,23 @@ public:
*/
SQMOD_NODISCARD String FetchCodeLine(const SQChar * src, SQInteger line, bool trim = true);
/* --------------------------------------------------------------------------------------------
* Register a pointer to a function used to processes commands from script.
* Returns -1 it failed (no free slot), 0 if it was already registered and 1 if it succeeded.
*/
int32_t RegisterExtCommand(ExtPluginCommand_t fn);
/* --------------------------------------------------------------------------------------------
* Remove a pointer to a function used to processes commands from script.
* Returns -1 it failed (no free slot) and 1 if it succeeded.
*/
int32_t UnregisterExtCommand(ExtPluginCommand_t fn);
/* --------------------------------------------------------------------------------------------
* Send a command to all functions currently registered to receive them.
*/
int32_t SendExtCommand(int32_t target, int32_t req, int32_t tag, const uint8_t * data, size_t size);
protected:
/* --------------------------------------------------------------------------------------------
@@ -710,6 +733,16 @@ public:
*/
void EmitClientScriptData(int32_t player_id, const uint8_t * data, size_t size);
/* --------------------------------------------------------------------------------------------
* Send a response to the script that may have resulted from a previous command.
*/
void EmitExtCommandReply(int32_t sender, int32_t tag, const uint8_t * data, size_t size);
/* --------------------------------------------------------------------------------------------
* Forward an event to the script from an external plug-in.
*/
void EmitExtCommandEvent(int32_t sender, int32_t tag, const uint8_t * data, size_t size);
public:
/* --------------------------------------------------------------------------------------------
@@ -864,6 +897,8 @@ public:
SignalPair mOnServerOption{};
SignalPair mOnScriptReload{};
SignalPair mOnScriptLoaded{};
SignalPair mOnExtCommandReply{};
SignalPair mOnExtCommandEvent{};
};
/* ------------------------------------------------------------------------------------------------
@@ -1049,4 +1084,37 @@ template < class F > SQMOD_NODISCARD inline int32_t ForeachConnectedPlayerUntil(
return -1;
}
/* ------------------------------------------------------------------------------------------------
* Used to select entity instances based on type.
*/
template < class > struct EntityInstSelect;
// Specialization for blips.
template < > struct EntityInstSelect< CBlip > {
static BlipInst & Get(int32_t id) { return Core::Get().GetBlip(id); }
};
// Specialization for checkpoints.
template < > struct EntityInstSelect< CCheckpoint > {
static CheckpointInst & Get(int32_t id) { return Core::Get().GetCheckpoint(id); }
};
// Specialization for keybinds.
template < > struct EntityInstSelect< CKeyBind > {
static KeyBindInst & Get(int32_t id) { return Core::Get().GetKeyBind(id); }
};
// Specialization for objects.
template < > struct EntityInstSelect< CObject > {
static ObjectInst & Get(int32_t id) { return Core::Get().GetObj(id); }
};
// Specialization for pickups.
template < > struct EntityInstSelect< CPickup > {
static PickupInst & Get(int32_t id) { return Core::Get().GetPickup(id); }
};
// Specialization for players.
template < > struct EntityInstSelect< CPlayer > {
static PlayerInst & Get(int32_t id) { return Core::Get().GetPlayer(id); }
};
// Specialization for vehicles.
template < > struct EntityInstSelect< CVehicle > {
static VehicleInst & Get(int32_t id) { return Core::Get().GetVehicle(id); }
};
} // Namespace:: SqMod

View File

@@ -285,13 +285,13 @@ public:
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept(fmt::runtime("Buffer capacity of (%u) is unable to host an element of size (%u)"),
ThrowMemExcept(fmt::runtime("Buffer capacity of ({}) is unable to host an element of size ({})"),
m_Cap, sizeof(T));
}
// Make sure that the specified element is withing buffer range
else if (n > (m_Cap - sizeof(T)))
{
ThrowMemExcept(fmt::runtime("Element of size (%d) at index (%u) is out of buffer capacity (%u)"),
ThrowMemExcept(fmt::runtime("Element of size ({}) at index ({}) is out of buffer capacity ({})"),
sizeof(T), n, m_Cap);
}
// Return the requested element
@@ -306,13 +306,13 @@ public:
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept(fmt::runtime("Buffer capacity of (%u) is unable to host an element of size (%u)"),
ThrowMemExcept(fmt::runtime("Buffer capacity of ({}) is unable to host an element of size ({})"),
m_Cap, sizeof(T));
}
// Make sure that the specified element is withing buffer range
else if (n > (m_Cap - sizeof(T)))
{
ThrowMemExcept(fmt::runtime("Element of size (%d) at index (%u) is out of buffer capacity (%u)"),
ThrowMemExcept(fmt::runtime("Element of size ({}) at index ({}) is out of buffer capacity ({})"),
sizeof(T), n, m_Cap);
}
// Return the requested element
@@ -359,7 +359,7 @@ public:
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept(fmt::runtime("Buffer capacity of (%u) is unable to host an element of size (%u)"),
ThrowMemExcept(fmt::runtime("Buffer capacity of ({}) is unable to host an element of size ({})"),
m_Cap, sizeof(T));
}
// Return the requested element
@@ -374,7 +374,7 @@ public:
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept(fmt::runtime("Buffer capacity of (%u) is unable to host an element of size (%u)"),
ThrowMemExcept(fmt::runtime("Buffer capacity of ({}) is unable to host an element of size ({})"),
m_Cap, sizeof(T));
}
// Return the requested element
@@ -389,7 +389,7 @@ public:
// Make sure that the buffer can host at least two elements of this type
if (m_Cap < (sizeof(T) * 2))
{
ThrowMemExcept(fmt::runtime("Buffer capacity of (%u) is unable to host two elements of size (%u)"),
ThrowMemExcept(fmt::runtime("Buffer capacity of ({}) is unable to host two elements of size ({})"),
m_Cap, sizeof(T));
}
// Return the requested element
@@ -404,7 +404,7 @@ public:
// Make sure that the buffer can host at least two elements of this type
if (m_Cap < (sizeof(T) * 2))
{
ThrowMemExcept(fmt::runtime("Buffer capacity of (%u) is unable to host two elements of size (%u)"),
ThrowMemExcept(fmt::runtime("Buffer capacity of ({}) is unable to host two elements of size ({})"),
m_Cap, sizeof(T));
}
// Return the requested element
@@ -419,7 +419,7 @@ public:
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept(fmt::runtime("Buffer capacity of (%u) is unable to host an element of size (%u)"),
ThrowMemExcept(fmt::runtime("Buffer capacity of ({}) is unable to host an element of size ({})"),
m_Cap, sizeof(T));
}
// Return the requested element
@@ -434,7 +434,7 @@ public:
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept(fmt::runtime("Buffer capacity of (%u) is unable to host an element of size (%u)"),
ThrowMemExcept(fmt::runtime("Buffer capacity of ({}) is unable to host an element of size ({})"),
m_Cap, sizeof(T));
}
// Return the requested element
@@ -449,7 +449,7 @@ public:
// Make sure that the buffer can host at least two elements of this type
if (m_Cap < (sizeof(T) * 2))
{
ThrowMemExcept(fmt::runtime("Buffer capacity of (%u) is unable to host two elements of size (%u)"),
ThrowMemExcept(fmt::runtime("Buffer capacity of ({}) is unable to host two elements of size ({})"),
m_Cap, sizeof(T));
}
// Return the requested element
@@ -464,7 +464,7 @@ public:
// Make sure that the buffer can host at least two elements of this type
if (m_Cap < (sizeof(T) * 2))
{
ThrowMemExcept(fmt::runtime("Buffer capacity of (%u) is unable to host two elements of size (%u)"),
ThrowMemExcept(fmt::runtime("Buffer capacity of ({}) is unable to host two elements of size ({})"),
m_Cap, sizeof(T));
}
// Return the requested element
@@ -540,7 +540,7 @@ public:
// Make sure that at least one element of this type exists after the cursor
if ((m_Cur + sizeof(T)) > m_Cap)
{
ThrowMemExcept(fmt::runtime("Element of size (%u) starting at (%u) exceeds buffer capacity (%u)"),
ThrowMemExcept(fmt::runtime("Element of size ({}) starting at ({}) exceeds buffer capacity ({})"),
sizeof(T), m_Cur, m_Cap);
}
// Return the requested element
@@ -555,7 +555,7 @@ public:
// Make sure that at least one element of this type exists after the cursor
if ((m_Cur + sizeof(T)) > m_Cap)
{
ThrowMemExcept(fmt::runtime("Element of size (%u) starting at (%u) exceeds buffer capacity (%u)"),
ThrowMemExcept(fmt::runtime("Element of size ({}) starting at ({}) exceeds buffer capacity ({})"),
sizeof(T), m_Cur, m_Cap);
}
// Return the requested element
@@ -570,7 +570,7 @@ public:
// The cursor must have at least one element of this type behind
if (m_Cur < sizeof(T))
{
ThrowMemExcept(fmt::runtime("Cannot read an element of size (%u) before the cursor at (%u)"),
ThrowMemExcept(fmt::runtime("Cannot read an element of size ({}) before the cursor at ({})"),
sizeof(T), m_Cur);
}
// Return the requested element
@@ -585,7 +585,7 @@ public:
// The cursor must have at least one element of this type behind
if (m_Cur < sizeof(T))
{
ThrowMemExcept(fmt::runtime("Cannot read an element of size (%u) before the cursor at (%u)"),
ThrowMemExcept(fmt::runtime("Cannot read an element of size ({}) before the cursor at ({})"),
sizeof(T), m_Cur);
}
// Return the requested element
@@ -600,13 +600,13 @@ public:
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept(fmt::runtime("Buffer capacity of (%u) is unable to host an element of size (%u)"),
ThrowMemExcept(fmt::runtime("Buffer capacity of ({}) is unable to host an element of size ({})"),
m_Cap, sizeof(T));
}
// There must be buffer left for at least two elements of this type after the cursor
else if ((m_Cur + (sizeof(T) * 2)) > m_Cap)
{
ThrowMemExcept(fmt::runtime("Element of size (%u) starting at (%u) exceeds buffer capacity (%u)"),
ThrowMemExcept(fmt::runtime("Element of size ({}) starting at ({}) exceeds buffer capacity ({})"),
sizeof(T), m_Cur + sizeof(T), m_Cap);
}
// Return the requested element
@@ -621,13 +621,13 @@ public:
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept(fmt::runtime("Buffer capacity of (%u) is unable to host an element of size (%u)"),
ThrowMemExcept(fmt::runtime("Buffer capacity of ({}) is unable to host an element of size ({})"),
m_Cap, sizeof(T));
}
// There must be buffer left for at least two elements of this type after the cursor
else if ((m_Cur + (sizeof(T) * 2)) > m_Cap)
{
ThrowMemExcept(fmt::runtime("Element of size (%u) starting at (%u) exceeds buffer capacity (%u)"),
ThrowMemExcept(fmt::runtime("Element of size ({}) starting at ({}) exceeds buffer capacity ({})"),
sizeof(T), m_Cur + sizeof(T), m_Cap);
}
// Return the requested element
@@ -708,7 +708,7 @@ public:
// See if the requested capacity doesn't exceed the limit
if (n > Max< T >())
{
ThrowMemExcept(fmt::runtime("Requested buffer of (%u) elements exceeds the (%u) limit"), n, Max< T >());
ThrowMemExcept(fmt::runtime("Requested buffer of ({}) elements exceeds the ({}) limit"), n, Max< T >());
}
// Is there an existing buffer?
else if (n && !m_Cap)

View File

@@ -248,6 +248,14 @@ String SqTypeName(HSQUIRRELVM vm, SQInteger idx)
return String(val.mPtr, static_cast< size_t >(val.mLen));
}
// ------------------------------------------------------------------------------------------------
String SqTypeName(HSQUIRRELVM vm, LightObj & obj)
{
const StackGuard sg(vm);
sq_pushobject(vm, obj);
return SqTypeName(vm, -1);
}
// ------------------------------------------------------------------------------------------------
LightObj BufferToStrObj(const Buffer & b)
{

View File

@@ -242,6 +242,11 @@ SQMOD_NODISCARD const SQChar * SqTypeName(SQObjectType type);
*/
SQMOD_NODISCARD String SqTypeName(HSQUIRRELVM vm, SQInteger idx);
/* ------------------------------------------------------------------------------------------------
* Retrieve the string representation of a certain type from a script object.
*/
SQMOD_NODISCARD String SqTypeName(HSQUIRRELVM vm, LightObj & obj);
/* ------------------------------------------------------------------------------------------------
* Create a script string instance from a buffer.
*/

View File

@@ -347,7 +347,7 @@ void Core::EmitIncomingConnection(char * player_name, size_t name_buffer_size, c
// Release any stored buffer information
m_IncomingNameBuffer = nullptr;
m_IncomingNameCapacity = 0;
// We catched the exception so we can release the assigned buffer
// We caught the exception so we can release the assigned buffer
throw; // re-throw it
}
// Release any stored buffer information
@@ -2246,6 +2246,44 @@ void Core::EmitClientScriptData(int32_t player_id, const uint8_t * data, size_t
m_ClientData.Release();
}
// ------------------------------------------------------------------------------------------------
void Core::EmitExtCommandReply(int32_t sender, int32_t tag, const uint8_t * data, size_t size)
{
SQMOD_CO_EV_TRACEBACK("[TRACE<] Core::ExtCommandReply(%i, %i, %p, %zu)", sender, tag, data, size)
// Don't even bother if there's no one listening
if (!(mOnExtCommandReply.first->IsEmpty()))
{
// Allocate a buffer with the received size
Buffer b(static_cast< Buffer::SzType >(size));
// Replicate the data to the allocated buffer
b.Write(0, reinterpret_cast< Buffer::ConstPtr >(data), static_cast< Buffer::SzType >(size));
// Prepare an object for the obtained buffer
LightObj obj(SqTypeIdentity< SqBuffer >{}, m_VM, std::move(b));
// Forward the event call
(*mOnExtCommandReply.first)(sender, tag, obj, size);
}
SQMOD_CO_EV_TRACEBACK("[TRACE>] Core::ExtCommandReply")
}
// ------------------------------------------------------------------------------------------------
void Core::EmitExtCommandEvent(int32_t sender, int32_t tag, const uint8_t * data, size_t size)
{
SQMOD_CO_EV_TRACEBACK("[TRACE<] Core::ExtCommandEvent(%i, %i, %p, %zu)", sender, tag, data, size)
// Don't even bother if there's no one listening
if (!(mOnExtCommandEvent.first->IsEmpty()))
{
// Allocate a buffer with the received size
Buffer b(static_cast< Buffer::SzType >(size));
// Replicate the data to the allocated buffer
b.Write(0, reinterpret_cast< Buffer::ConstPtr >(data), static_cast< Buffer::SzType >(size));
// Prepare an object for the obtained buffer
LightObj obj(SqTypeIdentity< SqBuffer >{}, m_VM, std::move(b));
// Forward the event call
(*mOnExtCommandEvent.first)(sender, tag, obj, size);
}
SQMOD_CO_EV_TRACEBACK("[TRACE>] Core::ExtCommandEvent")
}
#undef NULL_SQOBJ_ // don't need this anymore
} // Namespace:: SqMod

View File

@@ -103,7 +103,7 @@ void ThreadPool::Terminate(bool SQ_UNUSED_ARG(shutdown))
// Is the item valid?
if (item)
{
item->OnCompleted(); // Allow the item to finish itself
[[maybe_unused]] auto _ = item->OnCompleted(true); // Allow the item to finish itself
}
// Item processed
item.reset();
@@ -125,7 +125,15 @@ void ThreadPool::Process()
// Is the item valid?
if (item)
{
item->OnCompleted(); // Allow the item to finish itself
try {
// Allow the item to finish itself
if (item->OnCompleted(false))
{
Enqueue(std::move(item)); // Queue again
}
} catch (const std::exception & e) {
LogErr("Exception occured in %s completion stage [%s] for [%s]", item->TypeName(), e.what(), item->IdentifiableInfo());
}
}
}
}
@@ -149,7 +157,11 @@ void ThreadPool::WorkerProc()
// Is there an item that requested to try again?
if (item)
{
item->OnAborted(true); // NOLINT(bugprone-use-after-move) There's an `if` condition above idiot!
try {
item->OnAborted(true); // NOLINT(bugprone-use-after-move) There's an `if` condition above idiot!
} catch (const std::exception & e) {
LogErr("Exception occured in %s cancelation stage [%s] for [%s]", item->TypeName(), e.what(), item->IdentifiableInfo());
}
}
// Exit the loop
break;
@@ -175,15 +187,30 @@ void ThreadPool::WorkerProc()
// Is there an item to be processed?
if (item)
{
item->OnAborted(false); // It should mark itself as aborted somehow!
try {
item->OnAborted(false); // It should mark itself as aborted somehow!
} catch (const std::exception & e) {
LogErr("Exception occured in %s forced cancelation stage [%s] for [%s]", item->TypeName(), e.what(), item->IdentifiableInfo());
}
}
// Exit the loop
break;
}
bool r;
// Attempt preparation
try {
r = item->OnPrepare();
} catch (const std::exception & e) {
LogErr("Exception occured in %s preparation stage [%s] for [%s]", item->TypeName(), e.what(), item->IdentifiableInfo());
}
// Perform the task
if (item->OnPrepare())
if (r)
{
retry = item->OnProcess();
try {
retry = item->OnProcess();
} catch (const std::exception & e) {
LogErr("Exception occured in %s processing stage [%s] for [%s]", item->TypeName(), e.what(), item->IdentifiableInfo());
}
}
// The task was performed
if (!retry)

View File

@@ -55,6 +55,16 @@ struct ThreadPoolItem
*/
ThreadPoolItem & operator = (ThreadPoolItem && o) = delete;
/* --------------------------------------------------------------------------------------------
* Provide a name to what type of task this is. Mainly for debugging purposes.
*/
SQMOD_NODISCARD virtual const char * TypeName() noexcept { return "worker item"; }
/* --------------------------------------------------------------------------------------------
* Provide unique information that may help identify the task. Mainly for debugging purposes.
*/
SQMOD_NODISCARD virtual const char * IdentifiableInfo() noexcept { return ""; }
/* --------------------------------------------------------------------------------------------
* Invoked in worker thread by the thread pool after obtaining the task from the queue.
* Must return true to indicate that the task can be performed. False indicates failure.
@@ -69,8 +79,10 @@ struct ThreadPoolItem
/* --------------------------------------------------------------------------------------------
* Invoked in main thread by the thread pool after the task was completed.
* If it returns true then it will be put back into the queue to be processed again.
* If the boolean parameter is true then the thread-pool is in the process of shutting down.
*/
virtual void OnCompleted() { }
SQMOD_NODISCARD virtual bool OnCompleted(bool SQ_UNUSED_ARG(stop)) { return false; }
/* --------------------------------------------------------------------------------------------
* Called in worker by the thread pool to let the task know that it will be aborted.
@@ -98,13 +110,13 @@ private:
* Destructor.
*/
~ThreadPool();
public:
// --------------------------------------------------------------------------------------------
using Item = std::unique_ptr< ThreadPoolItem >; // Owning pointer of an item.
private:
// --------------------------------------------------------------------------------------------
using Pool = std::vector< std::thread >; // Worker container.
using Item = std::unique_ptr< ThreadPoolItem >; // Owning pointer of an item.
// --------------------------------------------------------------------------------------------
using Finished = moodycamel::ConcurrentQueue< Item >; // Finished items.
// --------------------------------------------------------------------------------------------
@@ -174,6 +186,22 @@ public:
* Queue an item to be processed. Will take ownership of the given pointer!
*/
void Enqueue(ThreadPoolItem * item)
{
Enqueue(Item{item});
}
/* --------------------------------------------------------------------------------------------
* Queue an item to be processed. Will take ownership of the given pointer!
*/
template < class T > void CastEnqueue(std::unique_ptr< T > && item)
{
Enqueue(Item{std::forward< std::unique_ptr< T > >(item)});
}
/* --------------------------------------------------------------------------------------------
* Queue an item to be processed. Will take ownership of the given pointer!
*/
void Enqueue(Item && item)
{
// Only queue valid items
if (!item || !m_Running) return;
@@ -183,7 +211,7 @@ public:
// Acquire a lock on the mutex
std::unique_lock< std::mutex > lock(m_Mutex);
// Push the item in the queue
m_Queue.push(Item(item));
m_Queue.push(std::forward< Item >(item));
// Release the mutex before notifying
lock.unlock();
// Notify one thread that there's work
@@ -191,18 +219,32 @@ public:
}
else
{
// Take ownership
Item i{item};
bool r;
// Attempt preparation
try {
r = item->OnPrepare();
} catch (const std::exception & e) {
LogErr("Exception occured in %s preparation stage [%s] for [%s]", item->TypeName(), e.what(), item->IdentifiableInfo());
}
// Perform the task in-place
if (i->OnPrepare())
if (r)
{
if (i->OnProcess())
try {
r = item->OnProcess();
} catch (const std::exception & e) {
LogErr("Exception occured in %s processing stage [%s] for [%s]", item->TypeName(), e.what(), item->IdentifiableInfo());
}
if (r)
{
i->OnAborted(true); // Not accepted in single thread
try {
item->OnAborted(true); // Not accepted in single thread
} catch (const std::exception & e) {
LogErr("Exception occured in %s cancelation stage [%s] for [%s]", item->TypeName(), e.what(), item->IdentifiableInfo());
}
}
}
// Task is completed in processing stage
m_Finished.enqueue(std::move(i));
m_Finished.enqueue(std::forward< Item >(item));
}
}
@@ -213,7 +255,6 @@ public:
{
return m_Threads.size();
}
};
} // Namespace:: SqMod

View File

@@ -468,6 +468,16 @@ void CCheckpoint::SetColorA(int32_t a) const
// Perform the requested operation
_Func->SetCheckPointColour(m_ID, r, g, b, a);
}
#ifdef VCMP_ENABLE_OFFICIAL
// ------------------------------------------------------------------------------------------------
LightObj & CCheckpoint::GetLegacyObject() const
{
// Validate the managed identifier
Validate();
// Return the requested information
return Core::Get().GetCheckpoint(m_ID).mLgObj;
}
#endif
// ------------------------------------------------------------------------------------------------
static LightObj & Checkpoint_CreateEx1a(int32_t world, bool sphere, float x, float y, float z,
@@ -517,6 +527,9 @@ void Register_CCheckpoint(HSQUIRRELVM vm)
.Prop(_SC("Tag"), &CCheckpoint::GetTag, &CCheckpoint::SetTag)
.Prop(_SC("Data"), &CCheckpoint::GetData, &CCheckpoint::SetData)
.Prop(_SC("Active"), &CCheckpoint::IsActive)
#ifdef VCMP_ENABLE_OFFICIAL
.Prop(_SC("Legacy"), &CCheckpoint::GetLegacyObject)
#endif
// Core Methods
.FmtFunc(_SC("SetTag"), &CCheckpoint::ApplyTag)
.Func(_SC("CustomEvent"), &CCheckpoint::CustomEvent)

View File

@@ -320,6 +320,12 @@ public:
* Modify the alpha transparency of the managed checkpoint entity.
*/
void SetColorA(int32_t a) const;
#ifdef VCMP_ENABLE_OFFICIAL
/* --------------------------------------------------------------------------------------------
* Retrieve legacy object instance for this entity.
*/
LightObj & GetLegacyObject() const;
#endif
};
} // Namespace:: SqMod

View File

@@ -833,6 +833,16 @@ void CObject::RotateByEulerZ(float z) const
// Perform the requested operation
_Func->RotateObjectByEuler(m_ID, 0.0f, 0.0f, z, mRotateByEulerDuration);
}
#ifdef VCMP_ENABLE_OFFICIAL
// ------------------------------------------------------------------------------------------------
LightObj & CObject::GetLegacyObject() const
{
// Validate the managed identifier
Validate();
// Return the requested information
return Core::Get().GetObj(m_ID).mLgObj;
}
#endif
// ------------------------------------------------------------------------------------------------
static LightObj & Object_CreateEx1a(int32_t model, int32_t world, float x, float y, float z,
@@ -883,6 +893,9 @@ void Register_CObject(HSQUIRRELVM vm)
.Prop(_SC("Tag"), &CObject::GetTag, &CObject::SetTag)
.Prop(_SC("Data"), &CObject::GetData, &CObject::SetData)
.Prop(_SC("Active"), &CObject::IsActive)
#ifdef VCMP_ENABLE_OFFICIAL
.Prop(_SC("Legacy"), &CObject::GetLegacyObject)
#endif
// Core Methods
.FmtFunc(_SC("SetTag"), &CObject::ApplyTag)
.Func(_SC("CustomEvent"), &CObject::CustomEvent)

View File

@@ -499,6 +499,12 @@ public:
* Modify the rotation on the z axis of the managed object entity.
*/
void RotateByEulerZ(float z) const;
#ifdef VCMP_ENABLE_OFFICIAL
/* --------------------------------------------------------------------------------------------
* Retrieve legacy object instance for this entity.
*/
LightObj & GetLegacyObject() const;
#endif
};
} // Namespace:: SqMod

View File

@@ -449,6 +449,16 @@ void CPickup::SetPositionZ(float z) const
// Perform the requested operation
_Func->SetPickupPosition(m_ID, z, y, z);
}
#ifdef VCMP_ENABLE_OFFICIAL
// ------------------------------------------------------------------------------------------------
LightObj & CPickup::GetLegacyObject() const
{
// Validate the managed identifier
Validate();
// Return the requested information
return Core::Get().GetPickup(m_ID).mLgObj;
}
#endif
// ------------------------------------------------------------------------------------------------
static LightObj & Pickup_CreateEx1a(int32_t model, int32_t world, int32_t quantity,
@@ -496,6 +506,9 @@ void Register_CPickup(HSQUIRRELVM vm)
.Prop(_SC("Tag"), &CPickup::GetTag, &CPickup::SetTag)
.Prop(_SC("Data"), &CPickup::GetData, &CPickup::SetData)
.Prop(_SC("Active"), &CPickup::IsActive)
#ifdef VCMP_ENABLE_OFFICIAL
.Prop(_SC("Legacy"), &CPickup::GetLegacyObject)
#endif
// Core Methods
.FmtFunc(_SC("SetTag"), &CPickup::ApplyTag)
.Func(_SC("CustomEvent"), &CPickup::CustomEvent)

View File

@@ -298,6 +298,12 @@ public:
* Modify the position on the z axis of the managed pickup entity.
*/
void SetPositionZ(float z) const;
#ifdef VCMP_ENABLE_OFFICIAL
/* --------------------------------------------------------------------------------------------
* Retrieve legacy object instance for this entity.
*/
LightObj & GetLegacyObject() const;
#endif
};
} // Namespace:: SqMod

View File

@@ -2593,7 +2593,16 @@ SQInteger CPlayer::AnnounceEx(HSQUIRRELVM vm)
// This function does not return a value
return 0;
}
#ifdef VCMP_ENABLE_OFFICIAL
// ------------------------------------------------------------------------------------------------
LightObj & CPlayer::GetLegacyObject() const
{
// Validate the managed identifier
Validate();
// Return the requested information
return Core::Get().GetPlayer(m_ID).mLgObj;
}
#endif
// ------------------------------------------------------------------------------------------------
SQInteger Player_FindAuto(HSQUIRRELVM vm)
{
@@ -2793,6 +2802,9 @@ void Register_CPlayer(HSQUIRRELVM vm)
.Prop(_SC("Tag"), &CPlayer::GetTag, &CPlayer::SetTag)
.Prop(_SC("Data"), &CPlayer::GetData, &CPlayer::SetData)
.Prop(_SC("Active"), &CPlayer::IsActive)
#ifdef VCMP_ENABLE_OFFICIAL
.Prop(_SC("Legacy"), &CPlayer::GetLegacyObject)
#endif
// Core Methods
.FmtFunc(_SC("SetTag"), &CPlayer::ApplyTag)
.Func(_SC("CustomEvent"), &CPlayer::CustomEvent)

View File

@@ -1101,6 +1101,12 @@ public:
* Send a formatted announcement message to the managed player entity.
*/
static SQInteger AnnounceEx(HSQUIRRELVM vm);
#ifdef VCMP_ENABLE_OFFICIAL
/* --------------------------------------------------------------------------------------------
* Retrieve legacy object instance for this entity.
*/
LightObj & GetLegacyObject() const;
#endif
};
} // Namespace:: SqMod

View File

@@ -2015,6 +2015,16 @@ void CVehicle::SetRelativeTurnSpeedZ(float z) const
// Perform the requested operation
_Func->SetVehicleTurnSpeed(m_ID, z, y, z, static_cast< uint8_t >(false), static_cast< uint8_t >(true));
}
#ifdef VCMP_ENABLE_OFFICIAL
// ------------------------------------------------------------------------------------------------
LightObj & CVehicle::GetLegacyObject() const
{
// Validate the managed identifier
Validate();
// Return the requested information
return Core::Get().GetVehicle(m_ID).mLgObj;
}
#endif
// ------------------------------------------------------------------------------------------------
static LightObj & Vehicle_CreateEx1a(int32_t model, int32_t world, float x, float y, float z, float angle,
@@ -2061,6 +2071,9 @@ void Register_CVehicle(HSQUIRRELVM vm)
.Prop(_SC("ID"), &CVehicle::GetID)
.Prop(_SC("Tag"), &CVehicle::GetTag, &CVehicle::SetTag)
.Prop(_SC("Data"), &CVehicle::GetData, &CVehicle::SetData)
#ifdef VCMP_ENABLE_OFFICIAL
.Prop(_SC("Legacy"), &CVehicle::GetLegacyObject)
#endif
.Prop(_SC("Active"), &CVehicle::IsActive)
// Core Methods
.FmtFunc(_SC("SetTag"), &CVehicle::ApplyTag)

View File

@@ -942,6 +942,12 @@ public:
* Modify the relative turn velocity on the z axis of the managed vehicle entity.
*/
void SetRelativeTurnSpeedZ(float z) const;
#ifdef VCMP_ENABLE_OFFICIAL
/* --------------------------------------------------------------------------------------------
* Retrieve legacy object instance for this entity.
*/
LightObj & GetLegacyObject() const;
#endif
};
} // Namespace:: SqMod

73
module/Exports.cpp Normal file
View File

@@ -0,0 +1,73 @@
// ------------------------------------------------------------------------------------------------
#include "Core.hpp"
// ------------------------------------------------------------------------------------------------
#include <cstring>
// ------------------------------------------------------------------------------------------------
#include <sqmod.h>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
static int32_t RegisterCommandFn(ExtPluginCommand_t fn)
{
return Core::Get().RegisterExtCommand(fn);
}
// ------------------------------------------------------------------------------------------------
static int32_t UnregisterCommandFn(ExtPluginCommand_t fn)
{
return Core::Get().UnregisterExtCommand(fn);
}
// ------------------------------------------------------------------------------------------------
static int32_t SendCommandFn(int32_t target, int32_t req, int32_t tag, const uint8_t * data, size_t size)
{
return Core::Get().SendExtCommand(target, req, tag, data, size);
}
// ------------------------------------------------------------------------------------------------
static int32_t SendCommandReplyFn(int32_t sender, int32_t tag, const uint8_t * data, size_t size)
{
// Mark the initialization as successful by default
const CoreState cs(SQMOD_SUCCESS);
// Forward the call to the script callbacks
Core::Get().EmitExtCommandReply(sender, tag, data, size);
// Return the last known plug-in state
return Core::Get().GetState();
}
// ------------------------------------------------------------------------------------------------
static int32_t SendCommandEventFn(int32_t sender, int32_t tag, const uint8_t * data, size_t size)
{
// Mark the initialization as successful by default
const CoreState cs(SQMOD_SUCCESS);
// Forward the call to the script callbacks
Core::Get().EmitExtCommandEvent(sender, tag, data, size);
// Return the last known plug-in state
return Core::Get().GetState();
}
// ------------------------------------------------------------------------------------------------
static const SQ_MOD_EXPORTS g_SqModExports{
sizeof(SQ_MOD_EXPORTS),
&RegisterCommandFn,
&UnregisterCommandFn,
&SendCommandFn,
&SendCommandReplyFn,
&SendCommandEventFn
};
// The server needs a pointer to a pointer, and a persistent one
static const SQ_MOD_EXPORTS * g_SqModExportsPtr = &g_SqModExports;
// ------------------------------------------------------------------------------------------------
void InitExports()
{
// Tell the server about the pointer to the exports structure
_Func->ExportFunctions(_Info->pluginId, reinterpret_cast< const void ** >(&g_SqModExportsPtr), sizeof(HSQ_MOD_EXPORTS));
}
} // Namespace:: SqMod

View File

@@ -47,9 +47,11 @@ struct CpBaseAction : public ThreadPoolItem
~CpBaseAction() override = default;
/* --------------------------------------------------------------------------------------------
* Task completed callback.
* Invoked in main thread by the thread pool after the task was completed.
* If it returns true then it will be put back into the queue to be processed again.
* If the boolean parameter is trye then the thread-pool is in the process of shutting down.
*/
void OnCompleted() override
SQMOD_NODISCARD bool OnCompleted(bool SQ_UNUSED_ARG(stop)) override
{
// Is there a callback?
if (!mCallback.IsNull())
@@ -58,6 +60,8 @@ struct CpBaseAction : public ThreadPoolItem
}
// Unlock the session
mInstance->mPending = nullptr;
// Don't re-queue
return false;
}
/* --------------------------------------------------------------------------------------------

View File

@@ -11,6 +11,13 @@
// ------------------------------------------------------------------------------------------------
#include <Poco/Checksum.h>
#include <Poco/Base32Encoder.h>
#include <Poco/Base32Decoder.h>
#include <Poco/Base64Encoder.h>
#include <Poco/Base64Decoder.h>
// ------------------------------------------------------------------------------------------------
#include <sstream>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@@ -30,7 +37,7 @@ SQInteger SqBuffer::WriteRawString(StackStrF & val) const
}
// Calculate the string length
Buffer::SzType length = ConvTo< Buffer::SzType >::From(val.mLen);
// Write the the string contents
// Write the string contents
m_Buffer->AppendS(val.mPtr, length);
// Return the length of the written string
return val.mLen;
@@ -224,7 +231,7 @@ AABB SqBuffer::ReadAABB() const
// Advance the buffer cursor
m_Buffer->Advance< AABB >(1);
// Return the requested information
return AABB(value);
return {value};
}
// ------------------------------------------------------------------------------------------------
@@ -237,7 +244,7 @@ Circle SqBuffer::ReadCircle() const
// Advance the buffer cursor
m_Buffer->Advance< Circle >(1);
// Return the requested information
return Circle(value);
return {value};
}
// ------------------------------------------------------------------------------------------------
@@ -250,7 +257,7 @@ Color3 SqBuffer::ReadColor3() const
// Advance the buffer cursor
m_Buffer->Advance< Color3 >(1);
// Return the requested information
return Color3(value);
return {value};
}
// ------------------------------------------------------------------------------------------------
@@ -263,7 +270,7 @@ Color4 SqBuffer::ReadColor4() const
// Advance the buffer cursor
m_Buffer->Advance< Color4 >(1);
// Return the requested information
return Color4(value);
return {value};
}
// ------------------------------------------------------------------------------------------------
@@ -276,7 +283,7 @@ Quaternion SqBuffer::ReadQuaternion() const
// Advance the buffer cursor
m_Buffer->Advance< Quaternion >(1);
// Return the requested information
return Quaternion(value);
return {value};
}
// ------------------------------------------------------------------------------------------------
@@ -289,7 +296,7 @@ Sphere SqBuffer::ReadSphere() const
// Advance the buffer cursor
m_Buffer->Advance< Sphere >(1);
// Return the requested information
return Sphere(value);
return {value};
}
// ------------------------------------------------------------------------------------------------
@@ -302,7 +309,7 @@ Vector2 SqBuffer::ReadVector2() const
// Advance the buffer cursor
m_Buffer->Advance< Vector2 >(1);
// Return the requested information
return Vector2(value);
return {value};
}
// ------------------------------------------------------------------------------------------------
@@ -315,7 +322,7 @@ Vector2i SqBuffer::ReadVector2i() const
// Advance the buffer cursor
m_Buffer->Advance< Vector2i >(1);
// Return the requested information
return Vector2i(value);
return {value};
}
// ------------------------------------------------------------------------------------------------
@@ -328,7 +335,7 @@ Vector3 SqBuffer::ReadVector3() const
// Advance the buffer cursor
m_Buffer->Advance< Vector3 >(1);
// Return the requested information
return Vector3(value);
return {value};
}
// ------------------------------------------------------------------------------------------------
@@ -341,7 +348,34 @@ Vector4 SqBuffer::ReadVector4() const
// Advance the buffer cursor
m_Buffer->Advance< Vector4 >(1);
// Return the requested information
return Vector4(value);
return {value};
}
// ------------------------------------------------------------------------------------------------
extern SQInteger SqFromNativeJSON(HSQUIRRELVM vm, const char * data, size_t size);
// ------------------------------------------------------------------------------------------------
SQInteger SqBuffer::GetJSON(HSQUIRRELVM vm) const
{
// Remember the current stack size
const SQInteger top = sq_gettop(vm);
// Was the JSON string size specified?
if (top < 2)
{
return sq_throwerror(vm, _SC("Please specify the size of the JSON string to parse"));
}
// Do we even point to a valid buffer?
if (!m_Buffer)
{
return sq_throwerror(vm, _SC("Invalid memory buffer reference"));
}
// Validate the buffer itself
else if (!(*m_Buffer))
{
return sq_throwerror(vm, _SC("Invalid memory buffer"));
}
// Attempt to create the JSON object and push it on the stack
return SqFromNativeJSON(vm, &m_Buffer->Cursor< char >(), static_cast< size_t >(Var< SQInteger >{vm, 2}.value));
}
// ------------------------------------------------------------------------------------------------
@@ -352,7 +386,7 @@ SQInteger SqBuffer::GetCRC32(SQInteger n) const
// Create the checksum computer
Poco::Checksum c(Poco::Checksum::TYPE_CRC32);
// Give it the data to process
c.update(&m_Buffer->Cursor< char >(), n >= 0 ? static_cast< uint32_t >(n) : m_Buffer->Remaining());
c.update(&m_Buffer->Cursor< char >(), ClampRemaining(n));
// return the result
return static_cast< SQInteger >(c.checksum());
}
@@ -365,12 +399,46 @@ SQInteger SqBuffer::GetADLER32(SQInteger n) const
// Create the checksum computer
Poco::Checksum c(Poco::Checksum::TYPE_ADLER32);
// Give it the data to process
c.update(&m_Buffer->Cursor< char >(), n >= 0 ? static_cast< uint32_t >(n) : m_Buffer->Remaining());
c.update(&m_Buffer->Cursor< char >(), ClampRemaining(n));
// return the result
return static_cast< SQInteger >(c.checksum());
}
// ------------------------------------------------------------------------------------------------
LightObj SqBuffer::GetBase32(SQInteger n) const
{
// Validate the managed buffer reference
ValidateDeeper();
// Create a string receiver
std::ostringstream out;
// Create the encoder
Poco::Base32Encoder enc(out);
// Encode the string
enc.write(&m_Buffer->Cursor< char >(), ClampRemaining(n));
// Close the encoder
enc.close();
// Return the resulted string
return LightObj{out.str()};
}
// ------------------------------------------------------------------------------------------------
LightObj SqBuffer::GetBase64(SQInteger n) const
{
// Validate the managed buffer reference
ValidateDeeper();
// Create a string receiver
std::ostringstream out;
// Create the encoder
Poco::Base64Encoder enc(out);
// Encode the string
enc.write(&m_Buffer->Cursor< char >(), ClampRemaining(n));
// Close the encoder
enc.close();
// Return the resulted string
return LightObj{out.str()};
}
// ================================================================================================
void Register_Buffer(HSQUIRRELVM vm)
{
@@ -457,6 +525,9 @@ void Register_Buffer(HSQUIRRELVM vm)
.Func(_SC("ReadVector4"), &SqBuffer::ReadVector4)
.Func(_SC("CRC32"), &SqBuffer::GetCRC32)
.Func(_SC("ADLER32"), &SqBuffer::GetADLER32)
.Func(_SC("Base32"), &SqBuffer::GetBase32)
.Func(_SC("Base64"), &SqBuffer::GetBase64)
.SquirrelMethod< SqBuffer, &SqBuffer::GetJSON >(_SC("GetJSON"))
);
}

View File

@@ -193,6 +193,24 @@ public:
return *m_Buffer;
}
/* --------------------------------------------------------------------------------------------
* Limit the specified amount at to the range of the cursor and the end of the buffer.
*/
SQMOD_NODISCARD SzType ClampRemaining(SQInteger n) const
{
// Do we even specify a buffer amount?
if (n >= 0)
{
// Is it within the range we currently have left?
if (static_cast< SzType >(n) <= m_Buffer->Remaining())
{
return static_cast< SzType >(n);
}
}
// Fall back to the actual remaining data
return m_Buffer->Remaining();
}
/* --------------------------------------------------------------------------------------------
* Retrieve a certain element type at the specified position.
*/
@@ -276,33 +294,41 @@ public:
/* --------------------------------------------------------------------------------------------
* Reposition the edit cursor to the specified number of elements ahead.
*/
void Advance(SQInteger n) const
SqBuffer & Advance(SQInteger n)
{
Valid().Advance(ConvTo< SzType >::From(n));
// Allow chaining
return *this;
}
/* --------------------------------------------------------------------------------------------
* Reposition the edit cursor to the specified number of elements behind.
*/
void Retreat(SQInteger n) const
SqBuffer & Retreat(SQInteger n)
{
Valid().Retreat(ConvTo< SzType >::From(n));
// Allow chaining
return *this;
}
/* --------------------------------------------------------------------------------------------
* Reposition the edit cursor to a fixed position within the buffer.
*/
void Move(SQInteger n) const
SqBuffer & Move(SQInteger n)
{
Valid().Move(ConvTo< SzType >::From(n));
// Allow chaining
return *this;
}
/* --------------------------------------------------------------------------------------------
* Append a value to the current cursor location and advance the cursor.
*/
void Push(SQInteger v) const
SqBuffer & Push(SQInteger v)
{
Valid().Push(ConvTo< Value >::From(v));
// Allow chaining
return *this;
}
/* --------------------------------------------------------------------------------------------
@@ -396,15 +422,17 @@ public:
/* --------------------------------------------------------------------------------------------
* Grow the size of the internal buffer by the specified amount of bytes.
*/
void Grow(SQInteger n) const
SqBuffer & Grow(SQInteger n)
{
return Valid().Grow(ConvTo< SzType >::From(n) * sizeof(Value));
Valid().Grow(ConvTo< SzType >::From(n) * sizeof(Value));
// Allow chaining
return *this;
}
/* --------------------------------------------------------------------------------------------
* Makes sure there is enough capacity to hold the specified element count.
*/
void Adjust(SQInteger n)
SqBuffer & Adjust(SQInteger n)
{
// Validate the managed buffer reference
Validate();
@@ -420,6 +448,8 @@ public:
{
STHROWF("{}", e.what()); // Re-package
}
// Allow chaining
return *this;
}
/* --------------------------------------------------------------------------------------------
@@ -752,6 +782,12 @@ public:
*/
SQMOD_NODISCARD Vector4 ReadVector4() const;
/* --------------------------------------------------------------------------------------------
* Transform a portion of the data in the buffer to a JSON object.
* This has the benefit that a temporary string doesn't have to be created.
*/
SQMOD_NODISCARD SQInteger GetJSON(HSQUIRRELVM vm) const;
/* --------------------------------------------------------------------------------------------
* Compute the CRC-32 checksums on the data in the buffer.
*/
@@ -761,6 +797,16 @@ public:
* Compute the Adler-32 checksums on the data in the buffer.
*/
SQMOD_NODISCARD SQInteger GetADLER32(SQInteger n) const;
/* --------------------------------------------------------------------------------------------
* Encode the specified range of data as base32 and return it.
*/
SQMOD_NODISCARD LightObj GetBase32(SQInteger n) const;
/* --------------------------------------------------------------------------------------------
* Encode the specified range of data as base64 and return it.
*/
SQMOD_NODISCARD LightObj GetBase64(SQInteger n) const;
};
} // Namespace:: SqMod

View File

@@ -2,6 +2,7 @@
#include "Library/JSON.hpp"
// ------------------------------------------------------------------------------------------------
#include <sajson.h>
#include <sqratConst.h>
// ------------------------------------------------------------------------------------------------
@@ -11,13 +12,10 @@
namespace SqMod {
// ------------------------------------------------------------------------------------------------
static SQInteger SqToJSON(HSQUIRRELVM vm) noexcept
{
return sq_throwerror(vm, _SC("Not implemented yet!"));
}
SQMOD_DECL_TYPENAME(SqCtxJSON, _SC("SqCtxJSON"))
// ------------------------------------------------------------------------------------------------
static SQInteger SqFromJson_Push(HSQUIRRELVM vm, const sajson::value & node) noexcept
static SQInteger SqFromJson_Push(HSQUIRRELVM vm, const sajson::value & node) noexcept // NOLINT(misc-no-recursion)
{
// Operation result
SQInteger r = SQ_OK;
@@ -151,11 +149,616 @@ static SQInteger SqFromJSON(HSQUIRRELVM vm) noexcept
return SQ_SUCCEEDED(r) ? 1 : r;
}
// ------------------------------------------------------------------------------------------------
SQInteger SqFromNativeJSON(HSQUIRRELVM vm, const char * data, size_t size)
{
// Attempt to parse the specified JSON string
const sajson::document & document = sajson::parse(sajson::dynamic_allocation(), sajson::string(data, size));
// See if there was an error
if (!document.is_valid())
{
return sq_throwerror(vm, document.get_error_message_as_cstring());
}
// Process the nodes that were parsed from the string
SQInteger r = SqFromJson_Push(vm, document.get_root());
// We either have a value to return or we propagate some error
return SQ_SUCCEEDED(r) ? 1 : r;
}
// ------------------------------------------------------------------------------------------------
CtxJSON & CtxJSON::OpenArray()
{
// Add the array-begin character
mOutput.push_back('[');
// Go forward one level
Advance();
// Allow chaining
return *this;
}
// ------------------------------------------------------------------------------------------------
CtxJSON & CtxJSON::CloseArray()
{
// If the last character is a comma then replace it
if (mOutput.back() == ',')
{
mOutput.back() = ']';
}
// Append the array-end character
else
{
mOutput.push_back(']');
}
// Move the comma after the closing character
mOutput.push_back(',');
// Go back one level
Retreat();
// Allow chaining
return *this;
}
// ------------------------------------------------------------------------------------------------
CtxJSON & CtxJSON::ReopenArray()
{
// If the last character is a comma then remove it
if (mOutput.back() == ',')
{
mOutput.pop_back();
}
// If the last character is the array-end character then replace it with a comma
if (mOutput.back() == ']')
{
mOutput.back() = ',';
}
// Allow chaining
return *this;
}
// ------------------------------------------------------------------------------------------------
CtxJSON & CtxJSON::OpenObject()
{
// Add the object-begin character
mOutput.push_back('{');
// Go forward one level
Advance();
// Allow chaining
return *this;
}
// ------------------------------------------------------------------------------------------------
CtxJSON & CtxJSON::CloseObject()
{
// If the last character is a comma then replace it
if (mOutput.back() == ',')
{
mOutput.back() = '}';
}
// Append the object-end character
else
{
mOutput.push_back('}');
}
// Move the comma after the closing character
mOutput.push_back(',');
// Go back one level
Retreat();
// Allow chaining
return *this;
}
// ------------------------------------------------------------------------------------------------
CtxJSON & CtxJSON::ReopenObject()
{
// If the last character is a comma then remove it
if (mOutput.back() == ',')
{
mOutput.pop_back();
}
// If the last character is the object-end character then replace it with a comma
if (mOutput.back() == '}')
{
mOutput.back() = ',';
}
// Allow chaining
return *this;
}
// ------------------------------------------------------------------------------------------------
CtxJSON & CtxJSON::MakeKey()
{
// If the last character is a comma then replace it
if (mOutput.back() == ',')
{
mOutput.back() = ':';
}
// Append the array-end character
else
{
mOutput.push_back(':');
}
// Allow the hook to react
if (mKeyHook)
{
mKeyHook(*this);
}
// Allow chaining
return *this;
}
// ------------------------------------------------------------------------------------------------
bool CtxJSON::CheckWeakRefWrap(HSQUIRRELVM vm, SQInteger idx) noexcept
{
SQRESULT r = sq_getweakrefval(vm, idx);
// Whether the type doesn't have to be wrapped
bool w = true;
// Attempt to grab the value pointed by the weak reference
if (SQ_SUCCEEDED(r))
{
// Attempt to serialize the actual value
w = sq_gettype(vm, -1) != OT_TABLE && sq_gettype(vm, -1) != OT_ARRAY && sq_gettype(vm, -1) == OT_INSTANCE;
// Pop the referenced value
sq_poptop(vm);
}
// Wrap the value by default
return w;
}
// ------------------------------------------------------------------------------------------------
SQRESULT CtxJSON::SerializeParams(HSQUIRRELVM vm)
{
bool wrap_everything_in_array = false;
// Clear the output buffer if necessary
mOutput.clear();
mDepth = 0;
// Fetch the number of objects on the stack
const auto top = sq_gettop(vm);
// If there's more than one argument then they all get wrapped inside an array
// If there is one argument and is not an array, table or instance then do the same
if (top > 2 || (sq_gettype(vm, 2) != OT_TABLE &&
sq_gettype(vm, 2) != OT_ARRAY &&
sq_gettype(vm, 2) != OT_INSTANCE &&
CheckWeakRefWrap(vm, 2)))
{
wrap_everything_in_array = true;
// Open an array
OpenArray();
}
// Serialize every specified argument
for (SQInteger i = 2; i <= top; ++i)
{
if (SQRESULT r = SerializeAt(vm, i); SQ_FAILED(r))
{
return r; // Propagate the error
}
}
// Was everything wrapped inside an array?
if (wrap_everything_in_array)
{
CloseArray();
}
// Remove trailing separator, if any
if (mOutput.back() == ',')
{
mOutput.pop_back();
}
// Push the output string on the stack
sq_pushstring(vm, mOutput.c_str(), static_cast< SQInteger >(mOutput.size()));
// Specify that we have a value on the stack
return 1;
}
// ------------------------------------------------------------------------------------------------
SQRESULT CtxJSON::SerializeAt(HSQUIRRELVM vm, SQInteger idx) // NOLINT(misc-no-recursion)
{
// Identify object type
switch (sq_gettype(vm, idx))
{
case OT_NULL: {
PushNull();
} break;
case OT_INTEGER: {
SQInteger v;
// Attempt to retrieve the value from the stack
if (SQRESULT r = sq_getinteger(vm, idx, &v); SQ_FAILED(r))
{
return r; // Propagate the error
}
// Write the value in the output
PushInteger(v);
} break;
case OT_FLOAT: {
SQFloat v;
// Attempt to retrieve the value from the stack
if (SQRESULT r = sq_getfloat(vm, idx, &v); SQ_FAILED(r))
{
return r; // Propagate the error
}
// Write the value in the output
#ifdef SQUSEDOUBLE
PushDouble(v);
#else
PushFloat(v);
#endif
} break;
case OT_BOOL: {
SQBool v;
// Attempt to retrieve the value from the stack
if (SQRESULT r = sq_getbool(vm, idx, &v); SQ_FAILED(r))
{
return r; // Propagate the error
}
// Write the value in the output
PushBool(v != SQFalse);
} break;
case OT_STRING: {
const SQChar * v = nullptr;
SQInteger n = 0;
// Attempt to retrieve and convert the string
if (SQRESULT r = sq_getstringandsize(vm, idx, &v, &n); SQ_FAILED(r))
{
return r; // Propagate the error
}
// Write the value in the output
PushString(v, static_cast< size_t >(n));
} break;
case OT_TABLE: {
if (SQRESULT r = SerializeTable(vm, idx); SQ_FAILED(r))
{
return r; // Propagate the error
}
} break;
case OT_ARRAY: {
if (SQRESULT r = SerializeArray(vm, idx); SQ_FAILED(r))
{
return r; // Propagate the error
}
} break;
case OT_INSTANCE: {
if (SQRESULT r = SerializeInstance(vm, idx); SQ_FAILED(r))
{
return r; // Propagate the error
}
} break;
case OT_WEAKREF: {
if (SQRESULT r = SerializeWeakRef(vm, idx); SQ_FAILED(r))
{
return r; // Propagate the error
}
} break;
case OT_USERDATA:
case OT_CLOSURE:
case OT_NATIVECLOSURE:
case OT_GENERATOR:
case OT_USERPOINTER:
case OT_THREAD:
case OT_FUNCPROTO:
case OT_CLASS:
case OT_OUTER:
return sq_throwerrorf(vm, _SC("Type (%s) is not serializable"), SqTypeName(sq_gettype(vm, 2)));
}
// Serialization was successful
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
SQRESULT CtxJSON::SerializeArray(HSQUIRRELVM vm, SQInteger idx) // NOLINT(misc-no-recursion)
{
// Begin array scope
OpenArray();
// Push null to initiate iteration
sq_pushnull(vm);
// So we can use absolute stack indexes to avoid errors
const auto top = sq_gettop(vm);
// Start iterating the array at the specified position in the stack
for(SQRESULT r = SQ_OK; SQ_SUCCEEDED(sq_next(vm, idx));)
{
// Attempt serialization of the currently iterated value
r = SerializeAt(vm, top + 2);
// Check for failures
if (SQ_FAILED(r))
{
// Pop the null iterator, key and value from the stack
sq_pop(vm, 3);
// Propagate the error
return r;
}
// Pop the key and value from the stack (i.e. cleanup after `sq_next`)
sq_pop(vm, 2);
}
// Pop the null iterator
sq_poptop(vm);
// Close array scope
CloseArray();
// Serialization was successful
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
SQRESULT CtxJSON::SerializeTable(HSQUIRRELVM vm, SQInteger idx) // NOLINT(misc-no-recursion)
{
// Begin object scope
OpenObject();
// Push null to initiate iteration
sq_pushnull(vm);
// So we can use absolute stack indexes to avoid errors
const auto top = sq_gettop(vm);
// Start iterating the object at the specified position in the stack
for(SQRESULT r = SQ_OK; SQ_SUCCEEDED(sq_next(vm, idx));)
{
if (sq_gettype(vm, -2) == OT_STRING)
{
// Attempt serialization of the currently iterated element key
r = SerializeAt(vm, top + 1);
// Can we proceed with the value?
if (SQ_SUCCEEDED(r))
{
// Mark the value above as the key of this element and
// attempt serialization of the currently iterated element value
r = MakeKey().SerializeAt(vm, top + 2);
}
}
else
{
r = sq_throwerror(vm, _SC("Only string values are accepted as object keys"));
}
// Check for failures
if (SQ_FAILED(r))
{
// Pop the null iterator, key and value from the stack
sq_pop(vm, 3);
// Propagate the error
return r;
}
// Pop the key and value from the stack (i.e. cleanup after `sq_next`)
sq_pop(vm, 2);
}
// Pop the null iterator
sq_poptop(vm);
// Close object scope
CloseObject();
// Serialization was successful
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
SQRESULT CtxJSON::SerializeInstance(HSQUIRRELVM vm, SQInteger idx)
{
sq_pushstring(vm, mMetaMethod.c_str(), static_cast< SQInteger >(mMetaMethod.size()));
// Attempt to retrieve the meta-method from the instance
if(SQRESULT r = sq_get(vm, idx); SQ_FAILED(r))
{
return r; // Propagate the error
}
// Make sure this is actually a closure/function that we can invoke
else if (const auto t = sq_gettype(vm, -1); t != OT_CLOSURE && t != OT_NATIVECLOSURE)
{
// Remove whatever is on the stack
sq_poptop(vm);
// Abort the operation as we can't do anything about it
return sq_throwerrorf(vm, _SC("`_tojson` meta-method is not a closure for type (%s)"), SqTypeName(vm, idx).c_str());
}
// Push the instance itself the stack (the environment)
sq_push(vm, idx);
// Push this instance on the stack (the json context)
ClassType< CtxJSON >::PushInstance(vm, this);
// Invoke the function to perform the conversion in this context
SQRESULT r = sq_call(vm, 2, SQFalse, SQFalse);
// Remove the closure from the stack
sq_poptop(vm);
// Propagate the result, whatever that is
return r;
}
// ------------------------------------------------------------------------------------------------
SQRESULT CtxJSON::SerializeWeakRef(HSQUIRRELVM vm, SQInteger idx) // NOLINT(misc-no-recursion)
{
SQRESULT r = sq_getweakrefval(vm, idx);
// Attempt to grab the value pointed by the weak reference
if (SQ_SUCCEEDED(r))
{
// Attempt to serialize the actual value
r = SerializeAt(vm, sq_gettop(vm));
// Pop the referenced value
sq_poptop(vm);
}
// Propagate the error, if any
return r;
}
// ------------------------------------------------------------------------------------------------
SQRESULT CtxJSON::PushValues(HSQUIRRELVM vm)
{
// Fetch the number of objects on the stack
const auto top = sq_gettop(vm);
// Do we have a value?
if (top < 2)
{
return sq_throwerror(vm, _SC("Must specify at least one value to be pushed"));
}
// Serialize every specified argument
for (SQInteger i = 2; i <= top; ++i)
{
if (SQRESULT r = SerializeAt(vm, i); SQ_FAILED(r))
{
return r; // Propagate the error
}
}
// Allow chaining
sq_push(vm, 1);
// Specify that a value was returned
return 1;
}
// ------------------------------------------------------------------------------------------------
SQRESULT CtxJSON::PushElement(HSQUIRRELVM vm)
{
// Fetch the number of objects on the stack
const auto top = sq_gettop(vm);
// Do we have a value?
if (top < 3)
{
return sq_throwerrorf(vm, _SC("Not enough parameters. Received %lld but %lld needed"), top-1, 2);
}
else if (sq_gettype(vm, 2) != OT_STRING)
{
return sq_throwerrorf(vm, _SC("Element key must be a string"));
}
// Attempt serialization of the currently iterated element key
if (SQRESULT r = SerializeAt(vm, 2); SQ_SUCCEEDED(r))
{
// Mark the value above as the key of this element and
// attempt serialization of the currently iterated element value
r = MakeKey().SerializeAt(vm, 3);
// Check for failures
if (SQ_FAILED(r))
{
return r; // Propagate the error
}
}
// Allow chaining
sq_push(vm, 1);
// Specify that a value was returned
return 1;
}
// ------------------------------------------------------------------------------------------------
CtxJSON & CtxJSON::PushKey(StackStrF & key)
{
// Validate the string value
if (key.mLen >= 0 && SQ_SUCCEEDED(key.mRes))
{
PushString(key.mPtr, static_cast< size_t >(key.mLen));
MakeKey();
}
else
{
STHROWF("Invalid object key");
}
// Allow chaining
return *this;
}
// ------------------------------------------------------------------------------------------------
void CtxJSON::PushNull()
{
mOutput.append("null,");
}
// ------------------------------------------------------------------------------------------------
void CtxJSON::PushInteger(SQInteger value)
{
fmt::format_int f(value);
// Append the formatted integer to the buffer
mOutput.append(f.data(), f.size());
mOutput.push_back(',');
}
// ------------------------------------------------------------------------------------------------
void CtxJSON::PushFloat(float value)
{
fmt::format_to(std::back_inserter(mOutput), "{},", value);
}
// ------------------------------------------------------------------------------------------------
void CtxJSON::PushDouble(double value)
{
fmt::format_to(std::back_inserter(mOutput), "{},", value);
}
// ------------------------------------------------------------------------------------------------
void CtxJSON::PushBool(bool value)
{
if (value)
{
mOutput.append("true,", 5);
}
else
{
mOutput.append("false,", 6);
}
}
// ------------------------------------------------------------------------------------------------
void CtxJSON::PushString(const SQChar * str)
{
mOutput.push_back('"');
mOutput.append(str);
mOutput.push_back('"');
mOutput.push_back(',');
// Allow the hook to know
mString.assign(str);
}
// ------------------------------------------------------------------------------------------------
void CtxJSON::PushString(const SQChar * str, size_t length)
{
mOutput.push_back('"');
mOutput.append(str, length);
mOutput.push_back('"');
mOutput.push_back(',');
// Allow the hook to know
mString.assign(str, length);
}
// ------------------------------------------------------------------------------------------------
static SQInteger SqToJSON(HSQUIRRELVM vm) noexcept
{
// Make sure the instance is cleaned up even in the case of exceptions
DeleteGuard< CtxJSON > sq_dg(new CtxJSON());
// Remember the instance, so we don't have to cast the script object back
auto ctx = sq_dg.Get();
// Turn it into a script object because it may be passed as a parameter to `_tojson` meta-methods
LightObj obj(sq_dg, vm);
// Proceed with the serialization
return ctx->SerializeParams(vm);
}
// ------------------------------------------------------------------------------------------------
static SQInteger SqToCompactJSON(HSQUIRRELVM vm) noexcept
{
// Make sure the instance is cleaned up even in the case of exceptions
DeleteGuard< CtxJSON > sq_dg(new CtxJSON(false));
// Remember the instance, so we don't have to cast the script object back
auto ctx = sq_dg.Get();
// Turn it into a script object because it may be passed as a parameter to `_tojson` meta-methods
LightObj obj(sq_dg, vm);
// Proceed with the serialization
return ctx->SerializeParams(vm);
}
// ================================================================================================
void Register_JSON(HSQUIRRELVM vm)
{
RootTable(vm).SquirrelFunc(_SC("SqToJSON"), SqToJSON);
RootTable(vm).SquirrelFunc(_SC("SqToCompactJSON"), SqToCompactJSON);
RootTable(vm).SquirrelFunc(_SC("SqFromJSON"), SqFromJSON);
// --------------------------------------------------------------------------------------------
RootTable(vm).Bind(_SC("SqCtxJSON"),
Class< CtxJSON, NoCopy< CtxJSON > >(vm, SqCtxJSON::Str)
// Constructors
.Ctor()
.Ctor< bool >()
.Ctor< bool, StackStrF & >()
// Meta-methods
.SquirrelFunc(_SC("_typename"), &SqCtxJSON::Fn)
// Properties
.Prop(_SC("Output"), &CtxJSON::GetOutput)
.Prop(_SC("Depth"), &CtxJSON::GetDepth)
.Prop(_SC("OOA"), &CtxJSON::GetObjectOverArray, &CtxJSON::SetObjectOverArray)
.Prop(_SC("ObjectOverArray"), &CtxJSON::GetObjectOverArray, &CtxJSON::SetObjectOverArray)
// Member Methods
.SquirrelMethod< CtxJSON, &CtxJSON::SerializeParams >(_SC("Serialize"))
.SquirrelMethod< CtxJSON, &CtxJSON::PushValues >(_SC("PushValues"))
.SquirrelMethod< CtxJSON, &CtxJSON::PushElement >(_SC("PushElement"))
.Func(_SC("OpenArray"), &CtxJSON::OpenArray)
.Func(_SC("CloseArray"), &CtxJSON::CloseArray)
.Func(_SC("OpenObject"), &CtxJSON::OpenObject)
.Func(_SC("CloseObject"), &CtxJSON::CloseObject)
.Func(_SC("MakeKey"), &CtxJSON::MakeKey)
.FmtFunc(_SC("PushKey"), &CtxJSON::PushKey)
.Func(_SC("SetOOA"), &CtxJSON::SetObjectOverArray)
.Func(_SC("SetObjectOverArray"), &CtxJSON::SetObjectOverArray)
);
}
} // Namespace:: SqMod

View File

@@ -5,12 +5,281 @@
#include "Library/IO/Buffer.hpp"
// ------------------------------------------------------------------------------------------------
#include <sajson.h>
#include <functional>
// ------------------------------------------------------------------------------------------------
#include <fmt/args.h>
#include <fmt/format.h>
#include <fmt/xchar.h>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* JSON serializer. The generated JSON output is always minified for efficiency reasons.
*/
struct CtxJSON
{
/* --------------------------------------------------------------------------------------------
* Output string.
*/
String mOutput{};
/* --------------------------------------------------------------------------------------------
* Prefer a table with named members even when a simple array would do the job.
* Take a Vector3 for example. Compact array [x, y, z] or named object {x: #.#, y: #.#, z: #.#}
*/
bool mObjectOverArray{true};
/* --------------------------------------------------------------------------------------------
* How nested are we currently.
*/
uint32_t mDepth{0};
/* --------------------------------------------------------------------------------------------
* The meta-method name to use on objects.
*/
String mMetaMethod{"_tojson"};
/* --------------------------------------------------------------------------------------------
* Last pushed string value. Can be used to heck for key name in the hook.
*/
String mString{};
/* --------------------------------------------------------------------------------------------
* Internal utility used to monitor the existence of certain keys to allow overloading.
*/
std::function< void(CtxJSON&) > mKeyHook{};
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
CtxJSON() = default;
/* --------------------------------------------------------------------------------------------
* Explicit constructor.
*/
explicit CtxJSON(bool ooa)
: CtxJSON()
{
mObjectOverArray = ooa;
}
/* --------------------------------------------------------------------------------------------
* Explicit constructor.
*/
CtxJSON(bool ooa, StackStrF & mmname)
: CtxJSON()
{
mObjectOverArray = ooa;
// Allow custom metamethod names
mMetaMethod.assign(mmname.mPtr, static_cast< size_t >(mmname.mLen));
}
/* --------------------------------------------------------------------------------------------
* Internal constructor.
*/
explicit CtxJSON(std::function< void(CtxJSON&) > && kh)
: CtxJSON()
{
mKeyHook = std::move(kh);
}
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
CtxJSON(const CtxJSON &) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
CtxJSON(CtxJSON &&) noexcept = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~CtxJSON() = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
CtxJSON & operator = (const CtxJSON &) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
CtxJSON & operator = (CtxJSON &&) noexcept = default;
/* --------------------------------------------------------------------------------------------
* Retrieve the current depth.
*/
SQMOD_NODISCARD const String & GetOutput() const noexcept
{
return mOutput;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the current depth.
*/
SQMOD_NODISCARD SQInteger GetDepth() const noexcept
{
return mDepth;
}
/* --------------------------------------------------------------------------------------------
* Retrieve whether objects are preferred over arrays.
*/
SQMOD_NODISCARD bool GetObjectOverArray() const noexcept
{
return mObjectOverArray;
}
/* --------------------------------------------------------------------------------------------
* Retrieve whether objects are preferred over arrays.
*/
CtxJSON & SetObjectOverArray(bool toggle) noexcept
{
mObjectOverArray = toggle;
// Allow chaining
return *this;
}
/* --------------------------------------------------------------------------------------------
* Increase indentation by one level.
*/
void Advance() noexcept
{
++mDepth;
}
/* --------------------------------------------------------------------------------------------
* Decrease indentation by one level.
*/
void Retreat() noexcept
{
assert(mDepth > 0);
if (mDepth) --mDepth;
}
/* --------------------------------------------------------------------------------------------
* Begin writing an array.
*/
CtxJSON & OpenArray();
/* --------------------------------------------------------------------------------------------
* Stop writing an array.
*/
CtxJSON & CloseArray();
/* --------------------------------------------------------------------------------------------
* Resume writing an array.
*/
CtxJSON & ReopenArray();
/* --------------------------------------------------------------------------------------------
* Begin writing an object.
*/
CtxJSON & OpenObject();
/* --------------------------------------------------------------------------------------------
* Stop writing an object.
*/
CtxJSON & CloseObject();
/* --------------------------------------------------------------------------------------------
* Resume writing an object.
*/
CtxJSON & ReopenObject();
/* --------------------------------------------------------------------------------------------
* Begin writing a key value.
*/
CtxJSON & MakeKey();
/* --------------------------------------------------------------------------------------------
* Check whether the specified weak-ref points to a type of value that must be wrapped.
*/
SQMOD_NODISCARD static bool CheckWeakRefWrap(HSQUIRRELVM vm, SQInteger idx) noexcept;
/* --------------------------------------------------------------------------------------------
* Serialize given arguments.
*/
SQRESULT SerializeParams(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Serialize the value a specific position in the stack.
*/
SQRESULT SerializeAt(HSQUIRRELVM vm, SQInteger idx);
/* --------------------------------------------------------------------------------------------
* Serialize the array a specific position in the stack. Stack index must be absolute!
*/
SQRESULT SerializeArray(HSQUIRRELVM vm, SQInteger idx);
/* --------------------------------------------------------------------------------------------
* Serialize the table a specific position in the stack. Stack index must be absolute!
*/
SQRESULT SerializeTable(HSQUIRRELVM vm, SQInteger idx);
/* --------------------------------------------------------------------------------------------
* Serialize the instance a specific position in the stack. Stack index must be absolute!
*/
SQRESULT SerializeInstance(HSQUIRRELVM vm, SQInteger idx);
/* --------------------------------------------------------------------------------------------
* Serialize the weak-ref a specific position in the stack. Stack index must be absolute!
*/
SQRESULT SerializeWeakRef(HSQUIRRELVM vm, SQInteger idx);
/* --------------------------------------------------------------------------------------------
* Serialize a value to the current container. It assumes an array or object is currently open.
*/
SQRESULT PushValues(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Serialize a key/value pair to the current object. It assumes an object is currently open.
*/
SQRESULT PushElement(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Push a key in the output. It assumes an object was open and previous element closed properly.
*/
CtxJSON & PushKey(StackStrF & key);
/* --------------------------------------------------------------------------------------------
* Write a null value to the output.
*/
void PushNull();
/* --------------------------------------------------------------------------------------------
* Write an integer value to the output.
*/
void PushInteger(SQInteger value);
/* --------------------------------------------------------------------------------------------
* Write a single precision floating point value to the output.
*/
void PushFloat(float value);
/* --------------------------------------------------------------------------------------------
* Write a double precision floating point value to the output.
*/
void PushDouble(double value);
/* --------------------------------------------------------------------------------------------
* Write a boolean value to the output.
*/
void PushBool(bool value);
/* --------------------------------------------------------------------------------------------
* Write a string value to the output.
*/
void PushString(const SQChar * str);
/* --------------------------------------------------------------------------------------------
* Write a string value to the output.
*/
void PushString(const SQChar * str, size_t length);
};
} // Namespace:: SqMod

View File

@@ -18,7 +18,7 @@
namespace SqMod {
// ------------------------------------------------------------------------------------------------
LightObj GteMySQLFromSession(Poco::Data::SessionImpl * session)
LightObj GetMySQLFromSession(Poco::Data::SessionImpl * session)
{
// Create a reference counted connection handle instance
MySQLConnRef ref(new MySQLConnHnd(session));
@@ -551,7 +551,7 @@ char DbConvTo< char >::From(const SQChar * value, unsigned long length, enum_fie
// ------------------------------------------------------------------------------------------------
void MySQLConnHnd::GrabCurrent()
{
mErrNo = mysql_errno(mPtr);
mErrNo = mysql_errno(Access());
mErrStr.assign(mysql_error(mPtr));
}
@@ -601,7 +601,16 @@ MySQLConnHnd::MySQLConnHnd()
MySQLConnHnd::MySQLConnHnd(Poco::Data::SessionImpl * session)
: MySQLConnHnd()
{
mSession.assign(session);
mSession.assign(session, true);
// Retrieve the internal handle property
mPtr = Poco::AnyCast< MYSQL * >(session->getProperty("handle"));
}
// ------------------------------------------------------------------------------------------------
MySQLConnHnd::MySQLConnHnd(Poco::AutoPtr< Poco::Data::SessionImpl > && session)
: MySQLConnHnd()
{
mSession = std::move(session);
// Retrieve the internal handle property
mPtr = Poco::AnyCast< MYSQL * >(session->getProperty("handle"));
}
@@ -712,7 +721,7 @@ void MySQLConnHnd::Disconnect()
uint64_t MySQLConnHnd::Execute(const SQChar * query, unsigned long size)
{
// Make sure that we are connected
if (!mPtr)
if (!Access())
{
STHROWF("Invalid MySQL connection");
}
@@ -863,7 +872,7 @@ void MySQLStmtBind::SetInput(enum_field_types type, BindType * bind, const char
// ------------------------------------------------------------------------------------------------
void MySQLStmtHnd::GrabCurrent()
{
mErrNo = mysql_stmt_errno(mPtr);
mErrNo = mysql_stmt_errno(Access());
mErrStr.assign(mysql_stmt_error(mPtr));
}
@@ -889,7 +898,7 @@ void MySQLStmtHnd::ThrowCurrent(const char * act)
void MySQLStmtHnd::ValidateParam(uint32_t idx, const char * file, int32_t line) const
{
// Is the handle valid?
if (mPtr == nullptr)
if (Access() == nullptr)
{
STHROWF("Invalid MySQL statement reference =>[{}:{}]", file, line);
}
@@ -902,7 +911,7 @@ void MySQLStmtHnd::ValidateParam(uint32_t idx, const char * file, int32_t line)
void MySQLStmtHnd::ValidateParam(uint32_t idx) const
{
// Is the handle valid?
if (mPtr == nullptr)
if (Access() == nullptr)
{
STHROWF("Invalid MySQL statement reference");
}
@@ -951,7 +960,7 @@ MySQLStmtHnd::~MySQLStmtHnd()
void MySQLStmtHnd::Create(const MySQLConnRef & conn, const SQChar * query)
{
// Is this statement already created?
if (mPtr != nullptr)
if (Access() != nullptr)
{
STHROWF("MySQL statement was already created");
}
@@ -1185,7 +1194,7 @@ void MySQLResHnd::ThrowCurrent(const char * act) const
void MySQLResHnd::ValidateField(uint32_t idx, const char * file, int32_t line) const
{
// Is the handle valid?
if (mPtr == nullptr)
if (Access() == nullptr)
{
STHROWF("Invalid MySQL result-set =>[{}:{}]", file, line);
}
@@ -1198,7 +1207,7 @@ void MySQLResHnd::ValidateField(uint32_t idx, const char * file, int32_t line) c
void MySQLResHnd::ValidateField(uint32_t idx) const
{
// Is the handle valid?
if (mPtr == nullptr)
if (Access() == nullptr)
{
STHROWF("Invalid MySQL result-set");
}
@@ -1213,7 +1222,7 @@ void MySQLResHnd::ValidateField(uint32_t idx) const
uint32_t MySQLResHnd::GetFieldIndex(const SQChar * name)
{
// Validate the handle
if (!mPtr)
if (!Access())
{
STHROWF("Invalid MySQL result-set");
}
@@ -1232,7 +1241,7 @@ uint32_t MySQLResHnd::GetFieldIndex(const SQChar * name)
void MySQLResHnd::Create(const MySQLConnRef & conn)
{
// Is this result-set already created?
if (mPtr != nullptr)
if (Access() != nullptr)
{
STHROWF("MySQL result-set was already created");
}
@@ -1279,7 +1288,7 @@ void MySQLResHnd::Create(const MySQLConnRef & conn)
void MySQLResHnd::Create(const MySQLStmtRef & stmt)
{
// Is this result-set already created?
if (mPtr != nullptr)
if (Access() != nullptr)
{
STHROWF("MySQL result-set was already created");
}
@@ -1369,7 +1378,7 @@ void MySQLResHnd::Create(const MySQLStmtRef & stmt)
uint64_t MySQLResHnd::RowIndex() const
{
// Is this result-set even valid?
if (!mPtr)
if (!Access())
{
STHROWF("Invalid MySQL result-set");
}
@@ -1386,7 +1395,7 @@ uint64_t MySQLResHnd::RowIndex() const
uint64_t MySQLResHnd::RowCount() const
{
// Is this result-set even valid?
if (!mPtr)
if (!Access())
{
STHROWF("Invalid MySQL result-set");
}
@@ -1403,7 +1412,7 @@ uint64_t MySQLResHnd::RowCount() const
bool MySQLResHnd::Next()
{
// Is this result-set even valid?
if (!mPtr)
if (!Access())
{
STHROWF("Invalid MySQL result-set");
}
@@ -1425,7 +1434,7 @@ bool MySQLResHnd::Next()
bool MySQLResHnd::SetRowIndex(uint64_t index)
{
// Is this result-set even valid?
if (!mPtr)
if (!Access())
{
STHROWF("Invalid MySQL result-set");
}
@@ -1671,7 +1680,7 @@ void MySQLConnection::ValidateCreated(const char * file, int32_t line) const
{
SqThrowF(SQMOD_RTFMT("Invalid MySQL connection reference =>[{}:{}]"), file, line);
}
else if (m_Handle->mPtr == nullptr)
else if (m_Handle->Access() == nullptr)
{
SqThrowF(SQMOD_RTFMT("Invalid MySQL connection =>[{}:{}]"), file, line);
}
@@ -1683,7 +1692,7 @@ void MySQLConnection::ValidateCreated() const
{
SqThrowF(fmt::runtime("Invalid MySQL connection reference"));
}
else if (m_Handle->mPtr == nullptr)
else if (m_Handle->Access() == nullptr)
{
SqThrowF(fmt::runtime("Invalid MySQL connection"));
}
@@ -1734,7 +1743,7 @@ SQInteger MySQLConnection::Insert(const SQChar * query)
SQMOD_THROW_CURRENT(*m_Handle, "Unable to execute MySQL query");
}
// Return the identifier of the inserted row
return static_cast< SQInteger >(mysql_insert_id(m_Handle->mPtr));
return static_cast< SQInteger >(mysql_insert_id(m_Handle->Access()));
}
// ------------------------------------------------------------------------------------------------
@@ -1872,12 +1881,12 @@ SQInteger MySQLConnection::InsertF(HSQUIRRELVM vm)
// Attempt to execute the specified query
try
{
if (mysql_real_query(conn->m_Handle->mPtr, val.mPtr, static_cast<unsigned long>(val.mLen)) != 0)
if (mysql_real_query(conn->m_Handle->Access(), val.mPtr, static_cast<unsigned long>(val.mLen)) != 0)
{
SQMOD_THROW_CURRENT(*(conn->m_Handle), "Unable to execute MySQL query");
}
// Return the identifier of the inserted row
sq_pushinteger(vm, static_cast< SQInteger >(mysql_insert_id(conn->m_Handle->mPtr)));
sq_pushinteger(vm, static_cast< SQInteger >(mysql_insert_id(conn->m_Handle->Access())));
}
catch (const std::exception & e)
{
@@ -1939,7 +1948,7 @@ SQInteger MySQLConnection::QueryF(HSQUIRRELVM vm)
// Attempt to execute the specified query
try
{
if (mysql_real_query(conn->m_Handle->mPtr, val.mPtr, static_cast<unsigned long>(val.mLen)) != 0)
if (mysql_real_query(conn->m_Handle->Access(), val.mPtr, static_cast<unsigned long>(val.mLen)) != 0)
{
SQMOD_THROW_CURRENT(*(conn->m_Handle), "Unable to execute MySQL query");
}
@@ -1966,7 +1975,7 @@ LightObj MySQLConnection::EscapeString(StackStrF & str)
// Allocate a buffer for the given string
std::vector< SQChar > buffer(static_cast< size_t >(str.mLen * 2 + 1));
// Attempt to escape the specified string
const unsigned long len = mysql_real_escape_string(m_Handle->mPtr, buffer.data(), str.mPtr,
const unsigned long len = mysql_real_escape_string(m_Handle->Access(), buffer.data(), str.mPtr,
static_cast<unsigned long>(str.mLen));
// Return the resulted string
return LightObj(buffer.data(), static_cast< SQInteger >(len), str.mVM);
@@ -2515,7 +2524,7 @@ void MySQLResultSet::ValidateCreated(const char * file, int32_t line) const
{
SqThrowF(SQMOD_RTFMT("Invalid MySQL result-set reference =>[{}:{}]"), file, line);
}
else if (m_Handle->mPtr == nullptr)
else if (m_Handle->Access() == nullptr)
{
SqThrowF(SQMOD_RTFMT("Invalid MySQL result-set =>[{}:{}]"), file, line);
}
@@ -2528,7 +2537,7 @@ void MySQLResultSet::ValidateCreated() const
{
SqThrowF(fmt::runtime("Invalid MySQL result-set reference"));
}
else if (m_Handle->mPtr == nullptr)
else if (m_Handle->Access() == nullptr)
{
SqThrowF(fmt::runtime("Invalid MySQL result-set"));
}
@@ -2795,7 +2804,7 @@ void MySQLStatement::ValidateCreated(const char * file, int32_t line) const
{
SqThrowF(SQMOD_RTFMT("Invalid MySQL statement reference =>[{}:{}]"), file, line);
}
else if (m_Handle->mPtr == nullptr)
else if (m_Handle->Access() == nullptr)
{
SqThrowF(SQMOD_RTFMT("Invalid MySQL statement =>[{}:{}]"), file, line);
}
@@ -2807,7 +2816,7 @@ void MySQLStatement::ValidateCreated() const
{
SqThrowF(fmt::runtime("Invalid MySQL statement reference"));
}
else if (m_Handle->mPtr == nullptr)
else if (m_Handle->Access() == nullptr)
{
SqThrowF(fmt::runtime("Invalid MySQL statement"));
}
@@ -2882,46 +2891,46 @@ void MySQLStatement::SetConnection(const MySQLConnection & conn)
int32_t MySQLStatement::Execute()
{
// Attempt to bind the parameters
if (mysql_stmt_bind_param(SQMOD_GET_CREATED(*this)->mPtr, m_Handle->mMyBinds))
if (mysql_stmt_bind_param(SQMOD_GET_CREATED(*this)->Access(), m_Handle->mMyBinds))
{
SQMOD_THROW_CURRENT(*m_Handle, "Cannot bind MySQL statement parameters");
}
// Attempt to execute the statement
else if (mysql_stmt_execute(m_Handle->mPtr))
else if (mysql_stmt_execute(m_Handle->Access()))
{
SQMOD_THROW_CURRENT(*m_Handle, "Cannot execute MySQL statement");
}
// Return the number of rows affected by this query
return static_cast< int32_t >(mysql_stmt_affected_rows(m_Handle->mPtr));
return static_cast< int32_t >(mysql_stmt_affected_rows(m_Handle->Access()));
}
// ------------------------------------------------------------------------------------------------
uint32_t MySQLStatement::Insert()
{
// Attempt to bind the parameters
if (mysql_stmt_bind_param(SQMOD_GET_CREATED(*this)->mPtr, m_Handle->mMyBinds))
if (mysql_stmt_bind_param(SQMOD_GET_CREATED(*this)->Access(), m_Handle->mMyBinds))
{
SQMOD_THROW_CURRENT(*m_Handle, "Cannot bind MySQL statement parameters");
}
// Attempt to execute the statement
else if (mysql_stmt_execute(m_Handle->mPtr))
else if (mysql_stmt_execute(m_Handle->Access()))
{
SQMOD_THROW_CURRENT(*m_Handle, "Cannot execute MySQL statement");
}
// Return the identifier of the inserted row
return static_cast< uint32_t >(mysql_stmt_insert_id(m_Handle->mPtr));
return static_cast< uint32_t >(mysql_stmt_insert_id(m_Handle->Access()));
}
// ------------------------------------------------------------------------------------------------
MySQLResultSet MySQLStatement::Query()
{
// Attempt to bind the parameters
if (mysql_stmt_bind_param(SQMOD_GET_CREATED(*this)->mPtr, m_Handle->mMyBinds))
if (mysql_stmt_bind_param(SQMOD_GET_CREATED(*this)->Access(), m_Handle->mMyBinds))
{
SQMOD_THROW_CURRENT(*m_Handle, "Cannot bind MySQL statement parameters");
}
// Attempt to execute the statement
else if (mysql_stmt_execute(m_Handle->mPtr))
else if (mysql_stmt_execute(m_Handle->Access()))
{
SQMOD_THROW_CURRENT(*m_Handle, "Cannot execute MySQL statement");
}

View File

@@ -268,6 +268,11 @@ public:
*/
explicit MySQLConnHnd(Poco::Data::SessionImpl * session);
/* --------------------------------------------------------------------------------------------
* Explicit constructor.
*/
explicit MySQLConnHnd(Poco::AutoPtr< Poco::Data::SessionImpl > && session);
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
@@ -301,6 +306,20 @@ public:
* Execute a query on the server.
*/
uint64_t Execute(const SQChar * query, unsigned long size = 0UL);
/* --------------------------------------------------------------------------------------------
* Access the connection pointer.
*/
SQMOD_NODISCARD Pointer Access() const
{
if (!mSession.isNull()) {
// Only reason this is necessary is to dirty the connection handle access time-stamp
// So it won't be closed/collected when it comes from a connection/session-pool
[[maybe_unused]] auto _ = mSession->isConnected();
}
// We yield access to the pointer anyway
return mPtr;
}
};
/* ------------------------------------------------------------------------------------------------
@@ -436,11 +455,11 @@ public:
// --------------------------------------------------------------------------------------------
unsigned long mParams; // Number of parameters in the statement.
MySQLStmtBind * mBinds; // List of parameter binds.
MySQLStmtBind * mBinds; // List of parameter binds.
BindType * mMyBinds; // List of parameter binds.
// --------------------------------------------------------------------------------------------
MySQLConnRef mConnection; // Reference to the associated connection.
MySQLConnRef mConnection; // Reference to the associated connection.
String mQuery; // The query string.
/* --------------------------------------------------------------------------------------------
@@ -488,6 +507,20 @@ public:
* Create the actual statement.
*/
void Create(const MySQLConnRef & conn, const SQChar * query);
/* --------------------------------------------------------------------------------------------
* Access the statement pointer.
*/
SQMOD_NODISCARD Pointer Access() const
{
if (bool(mConnection) && !(mConnection->mSession.isNull())) {
// Only reason this is necessary is to dirty the connection handle access time-stamp
// So it won't be closed/collected when it comes from a connection/session-pool
[[maybe_unused]] auto _ = mConnection->mSession->isConnected();
}
// We yield access to the pointer anyway
return mPtr;
}
};
/* ------------------------------------------------------------------------------------------------
@@ -631,13 +664,13 @@ public:
uint32_t mFieldCount; // Number of fields in the result-set.
unsigned long * mLengths; // Data length when the result-set came from a connection.
FieldType * mFields; // Fields in the results set.
MySQLResBind * mBinds; // Bind wrappers.
MySQLResBind * mBinds; // Bind wrappers.
BindType * mMyBinds; // Bind points.
RowType mRow; // Row data.
// --------------------------------------------------------------------------------------------
MySQLConnRef mConnection; // Associated connection.
MySQLStmtRef mStatement; // Associated statement.
MySQLConnRef mConnection; // Associated connection.
MySQLStmtRef mStatement; // Associated statement.
IndexMap mIndexes; // MySQLField names and their associated index.
public:
@@ -718,6 +751,19 @@ public:
*/
bool SetRowIndex(uint64_t index);
/* --------------------------------------------------------------------------------------------
* Access the resource pointer.
*/
SQMOD_NODISCARD Pointer Access() const
{
if (bool(mConnection) && !(mConnection->mSession.isNull())) {
// Only reason this is necessary is to dirty the connection handle access time-stamp
// So it won't be closed/collected when it comes from a connection/session-pool
[[maybe_unused]] auto _ = mConnection->mSession->isConnected();
}
// We yield access to the pointer anyway
return mPtr;
}
};
/* ------------------------------------------------------------------------------------------------
@@ -1239,7 +1285,7 @@ public:
*/
SQMOD_NODISCARD const SQChar * ToString() const
{
return m_Handle ? mysql_get_host_info(m_Handle->mPtr) : _SC("");
return m_Handle ? mysql_get_host_info(m_Handle->Access()) : _SC("");
}
/* --------------------------------------------------------------------------------------------
@@ -1268,7 +1314,7 @@ public:
*/
SQMOD_NODISCARD bool IsConnected() const
{
return m_Handle && (m_Handle->mPtr != nullptr);
return m_Handle && (m_Handle->Access() != nullptr);
}
/* --------------------------------------------------------------------------------------------
@@ -1468,7 +1514,7 @@ public:
{
// Attempt to toggle auto-commit if necessary
if (SQMOD_GET_CREATED(*this)->mAutoCommit != toggle &&
mysql_autocommit(m_Handle->mPtr, static_cast< MySQLStmtBind::BoolType >(toggle)) != 0)
mysql_autocommit(m_Handle->Access(), static_cast< MySQLStmtBind::BoolType >(toggle)) != 0)
{
SQMOD_THROW_CURRENT(*m_Handle, "Cannot toggle auto-commit");
}

View File

@@ -22,6 +22,8 @@ void InitializeNet()
#endif
#ifndef NO_SSL
f |= MG_FEATURES_SSL;
#else
OutputMessage("Network compiled without SSL support.");
#endif
#ifndef NO_CGI
f |= MG_FEATURES_CGI;
@@ -89,6 +91,9 @@ WebSocketClient & WebSocketClient::Connect()
{
STHROWF("Connection failed: {}", err_buf);
}
// Reset memebrs
mClosing.store(false);
mClosed.store(false);
// Allow chaining
return *this;
}
@@ -113,6 +118,9 @@ WebSocketClient & WebSocketClient::ConnectExt()
{
STHROWF("Connection failed: {}", err_buf);
}
// Reset memebrs
mClosing.store(false);
mClosed.store(false);
// Allow chaining
return *this;
}
@@ -129,6 +137,11 @@ int WebSocketClient::DataHandler(int flags, char * data, size_t data_len) noexce
{
LogFtl("Failed to queue web-socket data");
}
// Should we auto-close the connection
if (((flags & 0xF) == MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE) && mAutoClose.load() == true)
{
return 0;
}
// Return 1 to keep the connection open
return 1;
}
@@ -167,6 +180,7 @@ void Register_Net(HSQUIRRELVM vm)
.Prop(_SC("OnClose"), &WebSocketClient::GetOnClose, &WebSocketClient::SetOnClose)
.Prop(_SC("Valid"), &WebSocketClient::IsValid)
.Prop(_SC("Closing"), &WebSocketClient::IsClosing)
.Prop(_SC("AutoClose"), &WebSocketClient::GetAutoClose, &WebSocketClient::SetAutoClose)
// Member Methods
.FmtFunc(_SC("SetTag"), &WebSocketClient::ApplyTag)
.FmtFunc(_SC("SetData"), &WebSocketClient::ApplyData)

View File

@@ -154,6 +154,18 @@ struct WebSocketClient : public SqChainedInstances< WebSocketClient >
*/
std::atomic< bool > mClosing{false};
/* --------------------------------------------------------------------------------------------
* Whether the closing callback was inoked (avoid recursive calls).
*/
std::atomic< bool > mClosed{false};
/* --------------------------------------------------------------------------------------------
* Whether to not keep the connection open after receiving the close event.
* Internally this event is ignored but if set to true the connection is immediatelly closed
* in the internal event handler, before the event may reach the script callback.
*/
std::atomic< bool > mAutoClose{false};
/* --------------------------------------------------------------------------------------------
* Server host to connect to, i.e. "echo.websocket.org" or "192.168.1.1" or "localhost".
*/
@@ -179,7 +191,8 @@ struct WebSocketClient : public SqChainedInstances< WebSocketClient >
*/
WebSocketClient()
: Base(), mHandle(nullptr), mQueue(1024), mOnData(), mOnClose(), mTag(), mData()
, mPort(0), mSecure(false), mClosing(false), mHost(), mPath(), mOrigin(), mExtensions()
, mPort(0), mSecure(false), mClosing(false), mClosed(false), mAutoClose(false)
, mHost(), mPath(), mOrigin(), mExtensions()
{
ChainInstance(); // Remember this instance
}
@@ -189,7 +202,7 @@ struct WebSocketClient : public SqChainedInstances< WebSocketClient >
*/
WebSocketClient(StackStrF & host, uint16_t port, StackStrF & path)
: Base(), mHandle(nullptr), mQueue(1024), mOnData(), mOnClose(), mTag(), mData()
, mPort(port), mSecure(false), mClosing(false)
, mPort(port), mSecure(false), mClosing(false), mClosed(false), mAutoClose(false)
, mHost(host.mPtr, host.GetSize())
, mPath(path.mPtr, path.GetSize())
, mOrigin(), mExtensions()
@@ -202,7 +215,7 @@ struct WebSocketClient : public SqChainedInstances< WebSocketClient >
*/
WebSocketClient(StackStrF & host, uint16_t port, StackStrF & path, bool secure)
: Base(), mHandle(nullptr), mQueue(1024), mOnData(), mOnClose(), mTag(), mData()
, mPort(port), mSecure(secure), mClosing(false)
, mPort(port), mSecure(secure), mClosing(false), mClosed(false), mAutoClose(false)
, mHost(host.mPtr, host.GetSize())
, mPath(path.mPtr, path.GetSize())
, mOrigin(), mExtensions()
@@ -215,7 +228,7 @@ struct WebSocketClient : public SqChainedInstances< WebSocketClient >
*/
WebSocketClient(StackStrF & host, uint16_t port, StackStrF & path, bool secure, StackStrF & origin)
: Base(), mHandle(nullptr), mQueue(1024), mOnData(), mOnClose(), mTag(), mData()
, mPort(port), mSecure(secure), mClosing(false)
, mPort(port), mSecure(secure), mClosing(false), mClosed(false), mAutoClose(false)
, mHost(host.mPtr, host.GetSize())
, mPath(path.mPtr, path.GetSize())
, mOrigin(origin.mPtr, origin.GetSize())
@@ -229,7 +242,7 @@ struct WebSocketClient : public SqChainedInstances< WebSocketClient >
*/
WebSocketClient(StackStrF & host, uint16_t port, StackStrF & path, bool secure, StackStrF & origin, StackStrF & ext)
: Base(), mHandle(nullptr), mQueue(1024), mOnData(), mOnClose(), mTag(), mData()
, mPort(port), mSecure(secure), mClosing(false)
, mPort(port), mSecure(secure), mClosing(false), mClosed(false), mAutoClose(false)
, mHost(host.mPtr, host.GetSize())
, mPath(path.mPtr, path.GetSize())
, mOrigin(origin.mPtr, origin.GetSize())
@@ -289,6 +302,22 @@ struct WebSocketClient : public SqChainedInstances< WebSocketClient >
return mClosing.load();
}
/* --------------------------------------------------------------------------------------------
* Retrieve whether auto-closing is enabled or not.
*/
SQMOD_NODISCARD bool GetAutoClose() const
{
return mAutoClose.load();
}
/* --------------------------------------------------------------------------------------------
* Modify whether auto-closing is enabled or not.
*/
void SetAutoClose(bool toggle)
{
mAutoClose.store(toggle);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the associated user tag.
*/
@@ -648,7 +677,7 @@ struct WebSocketClient : public SqChainedInstances< WebSocketClient >
/* --------------------------------------------------------------------------------------------
* Sends the contents of the given buffer through the socket as a single frame.
*/
SQMOD_NODISCARD SQInteger SendOpCode(SqBuffer & buf, SQInteger opcode)
SQMOD_NODISCARD SQInteger SendOpCode(SQInteger opcode)
{
return mg_websocket_client_write(Valid(), static_cast< int >(opcode), nullptr, 0);
}
@@ -706,9 +735,12 @@ struct WebSocketClient : public SqChainedInstances< WebSocketClient >
}
}
// Is the server closing the connection?
if (closing && !mOnClose.IsNull())
if (closing && !mClosed.load() && !mOnClose.IsNull())
{
mOnClose.Execute(); // Let the user know
// Let the user know
mOnClose.Execute();
// Prevent calling this callback again
mClosed.store(true);
}
}

View File

@@ -363,7 +363,7 @@ static const EnumElement g_MainEnum[] = {
};
// ------------------------------------------------------------------------------------------------
LightObj GteSQLiteFromSession(Poco::Data::SessionImpl * session)
LightObj GetSQLiteFromSession(Poco::Data::SessionImpl * session)
{
// Create a reference counted connection handle instance
SQLiteConnRef ref(new SQLiteConnHnd(session));
@@ -623,9 +623,18 @@ SQLiteConnHnd::SQLiteConnHnd()
SQLiteConnHnd::SQLiteConnHnd(Poco::Data::SessionImpl * session)
: SQLiteConnHnd()
{
mSession.assign(session);
mSession.assign(session, true);
// Retrieve the internal handle property
mPtr = Poco::AnyCast< sqlite3 * >(session->getProperty("handle"));
mPtr = Poco::AnyCast< sqlite3 * >(mSession->getProperty("handle"));
}
// ------------------------------------------------------------------------------------------------
SQLiteConnHnd::SQLiteConnHnd(Poco::AutoPtr< Poco::Data::SessionImpl > && session)
: SQLiteConnHnd()
{
mSession = std::move(session);
// Retrieve the internal handle property
mPtr = Poco::AnyCast< sqlite3 * >(mSession->getProperty("handle"));
}
// ------------------------------------------------------------------------------------------------
@@ -656,7 +665,7 @@ SQLiteConnHnd::~SQLiteConnHnd()
void SQLiteConnHnd::Create(const SQChar * name, int32_t flags, const SQChar * vfs)
{
// Make sure a previous connection doesn't exist
if (mPtr)
if (Access())
{
STHROWF("Unable to connect to database. Database already connected");
}
@@ -689,7 +698,7 @@ void SQLiteConnHnd::Create(const SQChar * name, int32_t flags, const SQChar * vf
int32_t SQLiteConnHnd::Flush(uint32_t num, Object & env, Function & func)
{
// Do we even have a valid connection?
if (!mPtr)
if (!Access())
{
return -1; // No connection!
}
@@ -780,7 +789,7 @@ int32_t SQLiteConnHnd::Flush(uint32_t num, Object & env, Function & func)
SQLiteStmtHnd::SQLiteStmtHnd(SQLiteConnRef conn)
: mPtr(nullptr)
, mStatus(SQLITE_OK)
, mConn(std::move(conn))
, mConnection(std::move(conn))
, mQuery()
, mColumns(0)
, mParameters(0)
@@ -800,7 +809,7 @@ SQLiteStmtHnd::~SQLiteStmtHnd()
// Attempt to finalize the statement
if ((sqlite3_finalize(mPtr)) != SQLITE_OK)
{
LogErr("Unable to finalize SQLite statement [%s]", mConn->ErrMsg());
LogErr("Unable to finalize SQLite statement [%s]", mConnection->ErrMsg());
}
}
}
@@ -809,12 +818,12 @@ SQLiteStmtHnd::~SQLiteStmtHnd()
void SQLiteStmtHnd::Create(const SQChar * query, SQInteger length)
{
// Make sure a previous statement doesn't exist
if (mPtr)
if (Access())
{
STHROWF("Unable to prepare statement. Statement already prepared");
}
// Is the specified database connection is valid?
else if (!mConn)
else if (!mConnection)
{
STHROWF("Unable to prepare statement. Invalid connection handle");
}
@@ -826,7 +835,7 @@ void SQLiteStmtHnd::Create(const SQChar * query, SQInteger length)
// Save the query string
mQuery.assign(query, static_cast< size_t >(length));
// Attempt to prepare a statement with the specified query string
if ((mStatus = sqlite3_prepare_v2(mConn->mPtr, mQuery.c_str(), ConvTo< int32_t >::From(mQuery.size()),
if ((mStatus = sqlite3_prepare_v2(mConnection->mPtr, mQuery.c_str(), ConvTo< int32_t >::From(mQuery.size()),
&mPtr, nullptr)) != SQLITE_OK)
{
// Clear the query string since it failed
@@ -834,7 +843,7 @@ void SQLiteStmtHnd::Create(const SQChar * query, SQInteger length)
// Explicitly make sure the handle is null
mPtr = nullptr;
// Now it's safe to throw the error
STHROWF("Unable to prepare statement [{}]", mConn->ErrMsg());
STHROWF("Unable to prepare statement [{}]", mConnection->ErrMsg());
}
else
{
@@ -849,7 +858,7 @@ void SQLiteStmtHnd::Create(const SQChar * query, SQInteger length)
int32_t SQLiteStmtHnd::GetColumnIndex(const SQChar * name, SQInteger length)
{
// Validate the handle
if (!mPtr)
if (!Access())
{
STHROWF("Invalid SQLite statement");
}
@@ -887,25 +896,25 @@ int32_t SQLiteStmtHnd::GetColumnIndex(const SQChar * name, SQInteger length)
// ------------------------------------------------------------------------------------------------
const char * SQLiteStmtHnd::ErrStr() const
{
return mConn ? sqlite3_errstr(sqlite3_errcode(mConn->mPtr)) : _SC("");
return mConnection ? sqlite3_errstr(sqlite3_errcode(mConnection->Access())) : _SC("");
}
// ------------------------------------------------------------------------------------------------
const char * SQLiteStmtHnd::ErrMsg() const
{
return mConn ? sqlite3_errmsg(mConn->mPtr) : _SC("");
return mConnection ? sqlite3_errmsg(mConnection->Access()) : _SC("");
}
// ------------------------------------------------------------------------------------------------
int32_t SQLiteStmtHnd::ErrNo() const
{
return mConn ? sqlite3_errcode(mConn->mPtr) : SQLITE_NOMEM;
return mConnection ? sqlite3_errcode(mConnection->Access()) : SQLITE_NOMEM;
}
// ------------------------------------------------------------------------------------------------
int32_t SQLiteStmtHnd::ExErrNo() const
{
return mConn ? sqlite3_extended_errcode(mConn->mPtr) : SQLITE_NOMEM;
return mConnection ? sqlite3_extended_errcode(mConnection->Access()) : SQLITE_NOMEM;
}
// ------------------------------------------------------------------------------------------------
@@ -947,7 +956,7 @@ void SQLiteConnection::ValidateCreated(const char * file, int32_t line) const
{
SqThrowF(SQMOD_RTFMT("Invalid SQLite connection reference =>[{}:{}]"), file, line);
}
else if (m_Handle->mPtr == nullptr)
else if (m_Handle->Access() == nullptr)
{
SqThrowF(SQMOD_RTFMT("Invalid SQLite connection =>[{}:{}]"), file, line);
}
@@ -959,7 +968,7 @@ void SQLiteConnection::ValidateCreated() const
{
SqThrowF(fmt::runtime("Invalid SQLite connection reference"));
}
else if (m_Handle->mPtr == nullptr)
else if (m_Handle->Access() == nullptr)
{
SqThrowF(fmt::runtime("Invalid SQLite connection"));
}
@@ -1055,14 +1064,14 @@ int32_t SQLiteConnection::Exec(StackStrF & str)
{
SQMOD_VALIDATE_CREATED(*this);
// Attempt to execute the specified query
m_Handle->mStatus = sqlite3_exec(m_Handle->mPtr, str.mPtr, nullptr, nullptr, nullptr);
m_Handle->mStatus = sqlite3_exec(m_Handle->Access(), str.mPtr, nullptr, nullptr, nullptr);
// Validate the execution result
if (m_Handle->mStatus != SQLITE_OK)
{
STHROWF("Unable to execute query [{}]", m_Handle->ErrMsg());
}
// Return rows affected by this query
return sqlite3_changes(m_Handle->mPtr);
return sqlite3_changes(m_Handle->Access());
}
// ------------------------------------------------------------------------------------------------
@@ -1135,12 +1144,12 @@ void SQLiteConnection::SetTracing(bool SQ_UNUSED_ARG(toggle)) // NOLINT(readabil
// Do we have to disable it?
else if (m_Handle->mTrace)
{
sqlite3_trace(m_Handle->mPtr, nullptr, nullptr);
sqlite3_trace(m_Handle->Access(), nullptr, nullptr);
}
// Go ahead and enable tracing
else
{
sqlite3_trace(m_Handle->mPtr, &SQLiteConnection::TraceOutput, nullptr);
sqlite3_trace(m_Handle->Access(), &SQLiteConnection::TraceOutput, nullptr);
}
#endif
}
@@ -1159,12 +1168,12 @@ void SQLiteConnection::SetProfiling(bool SQ_UNUSED_ARG(toggle)) // NOLINT(readab
// Do we have to disable it?
else if (m_Handle->mProfile)
{
sqlite3_profile(m_Handle->mPtr, nullptr, nullptr);
sqlite3_profile(m_Handle->Access(), nullptr, nullptr);
}
// Go ahead and enable profiling
else
{
sqlite3_profile(m_Handle->mPtr, &SQLiteConnection::ProfileOutput, nullptr);
sqlite3_profile(m_Handle->Access(), &SQLiteConnection::ProfileOutput, nullptr);
}
#endif
}
@@ -1174,7 +1183,7 @@ void SQLiteConnection::SetBusyTimeout(int32_t millis)
{
SQMOD_VALIDATE_CREATED(*this);
// Apply the requested timeout
if ((m_Handle->mStatus = sqlite3_busy_timeout(m_Handle->mPtr, millis)) != SQLITE_OK)
if ((m_Handle->mStatus = sqlite3_busy_timeout(m_Handle->Access(), millis)) != SQLITE_OK)
{
STHROWF("Unable to set busy timeout [{}]", m_Handle->ErrMsg());
}
@@ -1295,7 +1304,7 @@ void SQLiteParameter::ValidateCreated(const char * file, int32_t line) const
{
SqThrowF(SQMOD_RTFMT("Invalid SQLite statement reference =>[{}:{}]"), file, line);
}
else if (m_Handle->mPtr == nullptr)
else if (m_Handle->Access() == nullptr)
{
SqThrowF(SQMOD_RTFMT("Invalid SQLite statement =>[{}:{}]"), file, line);
}
@@ -1312,7 +1321,7 @@ void SQLiteParameter::ValidateCreated() const
{
SqThrowF(fmt::runtime("Invalid SQLite statement reference"));
}
else if (m_Handle->mPtr == nullptr)
else if (m_Handle->Access() == nullptr)
{
SqThrowF(fmt::runtime("Invalid SQLite statement"));
}
@@ -1403,7 +1412,7 @@ void SQLiteParameter::SetIndex(const Object & param)
STHROWF("Cannot use an empty parameter name");
}
// Attempt to find a parameter with the specified name
idx = sqlite3_bind_parameter_index(SQMOD_GET_CREATED(*this)->mPtr, val.mPtr);
idx = sqlite3_bind_parameter_index(SQMOD_GET_CREATED(*this)->Access(), val.mPtr);
} break;
// Is this an integer value? (or at least can be easily converted to one)
case OT_INTEGER:
@@ -1435,7 +1444,7 @@ void SQLiteParameter::SetIndex(const Object & param)
// Attempt to find a parameter with the specified name
else
{
idx = sqlite3_bind_parameter_index(SQMOD_GET_CREATED(*this)->mPtr, val.mPtr);
idx = sqlite3_bind_parameter_index(SQMOD_GET_CREATED(*this)->Access(), val.mPtr);
}
} break;
// We don't recognize this kind of value!
@@ -1458,7 +1467,7 @@ Object SQLiteParameter::GetStatement() const
// ------------------------------------------------------------------------------------------------
Object SQLiteParameter::GetConnection() const
{
return GetConnectionObj(SQMOD_GET_VALID(*this)->mConn);
return GetConnectionObj(SQMOD_GET_VALID(*this)->mConnection);
}
// ------------------------------------------------------------------------------------------------
@@ -1503,7 +1512,7 @@ void SQLiteParameter::SetBool(bool value)
{
SQMOD_VALIDATE_CREATED(*this);
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_int(m_Handle->mPtr, m_Index, value);
m_Handle->mStatus = sqlite3_bind_int(m_Handle->Access(), m_Index, value);
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -1516,7 +1525,7 @@ void SQLiteParameter::SetChar(SQInteger value)
{
SQMOD_VALIDATE_CREATED(*this);
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_int(m_Handle->mPtr, m_Index, ConvTo< SQChar >::From(value));
m_Handle->mStatus = sqlite3_bind_int(m_Handle->Access(), m_Index, ConvTo< SQChar >::From(value));
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -1529,7 +1538,7 @@ void SQLiteParameter::SetInteger(SQInteger value)
{
SQMOD_VALIDATE_CREATED(*this);
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_integer(m_Handle->mPtr, m_Index, value);
m_Handle->mStatus = sqlite3_bind_integer(m_Handle->Access(), m_Index, value);
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -1542,7 +1551,7 @@ void SQLiteParameter::SetInt8(SQInteger value)
{
SQMOD_VALIDATE_CREATED(*this);
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_int(m_Handle->mPtr, m_Index, ConvTo< int8_t >::From(value));
m_Handle->mStatus = sqlite3_bind_int(m_Handle->Access(), m_Index, ConvTo< int8_t >::From(value));
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -1555,7 +1564,7 @@ void SQLiteParameter::SetUint8(SQInteger value)
{
SQMOD_VALIDATE_CREATED(*this);
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_int(m_Handle->mPtr, m_Index, ConvTo< uint8_t >::From(value));
m_Handle->mStatus = sqlite3_bind_int(m_Handle->Access(), m_Index, ConvTo< uint8_t >::From(value));
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -1568,7 +1577,7 @@ void SQLiteParameter::SetInt16(SQInteger value)
{
SQMOD_VALIDATE_CREATED(*this);
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_int(m_Handle->mPtr, m_Index, ConvTo< int16_t >::From(value));
m_Handle->mStatus = sqlite3_bind_int(m_Handle->Access(), m_Index, ConvTo< int16_t >::From(value));
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -1581,7 +1590,7 @@ void SQLiteParameter::SetUint16(SQInteger value)
{
SQMOD_VALIDATE_CREATED(*this);
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_int(m_Handle->mPtr, m_Index, ConvTo< uint16_t >::From(value));
m_Handle->mStatus = sqlite3_bind_int(m_Handle->Access(), m_Index, ConvTo< uint16_t >::From(value));
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -1594,7 +1603,7 @@ void SQLiteParameter::SetInt32(SQInteger value)
{
SQMOD_VALIDATE_CREATED(*this);
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_int(m_Handle->mPtr, m_Index, ConvTo< int32_t >::From(value));
m_Handle->mStatus = sqlite3_bind_int(m_Handle->Access(), m_Index, ConvTo< int32_t >::From(value));
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -1607,7 +1616,7 @@ void SQLiteParameter::SetUint32(SQInteger value)
{
SQMOD_VALIDATE_CREATED(*this);
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_int(m_Handle->mPtr, m_Index, static_cast< int32_t >(ConvTo< uint32_t >::From(value)));
m_Handle->mStatus = sqlite3_bind_int(m_Handle->Access(), m_Index, static_cast< int32_t >(ConvTo< uint32_t >::From(value)));
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -1620,7 +1629,7 @@ void SQLiteParameter::SetInt64(SQInteger value)
{
SQMOD_VALIDATE_CREATED(*this);
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_int64(m_Handle->mPtr, m_Index, value);
m_Handle->mStatus = sqlite3_bind_int64(m_Handle->Access(), m_Index, value);
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -1633,7 +1642,7 @@ void SQLiteParameter::SetUint64(SQInteger value)
{
SQMOD_VALIDATE_CREATED(*this);
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_int64(m_Handle->mPtr, m_Index, value);
m_Handle->mStatus = sqlite3_bind_int64(m_Handle->Access(), m_Index, value);
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -1646,7 +1655,7 @@ void SQLiteParameter::SetFloat(SQFloat value)
{
SQMOD_VALIDATE_CREATED(*this);
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_double(m_Handle->mPtr, m_Index, value);
m_Handle->mStatus = sqlite3_bind_double(m_Handle->Access(), m_Index, value);
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -1659,7 +1668,7 @@ void SQLiteParameter::SetFloat32(SQFloat value)
{
SQMOD_VALIDATE_CREATED(*this);
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_double(m_Handle->mPtr, m_Index, ConvTo< float >::From(value));
m_Handle->mStatus = sqlite3_bind_double(m_Handle->Access(), m_Index, ConvTo< float >::From(value));
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -1672,7 +1681,7 @@ void SQLiteParameter::SetFloat64(SQFloat value)
{
SQMOD_VALIDATE_CREATED(*this);
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_double(m_Handle->mPtr, m_Index, value);
m_Handle->mStatus = sqlite3_bind_double(m_Handle->Access(), m_Index, value);
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -1685,7 +1694,7 @@ void SQLiteParameter::SetString(StackStrF & value)
{
SQMOD_VALIDATE_CREATED(*this);
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_text(m_Handle->mPtr, m_Index, value.mPtr, static_cast<int>(value.mLen), SQLITE_TRANSIENT);
m_Handle->mStatus = sqlite3_bind_text(m_Handle->Access(), m_Index, value.mPtr, static_cast<int>(value.mLen), SQLITE_TRANSIENT);
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -1698,7 +1707,7 @@ void SQLiteParameter::SetStringRaw(const SQChar * value, SQInteger length)
{
SQMOD_VALIDATE_CREATED(*this);
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_text(m_Handle->mPtr, m_Index, value, static_cast<int>(length), SQLITE_TRANSIENT);
m_Handle->mStatus = sqlite3_bind_text(m_Handle->Access(), m_Index, value, static_cast<int>(length), SQLITE_TRANSIENT);
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -1711,7 +1720,7 @@ void SQLiteParameter::SetZeroBlob(SQInteger size)
{
SQMOD_VALIDATE_CREATED(*this);
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_zeroblob(m_Handle->mPtr, m_Index, ConvTo< int32_t >::From(size));
m_Handle->mStatus = sqlite3_bind_zeroblob(m_Handle->Access(), m_Index, ConvTo< int32_t >::From(size));
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -1743,7 +1752,7 @@ void SQLiteParameter::SetBlob(const Object & value)
len = sqstd_getblobsize(vm, -1);
}
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_blob(m_Handle->mPtr, m_Index, ptr, static_cast<int>(len), SQLITE_TRANSIENT);
m_Handle->mStatus = sqlite3_bind_blob(m_Handle->Access(), m_Index, ptr, static_cast<int>(len), SQLITE_TRANSIENT);
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -1757,9 +1766,9 @@ void SQLiteParameter::SetData(const SqBuffer & value)
Buffer & buff = *value.GetRef();
// Attempt to bind the specified value
#ifdef _SQ64
m_Handle->mStatus = sqlite3_bind_blob64(m_Handle->mPtr, m_Index, buff.Data(), buff.Position(), SQLITE_TRANSIENT);
m_Handle->mStatus = sqlite3_bind_blob64(m_Handle->Access(), m_Index, buff.Data(), buff.Position(), SQLITE_TRANSIENT);
#else
m_Handle->mStatus = sqlite3_bind_blob(m_Handle->mPtr, m_Index, buff.Data(), buff.Position(), SQLITE_TRANSIENT);
m_Handle->mStatus = sqlite3_bind_blob(m_Handle->Access(), m_Index, buff.Data(), buff.Position(), SQLITE_TRANSIENT);
#endif
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
@@ -1788,9 +1797,9 @@ void SQLiteParameter::SetDataEx(const SqBuffer & value, SQInteger offset, SQInte
}
// Attempt to bind the specified value
#ifdef _SQ64
m_Handle->mStatus = sqlite3_bind_blob64(m_Handle->mPtr, m_Index, (buff.Data() + offset), static_cast< sqlite3_uint64 >(offset + length), SQLITE_TRANSIENT);
m_Handle->mStatus = sqlite3_bind_blob64(m_Handle->Access(), m_Index, (buff.Data() + offset), static_cast< sqlite3_uint64 >(offset + length), SQLITE_TRANSIENT);
#else
m_Handle->mStatus = sqlite3_bind_blob(m_Handle->mPtr, m_Index, (buff.Data() + offset), static_cast< int >(offset + length), SQLITE_TRANSIENT);
m_Handle->mStatus = sqlite3_bind_blob(m_Handle->Access(), m_Index, (buff.Data() + offset), static_cast< int >(offset + length), SQLITE_TRANSIENT);
#endif
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
@@ -1806,7 +1815,7 @@ void SQLiteParameter::SetDate(const Date & value)
// Attempt to generate the specified date string
auto str = fmt::format("{} 00:00:00", value.ToString());
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_text(m_Handle->mPtr, m_Index, str.data(), static_cast< int >(str.size()), SQLITE_TRANSIENT);
m_Handle->mStatus = sqlite3_bind_text(m_Handle->Access(), m_Index, str.data(), static_cast< int >(str.size()), SQLITE_TRANSIENT);
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -1829,7 +1838,7 @@ void SQLiteParameter::SetDateEx(SQInteger year, SQInteger month, SQInteger day)
// Attempt to generate the specified date string
auto str = fmt::format("{}-{}-{} 00:00:00", y, m, d);
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_text(m_Handle->mPtr, m_Index, str.data(), static_cast< int >(str.size()), SQLITE_TRANSIENT);
m_Handle->mStatus = sqlite3_bind_text(m_Handle->Access(), m_Index, str.data(), static_cast< int >(str.size()), SQLITE_TRANSIENT);
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -1842,7 +1851,7 @@ void SQLiteParameter::SetTime(const Time & value)
{
SQMOD_VALIDATE_CREATED(*this);
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_int(m_Handle->mPtr, m_Index, static_cast<int>(value.GetTimestamp().GetSecondsI()));
m_Handle->mStatus = sqlite3_bind_int(m_Handle->Access(), m_Index, static_cast<int>(value.GetTimestamp().GetSecondsI()));
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -1874,7 +1883,7 @@ void SQLiteParameter::SetTimeEx(SQInteger hour, SQInteger minute, SQInteger seco
STHROWF("Second value is out of range: {} >= 60", s);
}
// Calculate the number of seconds in the specified time and bind the resulted value
m_Handle->mStatus = sqlite3_bind_int(m_Handle->mPtr, m_Index, (h * (60 * 60)) + (m * 60) + s);
m_Handle->mStatus = sqlite3_bind_int(m_Handle->Access(), m_Index, (h * (60 * 60)) + (m * 60) + s);
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -1922,7 +1931,7 @@ void SQLiteParameter::SetDatetimeEx(SQInteger year, SQInteger month, SQInteger d
// Attempt to generate the specified date string
auto str = fmt::format(_SC("{:04}-{:02}-{:02} {:02}:{:02}:{:02}"), y, mo, d, h, mi, s);
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_text(m_Handle->mPtr, m_Index, str.data(), static_cast< int >(str.size()), SQLITE_TRANSIENT);
m_Handle->mStatus = sqlite3_bind_text(m_Handle->Access(), m_Index, str.data(), static_cast< int >(str.size()), SQLITE_TRANSIENT);
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -1935,7 +1944,7 @@ void SQLiteParameter::SetNow()
{
SQMOD_VALIDATE_CREATED(*this);
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_int(m_Handle->mPtr, m_Index,
m_Handle->mStatus = sqlite3_bind_int(m_Handle->Access(), m_Index,
static_cast< int32_t >(std::time(nullptr)));
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
@@ -1949,7 +1958,7 @@ void SQLiteParameter::SetNull()
{
SQMOD_VALIDATE_CREATED(*this);
// Attempt to bind the specified value
m_Handle->mStatus = sqlite3_bind_null(m_Handle->mPtr, m_Index);
m_Handle->mStatus = sqlite3_bind_null(m_Handle->Access(), m_Index);
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -2001,7 +2010,7 @@ void SQLiteColumn::ValidateCreated(const char * file, int32_t line) const
{
SqThrowF(SQMOD_RTFMT("Invalid SQLite statement reference =>[{}:{}]"), file, line);
}
else if (m_Handle->mPtr == nullptr)
else if (m_Handle->Access() == nullptr)
{
SqThrowF(SQMOD_RTFMT("Invalid SQLite statement =>[{}:{}]"), file, line);
}
@@ -2018,7 +2027,7 @@ void SQLiteColumn::ValidateCreated() const
{
SqThrowF(fmt::runtime("Invalid SQLite statement reference"));
}
else if (m_Handle->mPtr == nullptr)
else if (m_Handle->Access() == nullptr)
{
SqThrowF(fmt::runtime("Invalid SQLite statement"));
}
@@ -2187,7 +2196,7 @@ Object SQLiteColumn::GetStatement() const
// ------------------------------------------------------------------------------------------------
Object SQLiteColumn::GetConnection() const
{
return GetConnectionObj(SQMOD_GET_VALID(*this)->mConn);
return GetConnectionObj(SQMOD_GET_VALID(*this)->mConnection);
}
// ------------------------------------------------------------------------------------------------
@@ -2233,7 +2242,7 @@ Object SQLiteColumn::GetValue() const
// Obtain the initial stack size
const StackGuard sg;
// Identify which type of value must be pushed on the stack
switch (sqlite3_column_type(m_Handle->mPtr, m_Index))
switch (sqlite3_column_type(m_Handle->Access(), m_Index))
{
// Is this a null value?
case SQLITE_NULL:
@@ -2243,28 +2252,28 @@ Object SQLiteColumn::GetValue() const
// Is this an integer?
case SQLITE_INTEGER:
{
sq_pushinteger(SqVM(), sqlite3_column_integer(m_Handle->mPtr, m_Index));
sq_pushinteger(SqVM(), sqlite3_column_integer(m_Handle->Access(), m_Index));
} break;
// Is this a floating point?
case SQLITE_FLOAT:
{
sq_pushfloat(SqVM(),
ConvTo< SQFloat >::From(sqlite3_column_double(m_Handle->mPtr, m_Index)));
ConvTo< SQFloat >::From(sqlite3_column_double(m_Handle->Access(), m_Index)));
} break;
// Is this a string?
case SQLITE_TEXT:
{
sq_pushstring(SqVM(),
reinterpret_cast< const SQChar * >(sqlite3_column_text(m_Handle->mPtr, m_Index)),
sqlite3_column_bytes(m_Handle->mPtr, m_Index));
reinterpret_cast< const SQChar * >(sqlite3_column_text(m_Handle->Access(), m_Index)),
sqlite3_column_bytes(m_Handle->Access(), m_Index));
} break;
// Is this raw data?
case SQLITE_BLOB:
{
// Retrieve the size of the blob that must be allocated
const int32_t size = sqlite3_column_bytes(m_Handle->mPtr, m_Index);
const int32_t size = sqlite3_column_bytes(m_Handle->Access(), m_Index);
// Retrieve the the actual blob data that must be returned
auto data = reinterpret_cast< const char * >(sqlite3_column_blob(m_Handle->mPtr, m_Index));
auto data = reinterpret_cast< const char * >(sqlite3_column_blob(m_Handle->Access(), m_Index));
// Attempt to create a buffer with the blob data on the stack
Var< const SqBuffer & >::push(SqVM(), SqBuffer(data, size, 0));
} break;
@@ -2282,7 +2291,7 @@ Object SQLiteColumn::GetNumber() const
// Obtain the initial stack size
const StackGuard sg;
// Identify which type of value must be pushed on the stack
switch (sqlite3_column_type(m_Handle->mPtr, m_Index))
switch (sqlite3_column_type(m_Handle->Access(), m_Index))
{
// Is this a null value?
case SQLITE_NULL:
@@ -2292,18 +2301,18 @@ Object SQLiteColumn::GetNumber() const
// Is this an integer?
case SQLITE_INTEGER:
{
sq_pushinteger(SqVM(), sqlite3_column_integer(m_Handle->mPtr, m_Index));
sq_pushinteger(SqVM(), sqlite3_column_integer(m_Handle->Access(), m_Index));
} break;
// Is this a floating point?
case SQLITE_FLOAT:
{
sq_pushfloat(SqVM(),
ConvTo< SQFloat >::From(sqlite3_column_double(m_Handle->mPtr, m_Index)));
ConvTo< SQFloat >::From(sqlite3_column_double(m_Handle->Access(), m_Index)));
} break;
// Is this a string?
case SQLITE_TEXT:
{
auto str = reinterpret_cast< const SQChar * >(sqlite3_column_text(m_Handle->mPtr, m_Index));
auto str = reinterpret_cast< const SQChar * >(sqlite3_column_text(m_Handle->Access(), m_Index));
// Is there even a string to parse?
if (!str || *str == '\0')
{
@@ -2334,7 +2343,7 @@ SQInteger SQLiteColumn::GetInteger() const
{
SQMOD_VALIDATE_ROW(*this);
// Return the requested information
return sqlite3_column_integer(m_Handle->mPtr, m_Index);
return sqlite3_column_integer(m_Handle->Access(), m_Index);
}
// ------------------------------------------------------------------------------------------------
@@ -2342,7 +2351,7 @@ SQFloat SQLiteColumn::GetFloat() const
{
SQMOD_VALIDATE_ROW(*this);
// Return the requested information
return ConvTo< SQFloat >::From(sqlite3_column_double(m_Handle->mPtr, m_Index));
return ConvTo< SQFloat >::From(sqlite3_column_double(m_Handle->Access(), m_Index));
}
// ------------------------------------------------------------------------------------------------
@@ -2350,7 +2359,7 @@ SQInteger SQLiteColumn::GetLong() const
{
SQMOD_VALIDATE_ROW(*this);
// Return the requested information
return sqlite3_column_int64(m_Handle->mPtr, m_Index);
return sqlite3_column_int64(m_Handle->Access(), m_Index);
}
// ------------------------------------------------------------------------------------------------
@@ -2360,8 +2369,8 @@ Object SQLiteColumn::GetString() const
// Obtain the initial stack size
const StackGuard sg;
// Push the column text on the stack
sq_pushstring(SqVM(), reinterpret_cast< const SQChar * >(sqlite3_column_text(m_Handle->mPtr, m_Index)),
sqlite3_column_bytes(m_Handle->mPtr, m_Index));
sq_pushstring(SqVM(), reinterpret_cast< const SQChar * >(sqlite3_column_text(m_Handle->Access(), m_Index)),
sqlite3_column_bytes(m_Handle->Access(), m_Index));
// Get the object from the stack and return it
return Var< Object >(SqVM(), -1).value;
}
@@ -2371,7 +2380,7 @@ bool SQLiteColumn::GetBoolean() const
{
SQMOD_VALIDATE_ROW(*this);
// Return the requested information
return sqlite3_column_int(m_Handle->mPtr, m_Index) > 0;
return sqlite3_column_int(m_Handle->Access(), m_Index) > 0;
}
// ------------------------------------------------------------------------------------------------
@@ -2379,7 +2388,7 @@ SQChar SQLiteColumn::GetChar() const
{
SQMOD_VALIDATE_ROW(*this);
// Return the requested information
return (SQChar)sqlite3_column_int(m_Handle->mPtr, m_Index);
return (SQChar)sqlite3_column_int(m_Handle->Access(), m_Index);
}
// ------------------------------------------------------------------------------------------------
@@ -2389,9 +2398,9 @@ Object SQLiteColumn::GetBuffer() const
// Remember the current stack size
const StackGuard sg;
// Retrieve the size of the blob that must be allocated
const int32_t size = sqlite3_column_bytes(m_Handle->mPtr, m_Index);
const int32_t size = sqlite3_column_bytes(m_Handle->Access(), m_Index);
// Retrieve the the actual blob data that must be returned
auto data = reinterpret_cast< const char * >(sqlite3_column_blob(m_Handle->mPtr, m_Index));
auto data = reinterpret_cast< const char * >(sqlite3_column_blob(m_Handle->Access(), m_Index));
// Attempt to create a buffer with the blob data on the stack
Var< const SqBuffer & >::push(SqVM(), SqBuffer(data, size, 0));
// Get the object from the stack and return it
@@ -2405,11 +2414,11 @@ Object SQLiteColumn::GetBlob() const
// Obtain the initial stack size
const StackGuard sg;
// Obtain the size of the data
const int32_t sz = sqlite3_column_bytes(m_Handle->mPtr, m_Index);
const int32_t sz = sqlite3_column_bytes(m_Handle->Access(), m_Index);
// Allocate a blob of the same size
SQUserPointer p = sqstd_createblob(SqVM(), sz);
// Obtain a pointer to the data
const void * b = sqlite3_column_blob(m_Handle->mPtr, m_Index);
const void * b = sqlite3_column_blob(m_Handle->Access(), m_Index);
// Could the memory blob be allocated?
if (!p)
{
@@ -2459,7 +2468,7 @@ void SQLiteStatement::ValidateCreated(const char * file, int32_t line) const
{
SqThrowF(SQMOD_RTFMT("Invalid SQLite statement reference =>[{}:{}]"), file, line);
}
else if (m_Handle->mPtr == nullptr)
else if (m_Handle->Access() == nullptr)
{
SqThrowF(SQMOD_RTFMT("Invalid SQLite statement =>[{}:{}]"), file, line);
}
@@ -2471,7 +2480,7 @@ void SQLiteStatement::ValidateCreated() const
{
SqThrowF(fmt::runtime("Invalid SQLite statement reference"));
}
else if (m_Handle->mPtr == nullptr)
else if (m_Handle->Access() == nullptr)
{
SqThrowF(fmt::runtime("Invalid SQLite statement"));
}
@@ -2587,7 +2596,7 @@ SQLiteStatement::SQLiteStatement(const SQLiteConnection & connection, StackStrF
// ------------------------------------------------------------------------------------------------
Object SQLiteStatement::GetConnection() const
{
return Object(new SQLiteConnection(SQMOD_GET_VALID(*this)->mConn));
return Object(new SQLiteConnection(SQMOD_GET_VALID(*this)->mConnection));
}
// ------------------------------------------------------------------------------------------------
@@ -2598,7 +2607,7 @@ SQLiteStatement & SQLiteStatement::Reset()
m_Handle->mGood = false;
m_Handle->mDone = false;
// Attempt to reset the statement to it's initial state
m_Handle->mStatus = sqlite3_reset(m_Handle->mPtr);
m_Handle->mStatus = sqlite3_reset(m_Handle->Access());
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -2616,7 +2625,7 @@ SQLiteStatement & SQLiteStatement::Clear()
m_Handle->mGood = false;
m_Handle->mDone = false;
// Attempt to clear the statement
m_Handle->mStatus = sqlite3_clear_bindings(m_Handle->mPtr);
m_Handle->mStatus = sqlite3_clear_bindings(m_Handle->Access());
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -2636,7 +2645,7 @@ int32_t SQLiteStatement::Exec()
STHROWF("Executed without resetting first");
}
// Attempt to step the statement
m_Handle->mStatus = sqlite3_step(m_Handle->mPtr);
m_Handle->mStatus = sqlite3_step(m_Handle->Access());
// Have we finished stepping?
if (m_Handle->mStatus == SQLITE_DONE)
{
@@ -2644,7 +2653,7 @@ int32_t SQLiteStatement::Exec()
m_Handle->mGood = false;
m_Handle->mDone = true;
// Return the changes made by this statement
return sqlite3_changes(m_Handle->mConn->mPtr);
return sqlite3_changes(m_Handle->mConnection->mPtr);
}
// Specify that we don't have any row and we haven't finished stepping
m_Handle->mGood = false;
@@ -2677,7 +2686,7 @@ bool SQLiteStatement::Step()
STHROWF("Stepped without resetting first");
}
// Attempt to step the statement
m_Handle->mStatus = sqlite3_step(m_Handle->mPtr);
m_Handle->mStatus = sqlite3_step(m_Handle->Access());
// Do we have a row available?
if (m_Handle->mStatus == SQLITE_ROW)
{
@@ -2827,7 +2836,7 @@ Table SQLiteStatement::GetTable(int32_t min, int32_t max) const
while (min <= max)
{
// Attempt to obtain the column name
const SQChar * name = sqlite3_column_name(m_Handle->mPtr, min);
const SQChar * name = sqlite3_column_name(m_Handle->Access(), min);
// Validate the obtained name
if (!name)
{
@@ -2859,7 +2868,7 @@ SQLiteTransaction::SQLiteTransaction(SQLiteConnRef db)
STHROWF("Invalid connection handle");
}
// Attempt to begin transaction
m_Handle->mStatus = sqlite3_exec(m_Handle->mPtr, "BEGIN", nullptr, nullptr, nullptr);
m_Handle->mStatus = sqlite3_exec(m_Handle->Access(), "BEGIN", nullptr, nullptr, nullptr);
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -2876,7 +2885,7 @@ SQLiteTransaction::~SQLiteTransaction()
return; // We're done here!
}
// Attempt to roll back changes because this failed to commit
m_Handle->mStatus = sqlite3_exec(m_Handle->mPtr, "ROLLBACK", nullptr, nullptr, nullptr);
m_Handle->mStatus = sqlite3_exec(m_Handle->Access(), "ROLLBACK", nullptr, nullptr, nullptr);
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{
@@ -2899,7 +2908,7 @@ bool SQLiteTransaction::Commit()
STHROWF("Transaction was already committed");
}
// Attempt to commit the change during this transaction
m_Handle->mStatus = sqlite3_exec(m_Handle->mPtr, "COMMIT", nullptr, nullptr, nullptr);
m_Handle->mStatus = sqlite3_exec(m_Handle->Access(), "COMMIT", nullptr, nullptr, nullptr);
// Validate the result
if (m_Handle->mStatus != SQLITE_OK)
{

View File

@@ -2,6 +2,7 @@
// ------------------------------------------------------------------------------------------------
#include "Core/Utility.hpp"
#include "Core/ThreadPool.hpp"
// ------------------------------------------------------------------------------------------------
#include "Library/IO/Buffer.hpp"
@@ -200,6 +201,11 @@ public:
*/
explicit SQLiteConnHnd(Poco::Data::SessionImpl * session);
/* --------------------------------------------------------------------------------------------
* Explicit constructor.
*/
explicit SQLiteConnHnd(Poco::AutoPtr< Poco::Data::SessionImpl > && session);
/* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
@@ -266,6 +272,20 @@ public:
{
return sqlite3_extended_errcode(mPtr);
}
/* --------------------------------------------------------------------------------------------
* Access the connection pointer.
*/
SQMOD_NODISCARD Pointer Access() const
{
if (!mSession.isNull()) {
// Only reason this is necessary is to dirty the connection handle access time-stamp
// So it won't be closed/collected when it comes from a connection/session-pool
[[maybe_unused]] auto _ = mSession->isConnected();
}
// We yield access to the pointer anyway
return mPtr;
}
};
/* ------------------------------------------------------------------------------------------------
@@ -298,7 +318,7 @@ public:
int32_t mStatus; // The last status code of this connection handle.
// --------------------------------------------------------------------------------------------
SQLiteConnRef mConn; // The handle to the associated database connection.
SQLiteConnRef mConnection; // The handle to the associated database connection.
// --------------------------------------------------------------------------------------------
String mQuery; // The query string used to create this statement.
@@ -387,6 +407,20 @@ public:
* Return the extended numeric result code for the most recent failed API call (if any).
*/
SQMOD_NODISCARD int32_t ExErrNo() const;
/* --------------------------------------------------------------------------------------------
* Access the statement pointer.
*/
SQMOD_NODISCARD Pointer Access() const
{
if (bool(mConnection) && !(mConnection->mSession.isNull())) {
// Only reason this is necessary is to dirty the connection handle access time-stamp
// So it won't be closed/collected when it comes from a connection/session-pool
[[maybe_unused]] auto _ = mConnection->mSession->isConnected();
}
// We yield access to the pointer anyway
return mPtr;
}
};
/* ------------------------------------------------------------------------------------------------
@@ -538,7 +572,7 @@ public:
*/
operator sqlite3 * () //NOLINT (intentionally implicit)
{
return m_Handle ? m_Handle->mPtr : nullptr;
return m_Handle ? m_Handle->Access() : nullptr;
}
/* --------------------------------------------------------------------------------------------
@@ -546,7 +580,7 @@ public:
*/
operator sqlite3 * () const //NOLINT (intentionally implicit)
{
return m_Handle ? m_Handle->mPtr : nullptr;
return m_Handle ? m_Handle->Access() : nullptr;
}
/* --------------------------------------------------------------------------------------------
@@ -578,7 +612,7 @@ public:
*/
SQMOD_NODISCARD bool IsConnected() const
{
return m_Handle && (m_Handle->mPtr != nullptr);
return m_Handle && (m_Handle->Access() != nullptr);
}
/* --------------------------------------------------------------------------------------------
@@ -691,6 +725,16 @@ public:
*/
Object Query(StackStrF & str) const;
/* --------------------------------------------------------------------------------------------
* Attempt to execute the specified query asynchronously.
*/
LightObj AsyncExec(StackStrF & str);
/* --------------------------------------------------------------------------------------------
* Attempt to create a statement from the specified query asynchronously.
*/
LightObj AsyncQuery(StackStrF & str) const;
/* --------------------------------------------------------------------------------------------
* See if the database connection was opened in read-only mode.
*/
@@ -869,8 +913,8 @@ class SQLiteParameter
private:
// --------------------------------------------------------------------------------------------
int32_t m_Index{0}; // The index of the managed parameter.
SQLiteStmtRef m_Handle{}; // Reference to the managed statement.
int32_t m_Index{0}; // The index of the managed parameter.
SQLiteStmtRef m_Handle{}; // Reference to the managed statement.
protected:
@@ -1045,7 +1089,7 @@ public:
// Can we attempt to return the parameter name?
if (m_Handle && m_Index)
{
const SQChar * val = sqlite3_bind_parameter_name(m_Handle->mPtr, m_Index);
const SQChar * val = sqlite3_bind_parameter_name(m_Handle->Access(), m_Index);
// Return the value if valid
return val ? val : String{};
}
@@ -1448,7 +1492,7 @@ public:
// Can we attempt to return the parameter name?
if (m_Handle && m_Index)
{
const SQChar * val = sqlite3_column_name(m_Handle->mPtr, m_Index);
const SQChar * val = sqlite3_column_name(m_Handle->Access(), m_Index);
// Return the value if valid
return val ? val : String{};
}
@@ -1672,6 +1716,15 @@ public:
SQMOD_GET_VALID(*this)->Create(query.mPtr, query.mLen);
}
/* --------------------------------------------------------------------------------------------
* Construct a statement under the specified connection using the specified string.
*/
SQLiteStatement(const SQLiteConnRef & connection, const SQChar * query, SQInteger length)
: m_Handle(new SQLiteStmtHnd(connection))
{
SQMOD_GET_VALID(*this)->Create(query, length);
}
/* --------------------------------------------------------------------------------------------
* Construct a statement under the specified connection using the specified string.
*/
@@ -1680,7 +1733,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Direct handle constructor.
*/
explicit SQLiteStatement(SQLiteStmtRef s)
explicit SQLiteStatement(SQLiteStmtRef s)
: m_Handle(std::move(s))
{
/* ... */
@@ -1727,7 +1780,7 @@ public:
*/
operator sqlite3_stmt * () // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
{
return m_Handle ? m_Handle->mPtr : nullptr;
return m_Handle ? m_Handle->Access() : nullptr;
}
/* --------------------------------------------------------------------------------------------
@@ -1735,7 +1788,7 @@ public:
*/
operator sqlite3_stmt * () const // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
{
return m_Handle ? m_Handle->mPtr : nullptr;
return m_Handle ? m_Handle->Access() : nullptr;
}
/* --------------------------------------------------------------------------------------------
@@ -1767,7 +1820,7 @@ public:
*/
SQMOD_NODISCARD bool IsPrepared() const
{
return m_Handle && (m_Handle->mPtr != nullptr);
return m_Handle && (m_Handle->Access() != nullptr);
}
/* --------------------------------------------------------------------------------------------

View File

@@ -8,6 +8,9 @@
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQMOD_DECL_TYPENAME(SqIdPoolTypename, _SC("SqIdPool"))
/* ------------------------------------------------------------------------------------------------
* Probably not the best implementation but should cover all sorts of weird cases.
*/
@@ -83,15 +86,18 @@ static SQInteger SqExtractIPv4(HSQUIRRELVM vm)
}
// ------------------------------------------------------------------------------------------------
extern void Register_IdPool(HSQUIRRELVM vm, Table & ns);
extern void Register_Vector(HSQUIRRELVM vm, Table & ns);
extern void Register_Native_String(HSQUIRRELVM vm, Table & ns);
extern void Register_ServerAnnouncer(HSQUIRRELVM vm, Table & ns);
// ================================================================================================
void Register_Utils(HSQUIRRELVM vm)
{
Table ns(vm);
Register_IdPool(vm, ns);
Register_Vector(vm, ns);
Register_Native_String(vm, ns);
Register_ServerAnnouncer(vm, ns);
@@ -101,4 +107,35 @@ void Register_Utils(HSQUIRRELVM vm)
RootTable(vm).Bind(_SC("SqUtils"), ns);
}
// ------------------------------------------------------------------------------------------------
void Register_IdPool(HSQUIRRELVM vm, Table & ns)
{
// --------------------------------------------------------------------------------------------
ns.Bind(_SC("IdPool"),
Class< SqIdPool, NoCopy< SqIdPool > >(vm, SqIdPoolTypename::Str)
// Constructors
.Ctor()
.template Ctor< SqIdPool::Type >()
.template Ctor< SqIdPool::Type, SqIdPool::Type >()
// Meta-methods
.SquirrelFunc(_SC("_typename"), &SqIdPoolTypename::Fn)
// Member Variables
.ConstVar(_SC("Next"), &SqIdPool::mNext)
.ConstVar(_SC("Step"), &SqIdPool::mStep)
.ConstVar(_SC("Start"), &SqIdPool::mStart)
// Properties
.Prop(_SC("FreeCount"), &SqIdPool::FreeCount)
.Prop(_SC("UsedCount"), &SqIdPool::UsedCount)
// Member Methods
.Func(_SC("Reset"), &SqIdPool::Reset)
.Func(_SC("Acquire"), &SqIdPool::Acquire)
.Func(_SC("Release"), &SqIdPool::Release)
.Func(_SC("Using"), &SqIdPool::Using)
.Func(_SC("EachUsed"), &SqIdPool::EachUsed)
.Func(_SC("WhileUsed"), &SqIdPool::WhileUsed)
.Func(_SC("EachFree"), &SqIdPool::EachFree)
.Func(_SC("WhileFree"), &SqIdPool::WhileFree)
);
}
} // Namespace:: SqMod

View File

@@ -6,4 +6,187 @@
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Helper utility used to provide reusable unique IDs of signed integer type.
* It is not thread-safe since the script runs in single-threaded mode.
*/
struct SqIdPool
{
using Type = SQInteger; // Type that is used to represent an ID.
using Pool = std::vector< Type >; // Container for both used and unused IDs.
// --------------------------------------------------------------------------------------------
// Pool of available IDs.
Pool mPool{};
// Pool of currently used IDs.
Pool mUsed{};
// The ID that will be generated next time the pool is empty.
Type mNext{0};
// How much to increment with each ID.
Type mStep{1};
// Where to start generating IDs.
Type mStart{0};
/* --------------------------------------------------------------------------------------------
* Base constructors.
*/
SqIdPool() noexcept = default;
SqIdPool(Type start) noexcept
: SqIdPool(start, 1)
{
}
SqIdPool(Type start, Type step) noexcept
: mPool(), mUsed(), mNext(start), mStep(step), mStart(start)
{
}
/* --------------------------------------------------------------------------------------------
* Copy/Move constructors (disabled).
*/
SqIdPool(const SqIdPool &) noexcept = delete;
SqIdPool(SqIdPool &&) noexcept = delete;
/* --------------------------------------------------------------------------------------------
* Copy/Move assignment operators (disabled).
*/
SqIdPool & operator = (const SqIdPool &) noexcept = delete;
SqIdPool & operator = (SqIdPool &&) noexcept = delete;
/* --------------------------------------------------------------------------------------------
* Discard all current IDs (free and used) and reset the start to the specified start.
* This invalidates all IDs that are currently left in use.
*/
void Reset()
{
mNext = mStart;
mPool.clear();
mUsed.clear();
}
/* --------------------------------------------------------------------------------------------
* Acquire a unique ID from the pool.
*/
SQMOD_NODISCARD Type Acquire()
{
Type id = mNext;
// Do we have some reusable IDs?
if (mPool.empty())
{
mNext += mStep; // Create a new one and update the next one
}
else
{
id = mPool.back(); // Get one from the back of the pool
mPool.pop_back(); // Remove it from the free pool
}
// Store it in the list of active IDs
mUsed.push_back(id);
// Return this ID
return id;
}
/* --------------------------------------------------------------------------------------------
* Release a unique ID back to the pool.
*/
bool Release(Type id)
{
// Find the specified ID into
for (Pool::size_type i = 0; i < mUsed.size(); ++i)
{
// Is this the ID we're looking for?
if (mUsed[i] == id)
{
// Swap the element with the last one
std::swap(mUsed[i], mUsed.back());
// Remove the last element
mUsed.pop_back();
// Make this ID available, again
mPool.push_back(id);
// We actually found this ID
return true;
}
}
// This ID does not belong to this pool
return false;
}
/* --------------------------------------------------------------------------------------------
* Check if the pool has the specified ID currently in use.
*/
SQMOD_NODISCARD bool Using(Type id)
{
return std::find(mUsed.begin(), mUsed.end(), id) != mUsed.end();
}
/* --------------------------------------------------------------------------------------------
* Retrieve the number of IDs that are currently available in the free pool.
*/
SQMOD_NODISCARD SQInteger FreeCount() const
{
return mPool.size();
}
/* --------------------------------------------------------------------------------------------
* Retrieve the number of IDs that are currently in use.
*/
SQMOD_NODISCARD SQInteger UsedCount() const
{
return mUsed.size();
}
/* --------------------------------------------------------------------------------------------
* Iterate all used IDs through a functor.
*/
void EachUsed(Function & fn) const
{
for (const auto & id : mUsed)
{
fn.Execute(id);
}
}
/* --------------------------------------------------------------------------------------------
* Iterate all used IDs through a functor until stopped (i.e false is returned).
*/
void WhileUsed(Function & fn) const
{
for (const auto & id : mUsed)
{
auto ret = fn.Eval(id);
// (null || true) == continue & false == break
if (!ret.IsNull() || !ret.template Cast< bool >())
{
break;
}
}
}
/* --------------------------------------------------------------------------------------------
* Iterate all free IDs through a functor.
*/
void EachFree(Function & fn) const
{
for (const auto & id : mPool)
{
fn.Execute(id);
}
}
/* --------------------------------------------------------------------------------------------
* Iterate all free IDs through a functor until stopped (i.e false is returned).
*/
void WhileFree(Function & fn) const
{
for (const auto & id : mPool)
{
auto ret = fn.Eval(id);
// (null || true) == continue & false == break
if (!ret.IsNull() || !ret.template Cast< bool >())
{
break;
}
}
}
};
} // Namespace:: SqMod

View File

@@ -17,7 +17,7 @@ namespace SqMod {
static bool g_Reload = false;
// ------------------------------------------------------------------------------------------------
//extern void InitExports();
extern void InitExports();
extern void InitializeNet();
extern void InitializePocoDataConnectors();
extern void ProcessRoutines();
@@ -97,7 +97,7 @@ static uint8_t OnServerInitialise()
{
SQMOD_SV_EV_TRACEBACK("[TRACE<] OnServerInitialise")
// Signal outside plug-ins to do fetch our proxies
//_Func->SendPluginCommand(0xDABBAD00, "%d", 1);
_Func->SendPluginCommand(SQMOD_INITIALIZE_CMD, "%d", 1);
// Attempt to load the module core
if (Core::Get().Execute())
{
@@ -1083,7 +1083,7 @@ SQMOD_API_EXPORT unsigned int VcmpPluginInit(PluginFuncs * funcs, PluginCallback
_Clbk->OnEntityStreamingChange = OnEntityStreamingChange;
#endif
// Attempt to initialize the plug-in exports
//InitExports();
InitExports();
// Dummy spacing
puts("");
// Initialization was successful

View File

@@ -52,6 +52,66 @@ SQMOD_DECL_TYPENAME(CPickupTn, _SC("CPickup"))
SQMOD_DECL_TYPENAME(CPlayerTn, _SC("CPlayer"))
SQMOD_DECL_TYPENAME(CVehicleTn, _SC("CVehicle"))
/* ------------------------------------------------------------------------------------------------
* Used to fetch the legacy entity instance even if a native one was specified.
*/
template < class LEGACY, class NATIVE > inline LEGACY & GetLgEnt(LightObj & o)
{
const auto type = static_cast< AbstractStaticClassData * >(o.GetTypeTag());
// Legacy entity type?
if (type == StaticClassTypeTag< LEGACY >::Get())
{
return *o.CastI< LEGACY >();
}
// Native entity type?
if (type == StaticClassTypeTag< NATIVE >::Get())
{
return *EntityInstSelect< NATIVE >::Get(o.CastI< NATIVE >()->GetID()).mLgInst;
}
STHROWF("Invalid entity type: {}", SqTypeName(SqVM(), o));
SQ_UNREACHABLE
}
/* ------------------------------------------------------------------------------------------------
* Used to fetch the native entity instance even if a legacy one was specified.
*/
template < class LEGACY, class NATIVE > inline NATIVE & GetNativeEnt(LightObj & o)
{
const auto type = static_cast< AbstractStaticClassData * >(o.GetTypeTag());
// Legacy entity type?
if (type == StaticClassTypeTag< LEGACY >::Get())
{
return o.CastI< LEGACY >()->Get();
}
// Native entity type?
if (type == StaticClassTypeTag< NATIVE >::Get())
{
return *EntityInstSelect< NATIVE >::Get(o.CastI< NATIVE >()->GetID()).mInst;
}
STHROWF("Invalid entity type: {}", SqTypeName(SqVM(), o));
SQ_UNREACHABLE
}
/* ------------------------------------------------------------------------------------------------
* Used to fetch the legacy entity identifier even if a native one was specified.
*/
template < class LEGACY, class NATIVE > SQMOD_NODISCARD inline int32_t GetLgEntID(LightObj & o)
{
const auto type = static_cast< AbstractStaticClassData * >(o.GetTypeTag());
// Legacy entity type?
if (type == StaticClassTypeTag< LEGACY >::Get())
{
return o.CastI< LEGACY >()->mID;
}
// Native entity type?
if (type == StaticClassTypeTag< NATIVE >::Get())
{
return o.CastI< NATIVE >()->GetID();
}
STHROWF("Invalid entity type: {}", SqTypeName(SqVM(), o));
SQ_UNREACHABLE
}
/* ------------------------------------------------------------------------------------------------
* Entity type enumeration.
*/
@@ -438,14 +498,14 @@ struct LgCheckpoint
SQMOD_NODISCARD int GetWorld() const { return Get().GetWorld(); }
SQMOD_NODISCARD LgARGB GetColor() const { const Color4 c = Get().GetColor(); return LgARGB{c.a, c.r, c.g, c.b}; }
SQMOD_NODISCARD LgEntityVector GetPos() const
{ return LgEntityVector(mID, LgEntityType::Checkpoint, 0, Get().GetPosition()); }
{ return {mID, LgEntityType::Checkpoint, 0, Get().GetPosition()}; }
SQMOD_NODISCARD float GetRadius() const { return Get().GetRadius(); }
SQMOD_NODISCARD int GetID() const { return mID; }
SQMOD_NODISCARD LgPlayer * GetOwner() const
{ const int id = Get().GetOwnerID(); return VALID_ENTITYEX(id, SQMOD_PLAYER_POOL) ? Core::Get().GetPlayer(id).mLgInst : nullptr; }
// --------------------------------------------------------------------------------------------
void Delete() const { _Func->DeleteCheckPoint(GetIdentifier()); }
SQMOD_NODISCARD bool StreamedToPlayer(LgPlayer & player) const;
SQMOD_NODISCARD bool StreamedToPlayer(LightObj & player) const;
};
/* ------------------------------------------------------------------------------------------------
@@ -506,11 +566,11 @@ struct LgObject
SQMOD_NODISCARD int GetAlpha() const { return Get().GetAlpha(); }
SQMOD_NODISCARD int GetWorld() const { return Get().GetWorld(); }
SQMOD_NODISCARD LgEntityVector GetPos() const
{ return LgEntityVector(mID, LgEntityType::Object, LgObjectVectorFlag::Pos, Get().GetPosition()); }
{ return {mID, LgEntityType::Object, LgObjectVectorFlag::Pos, Get().GetPosition()}; }
SQMOD_NODISCARD LgEntityQuaternion GetRotation() const
{ return LgEntityQuaternion(mID, LgEntityType::Object, 0, Get().GetRotation()); }
{ return {mID, LgEntityType::Object, 0, Get().GetRotation()}; }
SQMOD_NODISCARD LgEntityVector GetRotationEuler() const
{ return LgEntityVector(mID, LgEntityType::Object, LgObjectVectorFlag::Rotation, Get().GetRotationEuler()); }
{ return {mID, LgEntityType::Object, LgObjectVectorFlag::Rotation, Get().GetRotationEuler()}; }
SQMOD_NODISCARD int GetID() const { return mID; }
SQMOD_NODISCARD bool GetReportingShots() const { return Get().GetShotReport(); }
SQMOD_NODISCARD bool GetReportingBumps() const { return Get().GetTouchedReport(); }
@@ -523,7 +583,7 @@ struct LgObject
void RotateToEuler(const Vector3 & rotation, int time) const { Get().RotateToEuler(rotation, static_cast< uint32_t >(time)); }
void RotateByEuler(const Vector3 & rotOffset, int time) const { Get().RotateByEuler(rotOffset, static_cast< uint32_t >(time)); }
void SetAlpha(int alpha, int fadeTime) const { Get().SetAlphaEx(alpha, static_cast< uint32_t >(fadeTime)); }
SQMOD_NODISCARD bool StreamedToPlayer(LgPlayer & player) const;
SQMOD_NODISCARD bool StreamedToPlayer(LightObj & player) const;
};
/* ------------------------------------------------------------------------------------------------
@@ -587,7 +647,7 @@ struct LgPickup
SQMOD_NODISCARD bool GetAuto() const { return Get().GetAutomatic(); }
SQMOD_NODISCARD int GetAutoTimer() const { return Get().GetAutoTimer(); }
SQMOD_NODISCARD LgEntityVector GetPos() const
{ return LgEntityVector(mID, LgEntityType::Pickup, 0, Get().GetPosition()); }
{ return {mID, LgEntityType::Pickup, 0, Get().GetPosition()}; }
SQMOD_NODISCARD int GetModel() const { return Get().GetModel(); }
SQMOD_NODISCARD int GetQuantity() const { return Get().GetQuantity(); }
SQMOD_NODISCARD int GetID() const { return mID; }
@@ -595,7 +655,7 @@ struct LgPickup
// --------------------------------------------------------------------------------------------
void Delete() const { _Func->DeletePickup(GetIdentifier()); }
void Respawn() const { Get().Refresh(); }
SQMOD_NODISCARD bool StreamedToPlayer(LgPlayer & player) const;
SQMOD_NODISCARD bool StreamedToPlayer(LightObj & player) const;
};
/* ------------------------------------------------------------------------------------------------
@@ -663,7 +723,7 @@ struct LgPlayer
void SetScore(int score) const { Get().SetScore(score); }
void SetImmunity(uint32_t immunity) const { Get().SetImmunity(immunity); }
void SetHeading(float heading) const { Get().SetHeading(heading); }
void SetVehicle(LgVehicle & vehicle) const;
void SetVehicle(LightObj & vehicle) const;
void SetFrozen(bool toggle) const { _Func->SetPlayerOption(GetIdentifier(), vcmpPlayerOptionControllable, static_cast< uint8_t >(!toggle)); }
void SetDriveByEnabled(bool toggle) const { _Func->SetPlayerOption(GetIdentifier(), vcmpPlayerOptionDriveBy, static_cast< uint8_t >(toggle)); }
void SetWhiteScanLines(bool toggle) const { _Func->SetPlayerOption(GetIdentifier(), vcmpPlayerOptionWhiteScanlines, static_cast< uint8_t >(toggle)); }
@@ -680,7 +740,7 @@ struct LgPlayer
void SetWantedLevel(int level) const { Get().SetWantedLevel(level); }
// --------------------------------------------------------------------------------------------
SQMOD_NODISCARD LgEntityVector GetPosition() const
{ return LgEntityVector(mID, LgEntityType::Player, LgPlayerVectorFlag::Pos, Get().GetPosition()); }
{ return {mID, LgEntityType::Player, LgPlayerVectorFlag::Pos, Get().GetPosition()}; }
SQMOD_NODISCARD int GetClass() const { return Get().GetClass(); }
SQMOD_NODISCARD bool GetAdmin() const { return Get().GetAdmin(); }
SQMOD_NODISCARD const SQChar * GetIP() const { return Get().GetIP(); }
@@ -692,7 +752,7 @@ struct LgPlayer
SQMOD_NODISCARD const SQChar * GetName() const { return Get().GetName(); }
SQMOD_NODISCARD int GetTeam() const { return Get().GetTeam(); }
SQMOD_NODISCARD int GetSkin() const { return Get().GetSkin(); }
SQMOD_NODISCARD LgEntityRGB GetColour() const { return LgEntityRGB(mID, LgEntityType::Player, 0, Get().GetColor()); }
SQMOD_NODISCARD LgEntityRGB GetColour() const { return {mID, LgEntityType::Player, 0, Get().GetColor()}; }
SQMOD_NODISCARD int GetMoney() const { return Get().GetMoney(); }
SQMOD_NODISCARD int GetScore() const { return Get().GetScore(); }
SQMOD_NODISCARD int GetPing() const { return Get().GetPing(); }
@@ -724,7 +784,7 @@ struct LgPlayer
SQMOD_NODISCARD LgPlayer * GetSpectateTarget() const
{ const int id = _Func->GetPlayerSpectateTarget(GetIdentifier()); return VALID_ENTITYEX(id, SQMOD_PLAYER_POOL) ? Core::Get().GetPlayer(id).mLgInst : nullptr; }
SQMOD_NODISCARD LgEntityVector GetSpeed() const
{ return LgEntityVector(mID, LgEntityType::Player, LgPlayerVectorFlag::Speed, Get().GetSpeed()); }
{ return {mID, LgEntityType::Player, LgPlayerVectorFlag::Speed, Get().GetSpeed()}; }
SQMOD_NODISCARD bool GetCanUseColors() const { return _Func->GetPlayerOption(GetIdentifier(), vcmpPlayerOptionChatTagsEnabled) >= 1; }
SQMOD_NODISCARD bool GetMarkerVisible() const { return _Func->GetPlayerOption(GetIdentifier(), vcmpPlayerOptionHasMarker) >= 1; }
SQMOD_NODISCARD bool GetDrunkStatus() const { return _Func->GetPlayerOption(GetIdentifier(), vcmpPlayerOptionDrunkEffects) >= 1; }
@@ -761,8 +821,8 @@ struct LgPlayer
SQMOD_NODISCARD int GetWeaponAtSlot(int slot) const { return Get().GetWeaponAtSlot(slot); }
SQMOD_NODISCARD int GetAmmoAtSlot(int slot) const { return Get().GetAmmoAtSlot(slot); }
void SetAlpha(int alpha, int fadeTime) const { Get().SetAlphaEx(alpha, fadeTime); }
SQMOD_NODISCARD bool StreamedToPlayer(const LgPlayer & player) const { return Get().IsStreamedFor(player.Get()); }
void SetVehicleSlot(const LgVehicle & vehicle, int slot) const;
SQMOD_NODISCARD bool StreamedToPlayer(LightObj & player) const { return Get().IsStreamedFor(GetNativeEnt< LgPlayer, CPlayer >(player)); }
void SetVehicleSlot(LightObj & vehicle, int slot) const;
void Select() const { Get().ForceSelect(); }
void RestoreCamera() const { Get().RestoreCamera(); }
void RemoveMarker() const { _Func->SetPlayerOption(GetIdentifier(), vcmpPlayerOptionHasMarker, 0); }
@@ -875,13 +935,13 @@ struct LgVehicle
SQMOD_NODISCARD int GetModel() const { return Get().GetModel(); }
SQMOD_NODISCARD uint32_t GetImmunity() const { return Get().GetImmunity(); }
SQMOD_NODISCARD LgEntityVector GetPosition() const
{ return LgEntityVector(mID, LgEntityType::Vehicle, LgVehicleVectorFlag::Pos, Get().GetPosition()); }
{ return {mID, LgEntityType::Vehicle, LgVehicleVectorFlag::Pos, Get().GetPosition()}; }
SQMOD_NODISCARD LgEntityVector GetSpawnPos() const
{ return LgEntityVector(mID, LgEntityType::Vehicle, LgVehicleVectorFlag::SpawnPos, Get().GetSpawnPosition()); }
{ return {mID, LgEntityType::Vehicle, LgVehicleVectorFlag::SpawnPos, Get().GetSpawnPosition()}; }
SQMOD_NODISCARD LgEntityQuaternion GetSpawnAngle() const
{ return LgEntityQuaternion(mID, LgEntityType::Vehicle, LgVehicleQuaternionFlag::SpawnAngle, Get().GetSpawnRotation()); }
{ return {mID, LgEntityType::Vehicle, LgVehicleQuaternionFlag::SpawnAngle, Get().GetSpawnRotation()}; }
SQMOD_NODISCARD LgEntityVector GetSpawnAngleEuler() const
{ return LgEntityVector(mID, LgEntityType::Vehicle, LgVehicleVectorFlag::SpawnAngle, Get().GetSpawnRotationEuler()); }
{ return {mID, LgEntityType::Vehicle, LgVehicleVectorFlag::SpawnAngle, Get().GetSpawnRotationEuler()}; }
SQMOD_NODISCARD uint32_t GetIdleRespawnTimer() const { return Get().GetIdleRespawnTimer(); }
SQMOD_NODISCARD float GetHealth() const { return Get().GetHealth(); }
SQMOD_NODISCARD int GetColour1() const { return Get().GetPrimaryColor(); }
@@ -899,21 +959,21 @@ struct LgVehicle
SQMOD_NODISCARD int GetSyncType() const { return Get().GetSyncType(); }
SQMOD_NODISCARD bool GetWrecked() const { return Get().IsWrecked(); }
SQMOD_NODISCARD LgEntityQuaternion GetRotation() const
{ return LgEntityQuaternion(mID, LgEntityType::Vehicle, LgVehicleQuaternionFlag::Angle, Get().GetRotation()); }
{ return {mID, LgEntityType::Vehicle, LgVehicleQuaternionFlag::Angle, Get().GetRotation()}; }
SQMOD_NODISCARD LgEntityVector GetEulerRotation() const
{ return LgEntityVector(mID, LgEntityType::Vehicle, LgVehicleVectorFlag::Angle, Get().GetRotationEuler()); }
{ return {mID, LgEntityType::Vehicle, LgVehicleVectorFlag::Angle, Get().GetRotationEuler()}; }
SQMOD_NODISCARD LgEntityVector GetSpeed() const
{ return LgEntityVector(mID, LgEntityType::Vehicle, LgVehicleVectorFlag::Speed, Get().GetSpeed()); }
{ return {mID, LgEntityType::Vehicle, LgVehicleVectorFlag::Speed, Get().GetSpeed()}; }
SQMOD_NODISCARD LgEntityVector GetRelativeSpeed() const
{ return LgEntityVector(mID, LgEntityType::Vehicle, LgVehicleVectorFlag::RelSpeed, Get().GetRelativeSpeed()); }
{ return {mID, LgEntityType::Vehicle, LgVehicleVectorFlag::RelSpeed, Get().GetRelativeSpeed()}; }
SQMOD_NODISCARD LgEntityVector GetTurnSpeed() const
{ return LgEntityVector(mID, LgEntityType::Vehicle, LgVehicleVectorFlag::TurnSpeed, Get().GetTurnSpeed()); }
{ return {mID, LgEntityType::Vehicle, LgVehicleVectorFlag::TurnSpeed, Get().GetTurnSpeed()}; }
SQMOD_NODISCARD LgEntityVector GetRelativeTurnSpeed() const
{ return LgEntityVector(mID, LgEntityType::Vehicle, LgVehicleVectorFlag::RelTurnSpeed, Get().GetRelativeTurnSpeed()); }
{ return {mID, LgEntityType::Vehicle, LgVehicleVectorFlag::RelTurnSpeed, Get().GetRelativeTurnSpeed()}; }
SQMOD_NODISCARD int GetRadio() const { return Get().GetRadio(); }
SQMOD_NODISCARD bool GetRadioLockStatus() const { return _Func->GetVehicleOption(GetIdentifier(), vcmpVehicleOptionRadioLocked) >= 1; }
SQMOD_NODISCARD bool GetGhost() const { return _Func->GetVehicleOption(GetIdentifier(), vcmpVehicleOptionGhost) >= 1; }
SQMOD_NODISCARD LgVector GetTurretRotation() const { const Vector2 v = Get().GetTurretRotation(); return LgVector(v.x, v.y, 0); }
SQMOD_NODISCARD LgVector GetTurretRotation() const { const Vector2 v = Get().GetTurretRotation(); return {v.x, v.y, 0}; }
SQMOD_NODISCARD bool GetSingleUse() const { return _Func->GetVehicleOption(GetIdentifier(), vcmpVehicleOptionSingleUse) >= 1; }
SQMOD_NODISCARD bool GetTaxiLight() const { return (_Func->GetVehicleLightsData(GetIdentifier()) & (1 << 8)) != 0; }
// --------------------------------------------------------------------------------------------
@@ -926,7 +986,7 @@ struct LgVehicle
void SetPartStatus(int part, int status) const { Get().SetPartStatus(part, status); }
SQMOD_NODISCARD int GetTyreStatus(int tyre) const { return Get().GetTyreStatus(tyre); }
void SetTyreStatus(int part, int status) const { Get().SetTyreStatus(part, status); }
SQMOD_NODISCARD bool GetStreamedForPlayer(LgPlayer & player) const { return Get().IsStreamedFor(player.Get()); }
SQMOD_NODISCARD bool GetStreamedForPlayer(LightObj & player) const { return Get().IsStreamedFor(GetNativeEnt< LgPlayer, CPlayer >(player)); }
SQMOD_NODISCARD LgPlayer * GetOccupant(int slot) const
{ const int id = _Func->GetVehicleOccupant(GetIdentifier(), slot); return VALID_ENTITYEX(id, SQMOD_PLAYER_POOL) ? Core::Get().GetPlayer(id).mLgInst : nullptr; }
void SetHandlingData(int rule, float value) const { Get().SetHandlingRule(rule, value); }
@@ -942,11 +1002,11 @@ struct LgVehicle
};
// ------------------------------------------------------------------------------------------------
inline bool LgCheckpoint::StreamedToPlayer(LgPlayer & player) const { return Get().IsStreamedFor(player.Get()); }
inline bool LgObject::StreamedToPlayer(LgPlayer & player) const { return Get().IsStreamedFor(player.Get()); }
inline bool LgPickup::StreamedToPlayer(LgPlayer & player) const { return Get().IsStreamedFor(player.Get()); }
inline void LgPlayer::SetVehicle(LgVehicle & vehicle) const { Get().Embark(vehicle.Get()); }
inline void LgPlayer::SetVehicleSlot(const LgVehicle & vehicle, int slot) const { Get().EmbarkEx(vehicle.Get(), slot, true, false); }
inline bool LgCheckpoint::StreamedToPlayer(LightObj & player) const { return Get().IsStreamedFor(GetNativeEnt< LgPlayer, CPlayer >(player)); }
inline bool LgObject::StreamedToPlayer(LightObj & player) const { return Get().IsStreamedFor(GetNativeEnt< LgPlayer, CPlayer >(player)); }
inline bool LgPickup::StreamedToPlayer(LightObj & player) const { return Get().IsStreamedFor(GetNativeEnt< LgPlayer, CPlayer >(player)); }
inline void LgPlayer::SetVehicle(LightObj & vehicle) const { Get().Embark(GetNativeEnt< LgVehicle, CVehicle >(vehicle)); }
inline void LgPlayer::SetVehicleSlot(LightObj & vehicle, int slot) const { Get().EmbarkEx(GetNativeEnt< LgVehicle, CVehicle >(vehicle), slot, true, false); }
// ------------------------------------------------------------------------------------------------
void LgCheckpointSetID(LgCheckpoint * inst, int32_t id) { assert(inst); if (inst) inst->mID = id; }
@@ -1233,11 +1293,11 @@ void Register_Official_Entity(HSQUIRRELVM vm)
}
// ------------------------------------------------------------------------------------------------
static void LgClientMessage(StackStrF & msg, LgPlayer & player, int r, int g, int b)
{ _Func->SendClientMessage(player.GetIdentifier(), Color4(static_cast< uint8_t >(r), static_cast< uint8_t >(g),
static void LgClientMessage(StackStrF & msg, LightObj & player, int r, int g, int b)
{ _Func->SendClientMessage(GetLgEntID< LgPlayer, CPlayer >(player), Color4(static_cast< uint8_t >(r), static_cast< uint8_t >(g),
static_cast< uint8_t >(b), 255).GetRGBA(), "%s", msg.mPtr); }
static void LgClientMessageWithAlpha(StackStrF & msg, LgPlayer & player, int r, int g, int b, int a)
{ _Func->SendClientMessage(player.GetIdentifier(), Color4(static_cast< uint8_t >(r), static_cast< uint8_t >(g),
static void LgClientMessageWithAlpha(StackStrF & msg, LightObj & player, int r, int g, int b, int a)
{ _Func->SendClientMessage(GetLgEntID< LgPlayer, CPlayer >(player), Color4(static_cast< uint8_t >(r), static_cast< uint8_t >(g),
static_cast< uint8_t >(b), static_cast< uint8_t >(a)).GetRGBA(), "%s", msg.mPtr); }
static void LgClientMessageToAll(StackStrF & msg, int r, int g, int b) {
const uint32_t c = Color4(static_cast< uint8_t >(r), static_cast< uint8_t >(g),
@@ -1249,10 +1309,10 @@ static void LgClientMessageToAllWithAlpha(StackStrF & msg, int r, int g, int b,
static_cast< uint8_t >(b), static_cast< uint8_t >(a)).GetRGBA();
ForeachActivePlayer([&](auto & p) { _Func->SendClientMessage(p.mID, c, "%s", msg.mPtr); });
}
static void LgGameMessage(StackStrF & msg, LgPlayer & player, int type)
{ _Func->SendGameMessage(player.GetIdentifier(), type, msg.mPtr); }
static void LgGameMessageAlternate(StackStrF & msg, LgPlayer & player)
{ { _Func->SendGameMessage(player.GetIdentifier(), 1, msg.mPtr); } }
static void LgGameMessage(StackStrF & msg, LightObj & player, int type)
{ _Func->SendGameMessage(GetLgEntID< LgPlayer, CPlayer >(player), type, msg.mPtr); }
static void LgGameMessageAlternate(StackStrF & msg, LightObj & player)
{ { _Func->SendGameMessage(GetLgEntID< LgPlayer, CPlayer >(player), 1, msg.mPtr); } }
static void LgGameMessageToAll(StackStrF & msg, int type)
{ _Func->SendGameMessage(-1, type, msg.mPtr); }
static void LgGameMessageToAllAlternate(StackStrF & msg)
@@ -1394,8 +1454,8 @@ static void LgUnbanIP(StackStrF & ip) { _Func->UnbanIP(const_cast< SQChar * >(ip
SQMOD_NODISCARD static bool LgIsIPBanned(StackStrF & ip) { return _Func->IsIPBanned(const_cast< SQChar * >(ip.mPtr)) >= 1; }
// ------------------------------------------------------------------------------------------------
SQMOD_NODISCARD static int LgGetPlayerIDFromName(StackStrF & name) { return _Func->GetPlayerIdFromName(name.mPtr); }
SQMOD_NODISCARD static bool LgIsWorldCompatibleWithPlayer (LgPlayer & player, int world)
{ return _Func->IsPlayerWorldCompatible(player.GetIdentifier(), world) >= 1; }
SQMOD_NODISCARD static bool LgIsWorldCompatibleWithPlayer(LightObj & player, int world)
{ return _Func->IsPlayerWorldCompatible(GetLgEntID< LgPlayer, CPlayer >(player), world) >= 1; }
// ------------------------------------------------------------------------------------------------
static LightObj & LgCreatePickupCompat(int model, const Vector3 & pos)
{ return Core::Get().NewPickup(model, 1, 0, pos.x, pos.y, pos.z, 255, false, SQMOD_CREATE_DEFAULT, NullLightObj()).mLgObj; }
@@ -1409,7 +1469,7 @@ static LightObj & LgCreatePickup(int model, int world, int quantity, const Vecto
static LightObj & LgCreateObject(int model, int world, const Vector3 & pos, int alpha)
{ return Core::Get().NewObject(model, world, pos.x, pos.y, pos.z, alpha, SQMOD_CREATE_DEFAULT, NullLightObj()).mLgObj; }
static LightObj & LgCreateCheckpoint(LightObj & player, int world, bool sphere, const Vector3 & pos, const Color4 & col, float radius) {
const int32_t id = player.IsNull() ? -1 : player.CastI< LgPlayer >()->GetIdentifier();
const int32_t id = player.IsNull() ? -1 : GetLgEntID< LgPlayer, CPlayer >(player);
return Core::Get().NewCheckpoint(id, world, sphere, pos.x, pos.y, pos.z, col.r, col.g, col.b, col.a, radius, SQMOD_CREATE_DEFAULT, NullLightObj()).mLgObj;
}
// ------------------------------------------------------------------------------------------------
@@ -1467,30 +1527,30 @@ SQMOD_NODISCARD static int LgBindKey(bool down, int key1, int key2, int key3)
SQMOD_NODISCARD static bool LgRemoveKeybind(int id) { return _Func->RemoveKeyBind(id) == vcmpErrorNone; }
static void LgRemoveAllKeybinds() { _Func->RemoveAllKeyBinds(); }
// ------------------------------------------------------------------------------------------------
SQMOD_NODISCARD static bool LgGetCinematicBorder(LgPlayer & player) { return _Func->GetPlayerOption(player.GetIdentifier(), vcmpPlayerOptionWidescreen) >= 1; }
SQMOD_NODISCARD static bool LgGetGreenScanLines(LgPlayer & player) { return _Func->GetPlayerOption(player.GetIdentifier(), vcmpPlayerOptionGreenScanlines) >= 1; }
SQMOD_NODISCARD static bool LgGetWhiteScanLines(LgPlayer & player) { return _Func->GetPlayerOption(player.GetIdentifier(), vcmpPlayerOptionWhiteScanlines) >= 1; }
static void LgSetCinematicBorder(LgPlayer & player, bool toggle) { _Func->SetPlayerOption(player.GetIdentifier(), vcmpPlayerOptionWidescreen, static_cast< uint8_t >(toggle)); }
static void LgSetGreenScanLines(LgPlayer & player, bool toggle) { _Func->SetPlayerOption(player.GetIdentifier(), vcmpPlayerOptionGreenScanlines, static_cast< uint8_t >(toggle)); }
static void LgSetWhiteScanLines(LgPlayer & player, bool toggle) { _Func->SetPlayerOption(player.GetIdentifier(), vcmpPlayerOptionWhiteScanlines, static_cast< uint8_t >(toggle)); }
SQMOD_NODISCARD static bool LgGetCinematicBorder(LightObj & player) { return _Func->GetPlayerOption(GetLgEntID< LgPlayer, CPlayer >(player), vcmpPlayerOptionWidescreen) >= 1; }
SQMOD_NODISCARD static bool LgGetGreenScanLines(LightObj & player) { return _Func->GetPlayerOption(GetLgEntID< LgPlayer, CPlayer >(player), vcmpPlayerOptionGreenScanlines) >= 1; }
SQMOD_NODISCARD static bool LgGetWhiteScanLines(LightObj & player) { return _Func->GetPlayerOption(GetLgEntID< LgPlayer, CPlayer >(player), vcmpPlayerOptionWhiteScanlines) >= 1; }
static void LgSetCinematicBorder(LightObj & player, bool toggle) { _Func->SetPlayerOption(GetLgEntID< LgPlayer, CPlayer >(player), vcmpPlayerOptionWidescreen, static_cast< uint8_t >(toggle)); }
static void LgSetGreenScanLines(LightObj & player, bool toggle) { _Func->SetPlayerOption(GetLgEntID< LgPlayer, CPlayer >(player), vcmpPlayerOptionGreenScanlines, static_cast< uint8_t >(toggle)); }
static void LgSetWhiteScanLines(LightObj & player, bool toggle) { _Func->SetPlayerOption(GetLgEntID< LgPlayer, CPlayer >(player), vcmpPlayerOptionWhiteScanlines, static_cast< uint8_t >(toggle)); }
// ------------------------------------------------------------------------------------------------
static void LgKickPlayer(LgPlayer & player) { _Func->KickPlayer(player.GetIdentifier()); }
static void LgBanPlayer(LgPlayer & player) { _Func->BanPlayer(player.GetIdentifier()); }
static void LgKickPlayer(LightObj & player) { _Func->KickPlayer(GetLgEntID< LgPlayer, CPlayer >(player)); }
static void LgBanPlayer(LightObj & player) { _Func->BanPlayer(GetLgEntID< LgPlayer, CPlayer >(player)); }
// ------------------------------------------------------------------------------------------------
static void LgMessage(StackStrF & msg) { _Func->SendClientMessage(-1, 0x0b5fa5ff, "%s", msg.mPtr); }
static void LgMessagePlayer(StackStrF & msg, LgPlayer & player) { _Func->SendClientMessage(player.GetIdentifier(), 0x0b5fa5ff, "%s", msg.mPtr); }
static void LgMessageAllExcept(StackStrF & msg, LgPlayer & player) {
const int32_t p = player.GetIdentifier();
static void LgMessagePlayer(StackStrF & msg, LightObj & player) { _Func->SendClientMessage(GetLgEntID< LgPlayer, CPlayer >(player), 0x0b5fa5ff, "%s", msg.mPtr); }
static void LgMessageAllExcept(StackStrF & msg, LightObj & player) {
const auto p = GetLgEntID< LgPlayer, CPlayer >(player);
const SQChar * m = msg.mPtr;
ForeachConnectedPlayer([=](int32_t id) { if (id != p) _Func->SendClientMessage(id, 0x0b5fa5ff, "%s", m); });
}
static void LgPrivMessage(LgPlayer & player, StackStrF & msg) { _Func->SendClientMessage(player.GetIdentifier(), 0x007f16ff, "** pm >> %s", msg.mPtr); }
static void LgPrivMessage(LightObj & player, StackStrF & msg) { _Func->SendClientMessage(GetLgEntID< LgPlayer, CPlayer >(player), 0x007f16ff, "** pm >> %s", msg.mPtr); }
static void LgPrivMessageAll(StackStrF & msg) {
const SQChar * m = msg.mPtr;
ForeachConnectedPlayer([=](int32_t id) { _Func->SendClientMessage(id, 0x007f16ff, "** pm >> %s", m); });
}
static void LgSendPlayerMessage(LgPlayer & source, LgPlayer & target, StackStrF & msg) {
_Func->SendClientMessage(target.GetIdentifier(), 0x007f16ff, "** pm from %s >> %s", source.Get().GetName(), msg.mPtr);
static void LgSendPlayerMessage(LightObj & source, LightObj & target, StackStrF & msg) {
_Func->SendClientMessage(GetLgEntID< LgPlayer, CPlayer >(target), 0x007f16ff, "** pm from %s >> %s",GetNativeEnt< LgPlayer, CPlayer >(source).GetName(), msg.mPtr);
}
// ------------------------------------------------------------------------------------------------
SQMOD_NODISCARD static const SQChar * LgGetWeaponName(int id) { return GetWeaponName(static_cast< uint32_t >(id)); }
@@ -1946,6 +2006,7 @@ void Register_Official_Functions(HSQUIRRELVM vm)
.Func(_SC("SetVehiclesForcedRespawnHeight"), LgSetVehiclesForcedRespawnHeight)
.SquirrelFunc(_SC("FindPlayer"), LgFindPlayer)
.SquirrelFunc(_SC("FindPlayerCompat"), LgFindPlayer)
.SquirrelFunc(_SC("InPoly"), LgInPoly)
.SquirrelFunc(_SC("SetAmmuWeapon"), LgSetAmmuWeapon)
@@ -2261,8 +2322,8 @@ struct LgStream {
int32_t id;
if (target.IsNull()) id = -1;
else if (target.GetType() == OT_INTEGER || target.GetType() == OT_FLOAT) id = target.Cast< int32_t >();
else if (static_cast< AbstractStaticClassData * >(target.GetTypeTag()) == StaticClassTypeTag< LgPlayer >::Get()) {
id = target.CastI< LgPlayer >()->GetIdentifier();
else if (target.GetType() == OT_INSTANCE) {
id = GetLgEntID< LgPlayer, CPlayer >(target);
} else STHROWF("Invalid target type");
if (id >= SQMOD_PLAYER_POOL) STHROWF("Invalid player ID");
_Func->SendClientScriptData(id, m_OutputStreamData, m_OutputStreamEnd);

View File

@@ -114,7 +114,7 @@ int32_t GetSkinID(StackStrF & name)
// Clone the string into an editable version
String str(name.mPtr, static_cast< size_t >(name.mLen));
// Strip non-alphanumeric characters from the name
str.erase(std::remove_if(str.begin(), str.end(), std::function<int(int)>(::isalnum)), str.end());
str.erase(std::remove_if(str.begin(), str.end(), [](char c) -> bool { return std::isalnum(c) == 0; }), str.end());
// Convert the string to lowercase
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
// See if we still have a valid name after the cleanup

View File

@@ -123,7 +123,7 @@ int32_t GetAutomobileID(StackStrF & name)
// Clone the string into an editable version
String str(name.mPtr, static_cast< size_t >(name.mLen));
// Strip non-alphanumeric characters from the name
str.erase(std::remove_if(str.begin(), str.end(), std::function<int(int)>(::isalnum)), str.end());
str.erase(std::remove_if(str.begin(), str.end(), [](char c) -> bool { return std::isalnum(c) == 0; }), str.end());
// Convert the string to lowercase
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
// See if we still have a valid name after the cleanup

View File

@@ -163,7 +163,7 @@ int32_t GetWeaponID(StackStrF & name)
// Clone the string into an editable version
String str(name.mPtr, static_cast< size_t >(name.mLen));
// Strip non-alphanumeric characters from the name
str.erase(std::remove_if(str.begin(), str.end(), std::function<int(int)>(::isalnum)), str.end());
str.erase(std::remove_if(str.begin(), str.end(), [](char c) -> bool { return std::isalnum(c) == 0; }), str.end());
// Convert the string to lowercase
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
// See if we still have a valid name after the cleanup
@@ -324,6 +324,7 @@ int32_t GetWeaponID(StackStrF & name)
// [S]PAS-12 Shotgun
// [S]tubby Shotgun
// [S]uicide
// Pump action [S]hotgun
case 's':
// [Sc]rewdriver
if (b == 'c') return SQMOD_WEAPON_SCREWDRIVER;
@@ -337,7 +338,7 @@ int32_t GetWeaponID(StackStrF & name)
else if (b == 't') return SQMOD_WEAPON_STUBBY;
// [Su]icide
else if (b == 'u') return SQMOD_WEAPON_SUICIDE;
// Pump action [Sh]otgun
// Pump action [Sh]otgun
else if (b == 'h') return SQMOD_WEAPON_SHOTGUN;
// Default to unknwon
else return SQMOD_UNKNOWN;

View File

@@ -1,5 +1,8 @@
// ------------------------------------------------------------------------------------------------
#include "PocoLib/Data.hpp"
#include "Core/ThreadPool.hpp"
#include "Library/SQLite.hpp"
#include "Library/MySQL.hpp"
#include "Poco/Data/SessionImpl.h"
// ------------------------------------------------------------------------------------------------
@@ -34,6 +37,7 @@ SQMOD_DECL_TYPENAME(SqPcDataStatement, _SC("SqDataStatement"))
SQMOD_DECL_TYPENAME(SqPcDataRecordSet, _SC("SqDataRecordSet"))
SQMOD_DECL_TYPENAME(SqPcDataTransaction, _SC("SqDataTransaction"))
SQMOD_DECL_TYPENAME(SqPcDataSessionPool, _SC("SqDataSessionPool"))
SQMOD_DECL_TYPENAME(SqPcSqDataAsyncBuilder, _SC("SqSqDataAsyncBuilder"))
SQMOD_DECL_TYPENAME(SqPcDataStatementResult, _SC("SqDataStatementResult"))
// ------------------------------------------------------------------------------------------------
@@ -67,7 +71,7 @@ static LightObj SQLiteEscapeString(StackStrF & str)
// Allocate a memory buffer
Buffer b(static_cast< Buffer::SzType >(str.mLen * 2 + 1));
// Attempt to escape the specified string
sqlite3_snprintf(b.Capacity(), b.Get< char >(), "%q", str.mPtr);
sqlite3_snprintf(static_cast< int >(b.Capacity()), b.Get< char >(), "%q", str.mPtr);
// Return the resulted string
return LightObj(b.Get< SQChar >(), -1);
}
@@ -91,7 +95,7 @@ static LightObj SQLiteEscapeStringEx(SQChar spec, StackStrF & str)
// Allocate a memory buffer
Buffer b(static_cast< Buffer::SzType >(str.mLen * 2 + 1));
// Attempt to escape the specified string
sqlite3_snprintf(b.Capacity(), b.Get< char >(), fs, str.mPtr);
sqlite3_snprintf(static_cast< int >(b.Capacity()), b.Get< char >(), fs, str.mPtr);
// Return the resulted string
return LightObj(b.Get< SQChar >(), -1);
}
@@ -197,7 +201,7 @@ SqDataStatement SqDataSession::GetStatement(StackStrF & data)
// ------------------------------------------------------------------------------------------------
SqDataRecordSet SqDataSession::GetRecordSet(StackStrF & data)
{
return SqDataRecordSet(*this, data);
return {*this, data};
}
// ------------------------------------------------------------------------------------------------
@@ -472,8 +476,8 @@ SqDataStatement & SqDataStatement::Into_(LightObj & obj, LightObj & def)
}
// ------------------------------------------------------------------------------------------------
extern LightObj GteSQLiteFromSession(Poco::Data::SessionImpl * session);
extern LightObj GteMySQLFromSession(Poco::Data::SessionImpl * session);
extern LightObj GetSQLiteFromSession(Poco::Data::SessionImpl * session);
extern LightObj GetMySQLFromSession(Poco::Data::SessionImpl * session);
// ------------------------------------------------------------------------------------------------
LightObj SqDataSessionPool::GetSq()
@@ -484,17 +488,553 @@ LightObj SqDataSessionPool::GetSq()
// Is this a SQLite session?
if (connector == "sqlite")
{
return GteSQLiteFromSession(session_impl);
return GetSQLiteFromSession(session_impl);
}
// Is this a MySQL session?
else if (connector == "mysql")
{
return GteMySQLFromSession(session_impl);
return GetMySQLFromSession(session_impl);
}
STHROWF("Unknown connector type {}", connector);
SQ_UNREACHABLE
}
// ------------------------------------------------------------------------------------------------
LightObj SqDataSessionPool::AsyncExec(StackStrF & sql)
{
return LightObj{SqTypeIdentity< SqDataAsyncBuilder >{}, SqVM(), get().impl(), sql, true, false, false};
}
// ------------------------------------------------------------------------------------------------
LightObj SqDataSessionPool::AsyncQuery(StackStrF & sql)
{
return LightObj{SqTypeIdentity< SqDataAsyncBuilder >{}, SqVM(), get().impl(), sql, false, true, false};
}
// ------------------------------------------------------------------------------------------------
LightObj SqDataSessionPool::IncAsyncQuery(StackStrF & sql)
{
return LightObj{SqTypeIdentity< SqDataAsyncBuilder >{}, SqVM(), get().impl(), sql, false, true, true};
}
// ------------------------------------------------------------------------------------------------
LightObj SqDataSessionPool::ExecAsyncQuery(StackStrF & sql)
{
return LightObj{SqTypeIdentity< SqDataAsyncBuilder >{}, SqVM(), get().impl(), sql, true, true, false};
}
/* ------------------------------------------------------------------------------------------------
* Asynchronous SQLite query execution implementation.
*/
struct SQLiteAsyncExec : public ThreadPoolItem
{
using SessionRef = Poco::AutoPtr< Poco::Data::SessionImpl >;
// --------------------------------------------------------------------------------------------
SQLiteConnRef mConnection{}; // Internal connection handle.
// --------------------------------------------------------------------------------------------
Function mResolved{}; // Callback to invoke when the task was completed.
Function mRejected{}; // Callback to invoke when the task was aborted.
// --------------------------------------------------------------------------------------------
int32_t mResult{SQLITE_OK}; // Execution result code.
int32_t mChanges{0}; // Rows affected by this query.
// --------------------------------------------------------------------------------------------
const SQChar * mQueryStr{nullptr}; // The query string that will be executed.
LightObj mQueryObj{}; // Strong reference to the query string object.
// --------------------------------------------------------------------------------------------
LightObj mCtx{}; // User specified context object, if any.
// --------------------------------------------------------------------------------------------
String mError{}; // Error message, if any.
/* --------------------------------------------------------------------------------------------
* Base constructor. Members are supposed to be validated and filled by the builder/proxy.
*/
SQLiteAsyncExec() = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~SQLiteAsyncExec() override = default;
/* --------------------------------------------------------------------------------------------
* Provide a name to what type of task this is. Mainly for debugging purposes.
*/
SQMOD_NODISCARD const char * TypeName() noexcept override { return "sqlite async execute"; }
/* --------------------------------------------------------------------------------------------
* Provide unique information that may help identify the task. Mainly for debugging purposes.
*/
SQMOD_NODISCARD const char * IdentifiableInfo() noexcept override { return mQueryStr; }
/* --------------------------------------------------------------------------------------------
* Invoked in worker thread by the thread pool after obtaining the task from the queue.
* Must return true to indicate that the task can be performed. False indicates failure.
*/
SQMOD_NODISCARD bool OnPrepare() override
{
// Coincidentally, this also dirties the handle time-stamp so, it doesn't get collected
return mConnection->mSession->isConnected();
}
/* --------------------------------------------------------------------------------------------
* Called in worker by the thread pool to performed by the associated tasks.
* Will be called continuously while the returned value is true. While false means it finished.
*/
SQMOD_NODISCARD bool OnProcess() override
{
char * err_msg = nullptr;
// Attempt to execute the specified query
mResult = sqlite3_exec(mConnection->Access(), mQueryStr, nullptr, nullptr, &err_msg);
// Store changes count
if (mResult == SQLITE_OK)
{
mChanges = sqlite3_changes(mConnection->Access());
}
// Check for error message
if (err_msg != nullptr)
{
mError.assign(err_msg);
sqlite3_free(err_msg);
}
// Don't retry
return false;
};
/* --------------------------------------------------------------------------------------------
* Invoked in main thread by the thread pool after the task was completed.
* If it returns true then it will be put back into the queue to be processed again.
* If the boolean parameter is try then the thread-pool is in the process of shutting down.
*/
SQMOD_NODISCARD bool OnCompleted(bool SQ_UNUSED_ARG(stop)) override
{
if (mResult == SQLITE_OK)
{
if (!mResolved.IsNull())
{
LightObj c{SqTypeIdentity< SQLiteConnection >{}, SqVM(), mConnection};
mResolved.Execute(c, mCtx, mChanges, mQueryObj);
}
}
else if (!mRejected.IsNull())
{
LightObj c{SqTypeIdentity< SQLiteConnection >{}, SqVM(), mConnection};
mRejected.Execute(c, mCtx, mResult, mError, mQueryObj);
}
// Finished
return false;
}
/* --------------------------------------------------------------------------------------------
* Called in worker by the thread pool to let the task know that it will be aborted.
* Most likely due to a shutdown of the thread pool.
*/
void OnAborted(bool SQ_UNUSED_ARG(retry)) override
{
// We don't really have to do anything for now
}
};
/* ------------------------------------------------------------------------------------------------
* Asynchronous SQLite statement implementation.
*/
struct SQLiteAsyncStmtBase : public ThreadPoolItem
{
using SessionRef = Poco::AutoPtr< Poco::Data::SessionImpl >;
// --------------------------------------------------------------------------------------------
SQLiteConnRef mConnection{}; // Internal connection handle.
SQLiteStmtRef mStatement{}; // Internal statement handle.
// --------------------------------------------------------------------------------------------
Function mResolved{}; // Callback to invoke when the task was completed.
Function mRejected{}; // Callback to invoke when the task was aborted.
Function mPrepared{}; // Callback to invoke when the task must be prepared.
// --------------------------------------------------------------------------------------------
SQLiteStatement * mStatementPtr{nullptr}; // Pointer to the script statement instance.
LightObj mStatementObj{}; // Strong reference to the statement instance object.
// --------------------------------------------------------------------------------------------
const SQChar * mQueryStr{nullptr}; // The query string that will be executed.
LightObj mQueryObj{}; // Strong reference to the query string object.
// --------------------------------------------------------------------------------------------
LightObj mCtx{}; // User specified context object, if any.
// --------------------------------------------------------------------------------------------
int32_t mChanges{0}; // Rows affected by this query.
bool mPrepped{false}; // Whether the statement was prepared.
bool mRow{false}; // Whether we still have rows to process.
/* --------------------------------------------------------------------------------------------
* Base constructor. Members are supposed to be validated and filled by the builder/proxy.
*/
SQLiteAsyncStmtBase() = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~SQLiteAsyncStmtBase() override = default;
/* --------------------------------------------------------------------------------------------
* Provide unique information that may help identify the task. Mainly for debugging purposes.
*/
SQMOD_NODISCARD const char * IdentifiableInfo() noexcept override { return mQueryStr; }
/* --------------------------------------------------------------------------------------------
* Invoked in worker thread by the thread pool after obtaining the task from the queue.
* Must return true to indicate that the task can be performed. False indicates failure.
*/
SQMOD_NODISCARD bool OnPrepare() override
{
// Coincidentally, this also dirties the handle time-stamp so, it doesn't get collected
if (mConnection->mSession->isConnected())
{
if (mStatement->mPtr == nullptr)
{
mStatement->Create(mQueryStr, static_cast< SQInteger >(strlen(mQueryStr)));
// Statement was not prepared/filled with information (yet)
mPrepped = mPrepared.IsNull();
}
// Prepared
return true;
}
// Can't prepare
return false;
}
/* --------------------------------------------------------------------------------------------
* Called in worker by the thread pool to let the task know that it will be aborted.
* Most likely due to a shutdown of the thread pool.
*/
void OnAborted(bool SQ_UNUSED_ARG(retry)) override
{
// We don't really have to do anything for now
}
};
/* ------------------------------------------------------------------------------------------------
* Asynchronous SQLite statement execution implementation.
*/
struct SQLiteAsyncStmtExec : public SQLiteAsyncStmtBase
{
/* --------------------------------------------------------------------------------------------
* Provide a name to what type of task this is. Mainly for debugging purposes.
*/
SQMOD_NODISCARD const char * TypeName() noexcept override { return "sqlite async query exec"; }
/* --------------------------------------------------------------------------------------------
* Called in worker by the thread pool to performed by the associated tasks.
* Will be called continuously while the returned value is true. While false means it finished.
*/
SQMOD_NODISCARD bool OnProcess() override
{
// Was the statement prepared?
if (mPrepped)
{
mChanges = mStatementPtr->Exec();
}
// Don't retry
return false;
};
/* --------------------------------------------------------------------------------------------
* Invoked in main thread by the thread pool after the task was completed.
* If it returns true then it will be put back into the queue to be processed again.
* If the boolean parameter is try then the thread-pool is in the process of shutting down.
*/
SQMOD_NODISCARD bool OnCompleted(bool SQ_UNUSED_ARG(stop)) override
{
if (mPrepped && mStatement->mDone)
{
if (!mResolved.IsNull())
{
LightObj o = mResolved.Eval(mStatementObj, mCtx, mChanges);
// Should we abort the whole thing?
if (!o.IsNull() && o.Cast< bool >() == false)
{
return false; // Allow to abort itself
}
}
// No longer prepared
mPrepped = false;
}
// Allow to prepare itself, either on initial call or again after execution
if (!mPrepped && (mStatement->mStatus == SQLITE_OK || mStatement->mStatus == SQLITE_DONE))
{
mPrepped = true;
// Is there a prepping callback?
if (!mPrepared.IsNull())
{
// Should we reset?
if (mStatement->mStatus == SQLITE_DONE)
{
mStatementPtr->Reset();
}
LightObj o = mPrepared.Eval(mStatementObj, mCtx);
// Should we abort the whole thing?
if (!o.IsNull())
{
return o.Cast< bool >(); // Allow to abort itself
}
else
{
return true; // Re-queue the task by default
}
}
}
else if (!mRejected.IsNull() && (mStatement->mStatus != SQLITE_OK && mStatement->mStatus != SQLITE_DONE))
{
mRejected.Execute(mStatementObj, mCtx, mQueryObj);
}
// Finished
return false;
}
};
/* ------------------------------------------------------------------------------------------------
* Asynchronous SQLite statement stepping implementation.
*/
struct SQLiteAsyncStmtStep : public SQLiteAsyncStmtBase
{
/* --------------------------------------------------------------------------------------------
* Provide a name to what type of task this is. Mainly for debugging purposes.
*/
SQMOD_NODISCARD const char * TypeName() noexcept override { return "sqlite async query step"; }
/* --------------------------------------------------------------------------------------------
* Called in worker by the thread pool to performed by the associated tasks.
* Will be called continuously while the returned value is true. While false means it finished.
*/
SQMOD_NODISCARD bool OnProcess() override
{
// Was the statement prepared?
if (mPrepped)
{
mRow = mStatementPtr->Step();
}
// Don't retry
return false;
};
/* --------------------------------------------------------------------------------------------
* Invoked in main thread by the thread pool after the task was completed.
* If it returns true then it will be put back into the queue to be processed again.
* If the boolean parameter is try then the thread-pool is in the process of shutting down.
*/
SQMOD_NODISCARD bool OnCompleted(bool SQ_UNUSED_ARG(stop)) override
{
// This is only done once, before performing any step
if (!mPrepped)
{
mPrepped = true;
// Is there a prepping callback?
if (!mPrepared.IsNull())
{
LightObj o = mPrepared.Eval(mStatementObj, mCtx);
// Should we abort the whole thing?
if (!o.IsNull())
{
return o.Cast< bool >(); // Allow to abort itself
}
else
{
return true; // Re-queue the task by default
}
}
}
if (mStatement->mGood || mStatement->mDone)
{
if (!mResolved.IsNull())
{
// You are expected to step the statement manually until the end
mResolved.Execute(mStatementObj, mCtx);
}
}
else if (!mRejected.IsNull())
{
mRejected.Execute(mStatementObj, mCtx, mQueryObj);
}
// Finished
return false;
}
};
/* ------------------------------------------------------------------------------------------------
* Asynchronous SQLite incremental statement stepping implementation.
*/
struct SQLiteAsyncStmtIncStep : public SQLiteAsyncStmtBase
{
/* --------------------------------------------------------------------------------------------
* Provide a name to what type of task this is. Mainly for debugging purposes.
*/
SQMOD_NODISCARD const char * TypeName() noexcept override { return "sqlite incremental async query step"; }
/* --------------------------------------------------------------------------------------------
* Called in worker by the thread pool to performed by the associated tasks.
* Will be called continuously while the returned value is true. While false means it finished.
*/
SQMOD_NODISCARD bool OnProcess() override
{
// Was the statement prepared?
if (mPrepped)
{
mRow = mStatementPtr->Step();
}
// Don't retry
return false;
};
/* --------------------------------------------------------------------------------------------
* Invoked in main thread by the thread pool after the task was completed.
* If it returns true then it will be put back into the queue to be processed again.
* If the boolean parameter is try then the thread-pool is in the process of shutting down.
*/
SQMOD_NODISCARD bool OnCompleted(bool SQ_UNUSED_ARG(stop)) override
{
// This is only done once, before performing any step
if (!mPrepped)
{
mPrepped = true;
// Is there a prepping callback?
if (!mPrepared.IsNull())
{
LightObj o = mPrepared.Eval(mStatementObj, mCtx);
// Should we abort the whole thing?
if (!o.IsNull())
{
return o.Cast< bool >(); // Allow to abort itself
}
else
{
return true; // Re-queue the task by default
}
}
}
if (mStatement->mGood && !mStatement->mDone)
{
if (!mResolved.IsNull())
{
// Should all steps be completed here?
if (stop)
{
do {
LightObj o = mResolved.Eval(mStatementObj, mCtx);
// Should we abort the whole thing?
if (!o.IsNull() && !o.Cast< bool >())
{
return false;
}
// Don't let exceptions be stupid
mRow = false;
// Force process whole statement
if (mStatement->mGood) OnProcess();
} while (mRow);
}
else
{
LightObj o = mResolved.Eval(mStatementObj, mCtx);
// Should we abort the whole thing?
if (!o.IsNull() && !o.Cast< bool >())
{
return false;
}
}
}
}
else if (!mRejected.IsNull() && (mStatement->mStatus != SQLITE_OK && mStatement->mStatus != SQLITE_DONE))
{
mRejected.Execute(mStatementObj, mCtx, mQueryObj);
}
// Re-queue if we still have rows to process
return mRow;
}
};
// ------------------------------------------------------------------------------------------------
SqDataAsyncBuilder::SqDataAsyncBuilder(Poco::Data::SessionImpl * session, StackStrF & sql, bool exec, bool stmt, bool inc) noexcept
: mSession(session, true)
, mResolved(), mRejected()
, mQueryStr(sql.mPtr), mQueryObj(sql.mObj)
, mExec(exec), mStmt(stmt), mInc(inc)
{
}
// ------------------------------------------------------------------------------------------------
void SqDataAsyncBuilder::Submit_(LightObj & ctx)
{
if (mSession.isNull())
{
STHROWF("Asynchronous query builder instance is invalid.");
}
auto & connector = mSession->connectorName();
// Is this a SQLite session?
if (connector == "sqlite")
{
// Retrieve the internal handle property
auto * connection = Poco::AnyCast< sqlite3 * >(mSession->getProperty("handle"));
// Is this a statement?
if (mStmt)
{
SQLiteAsyncStmtBase * item = nullptr;
// Is this just for executing?
if (mExec)
{
item = static_cast< SQLiteAsyncStmtBase * >(new SQLiteAsyncStmtExec());
}
// Is this incremental?
else if (mInc)
{
item = static_cast< SQLiteAsyncStmtBase * >(new SQLiteAsyncStmtIncStep());
}
else
{
item = static_cast< SQLiteAsyncStmtBase * >(new SQLiteAsyncStmtStep());
}
// Take ownership before any exception can be thrown
std::unique_ptr< ThreadPoolItem > task{static_cast< ThreadPoolItem * >(item)};
// Populate task information
item->mConnection = SQLiteConnRef{new SQLiteConnHnd(std::move(mSession))};
item->mStatement = SQLiteStmtRef{new SQLiteStmtHnd(item->mConnection)};
item->mResolved = std::move(mResolved);
item->mRejected = std::move(mRejected);
item->mPrepared = std::move(mPrepared);
item->mStatementObj = LightObj{SqTypeIdentity< SQLiteStatement >{}, SqVM(), item->mStatement};
item->mStatementPtr = item->mStatementObj.CastI< SQLiteStatement >();
item->mQueryStr = mQueryStr;
item->mQueryObj = std::move(mQueryObj);
item->mCtx = std::move(ctx);
// Submit the task
ThreadPool::Get().Enqueue(std::move(task));
}
else
{
auto * item = new SQLiteAsyncExec();
// Take ownership before any exception can be thrown
std::unique_ptr< ThreadPoolItem > task{static_cast< ThreadPoolItem * >(item)};
// Populate task information
item->mConnection = SQLiteConnRef{new SQLiteConnHnd(std::move(mSession))};
item->mResolved = std::move(mResolved);
item->mRejected = std::move(mRejected);
item->mQueryStr = mQueryStr;
item->mQueryObj = std::move(mQueryObj);
item->mCtx = std::move(ctx);
// Submit the task
ThreadPool::Get().Enqueue(std::move(task));
}
}
// Is this a MySQL session?
else if (connector == "mysql")
{
if (mStmt)
{
//...
}
else
{
//...
}
}
else
{
STHROWF("Unknown connector type {}", connector);
}
}
// ------------------------------------------------------------------------------------------------
LightObj SqDataSessionPool::GetProperty(StackStrF & name)
{
@@ -771,6 +1311,19 @@ void Register_POCO_Data(HSQUIRRELVM vm, Table &)
.Overload(_SC("Value"), &SqDataRecordSet::GetValueOr)
);
// --------------------------------------------------------------------------------------------
ns.Bind(_SC("AsyncBuilder"),
Class< SqDataAsyncBuilder, NoConstructor< SqDataAsyncBuilder > >(vm, SqPcSqDataAsyncBuilder::Str)
// Meta-methods
.SquirrelFunc(_SC("_typename"), &SqPcSqDataAsyncBuilder::Fn)
// Member Methods
.CbFunc(_SC("Resolved"), &SqDataAsyncBuilder::OnResolved)
.CbFunc(_SC("Rejected"), &SqDataAsyncBuilder::OnRejected)
.CbFunc(_SC("Prepared"), &SqDataAsyncBuilder::OnPrepared)
// Overloaded methods
.Overload(_SC("Submit"), &SqDataAsyncBuilder::Submit)
.Overload(_SC("Submit"), &SqDataAsyncBuilder::Submit_)
);
// --------------------------------------------------------------------------------------------
ns.Bind(_SC("SessionPool"),
Class< SqDataSessionPool, NoCopy< SqDataSessionPool > >(vm, SqPcDataSessionPool::Str)
// Constructors
@@ -790,6 +1343,10 @@ void Register_POCO_Data(HSQUIRRELVM vm, Table &)
// Member Methods
.Func(_SC("Get"), &SqDataSessionPool::Get)
.Func(_SC("GetSq"), &SqDataSessionPool::GetSq)
.FmtFunc(_SC("AsyncExec"), &SqDataSessionPool::AsyncExec)
.FmtFunc(_SC("AsyncQuery"), &SqDataSessionPool::AsyncQuery)
.FmtFunc(_SC("IncAsyncQuery"), &SqDataSessionPool::IncAsyncQuery)
.FmtFunc(_SC("ExecAsyncQuery"), &SqDataSessionPool::ExecAsyncQuery)
.FmtFunc(_SC("GetWithProperty"), &SqDataSessionPool::GetWithProperty)
.FmtFunc(_SC("GetWithFeature"), &SqDataSessionPool::GetWithFeature)
.FmtFunc(_SC("SetFeature"), &SqDataSessionPool::SetFeature)

View File

@@ -1771,6 +1771,26 @@ struct SqDataSessionPool : public SessionPool
*/
LightObj GetSq();
/* --------------------------------------------------------------------------------------------
* Create an asynchronous query execution builder.
*/
LightObj AsyncExec(StackStrF & sql);
/* --------------------------------------------------------------------------------------------
* Create an asynchronous query execution builder.
*/
LightObj AsyncQuery(StackStrF & sql);
/* --------------------------------------------------------------------------------------------
* Create an asynchronous query execution builder.
*/
LightObj IncAsyncQuery(StackStrF & sql);
/* --------------------------------------------------------------------------------------------
* Create an asynchronous query execution builder.
*/
LightObj ExecAsyncQuery(StackStrF & sql);
/* --------------------------------------------------------------------------------------------
* Retrieve a Session with requested property set.
*/
@@ -2075,4 +2095,92 @@ struct SqDataTransaction : public Transaction
}
};
/* ------------------------------------------------------------------------------------------------
* Common session action implementation.
*/
struct SqDataAsyncBuilder
{
using SessionRef = Poco::AutoPtr< Poco::Data::SessionImpl >;
// --------------------------------------------------------------------------------------------
SessionRef mSession{}; // The connection that will be used by the task.
// --------------------------------------------------------------------------------------------
Function mResolved{}; // Callback to invoke when the task was completed.
Function mRejected{}; // Callback to invoke when the task was aborted.
Function mPrepared{}; // Callback to invoke when the task was must be prepared.
// --------------------------------------------------------------------------------------------
const SQChar * mQueryStr{nullptr}; // The query string that will be executed.
LightObj mQueryObj{}; // Strong reference to the query string object.
// --------------------------------------------------------------------------------------------
bool mExec{false}; // Whether this is a query execution.
bool mStmt{false}; // Whether this is a query statement.
bool mInc{false}; // Whether this is an incremental query statement.
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
SqDataAsyncBuilder(Poco::Data::SessionImpl * session, StackStrF & sql, bool exec, bool stmt, bool inc) noexcept;
/* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
SqDataAsyncBuilder(const SqDataAsyncBuilder & o) = delete;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
SqDataAsyncBuilder(SqDataAsyncBuilder && o) = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~SqDataAsyncBuilder() = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
*/
SqDataAsyncBuilder & operator = (const SqDataAsyncBuilder & o) = delete;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
SqDataAsyncBuilder & operator = (SqDataAsyncBuilder && o) = default;
/* --------------------------------------------------------------------------------------------
* Create the task with the supplied information and submit it to the worker pool.
*/
void Submit() { Submit_(NullLightObj()); }
/* --------------------------------------------------------------------------------------------
* Create the task with the supplied information and submit it to the worker pool.
*/
void Submit_(LightObj & ctx);
/* --------------------------------------------------------------------------------------------
* Set the callback to be executed if the query was resolved.
*/
SqDataAsyncBuilder & OnResolved(Function & cb)
{
mResolved = std::move(cb);
return *this; // Allow chaining
}
/* --------------------------------------------------------------------------------------------
* Set the callback to be executed if the query was rejected/failed.
*/
SqDataAsyncBuilder & OnRejected(Function & cb)
{
mRejected = std::move(cb);
return *this; // Allow chaining
}
/* --------------------------------------------------------------------------------------------
* Set the callback to be executed in order to compose the query/statement.
*/
SqDataAsyncBuilder & OnPrepared(Function & cb)
{
mPrepared = std::move(cb);
return *this; // Allow chaining
}
};
} // Namespace:: SqMod

View File

@@ -1,6 +1,3 @@
#ifndef _REGISTER_HPP_
#define _REGISTER_HPP_
// ------------------------------------------------------------------------------------------------
#include <squirrelex.h>
@@ -144,5 +141,3 @@ bool RegisterAPI(HSQUIRRELVM vm)
}
} // Namespace:: SqMod
#endif // _REGISTER_HPP_

75
module/SDK/sqmod.h Normal file
View File

@@ -0,0 +1,75 @@
#if !defined(_SQ_MOD_API_H_)
#define _SQ_MOD_API_H_
#include <stdint.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#ifndef SQMOD_API_EXPORT
#if defined(_MSC_VER)
#define SQMOD_API_EXPORT extern "C" __declspec(dllexport)
#elif defined(__GNUC__)
#define SQMOD_API_EXPORT extern "C"
#else
#define SQMOD_API_EXPORT extern "C"
#endif
#endif
#define SQMOD_HOST_NAME "SqModHost"
#define SQMOD_INITIALIZE_CMD 0xDABBAD00 // host plug-in was initialized
#define SQMOD_LOAD_CMD 0xDEADBABE // API is being registered
#define SQMOD_TERMINATE_CMD 0xDEADC0DE // release your resources
#define SQMOD_CLOSING_CMD 0xBAAAAAAD // virtual machine is closing
#define SQMOD_RELEASED_CMD 0xDEADBEAF // virtual machine was closed
#define SQMOD_API_VER 1
// --------------------------------------------------------------------------------------------
typedef int32_t(*ExtPluginCommand_t)(int32_t, int32_t, int32_t, const uint8_t *, size_t);
/* --------------------------------------------------------------------------------------------
* The structure exported by the host plug-in to import the module and squirrel API.
*/
typedef struct
{
uint32_t StructSize;
/* Register a pointer to a function used to processes commands from script.
This is like the functionality offered by SendPluginCommand but offers interaction from
script side without interfering with other native plugins. And also offers a few extra
methods of identification to provide back and forth communication.
It offers a bare minimum, primitive way of interacting with the script from native plug-ins.
return : -1 it failed (no free slot), 0 if it was already registered and 1 if it succeeded.
*/
int32_t (*RegisterCommand) (ExtPluginCommand_t fn);
/* Remove a pointer to a function used to processes commands from script.
return : -1 it failed (no free slot) and 1 if it succeeded.
*/
int32_t (*UnregisterCommand) (ExtPluginCommand_t fn);
/* Send a command to all functions currently registered to receive them. This is mostly used by the script.
target - ideally a unique value that can be used to identify the intended audience for the command.
req : ideally a unique value that can be used to identify the requested information from the command.
tag : ideally a unique value that can be used to identify a later response if one is generated.
data : binary data that represents the command payload. the command is free to interpret it however it wants.
size : size of the binary data. most likely in bytes but the command is free to interpret it however it wants.
*/
int32_t (*SendCommand) (int32_t target, int32_t req, int32_t tag, const uint8_t * data, size_t size);
/* Send a response to the script that may have resulted from a previous command. This is mostly by the native plug-ins.
sender : ideally a unique value that can be used to identify the intended who generated the response.
tag : ideally a unique value that can be used to identify what the generated response might contain/provide.
data : binary data that represents the command payload. the command is free to interpret it however it wants.
size : size of the binary data. most likely in bytes but the command is free to interpret it however it wants.
*/
int32_t (*SendCommandReply) (int32_t sender, int32_t tag, const uint8_t * data, size_t size);
/* Forward an event to the script from an external plug-in. This is mostly by the native plug-ins.
Similar to SendCommandReply but may not have been the result of a previous command.
*/
int32_t (*SendCommandEvent) (int32_t sender, int32_t tag, const uint8_t * data, size_t size);
} sq_mod_exports, SQ_MOD_EXPORTS, *HSQ_MOD_EXPORTS;
#ifdef __cplusplus
} /*extern "C"*/
#endif // __cplusplus
#endif /*_SQ_MOD_API_H_*/

View File

@@ -75,7 +75,7 @@
#define SQMOD_NAME "Squirrel Module"
#define SQMOD_AUTHOR "Sandu Liviu Catalin (S.L.C)"
#define SQMOD_COPYRIGHT "Copyright (C) 2021 Sandu Liviu Catalin"
#define SQMOD_COPYRIGHT "Copyright (C) 2022 Sandu Liviu Catalin"
#define SQMOD_HOST_NAME "SqModHost"
#define SQMOD_VERSION 001
#define SQMOD_VERSION_STR "0.0.1"

View File

@@ -88,6 +88,15 @@ struct LightObj {
sq_addref(SqVM(), &mObj);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Constructs a LightObj from a string object
///
/// \param o Squirrel object
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
explicit LightObj(const string & s) : LightObj(s.c_str(), static_cast< SQInteger >(s.size())) {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Constructs a LightObj from a Squirrel object at a certain index on the stack
///
@@ -186,21 +195,7 @@ struct LightObj {
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<class T, class... A>
LightObj(SqTypeIdentity< T > SQ_UNUSED_ARG(t), HSQUIRRELVM vm, A&&... a) {
// Create the instance and guard it to make sure it gets deleted in case of exceptions
DeleteGuard< T > instance(new T(std::forward< A >(a)...));
// Preserve the stack state
const StackGuard sg(vm);
// Push the instance on the stack
ClassType<T>::PushInstance(vm, instance);
// Attempt to retrieve it
if (SQ_FAILED(sq_getstackobj(vm, -1, &mObj))) {
sq_resetobject(&mObj);
} else {
sq_addref(vm, &mObj);
}
// Stop guarding the instance
instance.Release();
LightObj(SqTypeIdentity< T > SQ_UNUSED_ARG(t), HSQUIRRELVM vm, A&&... a) : LightObj(DeleteGuard< T >(new T(std::forward< A >(a)...))) {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -237,7 +232,21 @@ struct LightObj {
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<class T>
LightObj(DeleteGuard<T> guard, HSQUIRRELVM v = SqVM()) : LightObj(guard.Get(), v) {
LightObj(DeleteGuard<T> && guard, HSQUIRRELVM v = SqVM()) : LightObj(guard.Get(), v) {
guard.Release();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Constructs an LightObj from a C++ instance wrapped inside a DeleteGuard
///
/// \param instance Pointer to a C++ class instance that has been bound already
/// \param v VM that the object will exist in
///
/// \tparam T Type of instance
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<class T>
LightObj(DeleteGuard<T> & guard, HSQUIRRELVM v = SqVM()) : LightObj(guard.Get(), v) {
guard.Release();
}

View File

@@ -35,6 +35,7 @@
#include <string>
#include <utility>
#include <exception>
#include <type_traits>
#include <unordered_map>
#include <Poco/Exception.h>
@@ -1580,12 +1581,31 @@ public:
}
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Helper structure for one element from the top of stack. Uses default global VM instead.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct SqPopTopGuardLite
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Base constructor.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SqPopTopGuardLite() noexcept = default;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Destructor. Pops the specified elements from the stack.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
~SqPopTopGuardLite()
{
sq_poptop(SqVM());
}
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Helper structure for one element from the top of stack.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct SqPopTopGuard
{
HSQUIRRELVM mVM; // The VM from which the elements must be popped.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Base constructor.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -1603,13 +1623,70 @@ struct SqPopTopGuard
}
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Helper structure for popping elements from the stack. Uses default global VM instead.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct SqPopGuardLite
{
SQInteger mNum{0}; // The number of elements to be popped.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Base constructor.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
explicit SqPopGuardLite(SQInteger num) noexcept
: mNum(num)
{
//...
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Destructor. Pops the specified elements from the stack.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
~SqPopGuardLite()
{
sq_pop(SqVM(), mNum);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Increment the number of elements to be popped.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SqPopGuardLite & operator ++ ()
{
++mNum;
return *this;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Decrement the number of elements to be popped.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SqPopGuardLite & operator -- ()
{
--mNum;
return *this;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Increase the number of elements to be popped.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SqPopGuardLite & operator += (SQInteger n)
{
mNum += n;
return *this;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Decrease the number of elements to be popped.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SqPopGuardLite & operator -= (SQInteger n)
{
mNum -= n;
return *this;
}
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Helper structure for popping elements from the stack.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct SqPopGuard
{
HSQUIRRELVM mVM; // The VM from which the elements must be popped.
SQInteger mNum; // The number of elements to be popped.
HSQUIRRELVM mVM{nullptr}; // The VM from which the elements must be popped.
SQInteger mNum{0}; // The number of elements to be popped.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Base constructor.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -1659,17 +1736,79 @@ struct SqPopGuard
}
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Implements RAII to restore the VM stack to it's initial size on function exit. Uses default global VM instead.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct StackGuardLite
{
SQInteger mTop{0}; ///< The top of the stack when this instance was created.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Default constructor.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
StackGuardLite() noexcept
: mTop(sq_gettop(SqVM()))
{
/* ... */
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Copy constructor. (disabled)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
StackGuardLite(const StackGuardLite &) = delete;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Move constructor. (disabled)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
StackGuardLite(StackGuardLite &&) = delete;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Destructor.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
~StackGuardLite()
{
Restore();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Copy assignment operator. (disabled)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
StackGuardLite & operator = (const StackGuardLite &) = delete;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Move assignment operator. (disabled)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
StackGuardLite & operator = (StackGuardLite &&) = delete;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Restore the stack to what was known to be.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Restore() const
{
auto vm = SqVM();
// Retrieve the new stack top
const SQInteger top = sq_gettop(vm);
// Did the stack size change?
if (top > mTop)
{
sq_pop(vm, top - mTop); // Trim the stack
}
}
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Implements RAII to restore the VM stack to it's initial size on function exit.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct StackGuard
{
HSQUIRRELVM mVM{nullptr}; ///< The VM where the stack should be restored.
SQInteger mTop{0}; ///< The top of the stack when this instance was created.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Default constructor.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
StackGuard()
: m_VM(SqVM()), m_Top(sq_gettop(m_VM))
: mVM(SqVM()), mTop(sq_gettop(mVM))
{
/* ... */
}
@@ -1677,8 +1816,8 @@ struct StackGuard
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Base constructor.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
StackGuard(HSQUIRRELVM vm)
: m_VM(vm), m_Top(sq_gettop(vm))
explicit StackGuard(HSQUIRRELVM vm)
: mVM(vm), mTop(sq_gettop(vm))
{
/* ... */
}
@@ -1717,18 +1856,13 @@ struct StackGuard
void Restore() const
{
// Retrieve the new stack top
const SQInteger top = sq_gettop(m_VM);
const SQInteger top = sq_gettop(mVM);
// Did the stack size change?
if (top > m_Top)
if (top > mTop)
{
sq_pop(m_VM, top - m_Top); // Trim the stack
sq_pop(mVM, top - mTop); // Trim the stack
}
}
private:
HSQUIRRELVM m_VM; ///< The VM where the stack should be restored.
SQInteger m_Top; ///< The top of the stack when this instance was created.
};
@@ -2131,18 +2265,13 @@ inline void ErrorToException(HSQUIRRELVM vm, bool keep = false) {
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template < typename T > struct DeleteGuard
{
private:
// --------------------------------------------------------------------------------------------
T * m_Ptr; // Pointer to the instance to manage.
public:
T * mPtr; // Pointer to the instance to manage.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Default constructor.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
explicit DeleteGuard(T * ptr)
: m_Ptr(ptr)
: mPtr(ptr)
{
/* ... */
}
@@ -2152,7 +2281,7 @@ public:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<class... A>
explicit DeleteGuard(SqInPlace SQ_UNUSED_ARG(t), A&&... a)
: m_Ptr(new T(std::forward< A >(a)...))
: mPtr(new T(std::forward< A >(a)...))
{
/* ... */
}
@@ -2166,9 +2295,9 @@ public:
/// Move constructor.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
DeleteGuard(DeleteGuard && o) noexcept
: m_Ptr(o.m_Ptr)
: mPtr(o.mPtr)
{
o.m_Ptr = nullptr;
o.mPtr = nullptr;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -2176,9 +2305,9 @@ public:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
~DeleteGuard()
{
if (m_Ptr)
if (mPtr)
{
delete m_Ptr;
delete mPtr;
}
}
@@ -2197,7 +2326,7 @@ public:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
operator T * () const
{
return m_Ptr;
return mPtr;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -2205,7 +2334,7 @@ public:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
T * Get() const
{
return m_Ptr;
return mPtr;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -2213,7 +2342,7 @@ public:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Release()
{
m_Ptr = nullptr;
mPtr = nullptr;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -2221,8 +2350,8 @@ public:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
T * Grab()
{
T * ptr = m_Ptr;
m_Ptr = nullptr;
T * ptr = mPtr;
mPtr = nullptr;
return ptr;
}
};
@@ -2320,18 +2449,13 @@ template < typename T > T * SqChainedInstances< T >::sHead = nullptr;
/// @cond DEV
/// Used internally to get and manipulate the underlying type of variables - retrieved from cppreference.com
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<class T> struct remove_const {typedef T type;};
template<class T> struct remove_const<const T> {typedef T type;};
template<class T> struct remove_volatile {typedef T type;};
template<class T> struct remove_volatile<volatile T> {typedef T type;};
template<class T> struct remove_cv {typedef typename remove_volatile<typename remove_const<T>::type>::type type;};
template<class T> struct is_pointer_helper {static constexpr bool value = false;};
template<class T> struct is_pointer_helper<T*> {static constexpr bool value = true;};
template<class T,class D> struct is_pointer_helper<SharedPtr<T,D> > {static constexpr bool value = true;};
template<class T,class D> struct is_pointer_helper<WeakPtr<T,D> > {static constexpr bool value = true;};
template<class T> struct is_pointer : is_pointer_helper<typename remove_cv<T>::type> {};
template<class T> struct is_reference {static constexpr bool value = false;};
template<class T> struct is_reference<T&> {static constexpr bool value = true;};
template<class T> using remove_const = std::remove_const< T >;
template<class T> using remove_volatile = std::remove_volatile< T >;
template<class T> using remove_cv = std::remove_cv< T >;
template<class T> struct is_pointer : public std::is_pointer< T > { };
template<class T,class D> struct is_pointer<SharedPtr<T,D> > : public std::true_type { };
template<class T,class D> struct is_pointer<WeakPtr<T,D> > : public std::true_type { };
template<class T> using is_reference = std::is_lvalue_reference< T >;
/// @endcond