plugins/adminmenu.sp
author David Anderson <dvander@alliedmods.net>
Sat May 24 06:48:35 2008 +0000 (2008-05-24)
changeset 2072 82d3186b9fa4
parent 1984 15e02f0df87c
child 2289 44045ead61cd
permissions -rw-r--r--
added quick tool for fetching dlls of mdmps
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Admin Menu Plugin
     5  * Creates the base admin menu, for plugins to add items to.
     6  *
     7  * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
     8  * =============================================================================
     9  *
    10  * This program is free software; you can redistribute it and/or modify it under
    11  * the terms of the GNU General Public License, version 3.0, as published by the
    12  * Free Software Foundation.
    13  * 
    14  * This program is distributed in the hope that it will be useful, but WITHOUT
    15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    17  * details.
    18  *
    19  * You should have received a copy of the GNU General Public License along with
    20  * this program.  If not, see <http://www.gnu.org/licenses/>.
    21  *
    22  * As a special exception, AlliedModders LLC gives you permission to link the
    23  * code of this program (as well as its derivative works) to "Half-Life 2," the
    24  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    25  * by the Valve Corporation.  You must obey the GNU General Public License in
    26  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    27  * this exception to all derivative works.  AlliedModders LLC defines further
    28  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    29  * or <http://www.sourcemod.net/license.php>.
    30  *
    31  * Version: $Id$
    32  */
    33 
    34 #pragma semicolon 1
    35 
    36 #include <sourcemod>
    37 #include <topmenus>
    38 
    39 public Plugin:myinfo = 
    40 {
    41 	name = "Admin Menu",
    42 	author = "AlliedModders LLC",
    43 	description = "Administration Menu",
    44 	version = SOURCEMOD_VERSION,
    45 	url = "http://www.sourcemod.net/"
    46 };
    47 
    48 new g_maxPlayers;
    49 
    50 /* Forwards */
    51 new Handle:hOnAdminMenuReady = INVALID_HANDLE;
    52 new Handle:hOnAdminMenuCreated = INVALID_HANDLE;
    53 
    54 /* Menus */
    55 new Handle:hAdminMenu = INVALID_HANDLE;
    56 
    57 /* Top menu objects */
    58 new TopMenuObject:obj_playercmds = INVALID_TOPMENUOBJECT;
    59 new TopMenuObject:obj_servercmds = INVALID_TOPMENUOBJECT;
    60 new TopMenuObject:obj_votingcmds = INVALID_TOPMENUOBJECT;
    61 
    62 #include "adminmenu/dynamicmenu.sp"
    63 
    64 public bool:AskPluginLoad(Handle:myself, bool:late, String:error[], err_max)
    65 {
    66 	CreateNative("GetAdminTopMenu", __GetAdminTopMenu);
    67 	CreateNative("AddTargetsToMenu", __AddTargetsToMenu);
    68 	CreateNative("AddTargetsToMenu2", __AddTargetsToMenu2);
    69 	RegPluginLibrary("adminmenu");
    70 	return true;
    71 }
    72 
    73 public OnPluginStart()
    74 {
    75 	LoadTranslations("common.phrases");
    76 	LoadTranslations("adminmenu.phrases");
    77 	
    78 	hOnAdminMenuCreated = CreateGlobalForward("OnAdminMenuCreated", ET_Ignore, Param_Cell);
    79 	hOnAdminMenuReady = CreateGlobalForward("OnAdminMenuReady", ET_Ignore, Param_Cell);
    80 
    81 	RegAdminCmd("sm_admin", Command_DisplayMenu, ADMFLAG_GENERIC, "Displays the admin menu");
    82 }
    83 
    84 public OnConfigsExecuted()
    85 {
    86 	decl String:path[PLATFORM_MAX_PATH];
    87 	decl String:error[256];
    88 	
    89 	BuildPath(Path_SM, path, sizeof(path), "configs/adminmenu_sorting.txt");
    90 	
    91 	if (!LoadTopMenuConfig(hAdminMenu, path, error, sizeof(error)))
    92 	{
    93 		LogError("Could not load admin menu config (file \"%s\": %s)", path, error);
    94 		return;
    95 	}
    96 }
    97 
    98 public OnMapStart()
    99 {
   100 	g_maxPlayers = GetMaxClients();
   101 	
   102 	ParseConfigs();
   103 }
   104 
   105 public OnAllPluginsLoaded()
   106 {
   107 	hAdminMenu = CreateTopMenu(DefaultCategoryHandler);
   108 	
   109 	obj_playercmds = AddToTopMenu(hAdminMenu, 
   110 		"PlayerCommands",
   111 		TopMenuObject_Category,
   112 		DefaultCategoryHandler,
   113 		INVALID_TOPMENUOBJECT);
   114 
   115 	obj_servercmds = AddToTopMenu(hAdminMenu,
   116 		"ServerCommands",
   117 		TopMenuObject_Category,
   118 		DefaultCategoryHandler,
   119 		INVALID_TOPMENUOBJECT);
   120 
   121 	obj_votingcmds = AddToTopMenu(hAdminMenu,
   122 		"VotingCommands",
   123 		TopMenuObject_Category,
   124 		DefaultCategoryHandler,
   125 		INVALID_TOPMENUOBJECT);
   126 		
   127 	BuildDynamicMenu();
   128 	
   129 	Call_StartForward(hOnAdminMenuCreated);
   130 	Call_PushCell(hAdminMenu);
   131 	Call_Finish();
   132 	
   133 	Call_StartForward(hOnAdminMenuReady);
   134 	Call_PushCell(hAdminMenu);
   135 	Call_Finish();
   136 }
   137 
   138 public DefaultCategoryHandler(Handle:topmenu, 
   139 						TopMenuAction:action,
   140 						TopMenuObject:object_id,
   141 						param,
   142 						String:buffer[],
   143 						maxlength)
   144 {
   145 	if (action == TopMenuAction_DisplayTitle)
   146 	{
   147 		if (object_id == INVALID_TOPMENUOBJECT)
   148 		{
   149 			Format(buffer, maxlength, "%T:", "Admin Menu", param);
   150 		}
   151 		else if (object_id == obj_playercmds)
   152 		{
   153 			Format(buffer, maxlength, "%T:", "Player Commands", param);
   154 		}
   155 		else if (object_id == obj_servercmds)
   156 		{
   157 			Format(buffer, maxlength, "%T:", "Server Commands", param);
   158 		}
   159 		else if (object_id == obj_votingcmds)
   160 		{
   161 			Format(buffer, maxlength, "%T:", "Voting Commands", param);
   162 		}
   163 	}
   164 	else if (action == TopMenuAction_DisplayOption)
   165 	{
   166 		if (object_id == obj_playercmds)
   167 		{
   168 			Format(buffer, maxlength, "%T", "Player Commands", param);
   169 		}
   170 		else if (object_id == obj_servercmds)
   171 		{
   172 			Format(buffer, maxlength, "%T", "Server Commands", param);
   173 		}
   174 		else if (object_id == obj_votingcmds)
   175 		{
   176 			Format(buffer, maxlength, "%T", "Voting Commands", param);
   177 		}
   178 	}
   179 }
   180 
   181 public __GetAdminTopMenu(Handle:plugin, numParams)
   182 {
   183 	return _:hAdminMenu;
   184 }
   185 
   186 public __AddTargetsToMenu(Handle:plugin, numParams)
   187 {
   188 	new bool:alive_only = false;
   189 	
   190 	if (numParams >= 4)
   191 	{
   192 		alive_only = GetNativeCell(4);
   193 	}
   194 	
   195 	return UTIL_AddTargetsToMenu(GetNativeCell(1), GetNativeCell(2), GetNativeCell(3), alive_only);
   196 }
   197 
   198 public __AddTargetsToMenu2(Handle:plugin, numParams)
   199 {
   200 	return UTIL_AddTargetsToMenu2(GetNativeCell(1), GetNativeCell(2), GetNativeCell(3));
   201 }
   202 
   203 public Action:Command_DisplayMenu(client, args)
   204 {
   205 	if (client == 0)
   206 	{
   207 		ReplyToCommand(client, "[SM] %t", "Command is in-game only");
   208 		return Plugin_Handled;
   209 	}
   210 	
   211 	DisplayTopMenu(hAdminMenu, client, TopMenuPosition_Start);
   212 	
   213 	return Plugin_Handled;
   214 }
   215 
   216 stock UTIL_AddTargetsToMenu2(Handle:menu, source_client, flags)
   217 {
   218 	new max_clients = GetMaxClients();
   219 	decl String:user_id[12];
   220 	decl String:name[MAX_NAME_LENGTH];
   221 	decl String:display[MAX_NAME_LENGTH+12];
   222 	
   223 	new num_clients;
   224 	
   225 	for (new i = 1; i <= max_clients; i++)
   226 	{
   227 		if (!IsClientConnected(i) || IsClientInKickQueue(i))
   228 		{
   229 			continue;
   230 		}
   231 		
   232 		PrintToServer("%d, %d, %d", i, flags, IsFakeClient(i));
   233 		
   234 		if (((flags & COMMAND_FILTER_NO_BOTS) == COMMAND_FILTER_NO_BOTS)
   235 			&& IsFakeClient(i))
   236 		{
   237 			continue;
   238 		}
   239 		
   240 		if (((flags & COMMAND_FILTER_CONNECTED) != COMMAND_FILTER_CONNECTED)
   241 			&& !IsClientInGame(i))
   242 		{
   243 			continue;
   244 		}
   245 		
   246 		if (((flags & COMMAND_FILTER_ALIVE) == COMMAND_FILTER_ALIVE) 
   247 			&& !IsPlayerAlive(i))
   248 		{
   249 			continue;
   250 		}
   251 		
   252 		if (((flags & COMMAND_FILTER_DEAD) == COMMAND_FILTER_DEAD)
   253 			&& IsPlayerAlive(i))
   254 		{
   255 			continue;
   256 		}
   257 		
   258 		if ((source_client && ((flags & COMMAND_FILTER_NO_IMMUNITY) != COMMAND_FILTER_NO_IMMUNITY))
   259 			&& !CanUserTarget(source_client, i))
   260 		{
   261 			continue;
   262 		}
   263 		
   264 		IntToString(GetClientUserId(i), user_id, sizeof(user_id));
   265 		GetClientName(i, name, sizeof(name));
   266 		Format(display, sizeof(display), "%s (%s)", name, user_id);
   267 		AddMenuItem(menu, user_id, display);
   268 		num_clients++;
   269 	}
   270 	
   271 	return num_clients;
   272 }
   273 
   274 stock UTIL_AddTargetsToMenu(Handle:menu, source_client, bool:in_game_only, bool:alive_only)
   275 {
   276 	new flags = 0;
   277 	
   278 	if (!in_game_only)
   279 	{
   280 		flags |= COMMAND_FILTER_CONNECTED;
   281 	}
   282 	
   283 	if (alive_only)
   284 	{
   285 		flags |= COMMAND_FILTER_ALIVE;
   286 	}
   287 	
   288 	return UTIL_AddTargetsToMenu2(menu, source_client, flags);
   289 }
   290