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

Compare commits

..

No commits in common. "4eaeeedf15bdabb1a73280c68574356191ca67f9" and "9298065cefe132f3482d1f97de1104acc5f43b3c" have entirely different histories.

7 changed files with 57 additions and 276 deletions

View File

@ -246,12 +246,7 @@ else()
endif() endif()
# Copy module into the plug-ins folder # Copy module into the plug-ins folder
add_custom_command(TARGET SqModule POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:SqModule> "${PROJECT_SOURCE_DIR}/bin/plugins") add_custom_command(TARGET SqModule POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:SqModule> "${PROJECT_SOURCE_DIR}/bin/plugins")
# Copy DPP into the bin folder
if (ENABLE_DISCORD)
if (WIN32 OR MINGW)
add_custom_command(TARGET SqModule POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:dpp> "${PROJECT_SOURCE_DIR}/bin")
endif()
endif()
# Copy several dependent DLLs on windows to make distribution easier (used mainly by people that distribute builds) # Copy several dependent DLLs on windows to make distribution easier (used mainly by people that distribute builds)
if(WIN32 AND MINGW AND COPY_DEPENDENCIES) if(WIN32 AND MINGW AND COPY_DEPENDENCIES)
get_filename_component(MINGW_BIN_PATH ${CMAKE_C_COMPILER} DIRECTORY REALPATH) get_filename_component(MINGW_BIN_PATH ${CMAKE_C_COMPILER} DIRECTORY REALPATH)

View File

@ -15,7 +15,6 @@ DpCluster::DpCluster(DpClusterOptions & o)
: Base(), mQueue(4096) : Base(), mQueue(4096)
, mC(std::make_unique< dpp::cluster >(o.mToken, o.mIntents, o.mShards, o.mClusterID, o.mMaxClusters, o.mCompressed, o.mPolicy, o.mRequestThreads, o.mRequestThreadsRaw)) , mC(std::make_unique< dpp::cluster >(o.mToken, o.mIntents, o.mShards, o.mClusterID, o.mMaxClusters, o.mCompressed, o.mPolicy, o.mRequestThreads, o.mRequestThreadsRaw))
, mSqEvents(), mEvents(), mEventsHandle() , mSqEvents(), mEvents(), mEventsHandle()
, mCCList(), mCCResults(std::make_shared< CCResultQueue >(4096))
{ {
// Make sure all event handles are not valid // Make sure all event handles are not valid
mEventsHandle.fill(0); mEventsHandle.fill(0);
@ -84,31 +83,6 @@ void DpCluster::Process(bool force)
p->Cleanup(); p->Cleanup();
} }
} }
CCResultItem cc_item;
// Retrieve each command completion result individually and process it
for (size_t count = mCCResults->size_approx(), n = 0; n <= count; ++n)
{
// Try to get a result from the queue
if (mCCResults->try_dequeue(cc_item))
{
CCResult & r = *cc_item;
// Get the script callback
Function & cb = *(r.first);
// Is there still a valid callback to invoke?
if (!cb.IsNull())
{
// Don't abort everything down the line for an error caused by a script callback
try {
cb.Execute(LightObj(SqTypeIdentity< DpCommandConfirmation >{}, cb.GetVM(), std::move(r.second)));
} catch (const std::exception & e) {
LogErr("Squirrel exception caught in discord command completion event");
LogSInf("Message: %s", e.what());
}
}
// Release the callback from the list
mCCList.erase(r.first);
}
}
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -148,7 +122,6 @@ void Register_Discord_Cluster(HSQUIRRELVM vm, Table & ns)
.Func(_SC("Stop"), &DpCluster::Stop) .Func(_SC("Stop"), &DpCluster::Stop)
.Func(_SC("EnableEvent"), &DpCluster::EnableEvent) .Func(_SC("EnableEvent"), &DpCluster::EnableEvent)
.Func(_SC("DisableEvent"), &DpCluster::DisableEvent) .Func(_SC("DisableEvent"), &DpCluster::DisableEvent)
.CbFunc(_SC("CurrentUserGetGuilds"), &DpCluster::CurrentUserGetGuilds)
); );
} }

View File

