1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01:00

Allow tasks to also be identified by their tag.

Change the behavior of DropTask to return boolean.
This commit is contained in:
Sandu Liviu Catalin 2022-03-02 18:57:11 +02:00
parent d246ee8430
commit 4af93aff14
2 changed files with 43 additions and 12 deletions

View File

@ -328,17 +328,40 @@ SQInteger Tasks::Find(int32_t id, int32_t type, SQInteger & pos, HSQUIRRELVM vm)
{ {
// Grab the top of the stack // Grab the top of the stack
const SQInteger top = sq_gettop(vm); const SQInteger top = sq_gettop(vm);
// Was there a callback specified? // Was there a callback or tag specified?
if (top <= 1) if (top <= 1)
{ {
return sq_throwerror(vm, "Missing task callback"); return sq_throwerror(vm, "Missing task callback or tag");
} }
SQRESULT res = SQ_OK; SQRESULT res = SQ_OK;
// Grab the hash of the callback object // Fetch the task identifier type
const SQHash chash = sq_gethash(vm, 2); const SQObjectType ot = sq_gettype(vm, 2);
// Are we looking for a task with a specific tag?
if (ot == OT_STRING)
{
// Attempt to retrieve the value from the stack as a string
StackStrF tag(vm, 2);
// Have we failed to retrieve the string?
if (SQ_FAILED(tag.Proc(true)))
{
return tag.mRes; // Propagate the error!
}
// Attempt to find the requested task
for (const auto & t : s_Tasks)
{
if (t.mEntity == id && t.mType == type && t.mTag.compare(0, String::npos, tag.mPtr) == 0)
{
pos = static_cast< SQInteger >(&t - s_Tasks); // Store the index of this element
}
}
}
// Validate the callback type
else if (ot != OT_CLOSURE && ot != OT_NATIVECLOSURE)
{
return sq_throwerror(vm, "Invalid callback type");
}
// Should we include the iterations in the criteria? // Should we include the iterations in the criteria?
if (top > 3) else if (top > 3)
{ {
SQInteger intrv = 0; SQInteger intrv = 0;
// Grab the interval from the stack // Grab the interval from the stack
@ -348,6 +371,8 @@ SQInteger Tasks::Find(int32_t id, int32_t type, SQInteger & pos, HSQUIRRELVM vm)
{ {
return res; // Propagate the error return res; // Propagate the error
} }
// Grab the hash of the callback object
const SQHash chash = sq_gethash(vm, 2);
// Attempt to find the requested task // Attempt to find the requested task
for (const auto & t : s_Tasks) for (const auto & t : s_Tasks)
{ {
@ -375,6 +400,8 @@ SQInteger Tasks::Find(int32_t id, int32_t type, SQInteger & pos, HSQUIRRELVM vm)
{ {
return res; // Propagate the error return res; // Propagate the error
} }
// Grab the hash of the callback object
const SQHash chash = sq_gethash(vm, 2);
// Cast iterations to the right type // Cast iterations to the right type
const Iterator itr = ConvTo< Iterator >::From(sqitr); const Iterator itr = ConvTo< Iterator >::From(sqitr);
// Attempt to find the requested task // Attempt to find the requested task
@ -388,6 +415,8 @@ SQInteger Tasks::Find(int32_t id, int32_t type, SQInteger & pos, HSQUIRRELVM vm)
} }
else else
{ {
// Grab the hash of the callback object
const SQHash chash = sq_gethash(vm, 2);
// Attempt to find the requested task // Attempt to find the requested task
for (const auto & t : s_Tasks) for (const auto & t : s_Tasks)
{ {
@ -416,7 +445,7 @@ SQInteger Tasks::Remove(int32_t id, int32_t type, HSQUIRRELVM vm)
// Did we find anything? // Did we find anything?
else if (pos < 0) else if (pos < 0)
{ {
return sq_throwerror(vm, "Unable to locate such task"); sq_pushbool(vm, SQFalse); // Unable to locate such task
} }
else else
{ {
@ -424,9 +453,11 @@ SQInteger Tasks::Remove(int32_t id, int32_t type, HSQUIRRELVM vm)
s_Tasks[pos].Terminate(); s_Tasks[pos].Terminate();
// Reset the timer // Reset the timer
s_Intervals[pos] = 0; s_Intervals[pos] = 0;
// A task was successfully removed
sq_pushbool(vm, SQTrue);
} }
// Specify that we don't return anything // Specify that we return a value
return 0; return 1;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------

View File

@ -37,9 +37,9 @@ private:
LightObj mData; // A reference to the arbitrary data associated with this instance. LightObj mData; // A reference to the arbitrary data associated with this instance.
Iterator mIterations; // Number of iterations before self destruct. Iterator mIterations; // Number of iterations before self destruct.
Interval mInterval; // Interval between task invocations. Interval mInterval; // Interval between task invocations.
int16_t mEntity; // The identifier of the entity to which is belongs. int16_t mEntity; // The identifier of the entity to which is belongs.
uint8_t mType; // The type of the entity to which is belongs. uint8_t mType; // The type of the entity to which is belongs.
uint8_t mArgc; // The number of arguments that the task must forward. uint8_t mArgc; // The number of arguments that the task must forward.
Argument mArgv[8]; // The arguments that the task must forward. Argument mArgv[8]; // The arguments that the task must forward.
/* ---------------------------------------------------------------------------------------- /* ----------------------------------------------------------------------------------------