diff --git a/extensions/topmenus/Makefile b/extensions/topmenus/Makefile index 15a4e10a..e3a7cf27 100644 --- a/extensions/topmenus/Makefile +++ b/extensions/topmenus/Makefile @@ -1,88 +1,88 @@ -#(C)2004-2006 SourceMM Development Team -# Makefile written by David "BAILOPAN" Anderson - -SMSDK = ../.. -SRCDS = ~/srcds -SOURCEMM = ../../../../sourcemm - -##################################### -### EDIT BELOW FOR OTHER PROJECTS ### -##################################### - -PROJECT = topmenus - -#Uncomment for SourceMM-enabled extensions -#LINK_HL2 = $(HL2LIB)/tier1_i486.a vstdlib_i486.so tier0_i486.so - -OBJECTS = sdk/smsdk_ext.cpp extension.cpp TopMenuManager.cpp TopMenu.cpp \ - sdk/sm_memtable.cpp - -############################################## -### CONFIGURE ANY OTHER FLAGS/OPTIONS HERE ### -############################################## - -C_OPT_FLAGS = -O3 -funroll-loops -s -pipe -fno-strict-aliasing -C_DEBUG_FLAGS = -g -ggdb3 -CPP_GCC4_FLAGS = -fvisibility=hidden -fvisibility-inlines-hidden -CPP = gcc-4.1 - -HL2PUB = $(HL2SDK)/public -HL2LIB = $(HL2SDK)/linux_sdk -HL2SDK = $(SOURCEMM)/hl2sdk -SMM_TRUNK = $(SOURCEMM)/trunk - -LINK = $(LINK_HL2) -static-libgcc - -INCLUDE = -I. -I.. -Isdk -I$(HL2PUB) -I$(HL2PUB)/dlls -I$(HL2PUB)/engine -I$(HL2PUB)/tier0 -I$(HL2PUB)/tier1 \ - -I$(HL2PUB)/vstdlib -I$(HL2SDK)/tier1 -I$(SMM_TRUNK) -I$(SMM_TRUNK)/sourcehook -I$(SMM_TRUNK)/sourcemm \ - -I$(SMSDK)/public -I$(SMSDK)/public/sourcepawn -I$(SMSDK)/public/extensions \ - -CFLAGS = -D_LINUX -DNDEBUG -Dstricmp=strcasecmp -D_stricmp=strcasecmp -D_strnicmp=strncasecmp -Dstrnicmp=strncasecmp -D_snprintf=snprintf -D_vsnprintf=vsnprintf -D_alloca=alloca -Dstrcmpi=strcasecmp -Wall -Werror -fPIC -msse -DSOURCEMOD_BUILD -DHAVE_STDINT_H -Wno-uninitialized -CPPFLAGS = -Wno-non-virtual-dtor -fno-exceptions -fno-rtti - -################################################ -### DO NOT EDIT BELOW HERE FOR MOST PROJECTS ### -################################################ - -ifeq "$(DEBUG)" "true" - BIN_DIR = Debug - CFLAGS += $(C_DEBUG_FLAGS) -else - BIN_DIR = Release - CFLAGS += $(C_OPT_FLAGS) -endif - - -GCC_VERSION := $(shell $(CPP) -dumpversion >&1 | cut -b1) -ifeq "$(GCC_VERSION)" "4" - CPPFLAGS += $(CPP_GCC4_FLAGS) -endif - -BINARY = $(PROJECT).ext.so - -OBJ_LINUX := $(OBJECTS:%.cpp=$(BIN_DIR)/%.o) - -$(BIN_DIR)/%.o: %.cpp - $(CPP) $(INCLUDE) $(CFLAGS) $(CPPFLAGS) -o $@ -c $< - -all: - mkdir -p $(BIN_DIR)/sdk - ln -sf $(SRCDS)/bin/vstdlib_i486.so vstdlib_i486.so - ln -sf $(SRCDS)/bin/tier0_i486.so tier0_i486.so - $(MAKE) extension - -extension: $(OBJ_LINUX) - $(CPP) $(INCLUDE) $(CFLAGS) $(CPPFLAGS) $(OBJ_LINUX) $(LINK) -shared -ldl -lm -o$(BIN_DIR)/$(BINARY) - -debug: - $(MAKE) all DEBUG=true - -default: all - -clean: - rm -rf Release/*.o - rm -rf Release/sdk/*.o - rm -rf Release/$(BINARY) - rm -rf Debug/*.o - rm -rf Debug/sdk/*.o - rm -rf Debug/$(BINARY) +#(C)2004-2006 SourceMM Development Team +# Makefile written by David "BAILOPAN" Anderson + +SMSDK = ../.. +SRCDS = ~/srcds +SOURCEMM = ../../../../sourcemm + +##################################### +### EDIT BELOW FOR OTHER PROJECTS ### +##################################### + +PROJECT = topmenus + +#Uncomment for SourceMM-enabled extensions +#LINK_HL2 = $(HL2LIB)/tier1_i486.a vstdlib_i486.so tier0_i486.so + +OBJECTS = sdk/smsdk_ext.cpp extension.cpp TopMenuManager.cpp TopMenu.cpp \ + sdk/sm_memtable.cpp smn_topmenus.cpp + +############################################## +### CONFIGURE ANY OTHER FLAGS/OPTIONS HERE ### +############################################## + +C_OPT_FLAGS = -O3 -funroll-loops -s -pipe -fno-strict-aliasing +C_DEBUG_FLAGS = -g -ggdb3 +CPP_GCC4_FLAGS = -fvisibility=hidden -fvisibility-inlines-hidden +CPP = gcc-4.1 + +HL2PUB = $(HL2SDK)/public +HL2LIB = $(HL2SDK)/linux_sdk +HL2SDK = $(SOURCEMM)/hl2sdk +SMM_TRUNK = $(SOURCEMM)/trunk + +LINK = $(LINK_HL2) -static-libgcc + +INCLUDE = -I. -I.. -Isdk -I$(HL2PUB) -I$(HL2PUB)/dlls -I$(HL2PUB)/engine -I$(HL2PUB)/tier0 -I$(HL2PUB)/tier1 \ + -I$(HL2PUB)/vstdlib -I$(HL2SDK)/tier1 -I$(SMM_TRUNK) -I$(SMM_TRUNK)/sourcehook -I$(SMM_TRUNK)/sourcemm \ + -I$(SMSDK)/public -I$(SMSDK)/public/sourcepawn -I$(SMSDK)/public/extensions \ + +CFLAGS = -D_LINUX -DNDEBUG -Dstricmp=strcasecmp -D_stricmp=strcasecmp -D_strnicmp=strncasecmp -Dstrnicmp=strncasecmp -D_snprintf=snprintf -D_vsnprintf=vsnprintf -D_alloca=alloca -Dstrcmpi=strcasecmp -Wall -Werror -fPIC -msse -DSOURCEMOD_BUILD -DHAVE_STDINT_H -Wno-uninitialized +CPPFLAGS = -Wno-non-virtual-dtor -fno-exceptions -fno-rtti + +################################################ +### DO NOT EDIT BELOW HERE FOR MOST PROJECTS ### +################################################ + +ifeq "$(DEBUG)" "true" + BIN_DIR = Debug + CFLAGS += $(C_DEBUG_FLAGS) +else + BIN_DIR = Release + CFLAGS += $(C_OPT_FLAGS) +endif + + +GCC_VERSION := $(shell $(CPP) -dumpversion >&1 | cut -b1) +ifeq "$(GCC_VERSION)" "4" + CPPFLAGS += $(CPP_GCC4_FLAGS) +endif + +BINARY = $(PROJECT).ext.so + +OBJ_LINUX := $(OBJECTS:%.cpp=$(BIN_DIR)/%.o) + +$(BIN_DIR)/%.o: %.cpp + $(CPP) $(INCLUDE) $(CFLAGS) $(CPPFLAGS) -o $@ -c $< + +all: + mkdir -p $(BIN_DIR)/sdk + ln -sf $(SRCDS)/bin/vstdlib_i486.so vstdlib_i486.so + ln -sf $(SRCDS)/bin/tier0_i486.so tier0_i486.so + $(MAKE) extension + +extension: $(OBJ_LINUX) + $(CPP) $(INCLUDE) $(CFLAGS) $(CPPFLAGS) $(OBJ_LINUX) $(LINK) -shared -ldl -lm -o$(BIN_DIR)/$(BINARY) + +debug: + $(MAKE) all DEBUG=true + +default: all + +clean: + rm -rf Release/*.o + rm -rf Release/sdk/*.o + rm -rf Release/$(BINARY) + rm -rf Debug/*.o + rm -rf Debug/sdk/*.o + rm -rf Debug/$(BINARY) diff --git a/extensions/topmenus/TopMenu.cpp b/extensions/topmenus/TopMenu.cpp index 08d23754..9b2803e3 100644 --- a/extensions/topmenus/TopMenu.cpp +++ b/extensions/topmenus/TopMenu.cpp @@ -59,25 +59,29 @@ TopMenu::TopMenu(ITopMenuObjectCallbacks *callbacks) TopMenu::~TopMenu() { /* Delete all categories */ - for (size_t i = 0; i < m_Categories.size(); i++) + size_t i = 1; + while (i >= m_Categories.size()) { - delete m_Categories[i]; + RemoveFromMenu(m_Categories[i - 1]->obj->object_id); } - /* Delete all items */ - for (size_t i = 0; i < m_Objects.size(); i++) + /* Remove all objects */ + for (i = 0; i < m_Objects.size(); i++) { + assert(m_Objects[i]->is_free == true); delete m_Objects[i]; } + m_pTitle->OnTopMenuObjectRemoved(this, 0); + /* Delete all cached config entries */ - for (size_t i = 0; i < m_Config.cats.size(); i++) + for (i = 0; i < m_Config.cats.size(); i++) { delete m_Config.cats[i]; } /* Sweep players */ - for (int i = 0; i <= m_max_clients; i++) + for (i = 0; i <= (size_t)m_max_clients; i++) { TearDownClient(&m_clients[i]); } @@ -135,6 +139,10 @@ unsigned int TopMenu::AddToMenu(const char *name, { return 0; } + else if (type != TopMenuObject_Item && type != TopMenuObject_Category) + { + return 0; + } /* If we're adding an item, make sure the parent is valid, * and that the parent is a category. @@ -239,18 +247,30 @@ void TopMenu::RemoveFromMenu(unsigned int object_id) if (obj->type == TopMenuObject_Category) { - /* Find it in the category list */ + /* Find it in the category list. */ for (size_t i = 0; i < m_Categories.size(); i++) { if (m_Categories[i]->obj == obj) { - /* Erase from here */ - delete m_Categories[i]; + /* Mark all children as removed + free. Note we could + * call into RemoveMenuItem() for this, but it'd be very + * inefficient! + */ + topmenu_category_t *cat = m_Categories[i]; + for (size_t j = 0; j < m_Categories[i]->obj_list.size(); j++) + { + cat->obj_list[j]->callbacks->OnTopMenuObjectRemoved(this, cat->obj_list[j]->object_id); + cat->obj_list[j]->is_free = true; + } + + /* Remove the category from the list, then delete it. */ m_Categories.erase(m_Categories.iterAt(i)); + delete cat; break; } } - /* Update us as changed */ + + /* Update the root as changed. */ m_SerialNo++; m_bCatsNeedResort = true; } @@ -278,6 +298,8 @@ void TopMenu::RemoveFromMenu(unsigned int object_id) break; } } + + /* Update the category as changed. */ parent_cat->reorder = true; parent_cat->serial++; } @@ -285,6 +307,9 @@ void TopMenu::RemoveFromMenu(unsigned int object_id) /* Finally, mark the object as free. */ obj->is_free = true; + + /* The callbacks pointer is still valid, so fire away! */ + obj->callbacks->OnTopMenuObjectRemoved(this, object_id); } bool TopMenu::DisplayMenu(int client, unsigned int hold_time, TopMenuPosition position) @@ -401,7 +426,7 @@ void TopMenu::OnMenuSelect2(IBaseMenu *menu, int client, unsigned int item, unsi } /* Pass the information on to the callback */ - obj->callbacks->OnTopMenuSelectOption(client, obj->object_id); + obj->callbacks->OnTopMenuSelectOption(this, client, obj->object_id); } } @@ -456,7 +481,7 @@ unsigned int TopMenu::OnMenuDisplayItem(IBaseMenu *menu, /* Ask the object to render the text for this client */ char renderbuf[64]; - obj->callbacks->OnTopMenuDrawOption(client, obj->object_id, renderbuf, sizeof(renderbuf)); + obj->callbacks->OnTopMenuDrawOption(this, client, obj->object_id, renderbuf, sizeof(renderbuf)); /* Build the new draw info */ ItemDrawInfo new_dr = dr; @@ -540,7 +565,8 @@ void TopMenu::UpdateClientRoot(int client, IGamePlayer *pGamePlayer) { obj_by_name_t *temp_obj = &item_list[i]; topmenu_object_t *obj = m_Categories[m_UnsortedCats[i]]->obj; - obj->callbacks->OnTopMenuDrawOption(client, + obj->callbacks->OnTopMenuDrawOption(this, + client, obj->object_id, temp_obj->name, sizeof(temp_obj->name)); @@ -561,7 +587,7 @@ void TopMenu::UpdateClientRoot(int client, IGamePlayer *pGamePlayer) /* Set the menu's title */ char renderbuf[128]; - m_pTitle->OnTopMenuDrawTitle(client, 0, renderbuf, sizeof(renderbuf)); + m_pTitle->OnTopMenuDrawTitle(this, client, 0, renderbuf, sizeof(renderbuf)); root_menu->SetDefaultTitle(renderbuf); /* The client is now fully updated */ @@ -627,7 +653,8 @@ void TopMenu::UpdateClientCategory(int client, unsigned int category) { obj_by_name_t *item = &item_list[i]; topmenu_object_t *obj = cat->unsorted[i]; - obj->callbacks->OnTopMenuDrawOption(client, + obj->callbacks->OnTopMenuDrawOption(this, + client, obj->object_id, item->name, sizeof(item->name)); @@ -648,7 +675,11 @@ void TopMenu::UpdateClientCategory(int client, unsigned int category) /* Set the menu's title */ char renderbuf[128]; - cat->obj->callbacks->OnTopMenuDrawTitle(client, cat->obj->object_id, renderbuf, sizeof(renderbuf)); + cat->obj->callbacks->OnTopMenuDrawTitle(this, + client, + cat->obj->object_id, + renderbuf, + sizeof(renderbuf)); cat_menu->SetDefaultTitle(renderbuf); /* We're done! */ @@ -820,6 +851,40 @@ bool TopMenu::LoadConfiguration(const char *file, char *error, size_t maxlength) return true; } +bool TopMenu::OnIdentityRemoval(IdentityToken_t *owner) +{ + /* First sweep the categories owned by us */ + CVector obj_list; + for (size_t i = 0; i < m_Categories.size(); i++) + { + if (m_Categories[i]->obj->owner == owner) + { + obj_list.push_back(m_Categories[i]->obj->object_id); + } + } + + for (size_t i = 0; i < obj_list.size(); i++) + { + RemoveFromMenu(obj_list[i]); + } + + /* Now we can look for actual items */ + for (size_t i = 0; i < m_Objects.size(); i++) + { + if (m_Objects[i]->is_free) + { + continue; + } + if (m_Objects[i]->owner == owner) + { + assert(m_Objects[i]->type != TopMenuObject_Category); + RemoveFromMenu(m_Objects[i]->object_id); + } + } + + return true; +} + #define PARSE_STATE_NONE 0 #define PARSE_STATE_CATEGORY 1 unsigned int ignore_parse_level = 0; @@ -900,6 +965,23 @@ SMCParseResult TopMenu::ReadSMC_LeavingSection() return SMCParse_Continue; } +unsigned int TopMenu::FindCategory(const char *name) +{ + topmenu_object_t **p_obj = m_ObjLookup.retrieve(name); + if (!p_obj) + { + return 0; + } + + topmenu_object_t *obj = *p_obj; + if (obj->type != TopMenuObject_Category) + { + return 0; + } + + return obj->object_id; +} + int _SortObjectNamesDescending(const void *ptr1, const void *ptr2) { obj_by_name_t *obj1 = (obj_by_name_t *)ptr1; diff --git a/extensions/topmenus/TopMenu.h b/extensions/topmenus/TopMenu.h index 2db5d3b2..9404398e 100644 --- a/extensions/topmenus/TopMenu.h +++ b/extensions/topmenus/TopMenu.h @@ -120,6 +120,7 @@ public: //ITopMenu unsigned int hold_time, TopMenuPosition position); virtual bool LoadConfiguration(const char *file, char *error, size_t maxlength); + virtual unsigned int FindCategory(const char *name); public: //IMenuHandler virtual void OnMenuSelect2(IBaseMenu *menu, int client, unsigned int item, unsigned int item_on_page); virtual void OnMenuDrawItem(IBaseMenu *menu, int client, unsigned int item, unsigned int &style); @@ -150,6 +151,7 @@ private: void OnClientConnected(int client); void OnClientDisconnected(int client); void OnServerActivated(int max_clients); + bool OnIdentityRemoval(IdentityToken_t *owner); private: config_root_t m_Config; /* Configuration from file */ topmenu_player_t *m_clients; /* Client array */ diff --git a/extensions/topmenus/TopMenuManager.cpp b/extensions/topmenus/TopMenuManager.cpp index 5a25c85f..4652f781 100644 --- a/extensions/topmenus/TopMenuManager.cpp +++ b/extensions/topmenus/TopMenuManager.cpp @@ -91,6 +91,24 @@ void TopMenuManager::OnServerActivated(int max_clients) is_server_activated = true; } +void TopMenuManager::OnPluginUnloaded(IPlugin *plugin) +{ + List::iterator iter = m_TopMenus.begin(); + + while (iter != m_TopMenus.end()) + { + if ((*iter)->OnIdentityRemoval(plugin->GetIdentity())) + { + iter++; + } + else + { + delete (*iter); + iter = m_TopMenus.erase(iter); + } + } +} + void TopMenuManager::DestroyTopMenu(ITopMenu *topmenu) { TopMenu *pMenu = (TopMenu *)topmenu; diff --git a/extensions/topmenus/TopMenuManager.h b/extensions/topmenus/TopMenuManager.h index 8d56f3fc..dc1efed6 100644 --- a/extensions/topmenus/TopMenuManager.h +++ b/extensions/topmenus/TopMenuManager.h @@ -35,6 +35,7 @@ #include #include +#include #include using namespace SourceMod; @@ -44,7 +45,8 @@ class TopMenu; class TopMenuManager : public ITopMenuManager, - public IClientListener + public IClientListener, + public IPluginsListener { public: const char *GetInterfaceName(); @@ -55,6 +57,7 @@ public: void OnClientConnected(int client); void OnClientDisconnected(int client); void OnServerActivated(int max_clients); + void OnPluginUnloaded(IPlugin *plugin); private: List m_TopMenus; }; diff --git a/extensions/topmenus/extension.cpp b/extensions/topmenus/extension.cpp index 9e2cb05f..97401b2b 100644 --- a/extensions/topmenus/extension.cpp +++ b/extensions/topmenus/extension.cpp @@ -31,6 +31,7 @@ #include "extension.h" #include "TopMenuManager.h" +#include "smn_topmenus.h" /** * @file extension.cpp @@ -44,12 +45,19 @@ SMEXT_LINK(&g_TopMenuExt); bool TopMenuExtension::SDK_OnLoad(char *error, size_t maxlength, bool late) { sharesys->AddInterface(myself, &g_TopMenus); + sharesys->AddNatives(myself, g_TopMenuNatives); + + plsys->AddPluginsListener(&g_TopMenus); playerhelpers->AddClientListener(&g_TopMenus); + Initialize_Natives(); + return true; } void TopMenuExtension::SDK_OnUnload() { + Shutdown_Natives(); playerhelpers->RemoveClientListener(&g_TopMenus); + plsys->RemovePluginsListener(&g_TopMenus); } diff --git a/extensions/topmenus/msvc8/topmenus.vcproj b/extensions/topmenus/msvc8/topmenus.vcproj index 9a4b0c22..48f95937 100644 --- a/extensions/topmenus/msvc8/topmenus.vcproj +++ b/extensions/topmenus/msvc8/topmenus.vcproj @@ -184,6 +184,10 @@ RelativePath="..\extension.cpp" > + + @@ -202,6 +206,10 @@ RelativePath="..\extension.h" > + + diff --git a/extensions/topmenus/sdk/smsdk_config.h b/extensions/topmenus/sdk/smsdk_config.h index 3a07cc29..847cdcf2 100644 --- a/extensions/topmenus/sdk/smsdk_config.h +++ b/extensions/topmenus/sdk/smsdk_config.h @@ -60,7 +60,7 @@ /** Enable interfaces you want to use here by uncommenting lines */ //#define SMEXT_ENABLE_FORWARDSYS -//#define SMEXT_ENABLE_HANDLESYS +#define SMEXT_ENABLE_HANDLESYS #define SMEXT_ENABLE_PLAYERHELPERS //#define SMEXT_ENABLE_DBMANAGER //#define SMEXT_ENABLE_GAMECONF @@ -71,7 +71,7 @@ //#define SMEXT_ENABLE_LIBSYS #define SMEXT_ENABLE_MENUS //#define SMEXT_ENABLE_ADTFACTORY -//#define SMEXT_ENABLE_PLUGINSYS +#define SMEXT_ENABLE_PLUGINSYS #define SMEXT_ENABLE_ADMINSYS #define SMEXT_ENABLE_TEXTPARSERS diff --git a/extensions/topmenus/smn_topmenus.cpp b/extensions/topmenus/smn_topmenus.cpp new file mode 100644 index 00000000..85843657 --- /dev/null +++ b/extensions/topmenus/smn_topmenus.cpp @@ -0,0 +1,269 @@ +/** + * vim: set ts=4 : + * ============================================================================= + * SourceMod Sample Extension + * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved. + * ============================================================================= + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License, version 3.0, as published by the + * Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + * + * As a special exception, AlliedModders LLC gives you permission to link the + * code of this program (as well as its derivative works) to "Half-Life 2," the + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software + * by the Valve Corporation. You must obey the GNU General Public License in + * all respects for all other code used. Additionally, AlliedModders LLC grants + * this exception to all derivative works. AlliedModders LLC defines further + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), + * or . + * + * Version: $Id$ + */ + +#include "extension.h" +#include "TopMenuManager.h" +#include "TopMenu.h" + +HandleType_t hTopMenuType; + +class TopMenuHandle : public IHandleTypeDispatch +{ +public: + void OnHandleDestroy(HandleType_t type, void *object) + { + ITopMenu *pTopMenu = (ITopMenu *)object; + g_TopMenus.DestroyTopMenu(pTopMenu); + } +} s_TopMenuHandle; + +void Initialize_Natives() +{ + hTopMenuType = handlesys->CreateType("ITopMenu", + &s_TopMenuHandle, + 0, + NULL, + NULL, + myself->GetIdentity(), + NULL); +} + +void Shutdown_Natives() +{ + handlesys->RemoveType(hTopMenuType, myself->GetIdentity()); +} + +enum TopMenuAction +{ + TopMenuAction_DrawOption = 0, + TopMenuAction_DrawTitle = 1, + TopMenuAction_SelectOption = 2, +}; + +class TopMenuCallbacks : public ITopMenuObjectCallbacks +{ +public: + TopMenuCallbacks(IPluginFunction *pFunction) : m_pFunction(pFunction) + { + } + + unsigned int OnTopMenuDrawOption(ITopMenu *menu, + int client, + unsigned int object_id, + char buffer[], + size_t maxlength) + { + cell_t result = ITEMDRAW_DEFAULT; + + m_pFunction->PushCell(m_hMenuHandle); + m_pFunction->PushCell(client); + m_pFunction->PushCell(object_id); + m_pFunction->PushStringEx(buffer, maxlength, 0, SM_PARAM_COPYBACK); + m_pFunction->PushCell(maxlength); + m_pFunction->Execute(&result); + + return result; + } + + void OnTopMenuDrawTitle(ITopMenu *menu, + int client, + unsigned int object_id, + char buffer[], + size_t maxlength) + { + m_pFunction->PushCell(m_hMenuHandle); + m_pFunction->PushCell(client); + m_pFunction->PushCell(object_id); + m_pFunction->PushStringEx(buffer, maxlength, 0, SM_PARAM_COPYBACK); + m_pFunction->PushCell(maxlength); + m_pFunction->Execute(NULL); + } + + void OnTopMenuSelectOption(ITopMenu *menu, + int client, + unsigned int object_id) + { + m_pFunction->PushCell(m_hMenuHandle); + m_pFunction->PushCell(client); + m_pFunction->PushCell(object_id); + m_pFunction->PushString(""); + m_pFunction->PushCell(0); + m_pFunction->Execute(NULL); + } + + void OnTopMenuObjectRemoved(ITopMenu *menu, unsigned int object_id) + { + delete this; + } + + Handle_t m_hMenuHandle; + IPluginFunction *m_pFunction; +}; + +static cell_t CreateTopMenu(IPluginContext *pContext, const cell_t *params) +{ + IPluginFunction *func = pContext->GetFunctionById(params[1]); + if (func == NULL) + { + return pContext ->ThrowNativeError("Invalid function specified"); + } + + TopMenuCallbacks *cb = new TopMenuCallbacks(func); + + ITopMenu *pMenu = g_TopMenus.CreateTopMenu(cb); + + if (!pMenu) + { + delete cb; + return BAD_HANDLE; + } + + Handle_t hndl = handlesys->CreateHandle(hTopMenuType, + pMenu, + pContext->GetIdentity(), + myself->GetIdentity(), + NULL); + if (hndl == 0) + { + g_TopMenus.DestroyTopMenu(pMenu); + return BAD_HANDLE; + } + + return hndl; +} + +static cell_t LoadTopMenuConfig(IPluginContext *pContext, const cell_t *params) +{ + HandleError err; + ITopMenu *pMenu; + HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity()); + + if ((err = handlesys->ReadHandle(params[1], hTopMenuType, &sec, (void **)&pMenu)) + != HandleError_None) + { + return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err); + } + + char *file, *err_buf; + pContext->LocalToString(params[2], &file); + pContext->LocalToString(params[3], &err_buf); + + char path[PLATFORM_MAX_PATH]; + g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file); + + return pMenu->LoadConfiguration(path, err_buf, params[4]) ? 1 : 0; +} + +static cell_t AddToTopMenu(IPluginContext *pContext, const cell_t *params) +{ + HandleError err; + ITopMenu *pMenu; + HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity()); + + if ((err = handlesys->ReadHandle(params[1], hTopMenuType, &sec, (void **)&pMenu)) + != HandleError_None) + { + return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err); + } + + IPluginFunction *func = pContext->GetFunctionById(params[1]); + if (func == NULL) + { + return pContext ->ThrowNativeError("Invalid function specified"); + } + + TopMenuCallbacks *cb = new TopMenuCallbacks(func); + + char *name, *cmdname; + pContext->LocalToString(params[2], &name); + pContext->LocalToString(params[5], &cmdname); + + TopMenuObjectType obj_type = (TopMenuObjectType)params[3]; + + unsigned int object_id; + if ((object_id = pMenu->AddToMenu(name, + obj_type, + cb, + pContext->GetIdentity(), + cmdname,params[6], + params[7])) == 0) + { + delete cb; + return 0; + } + + return object_id; +} + +static cell_t RemoveFromTopMenu(IPluginContext *pContext, const cell_t *params) +{ + HandleError err; + ITopMenu *pMenu; + HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity()); + + if ((err = handlesys->ReadHandle(params[1], hTopMenuType, &sec, (void **)&pMenu)) + != HandleError_None) + { + return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err); + } + + pMenu->RemoveFromMenu(params[2]); + + return 1; +} + +static cell_t FindTopMenuCategory(IPluginContext *pContext, const cell_t *params) +{ + HandleError err; + ITopMenu *pMenu; + HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity()); + + if ((err = handlesys->ReadHandle(params[1], hTopMenuType, &sec, (void **)&pMenu)) + != HandleError_None) + { + return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err); + } + + char *name; + pContext->LocalToString(params[2], &name); + + return pMenu->FindCategory(name); +} + +sp_nativeinfo_t g_TopMenuNatives[] = +{ + {"AddToTopMenu", AddToTopMenu}, + {"CreateTopMenu", CreateTopMenu}, + {"LoadTopMenuConfig", LoadTopMenuConfig}, + {"RemoveFromTopMenu", RemoveFromTopMenu}, + {"FindTopMenuCategory", FindTopMenuCategory}, + {NULL, NULL}, +}; diff --git a/extensions/topmenus/smn_topmenus.h b/extensions/topmenus/smn_topmenus.h new file mode 100644 index 00000000..cda80fc6 --- /dev/null +++ b/extensions/topmenus/smn_topmenus.h @@ -0,0 +1,41 @@ +/** + * vim: set ts=4 : + * ============================================================================= + * SourceMod Sample Extension + * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved. + * ============================================================================= + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License, version 3.0, as published by the + * Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + * + * As a special exception, AlliedModders LLC gives you permission to link the + * code of this program (as well as its derivative works) to "Half-Life 2," the + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software + * by the Valve Corporation. You must obey the GNU General Public License in + * all respects for all other code used. Additionally, AlliedModders LLC grants + * this exception to all derivative works. AlliedModders LLC defines further + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), + * or . + * + * Version: $Id$ + */ + +#ifndef _INCLUDE_SOURCEMOD_EXTENSION_TOPMENUS_NATIVES_H_ +#define _INCLUDE_SOURCEMOD_EXTENSION_TOPMENUS_NATIVES_H_ + +void Initialize_Natives(); +void Shutdown_Natives(); + +extern sp_nativeinfo_t g_TopMenuNatives[]; + +#endif //_INCLUDE_SOURCEMOD_EXTENSION_TOPMENUS_NATIVES_H_ + diff --git a/plugins/include/topmenus.inc b/plugins/include/topmenus.inc new file mode 100644 index 00000000..24d3b383 --- /dev/null +++ b/plugins/include/topmenus.inc @@ -0,0 +1,190 @@ +/** + * vim: set ts=4 : + * ============================================================================= + * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved. + * ============================================================================= + * + * This file is part of the SourceMod/SourcePawn SDK. + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License, version 3.0, as published by the + * Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + * + * As a special exception, AlliedModders LLC gives you permission to link the + * code of this program (as well as its derivative works) to "Half-Life 2," the + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software + * by the Valve Corporation. You must obey the GNU General Public License in + * all respects for all other code used. Additionally, AlliedModders LLC grants + * this exception to all derivative works. AlliedModders LLC defines further + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), + * or . + * + * Version: $Id$ + */ + +#if defined _topmenus_included + #endinput +#endif +#define _topmenus_included + +#include + +/** + * Actions a top menu will take on an object. + */ +enum TopMenuAction +{ + /** + * An option is being drawn for a menu (or for sorting purposes). + * + * INPUT : TopMenu Handle, (param1) Client index, (param2) object ID. + * OUTPUT: Buffer for rendering, maxlength of buffer. + * OUTPUT: Must return an ITEMDRAW constant. + */ + TopMenuAction_DrawOption = 0, + + /** + * The title of a menu is being drawn for a given object. + * + * Note: The Object ID will be 0 if drawing the root title. + * + * INPUT : TopMenu Handle, (param1) Client index, (param2) object ID. + * OUTPUT: Buffer for rendering, maxlength of buffer. + */ + TopMenuAction_DrawTitle = 1, + + /** + * A menu option has been selected. + * + * INPUT : TopMenu Handle, (param1) Client index, (param2) object ID. + */ + TopMenuAction_SelectOption = 2, +}; + +/** + * Top menu object types. + */ +enum TopMenuObjectType +{ + TopMenuObject_Category = 0, /**< Category (sub-menu branching from root) */ + TopMenuObject_Item = 1 /**< Item on a sub-menu */ +}; + +/** + * Top menu object tag for type checking. + */ +enum TopMenuObject +{ + INVALID_TOPMENUOBJECT = 0, +} + +/** + * TopMenu callback prototype. + * + * @param topmenu Handle to the TopMenu. + * @param action TopMenuAction being performed. + * @param param1 First parameter (dependent on action). + * @param param2 Second parameter (dependent on action). + * @param buffer Output buffer (not always used). + * @param maxlength Output buffer (not always used). + * @noreturn + */ +functag TopMenuHandler public(Handle:topmenu, + TopMenuAction:action, + param1, + param2, + String:buffer[], + maxlength); + +/** + * Creates a TopMenu. + * + * @param handler Handler to use for drawing the root title. + * @return A new TopMenu Handle, or INVALID_HANDLE on failure. + */ +native Handle:CreateTopMenu(TopMenuHandler:handler); + +/** + * Re-sorts the items in a TopMenu via a configuration file. + * + * The format of the configuration file should be a Valve Key-Values + * formatted file that SourceMod can parse. There should be one root + * section, and one sub-section for each category. Each sub-section's + * name should match the category name. + * + * Each sub-section may only contain key/value pairs in the form of: + * key: "item" + * value: Name of the item as passed to AddToTopMenu(). + * + * The TopMenu will draw items in the order declared in the configuration + * file. If items do not appear in the configuration file, they are sorted + * per-player based on how the handler function renders for that player. + * These items appear after the configuration sorted items. + * + * @param topmenu TopMenu Handle. + * @param file File path. + * @param error Error buffer. + * @param maxlength Maximum size of the error buffer. + * Error buffer will be filled with a + * zero-terminated string if false is + * returned. + * @return True on success, false on failure. + * @error Invalid TopMenu Handle. + */ +native bool:LoadTopMenuConfig(Handle:topmenu, const String:file[], String:error[], maxlength); + +/** + * Adds an object to a TopMenu. + * + * @param topmenu TopMenu Handle. + * @param name Object name (MUST be unique). + * @param type Object type. + * @param handler Handler for object. + * @param cmdname Command name (for access overrides). + * @param flags Default access flags. + * @param parent Parent object ID, or INVALID_TOPMENUOBJECT for none. + * Items must have a category parent. + * Categories must not have a parent. + * @return A new TopMenuObject ID, or INVALID_TOPMENUOBJECT on + * failure. + * @error Invalid TopMenu Handle. + */ +native TopMenuObject:AddToTopMenu(Handle:topmenu, + const String:name[], + TopMenuObjectType:type, + TopMenuHandler:handler, + const String:cmdname[], + flags, + TopMenuObject:parent); + +/** + * Removes an object from a TopMenu. + * + * Plugins' objects are automatically removed all TopMenus when the given + * plugin unloads or pauses. In the case of unpausing, all items are restored. + * + * @param topmenu TopMenu Handle. + * @param object TopMenuObject ID. + * @noreturn + * @error Invalid TopMenu Handle. + */ +native RemoveFromTopMenu(Handle:topmenu, TopMenuObject:object); + +/** + * Finds a category's object ID in a TopMenu. + * + * @param topmenu TopMenu Handle. + * @param name Object's unique name. + * @return TopMenuObject ID on success, or + * INVALID_TOPMENUOBJECT on failure. + * @error Invalid TopMenu Handle. + */ +native TopMenuObject:FindTopMenuCategory(Handle:topmenu, const String:name[]); diff --git a/public/extensions/ITopMenus.h b/public/extensions/ITopMenus.h index 48b700f6..06b00fef 100644 --- a/public/extensions/ITopMenus.h +++ b/public/extensions/ITopMenus.h @@ -66,6 +66,8 @@ namespace SourceMod TopMenuPosition_LastCategory = 3, /**< Last position in their last category */ }; + class ITopMenu; + /** * @brief Top Menu callbacks for rendering/drawing. */ @@ -85,6 +87,7 @@ namespace SourceMod /** * @brief Requests how the given item should be drawn for a client. * + * @param menu A pointer to the parent ITopMenu. * @param client Client index. * @param object_id Object ID returned from ITopMenu::AddToMenu(). * @param buffer Buffer to store rendered text. @@ -92,7 +95,8 @@ namespace SourceMod * @return ITEMDRAW flags to disable or not draw the * option for this operation. */ - virtual unsigned int OnTopMenuDrawOption(int client, + virtual unsigned int OnTopMenuDrawOption(ITopMenu *menu, + int client, unsigned int object_id, char buffer[], size_t maxlength) =0; @@ -101,13 +105,15 @@ namespace SourceMod * @brief Requests how the given item's title should be drawn for * a client. This is called on any object_id that is a category. * + * @param menu A pointer to the parent ITopMenu. * @param client Client index. * @param object_id Object ID returned from ITopMenu::AddToMenu(), * or 0 if the title is the root menu title. * @param buffer Buffer to store rendered text. * @param maxlength Maximum length of the rendering buffer. */ - virtual void OnTopMenuDrawTitle(int client, + virtual void OnTopMenuDrawTitle(ITopMenu *menu, + int client, unsigned int object_id, char buffer[], size_t maxlength) =0; @@ -115,10 +121,22 @@ namespace SourceMod /** * @brief Notifies the listener that the menu option has been selected. * + * @param menu A pointer to the parent ITopMenu. * @param client Client index. * @param object_id Object ID returned from ITopMenu::AddToMenu(). */ - virtual void OnTopMenuSelectOption(int client, unsigned int object_id) =0; + virtual void OnTopMenuSelectOption(ITopMenu *menu, + int client, + unsigned int object_id) =0; + + /** + * @brief Notified when the given item is removed. + * + * @param menu A pointer to the parent ITopMenu. + * @param object_id Object ID returned from ITopMenu::AddToMenu(), + * or 0 if the title callbacks are being removed. + */ + virtual void OnTopMenuObjectRemoved(ITopMenu *menu, unsigned int object_id) =0; }; /** @@ -154,7 +172,7 @@ namespace SourceMod * @brief Removes an object from a menu. If the object has any * children, those will be removed. * - * @param command_id Command ID returned from AddToMenu. + * @param object_id Object ID returned from AddToMenu. */ virtual void RemoveFromMenu(unsigned int object_id) =0; @@ -187,6 +205,14 @@ namespace SourceMod * @return True on success, false on failure. */ virtual bool LoadConfiguration(const char *file, char *error, size_t maxlength) =0; + + /** + * @brief Finds a category's ID by name. + * + * @param name Category's name. + * @return Object ID of the category, or 0 if none. + */ + virtual unsigned int FindCategory(const char *name) =0; }; /**