From 5ac1379b67df2b2cba7050d78098cf3978fb719f Mon Sep 17 00:00:00 2001 From: BotoX Date: Thu, 5 Sep 2019 08:43:33 +0200 Subject: [PATCH] Add MenuSetClientMapping native. --- core/MenuStyle_Base.cpp | 32 +++++++++++++++++++++----------- core/MenuStyle_Base.h | 2 +- core/logic/smn_menus.cpp | 29 +++++++++++++++++++++++++++++ plugins/include/menus.inc | 18 ++++++++++++++++++ public/IMenuManager.h | 8 ++++++++ 5 files changed, 77 insertions(+), 12 deletions(-) diff --git a/core/MenuStyle_Base.cpp b/core/MenuStyle_Base.cpp index 8a7657e6..acb701f7 100644 --- a/core/MenuStyle_Base.cpp +++ b/core/MenuStyle_Base.cpp @@ -607,7 +607,7 @@ bool BaseMenuStyle::RedoClientMenu(int client, ItemOrder order) CBaseMenu::CBaseMenu(IMenuHandler *pHandler, IMenuStyle *pStyle, IdentityToken_t *pOwner) : m_pStyle(pStyle), m_Pagination(7), m_bShouldDelete(false), m_bCancelling(false), m_pOwner(pOwner ? pOwner : g_pCoreIdent), m_bDeleting(false), m_bWillFreeHandle(false), -m_hHandle(BAD_HANDLE), m_pHandler(pHandler), m_nFlags(MENUFLAG_BUTTON_EXIT), m_RandomMapLen(0) +m_hHandle(BAD_HANDLE), m_pHandler(pHandler), m_nFlags(MENUFLAG_BUTTON_EXIT) { } @@ -684,7 +684,7 @@ const char *CBaseMenu::GetItemInfo(unsigned int position, ItemDrawInfo *draw/* = if (position >= m_items.length()) return NULL; - if (client > 0 && position < m_RandomMapLen) + if (client > 0 && position < m_RandomMaps[client].length()) { position = m_RandomMaps[client][position]; } @@ -701,19 +701,19 @@ const char *CBaseMenu::GetItemInfo(unsigned int position, ItemDrawInfo *draw/* = void CBaseMenu::ShufflePerClient(int start, int stop) { // limit map len to 255 items since it's using uint8 - m_RandomMapLen = MIN(GetItemCount(), 255); - if(stop >= 0) - m_RandomMapLen = MIN(m_RandomMapLen, (unsigned int)stop); + int length = MIN(GetItemCount(), 255); + if (stop >= 0) + length = MIN(length, (unsigned int)stop); - for(int i = 1; i < SM_MAXPLAYERS + 1; i++) + for (int i = 1; i < SM_MAXPLAYERS + 1; i++) { // populate per-client map ... - m_RandomMaps[i].resize(m_RandomMapLen); - for(unsigned int j = 0; j < m_RandomMapLen; j++) + m_RandomMaps[i].resize(length); + for (unsigned int j = 0; j < length; j++) m_RandomMaps[i][j] = j; // ... and random shuffle it - for(int j = m_RandomMapLen - 1; j > start; j--) + for (int j = length - 1; j > start; j--) { int x = rand() % (j - start + 1) + start; uint8_t tmp = m_RandomMaps[i][x]; @@ -723,14 +723,24 @@ void CBaseMenu::ShufflePerClient(int start, int stop) } } +void CBaseMenu::SetClientMapping(int client, int *array, int length) +{ + length = MIN(length, 255); + m_RandomMaps[client].resize(length); + for (int i = 0; i < length; i++) + { + m_RandomMaps[client][i] = array[i]; + } +} + bool CBaseMenu::IsPerClientShuffled() { - return m_RandomMapLen > 0; + return m_RandomMaps[1].length() > 0; } unsigned int CBaseMenu::GetRealItemIndex(int client, unsigned int position) { - if (client > 0 && position < m_RandomMapLen) + if (client > 0 && position < m_RandomMaps[client].length()) { position = m_RandomMaps[client][position]; return m_items[position].index; diff --git a/core/MenuStyle_Base.h b/core/MenuStyle_Base.h index 4319ddfc..acc58622 100644 --- a/core/MenuStyle_Base.h +++ b/core/MenuStyle_Base.h @@ -157,6 +157,7 @@ public: virtual void SetMenuOptionFlags(unsigned int flags); virtual IMenuHandler *GetHandler(); virtual void ShufflePerClient(int start, int stop); + virtual void SetClientMapping(int client, int *array, int length); virtual bool IsPerClientShuffled(); virtual unsigned int GetRealItemIndex(int client, unsigned int position); unsigned int GetBaseMemUsage(); @@ -175,7 +176,6 @@ protected: Handle_t m_hHandle; IMenuHandler *m_pHandler; unsigned int m_nFlags; - unsigned int m_RandomMapLen; ke::Vector m_RandomMaps[SM_MAXPLAYERS+1]; }; diff --git a/core/logic/smn_menus.cpp b/core/logic/smn_menus.cpp index 505f9a02..e2570718 100644 --- a/core/logic/smn_menus.cpp +++ b/core/logic/smn_menus.cpp @@ -862,6 +862,33 @@ static cell_t MenuShufflePerClient(IPluginContext *pContext, const cell_t *param return 1; } +static cell_t MenuSetClientMapping(IPluginContext *pContext, const cell_t *params) +{ + Handle_t hndl = (Handle_t)params[1]; + HandleError err; + IBaseMenu *menu; + + if ((err = ReadMenuHandle(params[1], &menu)) != HandleError_None) + { + return pContext->ThrowNativeError("Menu handle %x is invalid (error %d)", hndl, err); + } + + int client = params[2]; + if (client < 1 || client > SM_MAXPLAYERS) + { + return pContext->ThrowNativeError("Invalid client index!"); + } + + cell_t *array; + pContext->LocalToPhysAddr(params[3], &array); + + int length = params[4]; + + menu->SetClientMapping(client, array, length); + + return 1; +} + static cell_t SetMenuPagination(IPluginContext *pContext, const cell_t *params) { Handle_t hndl = (Handle_t)params[1]; @@ -1676,6 +1703,7 @@ REGISTER_NATIVES(menuNatives) {"SetVoteResultCallback", SetVoteResultCallback}, {"VoteMenu", VoteMenu}, {"MenuShufflePerClient", MenuShufflePerClient}, + {"MenuSetClientMapping", MenuSetClientMapping}, {"SetMenuNoVoteButton", SetMenuNoVoteButton}, // Transitional syntax support. @@ -1705,6 +1733,7 @@ REGISTER_NATIVES(menuNatives) {"Menu.Cancel", CancelMenu}, {"Menu.DisplayVote", VoteMenu}, {"Menu.ShufflePerClient", MenuShufflePerClient}, + {"Menu.SetClientMapping", MenuSetClientMapping}, {"Menu.Pagination.get", GetMenuPagination}, {"Menu.Pagination.set", SetMenuPagination}, {"Menu.OptionFlags.get", GetMenuOptionFlags}, diff --git a/plugins/include/menus.inc b/plugins/include/menus.inc index 2637cdc9..5923ece2 100644 --- a/plugins/include/menus.inc +++ b/plugins/include/menus.inc @@ -318,6 +318,13 @@ methodmap Menu < Handle // @param stop Menu item index to stop randomizing at. -1 = infinite public native void ShufflePerClient(int start=0, int stop=-1); + // Fills the client vote option mapping with user supplied values. + // + // @param client Client index. + // @param array Integer array with mapping. + // @param length Length of array. + public native void SetClientMapping(int client, int[] array, int length); + // Sets the menu's default title/instruction message. // // @param fmt Message string format @@ -560,11 +567,22 @@ native bool GetMenuItem(Handle menu, /** * Generates a per-client random mapping for the current vote options. * + * @param menu Menu Handle. * @param start Menu item index to start randomizing from. * @param stop Menu item index to stop randomizing at. -1 = infinite */ native void MenuShufflePerClient(Handle menu, int start=0, int stop=-1); +/* + * Fills the client vote option mapping with user supplied values. + * + * @param menu Menu Handle. + * @param client Client index. + * @param array Integer array with mapping. + * @param length Length of array. + */ +native void MenuSetClientMapping(Handle menu, int client, int[] array, int length); + /** * Returns the first item on the page of a currently selected menu. * diff --git a/public/IMenuManager.h b/public/IMenuManager.h index 6d74ea3b..9b0978b6 100644 --- a/public/IMenuManager.h +++ b/public/IMenuManager.h @@ -645,6 +645,14 @@ namespace SourceMod */ virtual void ShufflePerClient(int start=0, int stop=-1) =0; + /** + * @brief Fills the client vote option mapping with user supplied values. + * @param client Client index. + * @param array Integer array with mapping. + * @param length Length of array. + */ + virtual void SetClientMapping(int client, int *array, int length) =0; + /** * @brief Returns true if the menu has a per-client random mapping. * @return True on success, false otherwise.