diff --git a/core/logic/smn_string.cpp b/core/logic/smn_string.cpp index 76ff2e05..40de4252 100644 --- a/core/logic/smn_string.cpp +++ b/core/logic/smn_string.cpp @@ -139,6 +139,22 @@ static cell_t StringToIntEx(IPluginContext *pCtx, const cell_t *params) return dummy - str; } +static cell_t StringToInt64(IPluginContext *pCtx, const cell_t *params) +{ + char *str, *dummy = NULL; + cell_t *addr; + pCtx->LocalToString(params[1], &str); + pCtx->LocalToPhysAddr(params[2], &addr); + + // uint64_t for correct signed right shift. + uint64_t number = (uint64_t)strtoll(str, &dummy, params[3]); + + addr[0] = (cell_t)(number & 0xFFFFFFFFull); + addr[1] = (cell_t)(number >> 32ull); + + return dummy - str; +} + static cell_t sm_numtostr(IPluginContext *pCtx, const cell_t *params) { char *str; @@ -148,6 +164,17 @@ static cell_t sm_numtostr(IPluginContext *pCtx, const cell_t *params) return static_cast(res); } +static cell_t Int64ToString(IPluginContext *pCtx, const cell_t *params) +{ + cell_t *num; + char *str; + pCtx->LocalToPhysAddr(params[1], &num); + pCtx->LocalToString(params[2], &str); + size_t res = ke::SafeSprintf(str, params[3], "%" KE_FMT_I64, (int64_t)num[1] << 32ll | (int64_t)num[0]); + + return static_cast(res); +} + static cell_t sm_strtofloat(IPluginContext *pCtx, const cell_t *params) { char *str, *dummy; @@ -591,6 +618,7 @@ REGISTER_NATIVES(basicStrings) {"FormatEx", sm_formatex}, {"GetCharBytes", GetCharBytes}, {"IntToString", sm_numtostr}, + {"Int64ToString", Int64ToString}, {"IsCharAlpha", IsCharAlpha}, {"IsCharLower", IsCharLower}, {"IsCharMB", IsCharMB}, @@ -607,6 +635,7 @@ REGISTER_NATIVES(basicStrings) {"strcopy", sm_strcopy}, {"StringToInt", sm_strconvint}, {"StringToIntEx", StringToIntEx}, + {"StringToInt64", StringToInt64}, {"StringToFloat", sm_strtofloat}, {"StringToFloatEx", StringToFloatEx}, {"StripQuotes", StripQuotes}, diff --git a/plugins/include/string.inc b/plugins/include/string.inc index 6f3f0dd3..c64d30db 100644 --- a/plugins/include/string.inc +++ b/plugins/include/string.inc @@ -195,6 +195,17 @@ native int StringToInt(const char[] str, int nBase=10); */ native int StringToIntEx(const char[] str, int &result, int nBase=10); +/** + * Converts a string to a 64-bit integer. + * + * @param str String to convert. + * @param result Array to store the upper and lower + * 32-bits of the 64-bit integer. + * @param nBase Numerical base to use. 10 is default. + * @return Number of characters consumed. + */ +native int StringToInt64(const char[] str, int result[2], int nBase=10); + /** * Converts an integer to a string. * @@ -205,6 +216,18 @@ native int StringToIntEx(const char[] str, int &result, int nBase=10); */ native int IntToString(int num, char[] str, int maxlength); +/** + * Converts a 64-bit integer to a string. + * + * @param num Array containing the upper and lower + * 32-bits of a 64-bit integer. + * @param str Buffer to store string in. + * @param maxlength Maximum length of string buffer. + * @return Number of characters written to the buffer, + * not including the null terminator. + */ +native int Int64ToString(const int num[2], char[] str, int maxlength); + /** * Converts a string to a floating point number. *