From 09c3e57b6d33447d9762836c9684322869ae7ce0 Mon Sep 17 00:00:00 2001 From: Robert MacGregor Date: Sat, 16 Aug 2014 03:45:03 -0400 Subject: [PATCH] Added sprintf --- Mod Sources/TSExtension/README.md | 6 +++ .../TSExtension/include/DXConCmds.h | 6 +-- .../TSExtension/source/BaseMod.cpp | 2 + .../TSExtension/source/DXConCmds.cpp | 44 +++++++++++++------ 4 files changed, 42 insertions(+), 16 deletions(-) diff --git a/Mod Sources/TSExtension/README.md b/Mod Sources/TSExtension/README.md index 07404cb..3fbe00d 100644 --- a/Mod Sources/TSExtension/README.md +++ b/Mod Sources/TSExtension/README.md @@ -17,3 +17,9 @@ This function did not exist before in Torque Script, it returns whether or not t #### Player.isJumping() This function did not exist before in Torque Script, it returns whether or not the Player is currently jumping. + +#### sprintf(format, ...) +This function did not exist before in Torque Script. It returns a formatted string according to format with all variable +arguments up to a total of twenty filled in. Refer to C's sprintf for more information. + +Note: Only %s should be used in the format as Torque Script passes all data around as strings. diff --git a/Mod Sources/TSExtension/TSExtension/include/DXConCmds.h b/Mod Sources/TSExtension/TSExtension/include/DXConCmds.h index 5e89f5a..15fdd30 100644 --- a/Mod Sources/TSExtension/TSExtension/include/DXConCmds.h +++ b/Mod Sources/TSExtension/TSExtension/include/DXConCmds.h @@ -25,8 +25,8 @@ bool conPlayerGetJettingState(SimObject *obj, S32 argc, const char* argv[]); const char* conGrenadeProjectileGetPosition(SimObject *obj, S32 argc, const char* argv[]); const char* conGrenadeProjectileGetVelocity(SimObject *obj, S32 argc, const char* argv[]); -// Projectile explode +// Projectile explode ------------------------------- bool conProjectileExplode(SimObject *obj, S32 argc, const char* argv[]); -// Tests Position Z -const char *conPlayerSetZ(SimObject *obj, S32 argc, const char *argv[]); \ No newline at end of file +// General Commands --------------------------------- +const char* conSprintf(SimObject *obj, S32 argc, const char* argv[]); \ No newline at end of file diff --git a/Mod Sources/TSExtension/TSExtension/source/BaseMod.cpp b/Mod Sources/TSExtension/TSExtension/source/BaseMod.cpp index 83bd0b7..b7a9173 100644 --- a/Mod Sources/TSExtension/TSExtension/source/BaseMod.cpp +++ b/Mod Sources/TSExtension/TSExtension/source/BaseMod.cpp @@ -18,5 +18,7 @@ extern "C" Con::addMethodS("GrenadeProjectile", "getposition", &conGrenadeProjectileGetPosition,"Accurately gets the position of the GrenadeProjectile", 2, 2); Con::addMethodS("GrenadeProjectile", "getvelocity", &conGrenadeProjectileGetVelocity,"Gets the velocity of the GrenadeProjectile", 2, 2); + + Con::addMethodS(NULL, "sprintf", &conSprintf,"Formats a string. See the C sprintf.", 2, 20); } } \ No newline at end of file diff --git a/Mod Sources/TSExtension/TSExtension/source/DXConCmds.cpp b/Mod Sources/TSExtension/TSExtension/source/DXConCmds.cpp index bdc8901..9bee9ac 100644 --- a/Mod Sources/TSExtension/TSExtension/source/DXConCmds.cpp +++ b/Mod Sources/TSExtension/TSExtension/source/DXConCmds.cpp @@ -1,7 +1,7 @@ /** * */ - + #include #include @@ -13,18 +13,6 @@ const char *conGetAddress(SimObject *obj, S32 argc, const char *argv[]) return result; } -const char *conPlayerSetZ(SimObject *obj, S32 argc, const char *argv[]) -{ - DX::Player test = DX::GetPlayerPointer(obj); - test.position_y = 100; - test.position_z = 300; - test.position_x = 500; - - char result[256]; - sprintf(result, "LOL"); - return result; -} - bool conPlayerGetJumpingState(SimObject *obj, S32 argc, const char* argv[]) { DX::Player operand = DX::GetPlayerPointer(obj); @@ -72,5 +60,35 @@ const char* conGrenadeProjectileGetVelocity(SimObject *obj, S32 argc, const char DX::GrenadeProjectile grenade = DX::GetGrenadeProjectilePointer(obj); sprintf_s<256>(result, "%f %f %f", grenade.velocity_x, grenade.velocity_y, grenade.velocity_z); + return result; +} + +// General Commands --------------------------------- +#include +#include +#include + + + +#define _crt_va_start(ap,v) ( ap = (va_list)_ADDRESSOF(v) + _INTSIZEOF(v) ) +#define _crt_va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) ) +#define _crt_va_end(ap) ( ap = (va_list)0 ) + +#define va_start _crt_va_start +#define va_arg _crt_va_arg +#define va_end _crt_va_end +const char* conSprintf(SimObject *obj, S32 argc, const char* argv[]) +{ + std::vector input; + for (unsigned int i = 2; i < argc; i++) + input.push_back(argv[i]); + + char result[256]; + + va_list variable_args = reinterpret_cast(input.data()); + //variable_args = (va_list)_ADDRESSOF(variable_args) + _INTSIZEOF(input.size() * 4); + vsprintf(result, argv[1], variable_args); + //va_end(variable_args); + return result; } \ No newline at end of file