Kawe Mazidjatari 1c32339305 Squirrel: reverse more squirrel types and structures
This patch removes a lot of old code. Context is now exclusively grabbed from the CSquirrelVM instance. This patch also comes with a few new types: SQArray and SQTable!

The implementation also allows pushing Vector3D's on the stack, but these are handled slightly differently.. The largest field in tagSQObjectValue is 8 bytes, Vector3D is 12 bytes unaligned, but the tagSQObjectValue field in the tagSQObject struct is aligned to a 8 byte boundary while the field prior is only 4 bytes, Vector3D starts right after the type field in the tagSQObject (at the padding) to keep the whole structure the same size, therefore a new field has been added in between the padding (_pad) with a simple Vector3D accessor.

Also added a hook to allow registering proper script enums.
2024-04-05 18:31:06 +02:00

175 lines
5.3 KiB
C++

//=============================================================================//
//
// Purpose: Squirrel API interface to engine
//
//=============================================================================//
#include "core/stdafx.h"
#include "squirrel.h"
#include "sqvm.h"
#include "sqarray.h"
#include "sqstring.h"
//---------------------------------------------------------------------------------
bool sq_aux_gettypedarg(HSQUIRRELVM v, SQInteger idx, SQObjectType type, SQObjectPtr** o)
{
*o = &stack_get(v, idx);
if (sq_type(**o) != type) {
SQObjectPtr oval;
v->PrintObjVal(**o, oval);
v_SQVM_RaiseError(v, _SC("wrong argument type, expected '%s' got '%.50s'"), IdType2Name(type), _stringval(oval));
return false;
}
return true;
}
//---------------------------------------------------------------------------------
#define _GETSAFE_OBJ(v,idx,type,o) { if(!sq_aux_gettypedarg(v,idx,type,&o)) return SQ_ERROR; }
#define sq_aux_paramscheck(v,count) \
{ \
if(sq_gettop(v) < count){ v_SQVM_RaiseError(v, _SC("not enough params in the stack")); return SQ_ERROR; }\
}
//---------------------------------------------------------------------------------
SQChar* sq_getstring(HSQUIRRELVM v, SQInteger i) // TODO: deprecate and remove!
{
return v->_stackbase[i]._unVal.pString->_val;
}
//---------------------------------------------------------------------------------
SQRESULT sq_getstring(HSQUIRRELVM v, SQInteger idx, const SQChar** c)
{
SQObjectPtr* o = NULL;
_GETSAFE_OBJ(v, idx, OT_STRING, o);
*c = _stringval(*o);
return SQ_OK;
}
//---------------------------------------------------------------------------------
SQInteger sq_getinteger(HSQUIRRELVM v, SQInteger i)
{
return v->_stackbase[i]._unVal.nInteger;
}
//---------------------------------------------------------------------------------
SQRESULT sq_get(HSQUIRRELVM v, SQInteger idx)
{
return v_sq_get(v, idx);
}
//---------------------------------------------------------------------------------
SQInteger sq_gettop(HSQUIRRELVM v)
{
return (v->_top - v->_bottom);
}
//---------------------------------------------------------------------------------
SQRESULT sq_pushroottable(HSQUIRRELVM v)
{
v->Push(v->_roottable);
return SQ_OK;
}
//---------------------------------------------------------------------------------
void sq_pushbool(HSQUIRRELVM v, SQBool b)
{
v->Push(b?true:false);
}
//---------------------------------------------------------------------------------
void sq_pushstring(HSQUIRRELVM v, const SQChar* s, SQInteger len)
{
if (s)
{
SQString* pString = SQString::Create(v->_sharedstate, s, len);
v->Push(pString);
}
else
v->Push(_null_);
}
//---------------------------------------------------------------------------------
void sq_pushinteger(HSQUIRRELVM v, SQInteger val)
{
v->Push(val);
}
//---------------------------------------------------------------------------------
void sq_pushfloat(HSQUIRRELVM v, SQFloat n)
{
v->Push(n);
}
//---------------------------------------------------------------------------------
void sq_newarray(HSQUIRRELVM v, SQInteger size)
{
v_sq_newarray(v, size);
}
//---------------------------------------------------------------------------------
void sq_newtable(HSQUIRRELVM v)
{
v_sq_newtable(v);
}
//---------------------------------------------------------------------------------
SQRESULT sq_newslot(HSQUIRRELVM v, SQInteger idx)
{
return v_sq_newslot(v, idx);
}
//---------------------------------------------------------------------------------
SQRESULT sq_arrayappend(HSQUIRRELVM v, SQInteger idx)
{
return v_sq_arrayappend(v, idx);
}
//---------------------------------------------------------------------------------
SQRESULT sq_pushstructure(HSQUIRRELVM v, const SQChar* name, const SQChar* member, const SQChar* codeclass1, const SQChar* codeclass2)
{
return v_sq_pushstructure(v, name, member, codeclass1, codeclass2);
}
//---------------------------------------------------------------------------------
SQRESULT sq_compilebuffer(HSQUIRRELVM v, SQBufState* bufferState, const SQChar* buffer, SQInteger level)
{
return v_sq_compilebuffer(v, bufferState, buffer, level);
}
//---------------------------------------------------------------------------------
SQRESULT sq_call(HSQUIRRELVM v, SQInteger params, SQBool retval, SQBool raiseerror)
{
return v_sq_call(v, params, retval, raiseerror);
}
SQRESULT sq_startconsttable(HSQUIRRELVM v)
{
return v_sq_startconsttable(v);
}
SQRESULT sq_endconsttable(HSQUIRRELVM v)
{
return v_sq_endconsttable(v);
}
void VSquirrelAPI::Detour(const bool bAttach) const
{
DetourSetup(&v_sq_pushroottable, &sq_pushroottable, bAttach);
//DetourSetup(&v_sq_pushbool, &sq_pushbool, bAttach);
//DetourSetup(&v_sq_pushstring, &sq_pushstring, bAttach);
//DetourSetup(&v_sq_pushinteger, &sq_pushinteger, bAttach);
//DetourSetup(&v_sq_pushfloat, &sq_pushfloat, bAttach);
DetourSetup(&v_sq_newarray, &sq_newarray, bAttach);
DetourSetup(&v_sq_newtable, &sq_newtable, bAttach);
DetourSetup(&v_sq_newslot, &sq_newslot, bAttach);
DetourSetup(&v_sq_arrayappend, &sq_arrayappend, bAttach);
DetourSetup(&v_sq_pushstructure, &sq_pushstructure, bAttach);
DetourSetup(&v_sq_compilebuffer, &sq_compilebuffer, bAttach);
DetourSetup(&v_sq_call, &sq_call, bAttach);
DetourSetup(&v_sq_startconsttable, &sq_startconsttable, bAttach);
DetourSetup(&v_sq_endconsttable, &sq_endconsttable, bAttach);
}