Move gnprintf/atcprintf from core to logic.

This commit is contained in:
David Anderson 2015-08-26 02:28:50 -04:00
parent 9d2bee261c
commit e78fe93e92
15 changed files with 1373 additions and 1265 deletions

View File

@ -75,6 +75,7 @@ binary.sources += [
'Logger.cpp', 'Logger.cpp',
'smn_core.cpp', 'smn_core.cpp',
'smn_menus.cpp', 'smn_menus.cpp',
'sprintf.cpp',
] ]
if builder.target_platform == 'windows': if builder.target_platform == 'windows':
binary.sources += ['thread/WinThreads.cpp'] binary.sources += ['thread/WinThreads.cpp']

View File

@ -1,5 +1,5 @@
/** /**
* vim: set ts=4 : * vim: set ts=4 sw=4 tw=99 noet :
* ============================================================================= * =============================================================================
* SourceMod * SourceMod
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
@ -32,6 +32,7 @@
#include "common_logic.h" #include "common_logic.h"
#include "PhraseCollection.h" #include "PhraseCollection.h"
#include "Translator.h" #include "Translator.h"
#include "sprintf.h"
#include <am-string.h> #include <am-string.h>
CPhraseCollection::CPhraseCollection() CPhraseCollection::CPhraseCollection()
@ -114,7 +115,7 @@ bool CPhraseCollection::FormatString(char *buffer,
unsigned int arg; unsigned int arg;
arg = 0; arg = 0;
if (!smcore.gnprintf(buffer, maxlength, format, this, params, numparams, arg, pOutLength, pFailPhrase)) if (!gnprintf(buffer, maxlength, format, this, params, numparams, arg, pOutLength, pFailPhrase))
{ {
return false; return false;
} }

View File

@ -40,6 +40,7 @@
#include <ILibrarySys.h> #include <ILibrarySys.h>
#include "PhraseCollection.h" #include "PhraseCollection.h"
#include "stringutil.h" #include "stringutil.h"
#include "sprintf.h"
#include <am-string.h> #include <am-string.h>
Translator g_Translator; Translator g_Translator;
@ -1059,7 +1060,7 @@ bool Translator::FormatString(char *buffer,
unsigned int arg; unsigned int arg;
arg = 0; arg = 0;
if (!smcore.gnprintf(buffer, maxlength, format, pPhrases, params, numparams, arg, pOutLength, pFailPhrase)) if (!gnprintf(buffer, maxlength, format, pPhrases, params, numparams, arg, pOutLength, pFailPhrase))
{ {
return false; return false;
} }

View File

@ -51,6 +51,7 @@
#include "AdminCache.h" #include "AdminCache.h"
#include "ProfileTools.h" #include "ProfileTools.h"
#include "Logger.h" #include "Logger.h"
#include "sprintf.h"
sm_core_t smcore; sm_core_t smcore;
IHandleSys *handlesys = &g_HandleSys; IHandleSys *handlesys = &g_HandleSys;
@ -124,6 +125,7 @@ static sm_logic_t logic =
g_pThreader, g_pThreader,
&g_Translator, &g_Translator,
stristr, stristr,
atcprintf,
CoreTranslate, CoreTranslate,
AddCorePhraseFile, AddCorePhraseFile,
UTIL_ReplaceAll, UTIL_ReplaceAll,

View File

@ -52,7 +52,7 @@ using namespace SourceHook;
* Add 1 to the RHS of this expression to bump the intercom file * Add 1 to the RHS of this expression to bump the intercom file
* This is to prevent mismatching core/logic binaries * This is to prevent mismatching core/logic binaries
*/ */
#define SM_LOGIC_MAGIC (0x0F47C0DE - 33) #define SM_LOGIC_MAGIC (0x0F47C0DE - 34)
#if defined SM_LOGIC #if defined SM_LOGIC
class IVEngineServer class IVEngineServer
@ -297,9 +297,6 @@ struct sm_core_t
void (*ConPrint)(const char *message); void (*ConPrint)(const char *message);
const char * (*GetCvarString)(ConVar*); const char * (*GetCvarString)(ConVar*);
bool (*GetCvarBool)(ConVar*); bool (*GetCvarBool)(ConVar*);
bool (*gnprintf)(char *, size_t, const char *, IPhraseCollection *, void **,
unsigned int, unsigned int &, size_t *, const char **);
size_t (*atcprintf)(char *, size_t, const char *, IPluginContext *, const cell_t *, int *);
bool (*GetGameName)(char *buffer, size_t maxlength); bool (*GetGameName)(char *buffer, size_t maxlength);
const char * (*GetGameDescription)(); const char * (*GetGameDescription)();
const char * (*GetSourceEngineName)(); const char * (*GetSourceEngineName)();
@ -319,6 +316,9 @@ struct sm_core_t
int (*GetImmunityMode)(); int (*GetImmunityMode)();
void (*UpdateAdminCmdFlags)(const char *cmd, OverrideType type, FlagBits bits, bool remove); void (*UpdateAdminCmdFlags)(const char *cmd, OverrideType type, FlagBits bits, bool remove);
bool (*LookForCommandAdminFlags)(const char *cmd, FlagBits *pFlags); bool (*LookForCommandAdminFlags)(const char *cmd, FlagBits *pFlags);
bool (*DescribePlayer)(int index, const char **namep, const char **authp, int *useridp);
int (*MaxClients)();
int (*GetGlobalTarget)();
const char *gamesuffix; const char *gamesuffix;
/* Data */ /* Data */
ServerGlobals *serverGlobals; ServerGlobals *serverGlobals;
@ -334,6 +334,7 @@ struct sm_logic_t
IThreader *threader; IThreader *threader;
ITranslator *translator; ITranslator *translator;
const char *(*stristr)(const char *, const char *); const char *(*stristr)(const char *, const char *);
size_t (*atcprintf)(char *, size_t, const char *, IPluginContext *, const cell_t *, int *);
bool (*CoreTranslate)(char *, size_t, const char *, unsigned int, size_t *, ...); bool (*CoreTranslate)(char *, size_t, const char *, unsigned int, size_t *, ...);
void (*AddCorePhraseFile)(const char *filename); void (*AddCorePhraseFile)(const char *filename);
unsigned int (*ReplaceAll)(char*, size_t, const char *, const char *, bool); unsigned int (*ReplaceAll)(char*, size_t, const char *, const char *, bool);

