mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2024-11-08 00:37:15 +01:00
Compare commits
5 Commits
9298065cef
...
4eaeeedf15
Author | SHA1 | Date | |
---|---|---|---|
|
4eaeeedf15 | ||
|
a8d1d85ed2 | ||
|
2f277c4486 | ||
|
0a44e9cfa4 | ||
|
237683e6ce |
@ -246,7 +246,12 @@ else()
|
||||
endif()
|
||||
# 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")
|
||||
|
||||
# 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)
|
||||
if(WIN32 AND MINGW AND COPY_DEPENDENCIES)
|
||||
get_filename_component(MINGW_BIN_PATH ${CMAKE_C_COMPILER} DIRECTORY REALPATH)
|
||||
|
@ -15,6 +15,7 @@ DpCluster::DpCluster(DpClusterOptions & o)
|
||||
: 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))
|
||||
, mSqEvents(), mEvents(), mEventsHandle()
|
||||
, mCCList(), mCCResults(std::make_shared< CCResultQueue >(4096))
|
||||
{
|
||||
// Make sure all event handles are not valid
|
||||
mEventsHandle.fill(0);
|
||||
@ -83,6 +84,31 @@ void DpCluster::Process(bool force)
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
@ -122,6 +148,7 @@ void Register_Discord_Cluster(HSQUIRRELVM vm, Table & ns)
|
||||
.Func(_SC("Stop"), &DpCluster::Stop)
|
||||
.Func(_SC("EnableEvent"), &DpCluster::EnableEvent)
|
||||
.Func(_SC("DisableEvent"), &DpCluster::DisableEvent)
|
||||
.CbFunc(_SC("CurrentUserGetGuilds"), &DpCluster::CurrentUserGetGuilds)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -333,6 +333,64 @@ private:
|
||||
void OnStageInstanceCreate(const dpp::stage_instance_create_t & ev);
|
||||
void OnStageInstanceUpdate(const dpp::stage_instance_update_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
|
||||
|
@ -10,6 +10,7 @@ SQMOD_DECL_TYPENAME(SqDpUptime, _SC("SqDiscordUptime"))
|
||||
SQMOD_DECL_TYPENAME(SqDpIconHash, _SC("SqDiscordIconHash"))
|
||||
SQMOD_DECL_TYPENAME(SqDpVoiceState, _SC("SqDiscordVoiceState"))
|
||||
SQMOD_DECL_TYPENAME(SqDpEmoji, _SC("SqDiscordEmoji"))
|
||||
SQMOD_DECL_TYPENAME(SqDpCommandConfirmation, _SC("SqDiscordCommandConfirmation"))
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Register_Discord_Misc(HSQUIRRELVM vm, Table & ns)
|
||||
@ -112,6 +113,17 @@ void Register_Discord_Misc(HSQUIRRELVM vm, Table & ns)
|
||||
.Func(_SC("BuildJSON"), &DpEmoji::BuildJSON_)
|
||||
.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
|
||||
|
@ -390,4 +390,104 @@ struct DpEmoji
|
||||
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
|
||||
|
@ -12,32 +12,49 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace Sqrat {
|
||||
|
||||
// Allow the VM to treat the dpp::snowflake type as a integer.
|
||||
template<> struct Var<dpp::snowflake>
|
||||
{
|
||||
dpp::snowflake value;
|
||||
Var(HSQUIRRELVM vm, SQInteger idx)
|
||||
{
|
||||
sq_getinteger(vm, idx, reinterpret_cast<SQInteger*>(&static_cast<uint64_t&>(value)));
|
||||
// // Allow the VM to treat the dpp::snowflake type as a integer.
|
||||
// template<> struct Var<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)));
|
||||
// }
|
||||
// };
|
||||
|
||||
// // 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)));
|
||||
}
|
||||
inline static void push(HSQUIRRELVM vm, const dpp::snowflake& value) noexcept
|
||||
{
|
||||
sq_pushinteger(vm, static_cast<SQInteger>(static_cast<uint64_t>(value)));
|
||||
static void push(HSQUIRRELVM vm, dpp::snowflake& value) noexcept {
|
||||
sq_pushinteger(vm, static_cast< SQInteger >(static_cast< std::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)));
|
||||
}
|
||||
template <> struct Var<dpp::snowflake&> : public Var<dpp::snowflake> {
|
||||
Var(HSQUIRRELVM vm, SQInteger idx) : Var<dpp::snowflake>(vm, idx) { }
|
||||
};
|
||||
template <> struct Var<const dpp::snowflake&> : public Var<dpp::snowflake> {
|
||||
Var(HSQUIRRELVM vm, SQInteger idx) : Var<dpp::snowflake>(vm, idx) { }
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ struct popAsInt
|
||||
T value;
|
||||
popAsInt(HSQUIRRELVM vm, SQInteger idx)
|
||||
{
|
||||
SQObjectType value_type = sq_gettype(vm, idx);
|
||||
const SQObjectType value_type = sq_gettype(vm, idx);
|
||||
switch(value_type) {
|
||||
case OT_BOOL:
|
||||
SQBool sqValueb;
|
||||
@ -60,7 +60,7 @@ struct popAsInt
|
||||
case OT_FLOAT:
|
||||
SQFloat sqValuef;
|
||||
sq_getfloat(vm, idx, &sqValuef);
|
||||
value = static_cast<T>(static_cast<int>(sqValuef));
|
||||
value = static_cast<T>(static_cast<SQInteger>(sqValuef));
|
||||
break;
|
||||
default:
|
||||
SQTHROW(vm, FormatTypeError(vm, idx, _SC("integer")));
|
||||
@ -86,7 +86,7 @@ struct popAsFloat
|
||||
T value;
|
||||
popAsFloat(HSQUIRRELVM vm, SQInteger idx)
|
||||
{
|
||||
SQObjectType value_type = sq_gettype(vm, idx);
|
||||
const SQObjectType value_type = sq_gettype(vm, idx);
|
||||
switch(value_type) {
|
||||
case OT_BOOL:
|
||||
SQBool sqValueb;
|
||||
|
Loading…
Reference in New Issue
Block a user