From 4af93aff14ca007dd363aff6a13b4a320f3239b1 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Wed, 2 Mar 2022 18:57:11 +0200 Subject: [PATCH] Allow tasks to also be identified by their tag. Change the behavior of DropTask to return boolean. --- module/Core/Tasks.cpp | 49 +++++++++++++++++++++++++++++++++++-------- module/Core/Tasks.hpp | 6 +++--- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/module/Core/Tasks.cpp b/module/Core/Tasks.cpp index 5845abcc..4ef5ab23 100644 --- a/module/Core/Tasks.cpp +++ b/module/Core/Tasks.cpp @@ -328,17 +328,40 @@ SQInteger Tasks::Find(int32_t id, int32_t type, SQInteger & pos, HSQUIRRELVM vm) { // Grab the top of the stack const SQInteger top = sq_gettop(vm); - // Was there a callback specified? + // Was there a callback or tag specified? if (top <= 1) { - return sq_throwerror(vm, "Missing task callback"); + return sq_throwerror(vm, "Missing task callback or tag"); } - SQRESULT res = SQ_OK; - // Grab the hash of the callback object - const SQHash chash = sq_gethash(vm, 2); + // Fetch the task identifier type + 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? - if (top > 3) + else if (top > 3) { SQInteger intrv = 0; // 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 } + // Grab the hash of the callback object + const SQHash chash = sq_gethash(vm, 2); // Attempt to find the requested task 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 } + // Grab the hash of the callback object + const SQHash chash = sq_gethash(vm, 2); // Cast iterations to the right type const Iterator itr = ConvTo< Iterator >::From(sqitr); // Attempt to find the requested task @@ -388,6 +415,8 @@ SQInteger Tasks::Find(int32_t id, int32_t type, SQInteger & pos, HSQUIRRELVM vm) } else { + // Grab the hash of the callback object + const SQHash chash = sq_gethash(vm, 2); // Attempt to find the requested task 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? else if (pos < 0) { - return sq_throwerror(vm, "Unable to locate such task"); + sq_pushbool(vm, SQFalse); // Unable to locate such task } else { @@ -424,9 +453,11 @@ SQInteger Tasks::Remove(int32_t id, int32_t type, HSQUIRRELVM vm) s_Tasks[pos].Terminate(); // Reset the timer s_Intervals[pos] = 0; + // A task was successfully removed + sq_pushbool(vm, SQTrue); } - // Specify that we don't return anything - return 0; + // Specify that we return a value + return 1; } // ------------------------------------------------------------------------------------------------ diff --git a/module/Core/Tasks.hpp b/module/Core/Tasks.hpp index 3a396d1c..b7e73b8f 100644 --- a/module/Core/Tasks.hpp +++ b/module/Core/Tasks.hpp @@ -37,9 +37,9 @@ private: LightObj mData; // A reference to the arbitrary data associated with this instance. Iterator mIterations; // Number of iterations before self destruct. Interval mInterval; // Interval between task invocations. - 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 mArgc; // The number of arguments that the task must forward. + 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 mArgc; // The number of arguments that the task must forward. Argument mArgv[8]; // The arguments that the task must forward. /* ----------------------------------------------------------------------------------------