View File

@ -35,6 +35,7 @@
#include <IPlayerHelpers.h> #include <IPlayerHelpers.h>
#include <ISourceMod.h> #include <ISourceMod.h>
#include <ITranslator.h> #include <ITranslator.h>
#include "sprintf.h"
static cell_t CheckCommandAccess(IPluginContext *pContext, const cell_t *params) static cell_t CheckCommandAccess(IPluginContext *pContext, const cell_t *params)
{ {
@ -90,7 +91,7 @@ static cell_t sm_PrintToServer(IPluginContext *pCtx, const cell_t *params)
int arg = 2; int arg = 2;
pCtx->LocalToString(params[1], &fmt); pCtx->LocalToString(params[1], &fmt);
size_t res = smcore.atcprintf(buffer, sizeof(buffer) - 2, fmt, pCtx, params, &arg); size_t res = atcprintf(buffer, sizeof(buffer) - 2, fmt, pCtx, params, &arg);
buffer[res++] = '\n'; buffer[res++] = '\n';
buffer[res] = '\0'; buffer[res] = '\0';
@ -129,7 +130,7 @@ static cell_t sm_PrintToConsole(IPluginContext *pCtx, const cell_t *params)
int arg = 3; int arg = 3;
pCtx->LocalToString(params[2], &fmt); pCtx->LocalToString(params[2], &fmt);
size_t res = smcore.atcprintf(buffer, sizeof(buffer) - 2, fmt, pCtx, params, &arg); size_t res = atcprintf(buffer, sizeof(buffer) - 2, fmt, pCtx, params, &arg);
buffer[res++] = '\n'; buffer[res++] = '\n';
buffer[res] = '\0'; buffer[res] = '\0';

View File

@ -35,6 +35,7 @@
#include "common_logic.h" #include "common_logic.h"
#include "ShareSys.h" #include "ShareSys.h"
#include "PluginSys.h" #include "PluginSys.h"
#include "sprintf.h"
using namespace SourceHook; using namespace SourceHook;
@ -412,7 +413,7 @@ static cell_t FormatNativeString(IPluginContext *pContext, const cell_t *params)
size_t written; size_t written;
{ {
DetectExceptions eh(pContext); DetectExceptions eh(pContext);
written = smcore.atcprintf(output_buffer, maxlen, format_buffer, s_curcaller, s_curparams, &var_param); written = atcprintf(output_buffer, maxlen, format_buffer, s_curcaller, s_curparams, &var_param);
if (eh.HasException()) if (eh.HasException())
return 0; return 0;
} }

View File

@ -40,6 +40,7 @@
#include <ITranslator.h> #include <ITranslator.h>
#include "common_logic.h" #include "common_logic.h"
#include "Logger.h" #include "Logger.h"
#include "sprintf.h"
#include <am-utility.h> #include <am-utility.h>
#include "handle_helpers.h" #include "handle_helpers.h"
@ -774,7 +775,7 @@ static cell_t sm_WriteFileLine(IPluginContext *pContext, const cell_t *params)
char buffer[2048]; char buffer[2048];
{ {
DetectExceptions eh(pContext); DetectExceptions eh(pContext);
smcore.atcprintf(buffer, sizeof(buffer), fmt, pContext, params, &arg); atcprintf(buffer, sizeof(buffer), fmt, pContext, params, &arg);
if (eh.HasException()) if (eh.HasException())
return 0; return 0;
} }
@ -809,7 +810,7 @@ static cell_t sm_BuildPath(IPluginContext *pContext, const cell_t *params)
{ {
DetectExceptions eh(pContext); DetectExceptions eh(pContext);
smcore.atcprintf(path, sizeof(path), fmt, pContext, params, &arg); atcprintf(path, sizeof(path), fmt, pContext, params, &arg);
if (eh.HasException()) if (eh.HasException())
return 0; return 0;
} }

View File

@ -35,6 +35,7 @@
#include <ITextParsers.h> #include <ITextParsers.h>
#include <ctype.h> #include <ctype.h>
#include "stringutil.h" #include "stringutil.h"
#include "sprintf.h"
#include <am-string.h> #include <am-string.h>
inline const char *_strstr(const char *str, const char *substr) inline const char *_strstr(const char *str, const char *substr)
@ -188,7 +189,7 @@ static cell_t sm_formatex(IPluginContext *pCtx, const cell_t *params)
pCtx->LocalToString(params[1], &buf); pCtx->LocalToString(params[1], &buf);
pCtx->LocalToString(params[3], &fmt); pCtx->LocalToString(params[3], &fmt);
res = smcore.atcprintf(buf, static_cast<size_t>(params[2]), fmt, pCtx, params, &arg); res = atcprintf(buf, static_cast<size_t>(params[2]), fmt, pCtx, params, &arg);
return static_cast<cell_t>(res); return static_cast<cell_t>(res);
} }
@ -257,7 +258,7 @@ static cell_t sm_format(IPluginContext *pCtx, const cell_t *params)
} }
buf = (copy) ? __copy_buf : destbuf; buf = (copy) ? __copy_buf : destbuf;
res = smcore.atcprintf(buf, maxlen, fmt, pCtx, params, &arg); res = atcprintf(buf, maxlen, fmt, pCtx, params, &arg);
if (copy) if (copy)
{ {
@ -307,7 +308,7 @@ static cell_t sm_vformat(IPluginContext *pContext, const cell_t *params)
pContext->LocalToString(params[3], &format); pContext->LocalToString(params[3], &format);
size_t total = smcore.atcprintf(destination, maxlen, format, pContext, local_params, &vargPos); size_t total = atcprintf(destination, maxlen, format, pContext, local_params, &vargPos);
/* Perform copy-on-write if we need to */ /* Perform copy-on-write if we need to */
if (copy) if (copy)

1261
core/logic/sprintf.cpp Normal file

File diff suppressed because it is too large Load Diff

56
core/logic/sprintf.h Normal file
View File

@ -0,0 +1,56 @@
// 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_core_logic_sprintf_h_
#define _include_sourcemod_core_logic_sprintf_h_
#include <sp_vm_api.h>
// "AMX Templated Cell Printf", originally. SourceMod doesn't have cell-strings
// so this is a normal sprintf(), except that its variadic arguments are
// derived from scripted arguments.
size_t atcprintf(char *buffer,
size_t maxlen,
const char *format,
SourcePawn::IPluginContext *pCtx,
const cell_t *params,
int *param);
// "Generic Printf", originally. This is similar to atcprintf, except arguments
// are provided as an array of opaque pointers, rather than scripted arguments
// or C++ va_lists. This is essentially what Core uses to translate and format
// phrases internally.
bool gnprintf(char *buffer,
size_t maxlen,
const char *format,
IPhraseCollection *pPhrases,
void **params,
unsigned int numparams,
unsigned int &curparam,
size_t *pOutLength,
const char **pFailPhrase);
#endif // _include_sourcemod_core_logic_sprintf_h_

View File

@ -516,6 +516,33 @@ void do_global_plugin_loads()
g_SourceMod.DoGlobalPluginLoads(); g_SourceMod.DoGlobalPluginLoads();
} }
static bool describe_player(int index, const char **namep, const char **authp, int *useridp)
{
CPlayer *player = g_Players.GetPlayerByIndex(index);
if (!player || !player->IsConnected())
return false;
if (namep)
*namep = player->GetName();
if (authp) {
const char *auth = player->GetAuthString();
*authp = (auth && *auth) ? auth : "STEAM_ID_PENDING";
}
if (useridp)
*useridp = GetPlayerUserId(player->GetEdict());
return true;
}
static int get_max_clients()
{
return g_Players.MaxClients();
}
static int get_global_target()
{
return g_SourceMod.GetGlobalTarget();
}
#if defined METAMOD_PLAPI_VERSION #if defined METAMOD_PLAPI_VERSION
#if SOURCE_ENGINE == SE_LEFT4DEAD #if SOURCE_ENGINE == SE_LEFT4DEAD
#define GAMEFIX "2.l4d" #define GAMEFIX "2.l4d"
@ -587,8 +614,6 @@ static sm_core_t core_bridge =
conprint, conprint,
get_cvar_string, get_cvar_string,
get_cvar_bool, get_cvar_bool,
gnprintf,
atcprintf,
get_game_name, get_game_name,
get_game_description, get_game_description,
get_source_engine_name, get_source_engine_name,
@ -608,6 +633,9 @@ static sm_core_t core_bridge =
get_immunity_mode, get_immunity_mode,
update_admin_cmd_flags, update_admin_cmd_flags,
look_for_cmd_admin_flags, look_for_cmd_admin_flags,
describe_player,
get_max_clients,
get_global_target,
GAMEFIX, GAMEFIX,
&serverGlobals, &serverGlobals,
}; };

