2016-02-27 10:57:10 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include "Common.hpp"
|
|
|
|
#include "Module.hpp"
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-04-14 02:08:35 +02:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdarg>
|
2016-02-28 16:38:57 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include <sqrat.h>
|
2016-02-27 10:57:10 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
namespace SqMod {
|
|
|
|
|
2016-02-28 16:38:57 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static SQChar g_Buffer[4096]; // Common buffer to reduce memory allocations.
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SStr GetTempBuff()
|
|
|
|
{
|
|
|
|
return g_Buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Uint32 GetTempBuffSize()
|
|
|
|
{
|
|
|
|
return sizeof(g_Buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void SqThrowF(CSStr str, ...)
|
|
|
|
{
|
|
|
|
// Initialize the argument list
|
|
|
|
va_list args;
|
|
|
|
va_start (args, str);
|
|
|
|
// Write the requested contents
|
2016-04-14 02:08:35 +02:00
|
|
|
if (std::vsnprintf(g_Buffer, sizeof(g_Buffer), str, args) < 0)
|
|
|
|
{
|
|
|
|
std::strcpy(g_Buffer, "Unknown error has occurred");
|
|
|
|
}
|
2016-02-28 16:38:57 +01:00
|
|
|
// Release the argument list
|
|
|
|
va_end(args);
|
|
|
|
// Throw the exception with the resulted message
|
|
|
|
throw Sqrat::Exception(g_Buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
CSStr FmtStr(CSStr str, ...)
|
|
|
|
{
|
|
|
|
// Initialize the argument list
|
|
|
|
va_list args;
|
|
|
|
va_start (args, str);
|
|
|
|
// Write the requested contents
|
2016-04-14 02:08:35 +02:00
|
|
|
if (std::vsnprintf(g_Buffer, sizeof(g_Buffer), str, args) < 0)
|
|
|
|
{
|
|
|
|
g_Buffer[0] = '\0'; // Make sure the string is terminated
|
|
|
|
}
|
2016-02-28 16:38:57 +01:00
|
|
|
// Release the argument list
|
|
|
|
va_end(args);
|
|
|
|
// Return the data from the buffer
|
|
|
|
return g_Buffer;
|
|
|
|
}
|
|
|
|
|
2016-04-14 02:08:35 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
StackGuard::StackGuard()
|
|
|
|
: m_VM(_SqVM), m_Top(sq_gettop(m_VM))
|
|
|
|
{
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
2016-02-28 16:38:57 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
StackGuard::StackGuard(HSQUIRRELVM vm)
|
2016-04-14 02:08:35 +02:00
|
|
|
: m_VM(vm), m_Top(sq_gettop(vm))
|
2016-02-28 16:38:57 +01:00
|
|
|
{
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
StackGuard::~StackGuard()
|
|
|
|
{
|
|
|
|
sq_pop(m_VM, sq_gettop(m_VM) - m_Top);
|
|
|
|
}
|
|
|
|
|
2016-04-14 02:08:35 +02:00
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
StackStrF::StackStrF(HSQUIRRELVM vm, SQInteger idx, bool fmt)
|
|
|
|
: mPtr(nullptr)
|
|
|
|
, mLen(-1)
|
|
|
|
, mRes(SQ_OK)
|
|
|
|
, mObj()
|
|
|
|
, mVM(vm)
|
|
|
|
{
|
|
|
|
const Int32 top = sq_gettop(vm);
|
|
|
|
// Reset the converted value object
|
|
|
|
sq_resetobject(&mObj);
|
|
|
|
// Was the string or value specified?
|
|
|
|
if (top <= (idx - 1))
|
|
|
|
{
|
|
|
|
mRes = sq_throwerror(vm, "Missing string or value");
|
|
|
|
}
|
|
|
|
// Do we have enough values to call the format function and are we allowed to?
|
|
|
|
else if (top > idx && fmt)
|
|
|
|
{
|
|
|
|
// Pointer to the generated string
|
|
|
|
SStr str = nullptr;
|
|
|
|
// Attempt to generate the specified string format
|
|
|
|
mRes = sqstd_format(vm, idx, &mLen, &str);
|
|
|
|
// Did the format succeeded but ended up with a null string pointer?
|
|
|
|
if (SQ_SUCCEEDED(mRes) && !str)
|
|
|
|
{
|
|
|
|
mRes = sq_throwerror(vm, "Unable to generate the string");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mPtr = const_cast< CSStr >(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Is the value on the stack an actual string?
|
|
|
|
else if (sq_gettype(vm, idx) == OT_STRING)
|
|
|
|
{
|
|
|
|
// Obtain a reference to the string object
|
|
|
|
mRes = sq_getstackobj(vm, idx, &mObj);
|
|
|
|
// Could we retrieve the object from the stack?
|
|
|
|
if (SQ_SUCCEEDED(mRes))
|
|
|
|
{
|
|
|
|
// Keep a strong reference to the object
|
|
|
|
sq_addref(vm, &mObj);
|
|
|
|
// Attempt to retrieve the string value from the stack
|
|
|
|
mRes = sq_getstring(vm, idx, &mPtr);
|
|
|
|
}
|
|
|
|
// Did the retrieval succeeded but ended up with a null string pointer?
|
|
|
|
if (SQ_SUCCEEDED(mRes) && !mPtr)
|
|
|
|
{
|
|
|
|
mRes = sq_throwerror(vm, "Unable to retrieve the string");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We have to try and convert it to string
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Attempt to convert the value from the stack to a string
|
|
|
|
mRes = sq_tostring(vm, idx);
|
|
|
|
// Could we convert the specified value to string?
|
|
|
|
if (SQ_SUCCEEDED(mRes))
|
|
|
|
{
|
|
|
|
// Obtain a reference to the resulted object
|
|
|
|
mRes = sq_getstackobj(vm, -1, &mObj);
|
|
|
|
// Could we retrieve the object from the stack?
|
|
|
|
if (SQ_SUCCEEDED(mRes))
|
|
|
|
{
|
|
|
|
// Keep a strong reference to the object
|
|
|
|
sq_addref(vm, &mObj);
|
|
|
|
// Attempt to obtain the string pointer
|
|
|
|
mRes = sq_getstring(vm, -1, &mPtr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Pop a value from the stack regardless of the result
|
|
|
|
sq_pop(vm, 1);
|
|
|
|
// Did the retrieval succeeded but ended up with a null string pointer?
|
|
|
|
if (SQ_SUCCEEDED(mRes) && !mPtr)
|
|
|
|
{
|
|
|
|
mRes = sq_throwerror(vm, "Unable to retrieve the value");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
StackStrF::~StackStrF()
|
|
|
|
{
|
|
|
|
if (mVM && !sq_isnull(mObj))
|
|
|
|
{
|
|
|
|
sq_release(mVM, &mObj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-27 10:57:10 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
int SampleFunction()
|
|
|
|
{
|
|
|
|
OutputMessage("Hello from the sample plugin function!");
|
|
|
|
return rand();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void SampleType::SampleMethod() const
|
|
|
|
{
|
|
|
|
OutputMessage("I have the values %d and %d", m_MyVal, mMyNum);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // Namespace:: SqMod
|