plugins/basecommands/execcfg.sp
author Michael McKoy <ferret99gt@gmail.com>
Sat Jan 12 19:45:36 2008 +0000 (2008-01-12)
changeset 1845 a2e4c1cbf800
parent 1789 55172f6df9a4
child 1947 e73a1e51833b
permissions -rw-r--r--
New stock to get m_fFlags, and #defines for its bits.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Basecommands Plugin
     5  * Provides exec cfg functionality
     6  *
     7  * SourceMod (C)2004-2007 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: admin-flatfile.sp 1438 2007-09-16 03:45:06Z dvander $
    32  */
    33 
    34 new Handle:g_ConfigMenu = INVALID_HANDLE;
    35 
    36 PerformExec(client, String:path[])
    37 {
    38 	if (!FileExists(path))
    39 	{
    40 		ReplyToCommand(client, "[SM] %t", "Config not found", path[4]);
    41 		return;
    42 	}
    43 
    44 	ShowActivity2(client, "[SM] ", "%t", "Executed config", path[4]);
    45 
    46 	LogAction(client, -1, "\"%L\" executed config (file \"%s\")", client, path[4]);
    47 
    48 	ServerCommand("exec \"%s\"", path[4]);
    49 }
    50 
    51 public AdminMenu_ExecCFG(Handle:topmenu, 
    52 					  TopMenuAction:action,
    53 					  TopMenuObject:object_id,
    54 					  param,
    55 					  String:buffer[],
    56 					  maxlength)
    57 {
    58 	if (action == TopMenuAction_DisplayOption)
    59 	{
    60 		Format(buffer, maxlength, "%T", "Exec CFG", param);
    61 	}
    62 	else if (action == TopMenuAction_SelectOption)
    63 	{
    64 		DisplayMenu(g_ConfigMenu, param, MENU_TIME_FOREVER);
    65 	}
    66 }
    67 
    68 public MenuHandler_ExecCFG(Handle:menu, MenuAction:action, param1, param2)
    69 {
    70 	if (action == MenuAction_Cancel)
    71 	{
    72 		if (param2 == MenuCancel_ExitBack && hTopMenu != INVALID_HANDLE)
    73 		{
    74 			DisplayTopMenu(hTopMenu, param1, TopMenuPosition_LastCategory);
    75 		}
    76 	}
    77 	else if (action == MenuAction_Select)
    78 	{
    79 		decl String:path[256];
    80 		
    81 		GetMenuItem(menu, param2, path, sizeof(path));
    82 	
    83 		PerformExec(param1, path);
    84 	}
    85 }
    86 
    87 public Action:Command_ExecCfg(client, args)
    88 {
    89 	if (args < 1)
    90 	{
    91 		ReplyToCommand(client, "[SM] Usage: sm_execcfg <filename>");
    92 		return Plugin_Handled;
    93 	}
    94 
    95 	new String:path[64] = "cfg/";
    96 	GetCmdArg(1, path[4], sizeof(path)-4);
    97 
    98 	PerformExec(client, path);
    99 
   100 	return Plugin_Handled;
   101 }
   102 
   103 new Handle:config_parser = INVALID_HANDLE;
   104 ParseConfigs()
   105 {
   106 	if (config_parser == INVALID_HANDLE)
   107 	{
   108 		config_parser = SMC_CreateParser();
   109 	}
   110 	
   111 	SMC_SetReaders(config_parser, NewSection, KeyValue, EndSection);
   112 	
   113 	if (g_ConfigMenu != INVALID_HANDLE)
   114 	{
   115 		CloseHandle(g_ConfigMenu);
   116 	}
   117 	
   118 	g_ConfigMenu = CreateMenu(MenuHandler_ExecCFG);
   119 	SetMenuTitle(g_ConfigMenu, "Choose Config");
   120 	SetMenuExitBackButton(g_ConfigMenu, true);
   121 	
   122 	decl String:configPath[256];
   123 	BuildPath(Path_SM, configPath, sizeof(configPath), "configs/adminmenu_cfgs.txt");
   124 	
   125 	if (!FileExists(configPath))
   126 	{
   127 		LogError("Unable to locate exec config file, no maps loaded.");
   128 			
   129 		return;		
   130 	}
   131 	
   132 	new line;
   133 	new SMCError:err = SMC_ParseFile(config_parser, configPath, line);
   134 	if (err != SMCError_Okay)
   135 	{
   136 		decl String:error[256];
   137 		SMC_GetErrorString(err, error, sizeof(error));
   138 		LogError("Could not parse file (line %d, file \"%s\"):", line, configPath);
   139 		LogError("Parser encountered error: %s", error);
   140 	}
   141 	
   142 	return;
   143 }
   144 
   145 public SMCResult:NewSection(Handle:smc, const String:name[], bool:opt_quotes)
   146 {
   147 
   148 }
   149 
   150 public SMCResult:KeyValue(Handle:smc, const String:key[], const String:value[], bool:key_quotes, bool:value_quotes)
   151 {
   152 	AddMenuItem(g_ConfigMenu, key, value);
   153 }
   154 
   155 public SMCResult:EndSection(Handle:smc)
   156 {
   157 	
   158 }