@ -333,64 +333,6 @@ private:
void OnStageInstanceCreate(const dpp::stage_instance_create_t & ev); void OnStageInstanceCreate(const dpp::stage_instance_create_t & ev);
void OnStageInstanceUpdate(const dpp::stage_instance_update_t & ev); void OnStageInstanceUpdate(const dpp::stage_instance_update_t & ev);
void OnStageInstanceDelete(const dpp::stage_instance_delete_t & ev); void OnStageInstanceDelete(const dpp::stage_instance_delete_t & ev);
public:
// List type for command completion callbacks.
using CCList = std::list< Function >;
/* --------------------------------------------------------------------------------------------
* List of command completion callbacks.
*/
CCList mCCList{};
/* --------------------------------------------------------------------------------------------
* Queue of iterators pointing to results of completed commands and their associated result.
*/
using CCResult = std::pair< CCList::iterator, DpCommandConfirmation::Ptr >;
using CCResultItem = std::unique_ptr< CCResult >;
using CCResultQueue = moodycamel::ConcurrentQueue< CCResultItem >;
/* --------------------------------------------------------------------------------------------
* Command completion links queue.
*/
std::shared_ptr< CCResultQueue > mCCResults{};
/* --------------------------------------------------------------------------------------------
* Maintains an iterator to the script callback associated with a completed command result.
*/
struct CCLink
{
// Iterator to the element where the state can be found.
CCList::iterator mItr{};
// Reference to the queue of iterators pointing to completed commands.
std::shared_ptr< CCResultQueue > mQueue{};
/* ----------------------------------------------------------------------------------------
* Base constructor.
*/
CCLink(CCList::iterator && itr, std::shared_ptr< CCResultQueue > & q)
: mItr(std::forward< CCList::iterator >(itr)), mQueue(q)
{ }
// Copy/Move constructors.
CCLink(const CCLink &) = default;
CCLink(CCLink &&) noexcept = default;
// Copy/Move assignment operators.
CCLink & operator = (const CCLink &) = default;
CCLink & operator = (CCLink &&) noexcept = default;
// Function call operator. Marks the linked callback as ready to invoke with obtained result.
void operator () (const dpp::confirmation_callback_t & cc) const
{
mQueue->enqueue(std::make_unique< CCResult >(std::move(mItr), std::make_unique< dpp::confirmation_callback_t >(cc.http_info)));
}
};
/* --------------------------------------------------------------------------------------------
* Get current (bot) user guilds.
* https://discord.com/developers/docs/resources/user#get-current-user-guilds
*/
void CurrentUserGetGuilds(Function & cb)
{
Valid("get message").current_user_get_guilds(CCLink(mCCList.emplace(mCCList.cend(), std::move(cb)), mCCResults));
}
}; };
} // Namespace:: SqMod } // Namespace:: SqMod

View File

@ -10,7 +10,6 @@ SQMOD_DECL_TYPENAME(SqDpUptime, _SC("SqDiscordUptime"))
SQMOD_DECL_TYPENAME(SqDpIconHash, _SC("SqDiscordIconHash")) SQMOD_DECL_TYPENAME(SqDpIconHash, _SC("SqDiscordIconHash"))
SQMOD_DECL_TYPENAME(SqDpVoiceState, _SC("SqDiscordVoiceState")) SQMOD_DECL_TYPENAME(SqDpVoiceState, _SC("SqDiscordVoiceState"))
SQMOD_DECL_TYPENAME(SqDpEmoji, _SC("SqDiscordEmoji")) SQMOD_DECL_TYPENAME(SqDpEmoji, _SC("SqDiscordEmoji"))
SQMOD_DECL_TYPENAME(SqDpCommandConfirmation, _SC("SqDiscordCommandConfirmation"))
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Register_Discord_Misc(HSQUIRRELVM vm, Table & ns) void Register_Discord_Misc(HSQUIRRELVM vm, Table & ns)
@ -113,17 +112,6 @@ void Register_Discord_Misc(HSQUIRRELVM vm, Table & ns)
.Func(_SC("BuildJSON"), &DpEmoji::BuildJSON_) .Func(_SC("BuildJSON"), &DpEmoji::BuildJSON_)
.Func(_SC("LoadImage"), &DpEmoji::LoadImage) .Func(_SC("LoadImage"), &DpEmoji::LoadImage)
); );
// --------------------------------------------------------------------------------------------
ns.Bind(_SC("CommandConfirmation"),
Class< DpCommandConfirmation, NoConstructor< DpCommandConfirmation > >(vm, SqDpCommandConfirmation::Str)
// Meta-methods
.SquirrelFunc(_SC("_typename"), &SqDpCommandConfirmation::Fn)
// Member Properties
.Prop(_SC("Valid"), &DpCommandConfirmation::IsValid)
.Prop(_SC("HttpBody"), &DpCommandConfirmation::GetHttpBody)
// Member Methods
//.Func(_SC("SetName"), &DpCommandConfirmation::ApplyName)
);
} }
} // Namespace:: SqMod } // Namespace:: SqMod