File diff suppressed because it is too large Load Diff

View File

@ -42,17 +42,7 @@ using namespace SourceMod;
#define IS_STR_FILLED(var) (var[0] != '\0') #define IS_STR_FILLED(var) (var[0] != '\0')
size_t atcprintf(char *buffer, size_t maxlen, const char *format, IPluginContext *pCtx, const cell_t *params, int *param);
unsigned int strncopy(char *dest, const char *src, size_t count); unsigned int strncopy(char *dest, const char *src, size_t count);
bool gnprintf(char *buffer,
size_t maxlen,
const char *format,
IPhraseCollection *pPhrases,
void **params,
unsigned int numparams,
unsigned int &curparam,
size_t *pOutLength,
const char **pFailPhrase);
size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...); size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...);
size_t UTIL_FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list ap); size_t UTIL_FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list ap);
char *sm_strdup(const char *str); char *sm_strdup(const char *str);

View File

@ -588,7 +588,7 @@ size_t SourceModBase::FormatString(char *buffer, size_t maxlength, IPluginContex
int lparam = ++param; int lparam = ++param;
return atcprintf(buffer, maxlength, fmt, pContext, params, &lparam); return logicore.atcprintf(buffer, maxlength, fmt, pContext, params, &lparam);
} }
const char *SourceModBase::GetSourceModPath() const const char *SourceModBase::GetSourceModPath() const