Move RootConsoleMenu into core/logic.

This commit is contained in:
David Anderson
2015-08-30 21:01:46 -07:00
parent 23d55dd9d5
commit 28346c99f2
12 changed files with 52 additions and 47 deletions
+1
View File
@@ -77,6 +77,7 @@ binary.sources += [
'smn_menus.cpp',
'sprintf.cpp',
'LibrarySys.cpp',
'RootConsoleMenu.cpp',
]
if builder.target_platform == 'windows':
binary.sources += ['thread/WinThreads.cpp']
+233
View File
@@ -0,0 +1,233 @@
// vim: set ts=4 sw=4 tw=99 noet :
// =============================================================================
// SourceMod
// Copyright (C) 2004-2009 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 <http://www.gnu.org/licenses/>.
//
// 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 <http://www.sourcemod.net/license.php>.
#include "RootConsoleMenu.h"
#include <amtl/am-string.h>
#include <sourcemod_version.h>
RootConsoleMenu g_RootMenu;
RootConsoleMenu::RootConsoleMenu()
{
}
RootConsoleMenu::~RootConsoleMenu()
{
List<ConsoleEntry *>::iterator iter;
for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
{
delete (*iter);
}
m_Menu.clear();
}
void RootConsoleMenu::OnSourceModStartup(bool late)
{
AddRootConsoleCommand3("version", "Display version information", this);
AddRootConsoleCommand3("credits", "Display credits listing", this);
}
void RootConsoleMenu::OnSourceModAllInitialized()
{
sharesys->AddInterface(NULL, this);
}
void RootConsoleMenu::OnSourceModShutdown()
{
RemoveRootConsoleCommand("credits", this);
RemoveRootConsoleCommand("version", this);
}
void RootConsoleMenu::ConsolePrint(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
smcore.ConsolePrintVa(fmt, ap);
va_end(ap);
}
bool RootConsoleMenu::AddRootConsoleCommand(const char *cmd, const char *text, IRootConsoleCommand *pHandler)
{
return false;
}
bool RootConsoleMenu::AddRootConsoleCommand2(const char *cmd, const char *text, IRootConsoleCommand *pHandler)
{
return false;
}
bool RootConsoleMenu::AddRootConsoleCommand3(const char *cmd,
const char *text,
IRootConsoleCommand *pHandler)
{
if (m_Commands.contains(cmd))
return false;
/* Sort this into the menu */
List<ConsoleEntry *>::iterator iter = m_Menu.begin();
ConsoleEntry *pEntry;
bool inserted = false;
while (iter != m_Menu.end())
{
pEntry = (*iter);
if (strcmp(cmd, pEntry->command.c_str()) < 0)
{
ConsoleEntry *pNew = new ConsoleEntry;
pNew->command.assign(cmd);
pNew->description.assign(text);
pNew->cmd = pHandler;
m_Commands.insert(cmd, pNew);
m_Menu.insert(iter, pNew);
inserted = true;
break;
}
iter++;
}
if (!inserted)
{
ConsoleEntry *pNew = new ConsoleEntry;
pNew->command.assign(cmd);
pNew->description.assign(text);
pNew->cmd = pHandler;
m_Commands.insert(cmd, pNew);
m_Menu.push_back(pNew);
}
return true;
}
bool RootConsoleMenu::RemoveRootConsoleCommand(const char *cmd, IRootConsoleCommand *pHandler)
{
m_Commands.remove(cmd);
List<ConsoleEntry *>::iterator iter;
ConsoleEntry *pEntry;
for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
{
pEntry = (*iter);
if (pEntry->command.compare(cmd) == 0)
{
delete pEntry;
m_Menu.erase(iter);
break;
}
}
return true;
}
void RootConsoleMenu::DrawGenericOption(const char *cmd, const char *text)
{
char buffer[255];
size_t len, cmdlen = strlen(cmd);
len = ke::SafeSprintf(buffer, sizeof(buffer), " %s", cmd);
if (cmdlen < 16)
{
size_t num = 16 - cmdlen;
for (size_t i = 0; i < num; i++)
{
buffer[len++] = ' ';
}
len += snprintf(&buffer[len], sizeof(buffer) - len, " - %s", text);
ConsolePrint("%s", buffer);
}
}
const char *RootConsoleMenu::GetInterfaceName()
{
return SMINTERFACE_ROOTCONSOLE_NAME;
}
unsigned int RootConsoleMenu::GetInterfaceVersion()
{
return SMINTERFACE_ROOTCONSOLE_VERSION;
}
void RootConsoleMenu::GotRootCmd(const ICommandArgs *cmd)
{
unsigned int argnum = cmd->ArgC();
if (argnum >= 2)
{
const char *cmdname = cmd->Arg(1);
ConsoleEntry *entry;
if (m_Commands.retrieve(cmdname, &entry))
{
entry->cmd->OnRootConsoleCommand(cmdname, cmd);
return;
}
}
ConsolePrint("SourceMod Menu:");
ConsolePrint("Usage: sm <command> [arguments]");
List<ConsoleEntry *>::iterator iter;
ConsoleEntry *pEntry;
for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
{
pEntry = (*iter);
DrawGenericOption(pEntry->command.c_str(), pEntry->description.c_str());
}
}
void RootConsoleMenu::OnRootConsoleCommand(const char *cmdname, const ICommandArgs *command)
{
if (strcmp(cmdname, "credits") == 0)
{
ConsolePrint(" SourceMod was developed by AlliedModders, LLC.");
ConsolePrint(" Development would not have been possible without the following people:");
ConsolePrint(" David \"BAILOPAN\" Anderson");
ConsolePrint(" Matt \"pRED\" Woodrow");
ConsolePrint(" Scott \"DS\" Ehlert");
ConsolePrint(" Fyren");
ConsolePrint(" Nicholas \"psychonic\" Hastings");
ConsolePrint(" Asher \"asherkin\" Baker");
ConsolePrint(" Borja \"faluco\" Ferrer");
ConsolePrint(" Pavol \"PM OnoTo\" Marko");
ConsolePrint(" Special thanks to Liam, ferret, and Mani");
ConsolePrint(" Special thanks to Viper and SteamFriends");
ConsolePrint(" http://www.sourcemod.net/");
}
else if (strcmp(cmdname, "version") == 0)
{
ConsolePrint(" SourceMod Version Information:");
ConsolePrint(" SourceMod Version: %s", SOURCEMOD_VERSION);
if (g_pSourcePawn2->IsJitEnabled())
ConsolePrint(" SourcePawn Engine: %s (build %s)", g_pSourcePawn2->GetEngineName(), g_pSourcePawn2->GetVersionString());
else
ConsolePrint(" SourcePawn Engine: %s (build %s NO JIT)", g_pSourcePawn2->GetEngineName(), g_pSourcePawn2->GetVersionString());
ConsolePrint(" SourcePawn API: v1 = %d, v2 = %d", g_pSourcePawn->GetEngineAPIVersion(), g_pSourcePawn2->GetAPIVersion());
ConsolePrint(" Compiled on: %s", SOURCEMOD_BUILD_TIME);
#if defined(SM_GENERATED_BUILD)
ConsolePrint(" Built from: https://github.com/alliedmodders/sourcemod/commit/%s", SOURCEMOD_SHA);
ConsolePrint(" Build ID: %s:%s", SOURCEMOD_LOCAL_REV, SOURCEMOD_SHA);
#endif
ConsolePrint(" http://www.sourcemod.net/");
}
}
+88
View File
@@ -0,0 +1,88 @@
// vim: set ts=4 sw=4 tw=99 noet :
// =============================================================================
// SourceMod
// Copyright (C) 2004-2015 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 <http://www.gnu.org/licenses/>.
//
// 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 <http://www.sourcemod.net/license.php>.
#ifndef _INCLUDE_SOURCEMOD_ROOT_CONSOLE_MENU_IMPL_H_
#define _INCLUDE_SOURCEMOD_ROOT_CONSOLE_MENU_IMPL_H_
#include "common_logic.h"
#include <IRootConsoleMenu.h>
#include <sh_list.h>
#include <sh_string.h>
#include <sm_namehashset.h>
using namespace SourceMod;
using namespace SourceHook;
struct ConsoleEntry
{
String command;
String description;
IRootConsoleCommand *cmd;
static inline bool matches(const char *name, const ConsoleEntry *entry)
{
return strcmp(name, entry->command.c_str()) == 0;
}
};
class RootConsoleMenu :
public SMGlobalClass,
public IRootConsoleCommand,
public IRootConsole
{
public:
RootConsoleMenu();
~RootConsoleMenu();
public:
const char *GetInterfaceName();
unsigned int GetInterfaceVersion();
public: //SMGlobalClass
void OnSourceModStartup(bool late);
void OnSourceModAllInitialized();
void OnSourceModShutdown();
public: //IRootConsoleCommand
void OnRootConsoleCommand(const char *cmdname, const ICommandArgs *command);
public: //IRootConsole
bool AddRootConsoleCommand(const char *cmd, const char *text, IRootConsoleCommand *pHandler);
bool RemoveRootConsoleCommand(const char *cmd, IRootConsoleCommand *pHandler);
void ConsolePrint(const char *fmt, ...);
void DrawGenericOption(const char *cmd, const char *text);
bool AddRootConsoleCommand2(const char *cmd,
const char *text,
IRootConsoleCommand *pHandler);
bool AddRootConsoleCommand3(const char *cmd,
const char *text,
IRootConsoleCommand *pHandler);
public:
void GotRootCmd(const ICommandArgs *cmd);
private:
NameHashSet<ConsoleEntry *> m_Commands;
List<ConsoleEntry *> m_Menu;
};
extern RootConsoleMenu g_RootMenu;
#endif // _INCLUDE_SOURCEMOD_ROOT_CONSOLE_MENU_IMPL_H_
+9 -2
View File
@@ -53,6 +53,7 @@
#include "Logger.h"
#include "sprintf.h"
#include "LibrarySys.h"
#include "RootConsoleMenu.h"
sm_core_t smcore;
IHandleSys *handlesys = &g_HandleSys;
@@ -63,7 +64,7 @@ ILibrarySys *libsys = &g_LibSys;
ITextParsers *textparser = &g_TextParser;
IVEngineServer *engine;
IShareSys *sharesys = &g_ShareSys;
IRootConsole *rootmenu;
IRootConsole *rootmenu = &g_RootMenu;
IPluginManager *pluginsys = g_PluginSys.GetOldAPI();
IForwardManager *forwardsys = &g_Forwards;
ITimerSystem *timersys;
@@ -120,6 +121,11 @@ static void RegisterProfiler(IProfilingTool *tool)
g_ProfileToolManager.RegisterTool(tool);
}
static void OnRootCommand(const ICommandArgs *args)
{
g_RootMenu.GotRootCmd(args);
}
static sm_logic_t logic =
{
NULL,
@@ -140,6 +146,7 @@ static sm_logic_t logic =
DumpHandles,
DumpAdminCache,
RegisterProfiler,
OnRootCommand,
&g_PluginSys,
&g_ShareSys,
&g_Extensions,
@@ -148,6 +155,7 @@ static sm_logic_t logic =
&g_Admins,
NULL,
&g_Logger,
&g_RootMenu,
-1.0f
};
@@ -161,7 +169,6 @@ static void logic_init(const sm_core_t* core, sm_logic_t* _logic)
engine = core->engine;
g_pSM = core->sm;
rootmenu = core->rootmenu;
timersys = core->timersys;
playerhelpers = core->playerhelpers;
gamehelpers = core->gamehelpers;
+4 -2
View File
@@ -52,7 +52,7 @@ using namespace SourceHook;
* Add 1 to the RHS of this expression to bump the intercom file
* This is to prevent mismatching core/logic binaries
*/
#define SM_LOGIC_MAGIC (0x0F47C0DE - 37)
#define SM_LOGIC_MAGIC (0x0F47C0DE - 38)
#if defined SM_LOGIC
class IVEngineServer
@@ -120,6 +120,7 @@ namespace SourceMod
class ITranslator;
class IGameConfig;
class IMenuManager;
class ICommandArgs;
}
class IVEngineServer;
@@ -283,7 +284,6 @@ struct sm_core_t
IVEngineServer *engine;
IFileSystem *filesystem;
IPlayerInfo_Logic *playerInfo;
IRootConsole *rootmenu;
ITimerSystem *timersys;
IPlayerManager *playerhelpers;
IGameHelpers *gamehelpers;
@@ -348,6 +348,7 @@ struct sm_logic_t
void (*DumpHandles)(void (*dumpfn)(const char *fmt, ...));
bool (*DumpAdminCache)(const char *filename);
void (*RegisterProfiler)(IProfilingTool *tool);
void (*OnRootCommand)(const ICommandArgs *args);
IScriptManager *scripts;
IShareSys *sharesys;
IExtensionSys *extsys;
@@ -356,6 +357,7 @@ struct sm_logic_t
IAdminSystem *adminsys;
IdentityToken_t *core_ident;
ILogger *logger;
IRootConsole *rootmenu;
float sentinel;
};