View File

@ -390,104 +390,4 @@ struct DpEmoji
SQMOD_NODISCARD std::string GetMention() const { return Valid().get_mention(); } SQMOD_NODISCARD std::string GetMention() const { return Valid().get_mention(); }
}; };
/* ------------------------------------------------------------------------------------------------
* The results of a REST call wrapped in a convenient struct.
*/
struct DpCommandConfirmation
{
using Ptr = std::unique_ptr< dpp::confirmation_callback_t >;
/* --------------------------------------------------------------------------------------------
* Referenced confirmation callback instance.
*/
Ptr mPtr{nullptr};
/* --------------------------------------------------------------------------------------------
* Whether the referenced pointer is owned.
*/
bool mOwned{false};
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
DpCommandConfirmation() noexcept = default;
/* --------------------------------------------------------------------------------------------
* Explicit constructor.
*/
explicit DpCommandConfirmation(Ptr::pointer ptr, bool owned = false) noexcept
: mPtr(ptr), mOwned(owned)
{ }
/* --------------------------------------------------------------------------------------------
* Explicit constructor.
*/
explicit DpCommandConfirmation(Ptr && ptr) noexcept
: mPtr(std::forward< Ptr >(ptr)), mOwned(true)
{ }
/* --------------------------------------------------------------------------------------------
* Explicit constructor.
*/
explicit DpCommandConfirmation(const Ptr::element_type & o) noexcept
: DpCommandConfirmation(new Ptr::element_type(o), true)
{ }
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
explicit DpCommandConfirmation(Ptr::element_type && o) noexcept
: DpCommandConfirmation(new Ptr::element_type(std::forward< Ptr::element_type >(o)), true)
{ }
/* --------------------------------------------------------------------------------------------
* Copy constructor (disabled).
*/
DpCommandConfirmation(const DpCommandConfirmation & o) = delete;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
DpCommandConfirmation(DpCommandConfirmation && o) noexcept = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~DpCommandConfirmation() noexcept { Cleanup(); }
/* --------------------------------------------------------------------------------------------
* Copy assignment operator (disabled).
*/
DpCommandConfirmation & operator = (const DpCommandConfirmation & o) = delete;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
DpCommandConfirmation & operator = (DpCommandConfirmation && o) noexcept
{
if (this != &o) {
Cleanup();
// Transfer members values
mPtr = std::move(o.mPtr);
mOwned = o.mOwned;
}
return *this;
}
/* --------------------------------------------------------------------------------------------
* Release any referenced resources and default to an empty/invalid state.
*/
void Cleanup()
{
// Do we own this to try delete it?
if (!mOwned && mPtr) {
// Not our job, simply forget about it
[[maybe_unused]] auto p = mPtr.release();
} else mPtr.reset(); // We own this so delete the instance
}
/* --------------------------------------------------------------------------------------------
* Validate the managed handle.
*/
void Validate() const { if (!mPtr) STHROWF("Invalid discord confirmation callback handle"); }
/* --------------------------------------------------------------------------------------------
* Validate the managed handle and retrieve a const reference to it.
*/
SQMOD_NODISCARD Ptr::element_type & Valid() const { Validate(); return *mPtr; }
/* --------------------------------------------------------------------------------------------
* Check whether a valid instance is managed.
*/
SQMOD_NODISCARD bool IsValid() const { return static_cast< bool >(mPtr); }
/* --------------------------------------------------------------------------------------------
* Retrieve the raw body string of the HTTP result.
*/
SQMOD_NODISCARD std::string & GetHttpBody() const { return Valid().http_info.body; }
};
} // Namespace:: SqMod } // Namespace:: SqMod

View File

