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
This commit is contained in:
David Anderson 2006-12-30 07:23:17 +00:00
parent 17af34bba5
commit 388c062c96
14 changed files with 242 additions and 71 deletions

View File

@ -186,6 +186,10 @@
RelativePath="..\CTextParsers.cpp"
>
</File>
<File
RelativePath="..\sm_autonatives.cpp"
>
</File>
<File
RelativePath="..\sm_memtable.cpp"
>
@ -232,6 +236,10 @@
RelativePath="..\CTextParsers.h"
>
</File>
<File
RelativePath="..\sm_autonatives.h"
>
</File>
<File
RelativePath="..\sm_globals.h"
>
@ -479,6 +487,10 @@
<Filter
Name="Header Files"
>
<File
RelativePath="..\..\sourcepawn\include\sp_typeutil.h"
>
</File>
<File
RelativePath="..\vm\sp_vm_basecontext.h"
>

7
core/sm_autonatives.cpp Normal file
View File

@ -0,0 +1,7 @@
#include "sm_autonatives.h"
#include "PluginSys.h"
void CoreNativesToAdd::OnSourceModAllInitialized()
{
g_PluginSys.RegisterNativesFromCore(m_NativeList);
}

22
core/sm_autonatives.h Normal file
View File

@ -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_

View File

@ -50,4 +50,6 @@ private:
extern ISourcePawnEngine *g_pSourcePawn;
extern IVirtualMachine *g_pVM;
#include "sm_autonatives.h"
#endif //_INCLUDE_SOURCEMOD_GLOBALS_H_

View File

@ -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)
{

View File

@ -3,14 +3,12 @@
#include <math.h>
#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_

View File

@ -1,7 +1,7 @@
#include <math.h>
#include <string.h>
#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<float>(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]));
}

View File

@ -1,7 +1,7 @@
#include "sm_platform.h"
#include <string.h>
#include "sp_vm_api.h"
#include "sp_vm_typeutil.h"
#include <stdlib.h>
#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<cell_t>(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},
};

View File

@ -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);
}

View File

@ -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.

111
plugins/include/string.inc Normal file
View File

@ -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);

View File

@ -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
}

View File

@ -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;
}

View File

@ -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 */
/**