From 388c062c96c59c40b0a3eef45e32754b6d46acb1 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sat, 30 Dec 2006 07:23:17 +0000 Subject: [PATCH] added automatic native registration added string include file renamed typeutil and its functions redid a bunch of the string natives string natives now register --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40238 --- core/msvc8/sourcemod_mm.vcproj | 12 ++ core/sm_autonatives.cpp | 7 ++ core/sm_autonatives.h | 22 ++++ core/sm_globals.h | 2 + core/sm_stringutil.cpp | 4 +- core/sm_stringutil.h | 6 +- core/smn_float.cpp | 88 +++++++------- core/smn_string.cpp | 37 ++++-- core/systems/PluginSys.cpp | 5 + core/systems/PluginSys.h | 10 +- plugins/include/string.inc | 111 ++++++++++++++++++ plugins/test.sma | 3 +- .../{sp_vm_typeutil.h => sp_typeutil.h} | 4 +- sourcepawn/include/sp_vm_types.h | 2 + 14 files changed, 242 insertions(+), 71 deletions(-) create mode 100644 core/sm_autonatives.cpp create mode 100644 core/sm_autonatives.h create mode 100644 plugins/include/string.inc rename sourcepawn/include/{sp_vm_typeutil.h => sp_typeutil.h} (58%) diff --git a/core/msvc8/sourcemod_mm.vcproj b/core/msvc8/sourcemod_mm.vcproj index 9c5b1610..aaf946b4 100644 --- a/core/msvc8/sourcemod_mm.vcproj +++ b/core/msvc8/sourcemod_mm.vcproj @@ -186,6 +186,10 @@ RelativePath="..\CTextParsers.cpp" > + + @@ -232,6 +236,10 @@ RelativePath="..\CTextParsers.h" > + + @@ -479,6 +487,10 @@ + + diff --git a/core/sm_autonatives.cpp b/core/sm_autonatives.cpp new file mode 100644 index 00000000..370386a6 --- /dev/null +++ b/core/sm_autonatives.cpp @@ -0,0 +1,7 @@ +#include "sm_autonatives.h" +#include "PluginSys.h" + +void CoreNativesToAdd::OnSourceModAllInitialized() +{ + g_PluginSys.RegisterNativesFromCore(m_NativeList); +} diff --git a/core/sm_autonatives.h b/core/sm_autonatives.h new file mode 100644 index 00000000..8b3c06ad --- /dev/null +++ b/core/sm_autonatives.h @@ -0,0 +1,22 @@ +#ifndef _INCLUDE_SOURCEMOD_CORE_AUTONATIVES_H_ +#define _INCLUDE_SOURCEMOD_CORE_AUTONATIVES_H_ + +#include "sm_globals.h" + +#define REGISTER_NATIVES(globname) \ + extern sp_nativeinfo_t globNatives##globname[]; \ + CoreNativesToAdd globNativesCls##globname(globNatives##globname); \ + sp_nativeinfo_t globNatives##globname[] = + +class CoreNativesToAdd : public SMGlobalClass +{ +public: + CoreNativesToAdd(sp_nativeinfo_t *pList) + : m_NativeList(pList) + { + } + void OnSourceModAllInitialized(); + sp_nativeinfo_t *m_NativeList; +}; + +#endif //_INCLUDE_SOURCEMOD_CORE_AUTONATIVES_H_ diff --git a/core/sm_globals.h b/core/sm_globals.h index 8426b0bd..87c45daf 100644 --- a/core/sm_globals.h +++ b/core/sm_globals.h @@ -50,4 +50,6 @@ private: extern ISourcePawnEngine *g_pSourcePawn; extern IVirtualMachine *g_pVM; +#include "sm_autonatives.h" + #endif //_INCLUDE_SOURCEMOD_GLOBALS_H_ diff --git a/core/sm_stringutil.cpp b/core/sm_stringutil.cpp index adca1a57..00601603 100644 --- a/core/sm_stringutil.cpp +++ b/core/sm_stringutil.cpp @@ -374,7 +374,7 @@ reswitch: CHECK_ARGS(0); cell_t *value; pCtx->LocalToPhysAddr(params[arg], &value); - AddFloat(&buf_p, llen, ctof(*value), width, prec); + AddFloat(&buf_p, llen, sp_ctof(*value), width, prec); arg++; break; } @@ -467,7 +467,7 @@ inline float StrConvFloat(const char *str) return (float)strtod(str, &dummy); } -int strncopy(char *dest, const char *src, size_t count) +unsigned int strncopy(char *dest, const char *src, size_t count) { if (!count) { diff --git a/core/sm_stringutil.h b/core/sm_stringutil.h index 2ac314c0..5dce299f 100644 --- a/core/sm_stringutil.h +++ b/core/sm_stringutil.h @@ -3,14 +3,12 @@ #include #include "sp_vm_api.h" -#include "sp_vm_typeutil.h" +#include "sp_typeutil.h" using namespace SourcePawn; size_t atcprintf(char *buffer, size_t maxlen, const char *format, IPluginContext *pCtx, const cell_t *params, int *param); const char *stristr(const char *str, const char *substr); -int StrConvInt(const char *str); -float StrConvFloat(const char *str); -int strncopy(char *dest, const char *src, size_t count); +unsigned int strncopy(char *dest, const char *src, size_t count); #endif // _INCLUDE_SOURCEMOD_STRINGUTIL_H_ diff --git a/core/smn_float.cpp b/core/smn_float.cpp index 96a4d129..a76e25cd 100644 --- a/core/smn_float.cpp +++ b/core/smn_float.cpp @@ -1,7 +1,7 @@ #include #include #include "sp_vm_api.h" -#include "sp_vm_typeutil.h" +#include "sp_typeutil.h" using namespace SourcePawn; @@ -57,49 +57,49 @@ static cell_t sm_float(IPluginContext *pCtx, const cell_t *params) { float val = static_cast(params[1]); - return ftoc(val); + return sp_ftoc(val); } static cell_t sm_floatabs(IPluginContext *pCtx, const cell_t *params) { - float val = ctof(params[1]); + float val = sp_ctof(params[1]); val = (val >= 0) ? val : -val; - return ftoc(val); + return sp_ftoc(val); } static cell_t sm_floatadd(IPluginContext *pCtx, const cell_t *params) { - float val = ctof(params[1]) + ctof(params[2]); + float val = sp_ctof(params[1]) + sp_ctof(params[2]); - return ftoc(val); + return sp_ftoc(val); } static cell_t sm_floatsub(IPluginContext *pCtx, const cell_t *params) { - float val = ctof(params[1]) - ctof(params[2]); + float val = sp_ctof(params[1]) - sp_ctof(params[2]); - return ftoc(val); + return sp_ftoc(val); } static cell_t sm_floatmul(IPluginContext *pCtx, const cell_t *params) { - float val = ctof(params[1]) * ctof(params[2]); + float val = sp_ctof(params[1]) * sp_ctof(params[2]); - return ftoc(val); + return sp_ftoc(val); } static cell_t sm_floatdiv(IPluginContext *pCtx, const cell_t *params) { - float val = ctof(params[1]) / ctof(params[2]); + float val = sp_ctof(params[1]) / sp_ctof(params[2]); - return ftoc(val); + return sp_ftoc(val); } static cell_t sm_floatcmp(IPluginContext *pCtx, const cell_t *params) { - float val1 = ctof(params[1]); - float val2 = ctof(params[2]); + float val1 = sp_ctof(params[1]); + float val2 = sp_ctof(params[2]); if (val1 > val2) { @@ -113,8 +113,8 @@ static cell_t sm_floatcmp(IPluginContext *pCtx, const cell_t *params) static cell_t sm_floatlog(IPluginContext *pCtx, const cell_t *params) { - float val = ctof(params[1]); - float base = ctof(params[2]); + float val = sp_ctof(params[1]); + float base = sp_ctof(params[2]); if ((val <= 0) || (base <= 0)) { @@ -127,39 +127,39 @@ static cell_t sm_floatlog(IPluginContext *pCtx, const cell_t *params) val = log(val) / log(base); } - return ftoc(val); + return sp_ftoc(val); } static cell_t sm_floatexp(IPluginContext *pCtx, const cell_t *params) { - float val = ctof(params[1]); + float val = sp_ctof(params[1]); - return ftoc(exp(val)); + return sp_ftoc(exp(val)); } static cell_t sm_floatpower(IPluginContext *pCtx, const cell_t *params) { - float base = ctof(params[1]); - float exponent = ctof(params[2]); + float base = sp_ctof(params[1]); + float exponent = sp_ctof(params[2]); - return ftoc(pow(base, exponent)); + return sp_ftoc(pow(base, exponent)); } static cell_t sm_floatsqroot(IPluginContext *pCtx, const cell_t *params) { - float val = ctof(params[1]); + float val = sp_ctof(params[1]); if (val < 0.0) { //:TODO: error out! we dont support complex numbers } - return ftoc(sqrt(val)); + return sp_ftoc(sqrt(val)); } static cell_t sm_floatround(IPluginContext *pCtx, const cell_t *params) { - float val = ctof(params[1]); + float val = sp_ctof(params[1]); switch (params[2]) { @@ -190,7 +190,7 @@ static cell_t sm_floatround(IPluginContext *pCtx, const cell_t *params) } } - return ftoc(val); + return sp_ftoc(val); } static cell_t sm_floatstr(IPluginContext *pCtx, const cell_t *params) @@ -203,70 +203,70 @@ static cell_t sm_floatstr(IPluginContext *pCtx, const cell_t *params) return 0; } - return ftoc((float)atof(str)); + return sp_ftoc((float)atof(str)); } static cell_t sm_floatfract(IPluginContext *pCtx, const cell_t *params) { - float val = ctof(params[1]); + float val = sp_ctof(params[1]); val = val - floor(val); - return ftoc(val); + return sp_ftoc(val); } static cell_t sm_floatsin(IPluginContext *pCtx, const cell_t *params) { - float val = ctof(params[1]); + float val = sp_ctof(params[1]); val = sin(AngleToRadians(val, params[2])); - return ftoc(val); + return sp_ftoc(val); } static cell_t sm_floatcos(IPluginContext *pCtx, const cell_t *params) { - float val = ctof(params[1]); + float val = sp_ctof(params[1]); val = cos(AngleToRadians(val, params[2])); - return ftoc(val); + return sp_ftoc(val); } static cell_t sm_floattan(IPluginContext *pCtx, const cell_t *params) { - float val = ctof(params[1]); + float val = sp_ctof(params[1]); val = tan(AngleToRadians(val, params[2])); - return ftoc(val); + return sp_ftoc(val); } static cell_t sm_floatasin(IPluginContext *pCtx, const cell_t *params) { - float val = ctof(params[1]); + float val = sp_ctof(params[1]); val = asin(val); - return ftoc(RadiansToAngle(val, params[2])); + return sp_ftoc(RadiansToAngle(val, params[2])); } static cell_t sm_floatacos(IPluginContext *pCtx, const cell_t *params) { - float val = ctof(params[1]); + float val = sp_ctof(params[1]); val = acos(val); - return ftoc(RadiansToAngle(val, params[2])); + return sp_ftoc(RadiansToAngle(val, params[2])); } static cell_t sm_floatatan(IPluginContext *pCtx, const cell_t *params) { - float val = ctof(params[1]); + float val = sp_ctof(params[1]); val = atan(val); - return ftoc(RadiansToAngle(val, params[2])); + return sp_ftoc(RadiansToAngle(val, params[2])); } static cell_t sm_floatatan2(IPluginContext *pCtx, const cell_t *params) { - float val1 = ctof(params[1]); - float val2 = ctof(params[2]); + float val1 = sp_ctof(params[1]); + float val2 = sp_ctof(params[2]); val1 = atan2(val1, val2); - return ftoc(RadiansToAngle(val1, params[3])); + return sp_ftoc(RadiansToAngle(val1, params[3])); } diff --git a/core/smn_string.cpp b/core/smn_string.cpp index e41df049..e5dbfd23 100644 --- a/core/smn_string.cpp +++ b/core/smn_string.cpp @@ -1,7 +1,7 @@ #include "sm_platform.h" #include -#include "sp_vm_api.h" -#include "sp_vm_typeutil.h" +#include +#include "sm_globals.h" #include "sm_stringutil.h" using namespace SourcePawn; @@ -15,14 +15,12 @@ inline const char *_strstr(const char *str, const char *substr) #endif } - /********************************************* * * * STRING MANIPULATION NATIVE IMPLEMENTATIONS * * * *********************************************/ - static cell_t sm_strlen(IPluginContext *pCtx, const cell_t *params) { char *str; @@ -50,7 +48,7 @@ static cell_t sm_contain(IPluginContext *pCtx, const cell_t *params) return -1; } -static cell_t sm_equal(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_strcmp(IPluginContext *pCtx, const cell_t *params) { typedef int (*STRCOMPARE)(const char *, const char *); STRCOMPARE func; @@ -61,7 +59,7 @@ static cell_t sm_equal(IPluginContext *pCtx, const cell_t *params) func = (params[3]) ? strcmp : stricmp; - return (func(str1, str2)) ? 0 : 1; + return (func(str1, str2)); } static cell_t sm_strcopy(IPluginContext *pCtx, const cell_t *params) @@ -74,12 +72,12 @@ static cell_t sm_strcopy(IPluginContext *pCtx, const cell_t *params) return strncopy(dest, src, params[2]); } -static cell_t sm_strtonum(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_strconvint(IPluginContext *pCtx, const cell_t *params) { - char *str; + char *str, *dummy; pCtx->LocalToString(params[1], &str); - return StrConvInt(str); + return static_cast(strtol(str, &dummy, params[2])); } static cell_t sm_numtostr(IPluginContext *pCtx, const cell_t *params) @@ -92,10 +90,12 @@ static cell_t sm_numtostr(IPluginContext *pCtx, const cell_t *params) static cell_t sm_strtofloat(IPluginContext *pCtx, const cell_t *params) { - char *str; + char *str, *dummy; pCtx->LocalToString(params[1], &str); - return ftoc(StrConvFloat(str)); + float val = (float)strtod(str, &dummy); + + return sp_ftoc(val); } static cell_t sm_floattostr(IPluginContext *pCtx, const cell_t *params) @@ -103,5 +103,18 @@ static cell_t sm_floattostr(IPluginContext *pCtx, const cell_t *params) char *str; pCtx->LocalToString(params[2], &str); - return snprintf(str, params[3], "%f", ctof(params[1])); + return snprintf(str, params[3], "%f", sp_ctof(params[1])); } + +REGISTER_NATIVES(basicstrings) +{ + {"strlen", sm_strlen}, + {"StrContains", sm_contain}, + {"StrCompare", sm_strcmp}, + {"StrCopy", sm_strcopy}, + {"StringToInt", sm_strconvint}, + {"IntToString", sm_numtostr}, + {"StringToFloat", sm_strtofloat}, + {"FloatToString", sm_floattostr}, + {NULL, NULL}, +}; diff --git a/core/systems/PluginSys.cpp b/core/systems/PluginSys.cpp index 6524fac4..2f087967 100644 --- a/core/systems/PluginSys.cpp +++ b/core/systems/PluginSys.cpp @@ -1136,3 +1136,8 @@ void CPluginManager::OnHandleDestroy(HandleType_t type, void *object) { /* We don't care about the internal object, actually */ } + +void CPluginManager::RegisterNativesFromCore(sp_nativeinfo_t *natives) +{ + m_natives.push_back(natives); +} diff --git a/core/systems/PluginSys.h b/core/systems/PluginSys.h index 6f58880a..f5e5c462 100644 --- a/core/systems/PluginSys.h +++ b/core/systems/PluginSys.h @@ -227,15 +227,15 @@ public: */ bool TestAliasMatch(const char *alias, const char *localdir); - /** - * Registers natives in core itself ONLY. - */ - void RegisterGlobalNatives(sp_nativeinfo_t *info[]); - /** * Returns whether anything loaded will be a late load. */ bool IsLateLoadTime(); + + /** + * Adds natives from core into the native pool. + */ + void RegisterNativesFromCore(sp_nativeinfo_t *natives); private: /** * Recursively loads all plugins in the given directory. diff --git a/plugins/include/string.inc b/plugins/include/string.inc new file mode 100644 index 00000000..4a8e4e96 --- /dev/null +++ b/plugins/include/string.inc @@ -0,0 +1,111 @@ +/** + * :TODO: license info + */ + +#if defined _string_included + #endinput +#endif +#define _string_included + +/** + * @GLOBAL@ + * Unless otherwise noted, all string functions which take in a writable buffer and maximum length + * should have the null terminator INCLUDED in the length. This means that this is valid: + * StrCopy(string, sizeof(string), ...) + */ + +/** + * Calculates the length of a string. + * + * @param str String to check. + * @return Length of string, in cells (NOT characters). + */ +native strlen(const String:str[]); + +/** + * Tests whether a string is found inside another string. + * + * @param str String to search in. + * @param substr Substring to find inside the original string. + * @param caseSensitive If true (default), search is case sensitive. + * If false, search is case insensitive. + * @return -1 on failure (no match found). Any other value + * indicates a position in the string where the match starts. + */ +native StrContains(const String:str[], const String:substr[], bool:caseSensitive=true); + +/** + * Compares two strings lexographically. + * + * @param str1 First string (left). + * @param str2 Second string (right). + * @param caseSensitive If true (default), comparison is case sensitive. + * If false, comparison is case insensitive. + * @return -1 if str1 < str2 + * 0 if str1 == str2 + * 1 if str1 > str2 + */ +native StrCompare(const String:str1[], const String:str2[], bool:caseSensitive=true); + +/** + * Returns whether two strings are equal. + * + * + * @param str1 First string (left). + * @param str2 Second string (right). + * @param caseSensitive If true (default), comparison is case sensitive. + * If false, comparison is case insensitive. + * @return True if equal, false otherwise. + */ +stock StrEqual(const String:str1[], const String:str2[], bool:caseSensitive=true) +{ + return (StrCompare(str1, str2, caseSensitive)); +} + +/** + * Copies one string to another string. + * NOTE: If the destination buffer is too small to hold the source string, + * the destination will be truncated. + * + * @param dest Destination string buffer to copy to. + * @param destlen Destination buffer length (includes null terminator). + * @param source Source string buffer to copy from. + * @return Number of cells written. + */ +native StrCopy(String:dest[], destLen, const String:source[]); + +/** + * Converts a string to an integer. + * + * @param str String to convert. + * @param nBase Numerical base to use. 10 is default. + * @return Integer conversion of string, or 0 on failure. + */ +native StringToInt(const String:str[], nBase=10); + +/** + * Converts an integer to a string. + * + * @param str Buffer to store string in. + * @param maxlength Maximum length of string buffer. + * @param num Integer to convert. + * @return Number of cells written to buffer. + */ +native IntToString(String:str[], maxlength, num); + +/** + * Converts a string to a floating point number. + * + * @param str String to convert to a foat. + * @return Floating point result, or 0.0 on error. + */ +native Float:StringToFloat(const String:str[]); + +/** + * Converts a floating point number to a string. + * + * @param str Buffer to store string in. + * @param maxlength Maximum length of string buffer. + * @param num Floating point number to convert. + */ +native Float:FloatToString(String:str[], maxlength, Float:num); diff --git a/plugins/test.sma b/plugins/test.sma index af46f8c6..c3354368 100644 --- a/plugins/test.sma +++ b/plugins/test.sma @@ -24,6 +24,5 @@ copy(String:dest[], maxlength, const String:source[]) public bool:AskPluginLoad(Handle:myself, bool:late, String:error[], err_max) { - copy(error, err_max, "I don't like food anymore!") - return false + return true } diff --git a/sourcepawn/include/sp_vm_typeutil.h b/sourcepawn/include/sp_typeutil.h similarity index 58% rename from sourcepawn/include/sp_vm_typeutil.h rename to sourcepawn/include/sp_typeutil.h index 40987e4d..921f9f74 100644 --- a/sourcepawn/include/sp_vm_typeutil.h +++ b/sourcepawn/include/sp_typeutil.h @@ -3,11 +3,11 @@ #include "sp_vm_types.h" -inline cell_t ftoc(float val) +inline cell_t sp_ftoc(float val) { return *(cell_t *)&val; } -inline float ctof(cell_t val) +inline float sp_ctof(cell_t val) { return *(float *)&val; } diff --git a/sourcepawn/include/sp_vm_types.h b/sourcepawn/include/sp_vm_types.h index 3065915c..e2f94114 100644 --- a/sourcepawn/include/sp_vm_types.h +++ b/sourcepawn/include/sp_vm_types.h @@ -7,6 +7,8 @@ typedef uint32_t ucell_t; typedef int32_t cell_t; typedef uint32_t funcid_t; +#include "sp_typeutil.h" + #define SP_MAX_EXEC_PARAMS 32 /* Maximum number of parameters in a function */ /**