@ -12,49 +12,32 @@
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
namespace Sqrat { namespace Sqrat {
// // Allow the VM to treat the dpp::snowflake type as a integer. // Allow the VM to treat the dpp::snowflake type as a integer.
// template<> struct Var<dpp::snowflake> template<> struct Var<dpp::snowflake>
// { {
// dpp::snowflake value; dpp::snowflake value;
// Var(HSQUIRRELVM vm, SQInteger idx) { Var(HSQUIRRELVM vm, SQInteger idx)
// sq_getinteger(vm, idx, reinterpret_cast<SQInteger*>(&static_cast<uint64_t&>(value))); {
// } sq_getinteger(vm, idx, reinterpret_cast<SQInteger*>(&static_cast<uint64_t&>(value)));
// inline static void push(HSQUIRRELVM vm, const dpp::snowflake& value) noexcept {
// sq_pushinteger(vm, static_cast<SQInteger>(static_cast<uint64_t>(value)));
// }
// };
// // Allow the VM to treat the dpp::snowflake type as a integer.
// template<> struct Var<const dpp::snowflake&>
// {
// dpp::snowflake value;
// Var(HSQUIRRELVM vm, SQInteger idx) {
// sq_getinteger(vm, idx, reinterpret_cast<SQInteger*>(&static_cast<uint64_t&>(value)));
// }
// inline static void push(HSQUIRRELVM vm, const dpp::snowflake& value) noexcept {
// sq_pushinteger(vm, static_cast<SQInteger>(static_cast<uint64_t>(value)));
// }
// };
// ------------------------------------------------------------------------------------------------
// Used to get and push dpp::snowflake instances to and from the stack
template <> struct Var<dpp::snowflake> {
dpp::snowflake value; ///< The actual value of get operations
Var(HSQUIRRELVM vm, SQInteger idx) : value(popAsInt< uint64_t, true >(vm, idx).value) { }
// Push dpp::snowflake instances to the stack as integers
static void push(HSQUIRRELVM vm, const dpp::snowflake& value) noexcept {
sq_pushinteger(vm, static_cast< SQInteger >(static_cast< std::uint64_t >(value)));
} }
static void push(HSQUIRRELVM vm, dpp::snowflake& value) noexcept { inline static void push(HSQUIRRELVM vm, const dpp::snowflake& value) noexcept
sq_pushinteger(vm, static_cast< SQInteger >(static_cast< std::uint64_t & >(value))); {
sq_pushinteger(vm, static_cast<SQInteger>(static_cast<uint64_t>(value)));
} }
}; };
template <> struct Var<dpp::snowflake&> : public Var<dpp::snowflake> {
Var(HSQUIRRELVM vm, SQInteger idx) : Var<dpp::snowflake>(vm, idx) { } // Allow the VM to treat the dpp::snowflake type as a integer.
}; template<> struct Var<const dpp::snowflake&>
template <> struct Var<const dpp::snowflake&> : public Var<dpp::snowflake> { {
Var(HSQUIRRELVM vm, SQInteger idx) : Var<dpp::snowflake>(vm, idx) { } dpp::snowflake value;
Var(HSQUIRRELVM vm, SQInteger idx)
{
sq_getinteger(vm, idx, reinterpret_cast<SQInteger*>(&static_cast<uint64_t&>(value)));
}
inline static void push(HSQUIRRELVM vm, const dpp::snowflake& value) noexcept
{
sq_pushinteger(vm, static_cast<SQInteger>(static_cast<uint64_t>(value)));
}
}; };
} }

View File

@ -45,7 +45,7 @@ struct popAsInt
T value; T value;
popAsInt(HSQUIRRELVM vm, SQInteger idx) popAsInt(HSQUIRRELVM vm, SQInteger idx)
{ {
const SQObjectType value_type = sq_gettype(vm, idx); SQObjectType value_type = sq_gettype(vm, idx);
switch(value_type) { switch(value_type) {
case OT_BOOL: case OT_BOOL:
SQBool sqValueb; SQBool sqValueb;
@ -60,7 +60,7 @@ struct popAsInt
case OT_FLOAT: case OT_FLOAT:
SQFloat sqValuef; SQFloat sqValuef;
sq_getfloat(vm, idx, &sqValuef); sq_getfloat(vm, idx, &sqValuef);
value = static_cast<T>(static_cast<SQInteger>(sqValuef)); value = static_cast<T>(static_cast<int>(sqValuef));
break; break;
default: default:
SQTHROW(vm, FormatTypeError(vm, idx, _SC("integer"))); SQTHROW(vm, FormatTypeError(vm, idx, _SC("integer")));
@ -86,7 +86,7 @@ struct popAsFloat
T value; T value;
popAsFloat(HSQUIRRELVM vm, SQInteger idx) popAsFloat(HSQUIRRELVM vm, SQInteger idx)
{ {
const SQObjectType value_type = sq_gettype(vm, idx); SQObjectType value_type = sq_gettype(vm, idx);
switch(value_type) { switch(value_type) {
case OT_BOOL: case OT_BOOL:
SQBool sqValueb; SQBool sqValueb;