1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-07-22 16:57:12 +02:00

Migrated the host module to C++ exceptions as well.

Also enabled the latest C++ revision in the project.
Replaced the Random library with the one provided by C++11.
Implemented a simple AES256 encryption class.
Various other fixes and improvements.
This commit is contained in:
Sandu Liviu Catalin
2016-03-10 05:57:13 +02:00
parent 3162221e7f
commit 70e5f0ba21
124 changed files with 14873 additions and 14062 deletions

View File

@@ -5,8 +5,8 @@
#include "SqBase.hpp"
// ------------------------------------------------------------------------------------------------
#include <math.h>
#include <assert.h>
#include <cmath>
#include <cassert>
// ------------------------------------------------------------------------------------------------
#include <vcmp.h>
@@ -19,7 +19,7 @@ namespace SqMod {
extern const SQChar * g_EmptyStr;
/* ------------------------------------------------------------------------------------------------
* Proxies to comunicate with the server.
* Proxies to communicate with the server.
*/
extern PluginFuncs* _Func;
extern PluginCallbacks* _Clbk;
@@ -28,7 +28,9 @@ extern PluginInfo* _Info;
// ------------------------------------------------------------------------------------------------
template < typename T > struct NumLimit;
// ------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Basic minimum and maximum values for primitive numeric types.
*/
template <> struct NumLimit< char > { static const char Min, Max; };
template <> struct NumLimit< signed char > { static const signed char Min, Max; };
template <> struct NumLimit< unsigned char > { static const unsigned char Min, Max; };
@@ -44,7 +46,58 @@ template <> struct NumLimit< float > { static const float Min, Max; };
template <> struct NumLimit< double > { static const double Min, Max; };
template <> struct NumLimit< long double > { static const long double Min, Max; };
// ------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Implements RAII to restore the VM stack to it's initial size on function exit.
*/
struct StackGuard
{
/* --------------------------------------------------------------------------------------------
* Base constructor.
*/
StackGuard(HSQUIRRELVM vm)
: m_Top(sq_gettop(vm)), m_VM(vm)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
StackGuard(const StackGuard &) = delete;
/* --------------------------------------------------------------------------------------------
* Move constructor. (disabled)
*/
StackGuard(StackGuard &&) = delete;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~StackGuard()
{
sq_pop(m_VM, sq_gettop(m_VM) - m_Top);
}
/* --------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
*/
StackGuard & operator = (const StackGuard &) = delete;
/* --------------------------------------------------------------------------------------------
* Move assignment operator. (disabled)
*/
StackGuard & operator = (StackGuard &&) = delete;
private:
// --------------------------------------------------------------------------------------------
Int32 m_Top; /* The top of the stack when this instance was created. */
HSQUIRRELVM m_VM; /* The VM where the stack should be restored. */
};
/* ------------------------------------------------------------------------------------------------
* Perform an equality comparison between two values taking into account floating point issues.
*/
template< typename T > inline bool EpsEq(const T a, const T b)
{
return abs(a - b) <= 0;
@@ -60,7 +113,9 @@ template <> inline bool EpsEq(const Float64 a, const Float64 b)
return fabs(a - b) <= 0.000000001d;
}
// ------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Perform a less than comparison between two values taking into account floating point issues.
*/
template< typename T > inline bool EpsLt(const T a, const T b)
{
return !EpsEq(a, b) && (a < b);
@@ -76,7 +131,9 @@ template <> inline bool EpsLt(const Float64 a, const Float64 b)
return !EpsEq(a, b) && (a - b) < 0.000000001d;
}
// ------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Perform a greater than comparison between two values taking into account floating point issues.
*/
template< typename T > inline bool EpsGt(const T a, const T b)
{
return !EpsEq(a, b) && (a > b);
@@ -92,7 +149,10 @@ template <> inline bool EpsGt(const Float64 a, const Float64 b)
return !EpsEq(a, b) && (a - b) > 0.000000001d;
}
// ------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Perform a less than or equal comparison between two values taking into account
* floating point issues.
*/
template< typename T > inline bool EpsLtEq(const T a, const T b)
{
return !EpsEq(a, b) || (a < b);
@@ -108,7 +168,10 @@ template <> inline bool EpsLtEq(const Float64 a, const Float64 b)
return !EpsEq(a, b) || (a - b) < 0.000000001d;
}
// ------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Perform a greater than or equal comparison between two values taking into account
* floating point issues.
*/
template< typename T > inline bool EpsGtEq(const T a, const T b)
{
return !EpsEq(a, b) || (a > b);
@@ -124,7 +187,9 @@ template <> inline bool EpsGtEq(const Float64 a, const Float64 b)
return !EpsEq(a, b) || (a - b) > 0.000000001d;
}
// ------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Force a value to be within a certain range.
*/
template< typename T > inline T Clamp(T val, T min, T max)
{
return val < min ? min : (val > max ? max : val);
@@ -144,6 +209,142 @@ inline Uint32 NextPow2(Uint32 num)
return ++num;
}
/* ------------------------------------------------------------------------------------------------
* Output a message only if the _DEBUG was defined.
*/
void OutputDebug(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Output a formatted user message to the console.
*/
void OutputMessage(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Output a formatted error message to the console.
*/
void OutputError(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Retrieve a reference to a null script object.
*/
Object & NullObject();
/* ------------------------------------------------------------------------------------------------
* Retrieve a reference to a null/empty script array.
*/
Array & NullArray();
/* ------------------------------------------------------------------------------------------------
* Retrieve a reference to a null script function.
*/
Function & NullFunction();
/* ------------------------------------------------------------------------------------------------
* Create a script object from the specified value on the default VM.
*/
template < typename T > Object MakeObject(const T & v)
{
PushVar< T >(DefaultVM::Get(), v);
Var< Object > var(DefaultVM::Get(), -1);
sq_pop(DefaultVM::Get(), 1);
return var.value;
}
/* ------------------------------------------------------------------------------------------------
* Create a script object from the specified value on the specified VM.
*/
template < typename T > Object MakeObject(HSQUIRRELVM vm, const T & v)
{
PushVar< T >(vm, v);
Var< Object > var(vm, -1);
sq_pop(vm, 1);
return var.value;
}
/* ------------------------------------------------------------------------------------------------
* Simple function to check whether the specified string can be considered as a boolean value
*/
bool SToB(CSStr str);
/* ------------------------------------------------------------------------------------------------
* Generate a formatted string and throw it as a sqrat exception.
*/
void SqThrowF(CCStr fmt, ...);
/* ------------------------------------------------------------------------------------------------
* Quickly generate a formatted string on a small static buffer without any memory allocations.
*/
CSStr ToStrF(CCStr fmt, ...);
/* ------------------------------------------------------------------------------------------------
* Generate a formatted string on a temporary buffer and return the string but not the buffer.
*/
CSStr ToStringF(CCStr fmt, ...);
/* ------------------------------------------------------------------------------------------------
* Obtain a randomly chosen color from a list of known colors.
*/
const Color3 & GetRandomColor();
/* ------------------------------------------------------------------------------------------------
* Attempt to identify the color in the specified name and return it.
*/
Color3 GetColor(CSStr name);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the AABB type from a string.
*/
const AABB & GetAABB(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Circle type from a string.
*/
const Circle & GetCircle(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Color3 type from a string.
*/
const Color3 & GetColor3(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Color4 type from a string.
*/
const Color4 & GetColor4(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Quaternion type from a string.
*/
const Quaternion & GetQuaternion(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Sphere type from a string.
*/
const Sphere & GetSphere(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Vector2 type from a string.
*/
const Vector2 & GetVector2(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Vector2i type from a string.
*/
const Vector2i & GetVector2i(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Vector3 type from a string.
*/
const Vector3 & GetVector3(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Vector4 type from a string.
*/
const Vector4 & GetVector4(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Forward declarations of the logging functions to avoid including the logger everywhere.
*/
// ------------------------------------------------------------------------------------------------
void LogDbg(CCStr fmt, ...);
void LogUsr(CCStr fmt, ...);
@@ -180,84 +381,6 @@ bool cLogSWrn(bool cond, CCStr fmt, ...);
bool cLogSErr(bool cond, CCStr fmt, ...);
bool cLogSFtl(bool cond, CCStr fmt, ...);
// ------------------------------------------------------------------------------------------------
void SqThrow(CCStr fmt, ...);
/* ------------------------------------------------------------------------------------------------
* Output a message only if the _DEBUG was defined.
*/
void OutputDebug(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Output a formatted user message to the console.
*/
void OutputMessage(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Output a formatted error message to the console.
*/
void OutputError(const char * msg, ...);
// ------------------------------------------------------------------------------------------------
Object & NullObject();
// ------------------------------------------------------------------------------------------------
Array & NullArray();
// ------------------------------------------------------------------------------------------------
Function & NullFunction();
// ------------------------------------------------------------------------------------------------
template < typename T > Object MakeObject(const T & v)
{
PushVar< T >(DefaultVM::Get(), v);
Var< Object > var(DefaultVM::Get(), -1);
sq_pop(DefaultVM::Get(), 1);
return var.value;
}
// ------------------------------------------------------------------------------------------------
template < typename T > Object MakeObject(HSQUIRRELVM vm, const T & v)
{
PushVar< T >(vm, v);
Var< Object > var(vm, -1);
sq_pop(vm, 1);
return var.value;
}
/* ------------------------------------------------------------------------------------------------
* Simple function to check whether the specified string can be considered as a boolean value
*/
bool SToB(CSStr str);
/* ------------------------------------------------------------------------------------------------
*
*/
CSStr ToStrF(CCStr fmt, ...);
/* ------------------------------------------------------------------------------------------------
*
*/
CSStr ToStringF(CCStr fmt, ...);
// ------------------------------------------------------------------------------------------------
const Color3 & GetRandomColor();
/* ------------------------------------------------------------------------------------------------
* Value extractors.
*/
Color3 GetColor(CSStr name);
AABB GetAABB(CSStr str, SQChar delim);
Circle GetCircle(CSStr str, SQChar delim);
Color3 GetColor3(CSStr str, SQChar delim);
Color4 GetColor4(CSStr str, SQChar delim);
Quaternion GetQuaternion(CSStr str, SQChar delim);
Sphere GetSphere(CSStr str, SQChar delim);
Vector2 GetVector2(CSStr str, SQChar delim);
Vector2i GetVector2i(CSStr str, SQChar delim);
Vector3 GetVector3(CSStr str, SQChar delim);
Vector4 GetVector4(CSStr str, SQChar delim);
} // Namespace:: SqMod
#endif // _BASE_SHARED_HPP_