From 39d5563a8c543e11fb1232d9b70fffcc0c993bd5 Mon Sep 17 00:00:00 2001 From: Nathan Bowhay Date: Thu, 29 Jan 2015 11:40:45 -0800 Subject: [PATCH 01/14] Added type checking functions added some type checking functions to TorqueScript. --- Engine/source/console/consoleFunctions.cpp | 200 +++++++++++++++++++++ Engine/source/console/consoleFunctions.h | 16 ++ 2 files changed, 216 insertions(+) create mode 100644 Engine/source/console/consoleFunctions.h diff --git a/Engine/source/console/consoleFunctions.cpp b/Engine/source/console/consoleFunctions.cpp index a5ea49f33..8d54ae076 100644 --- a/Engine/source/console/consoleFunctions.cpp +++ b/Engine/source/console/consoleFunctions.cpp @@ -25,6 +25,11 @@ #include "console/consoleInternal.h" #include "console/engineAPI.h" #include "console/ast.h" + +#ifndef _CONSOLFUNCTIONS_H_ +#include "console/consoleFunctions.h" +#endif + #include "core/strings/findMatch.h" #include "core/strings/stringUnit.h" #include "core/strings/unicode.h" @@ -45,6 +50,132 @@ bool LinkConsoleFunctions = false; // Buffer for expanding script filenames. static char scriptFilenameBuffer[1024]; +bool isInt(const char* str) +{ + int len = dStrlen(str); + if(len <= 0) + return false; + + // Ingore whitespace + int start = 0; + for(int i = start; i < len; i++) + if(str[i] != ' ') + { + start = i; + break; + } + + for(int i = start; i < len; i++) + switch(str[i]) + { + case '+': case '-': + if(i != 0) + return false; + break; + case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0': + break; + case ' ': // ignore whitespace + for(int j = i+1; j < len; j++) + if(str[j] != ' ') + return false; + return true; + break; + default: + return false; + } + return true; +} + +bool isFloat(const char* str, bool sciOk = false) +{ + int len = dStrlen(str); + if(len <= 0) + return false; + + // Ingore whitespace + int start = 0; + for(int i = start; i < len; i++) + if(str[i] != ' ') + { + start = i; + break; + } + + bool seenDot = false; + int eLoc = -1; + for(int i = 0; i < len; i++) + switch(str[i]) + { + case '+': case '-': + if(sciOk) + { + //Haven't found e or scientific notation symbol + if(eLoc == -1) + { + //only allowed in beginning + if(i != 0) + return false; + } + else + { + //if not right after the e + if(i != (eLoc + 1)) + return false; + } + } + else + { + //only allowed in beginning + if(i != 0) + return false; + } + break; + case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0': + break; + case 'e': case 'E': + if(!sciOk) + return false; + else + { + //already saw it so can't have 2 + if(eLoc != -1) + return false; + + eLoc = i; + } + break; + case '.': + if(seenDot | (sciOk && eLoc != -1)) + return false; + seenDot = true; + break; + case ' ': // ignore whitespace + for(int j = i+1; j < len; j++) + if(str[j] != ' ') + return false; + return true; + break; + default: + return false; + } + return true; +} + +bool isValidIP(const char* ip) +{ + unsigned b1, b2, b3, b4; + unsigned char c; + int rc = dSscanf(ip, "%3u.%3u.%3u.%3u%c", &b1, &b2, &b3, &b4, &c); + if (rc != 4 && rc != 5) return false; + if ((b1 | b2 | b3 | b4) > 255) return false; + if (dStrspn(ip, "0123456789.") < dStrlen(ip)) return false; + return true; +} + +bool isValidPort(U16 port) +{ + return (port >= 0 && port <=65535); +} //============================================================================= // String Functions. @@ -815,6 +946,75 @@ DefineConsoleFunction( strrchrpos, S32, ( const char* str, const char* chr, S32 return index; } +//---------------------------------------------------------------- + +// Warning: isInt and isFloat are very 'strict' and might need to be adjusted to allow other values. //seanmc +DefineConsoleFunction( isInt, bool, ( const char* str),, + "Returns true if the string is an integer.\n" + "@param str The string to test.\n" + "@return true if @a str is an integer and false if not\n\n" + "@tsexample\n" + "isInt( \"13\" ) // Returns true.\n" + "@endtsexample\n" + "@ingroup Strings" ) +{ + return isInt(str); +} + +//---------------------------------------------------------------- + +DefineConsoleFunction( isFloat, bool, ( const char* str, bool sciOk), (false), + "Returns true if the string is a float.\n" + "@param str The string to test.\n" + "@param sciOk Test for correct scientific notation and accept it (ex. 1.2e+14)" + "@return true if @a str is a float and false if not\n\n" + "@tsexample\n" + "isFloat( \"13.5\" ) // Returns true.\n" + "@endtsexample\n" + "@ingroup Strings" ) +{ + return isFloat(str, sciOk); +} + +//---------------------------------------------------------------- + +DefineConsoleFunction( isValidPort, bool, ( const char* str),, + "Returns true if the string is a valid port number.\n" + "@param str The string to test.\n" + "@return true if @a str is a port and false if not\n\n" + "@tsexample\n" + "isValidPort( \"8080\" ) // Returns true.\n" + "@endtsexample\n" + "@ingroup Strings" ) +{ + if(isInt(str)) + { + U16 port = dAtous(str); + return isValidPort(port); + } + else + return false; +} + +//---------------------------------------------------------------- + +DefineConsoleFunction( isValidIP, bool, ( const char* str),, + "Returns true if the string is a valid ip address, excepts localhost.\n" + "@param str The string to test.\n" + "@return true if @a str is a valid ip address and false if not\n\n" + "@tsexample\n" + "isValidIP( \"localhost\" ) // Returns true.\n" + "@endtsexample\n" + "@ingroup Strings" ) +{ + if(dStrcmp(str, "localhost") == 0) + { + return true; + } + else + return isValidIP(str); +} + //============================================================================= // Field Manipulators. //============================================================================= diff --git a/Engine/source/console/consoleFunctions.h b/Engine/source/console/consoleFunctions.h new file mode 100644 index 000000000..1cb19d1df --- /dev/null +++ b/Engine/source/console/consoleFunctions.h @@ -0,0 +1,16 @@ +#ifndef _CONSOLFUNCTIONS_H_ +#define _CONSOLFUNCTIONS_H_ + +#ifndef _STRINGFUNCTIONS_H_ +#include "core/strings/stringFunctions.h" +#endif + +bool isInt(const char* str); + +bool isFloat(const char* str); + +bool isValidIP(const char* ip); + +bool isValidPort(U16 port); + +#endif \ No newline at end of file From 14037a742a41eba97b65f01cb9094c1e60291f19 Mon Sep 17 00:00:00 2001 From: Nathan Bowhay Date: Thu, 29 Jan 2015 11:48:15 -0800 Subject: [PATCH 02/14] Added string manipulation functions added some string manipulation functions, some are slightly different versions of existing functions. --- Engine/source/console/consoleFunctions.cpp | 97 ++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/Engine/source/console/consoleFunctions.cpp b/Engine/source/console/consoleFunctions.cpp index 8d54ae076..47a57ecdb 100644 --- a/Engine/source/console/consoleFunctions.cpp +++ b/Engine/source/console/consoleFunctions.cpp @@ -370,6 +370,40 @@ DefineConsoleFunction( strlen, S32, ( const char* str ),, return dStrlen( str ); } +//----------------------------------------------------------------------------- +DefineConsoleFunction( strlenskip, S32, ( const char* str, const char* first, const char* last ),, + "Calculate the length of a string in characters, skipping everything between and including first and last.\n" + "@param str A string.\n" + "@param first First character to look for to skip block of text.\n" + "@param last Second character to look for to skip block of text.\n" + "@return The length of the given string skipping blocks of text between characters.\n" + "@ingroup Strings" ) +{ + const UTF8* pos = str; + U32 size = 0; + U32 length = dStrlen(str); + bool count = true; + + //loop through each character counting each character, skipping tags (anything with < followed by >) + for(U32 i = 0; i < length; i++, pos++) + { + if(count) + { + if(*pos == first[0]) + count = false; + else + size++; + } + else + { + if(*pos == last[0]) + count = true; + } + } + + return S32(size); +} + //----------------------------------------------------------------------------- DefineConsoleFunction( strstr, S32, ( const char* string, const char* substring ),, @@ -416,6 +450,33 @@ DefineConsoleFunction( strpos, S32, ( const char* haystack, const char* needle, //----------------------------------------------------------------------------- +DefineConsoleFunction( strposr, S32, ( const char* haystack, const char* needle, S32 offset ), ( 0 ), + "Find the start of @a needle in @a haystack searching from right to left beginning at the given offset.\n" + "@param haystack The string to search.\n" + "@param needle The string to search for.\n" + "@return The index at which the first occurrence of @a needle was found in @a heystack or -1 if no match was found.\n\n" + "@tsexample\n" + "strposr( \"b ab\", \"b\", 1 ) // Returns 2.\n" + "@endtsexample\n" + "@ingroup Strings" ) +{ + U32 sublen = dStrlen( needle ); + U32 strlen = dStrlen( haystack ); + S32 start = strlen - offset; + + if(start < 0 || start > strlen) + return -1; + + if (start + sublen > strlen) + start = strlen - sublen; + for(; start >= 0; start--) + if(!dStrncmp(haystack + start, needle, sublen)) + return start; + return -1; +} + +//----------------------------------------------------------------------------- + DefineConsoleFunction( ltrim, const char*, ( const char* str ),, "Remove leading whitespace from the string.\n" "@param str A string.\n" @@ -762,6 +823,18 @@ DefineConsoleFunction( stripTrailingNumber, String, ( const char* str ),, return String::GetTrailingNumber( str, suffix ); } +//----------------------------------------------------------------------------- + +DefineConsoleFunction( getFirstNumber, String, ( const char* str ),, + "Get the first occuring number from @a str.\n" + "@param str The string from which to read out the first number.\n" + "@return String representation of the number or "" if no number.\n\n") +{ + U32 start; + U32 end; + return String::GetFirstNumber(str, start, end); +} + //---------------------------------------------------------------- DefineConsoleFunction( isspace, bool, ( const char* str, S32 index ),, @@ -948,6 +1021,30 @@ DefineConsoleFunction( strrchrpos, S32, ( const char* str, const char* chr, S32 //---------------------------------------------------------------- +DefineConsoleFunction( strToggleCaseToWords, const char*, ( const char* str ),, + "Parse a Toggle Case word into separate words.\n" + "@param str The string to parse.\n" + "@return new string space separated.\n\n" + "@tsexample\n" + "strToggleCaseToWords( \"HelloWorld\" ) // Returns \"Hello World\".\n" + "@endtsexample\n" + "@ingroup Strings" ) +{ + String newStr; + for(S32 i = 0; str[i]; i++) + { + //If capitol add a space + if(i != 0 && str[i] >= 65 && str[i] <= 90) + newStr += " "; + + newStr += str[i]; + } + + return Con::getReturnBuffer(newStr); +} + +//---------------------------------------------------------------- + // Warning: isInt and isFloat are very 'strict' and might need to be adjusted to allow other values. //seanmc DefineConsoleFunction( isInt, bool, ( const char* str),, "Returns true if the string is an integer.\n" From 2d7472d16067f1a03c3d76bfa05e6b7871042549 Mon Sep 17 00:00:00 2001 From: Nathan Bowhay Date: Thu, 29 Jan 2015 11:51:05 -0800 Subject: [PATCH 03/14] add case sensitive strings Added case sensitive strings function to add them to the string table. --- Engine/source/console/consoleFunctions.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Engine/source/console/consoleFunctions.cpp b/Engine/source/console/consoleFunctions.cpp index 47a57ecdb..f08ad2ac4 100644 --- a/Engine/source/console/consoleFunctions.cpp +++ b/Engine/source/console/consoleFunctions.cpp @@ -1112,6 +1112,17 @@ DefineConsoleFunction( isValidIP, bool, ( const char* str),, return isValidIP(str); } +//---------------------------------------------------------------- + +// Torque won't normally add another string if it already exists with another casing, +// so this forces the addition. It should be called once near the start, such as in main.cs. +ConsoleFunction(addCaseSensitiveStrings,void,2,0,"[string1, string2, ...]" + "Adds case sensitive strings to the StringTable.") +{ + for(int i = 1; i < argc; i++) + StringTable->insert(argv[i], true); +} + //============================================================================= // Field Manipulators. //============================================================================= From 6a5fd4eceb5e14209cebb303a7d2eae790a3b748 Mon Sep 17 00:00:00 2001 From: Nathan Bowhay Date: Thu, 29 Jan 2015 11:53:25 -0800 Subject: [PATCH 04/14] date number to string added month and week number to string console functions. --- Engine/source/console/consoleFunctions.cpp | 43 ++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/Engine/source/console/consoleFunctions.cpp b/Engine/source/console/consoleFunctions.cpp index f08ad2ac4..9fe5708fd 100644 --- a/Engine/source/console/consoleFunctions.cpp +++ b/Engine/source/console/consoleFunctions.cpp @@ -1233,6 +1233,49 @@ DefineConsoleFunction( getWordCount, S32, ( const char* text ),, //----------------------------------------------------------------------------- +DefineEngineFunction( monthNumToStr, String, ( S32 num, bool abbreviate ), (false), + "@brief returns month as a word given a number or \"\" if number is bad" + "@return month as a word given a number or \"\" if number is bad" + "@ingroup FileSystem") +{ + switch(num) + { + case 1: return abbreviate ? "Jan" : "January"; break; + case 2: return abbreviate ? "Feb" : "February"; break; + case 3: return abbreviate ? "Mar" : "March"; break; + case 4: return abbreviate ? "Apr" : "April"; break; + case 5: return "May"; break; + case 6: return abbreviate ? "Jun" : "June"; break; + case 7: return abbreviate ? "Jul" : "July"; break; + case 8: return abbreviate ? "Aug" : "August"; break; + case 9: return abbreviate ? "Sep" : "September"; break; + case 10: return abbreviate ? "Oct" : "October"; break; + case 11: return abbreviate ? "Nov" : "November"; break; + case 12: return abbreviate ? "Dec" : "December"; break; + default: return ""; + } +} + +DefineEngineFunction( weekdayNumToStr, String, ( S32 num, bool abbreviate ), (false), + "@brief returns weekday as a word given a number or \"\" if number is bad" + "@return weekday as a word given a number or \"\" if number is bad" + "@ingroup FileSystem") +{ + switch(num) + { + case 0: return abbreviate ? "Sun" : "Sunday"; break; + case 1: return abbreviate ? "Mon" : "Monday"; break; + case 2: return abbreviate ? "Tue" : "Tuesday"; break; + case 3: return abbreviate ? "Wed" : "Wednesday"; break; + case 4: return abbreviate ? "Thu" : "Thursday"; break; + case 5: return abbreviate ? "Fri" : "Friday"; break; + case 6: return abbreviate ? "Sat" : "Saturday"; break; + default: return ""; + } +} + +//----------------------------------------------------------------------------- + DefineConsoleFunction( getField, const char*, ( const char* text, S32 index ),, "Extract the field at the given @a index in the newline and/or tab separated list in @a text.\n" "Fields in @a text must be separated by newlines and/or tabs.\n" From 55b91606e6d6fce1805bf341bbc042025aba4dd6 Mon Sep 17 00:00:00 2001 From: Nathan Bowhay Date: Thu, 29 Jan 2015 11:54:44 -0800 Subject: [PATCH 05/14] Added more token functions Added a bunch more token functions and added comments to word equivalents to let you know about the token version. --- Engine/source/console/consoleFunctions.cpp | 113 +++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/Engine/source/console/consoleFunctions.cpp b/Engine/source/console/consoleFunctions.cpp index 9fe5708fd..f7680a82c 100644 --- a/Engine/source/console/consoleFunctions.cpp +++ b/Engine/source/console/consoleFunctions.cpp @@ -1141,6 +1141,7 @@ DefineConsoleFunction( getWord, const char*, ( const char* text, S32 index ),, "@endtsexample\n\n" "@see getWords\n" "@see getWordCount\n" + "@see getToken\n" "@see getField\n" "@see getRecord\n" "@ingroup FieldManip" ) @@ -1164,6 +1165,7 @@ DefineConsoleFunction( getWords, const char*, ( const char* text, S32 startIndex "@endtsexample\n\n" "@see getWord\n" "@see getWordCount\n" + "@see getTokens\n" "@see getFields\n" "@see getRecords\n" "@ingroup FieldManip" ) @@ -1188,6 +1190,7 @@ DefineConsoleFunction( setWord, const char*, ( const char* text, S32 index, cons "setWord( \"a b c d\", 2, \"f\" ) // Returns \"a b f d\"\n" "@endtsexample\n\n" "@see getWord\n" + "@see setToken\n" "@see setField\n" "@see setRecord\n" "@ingroup FieldManip" ) @@ -1207,6 +1210,7 @@ DefineConsoleFunction( removeWord, const char*, ( const char* text, S32 index ), "@tsexample\n" "removeWord( \"a b c d\", 2 ) // Returns \"a b d\"\n" "@endtsexample\n\n" + "@see removeToken\n" "@see removeField\n" "@see removeRecord\n" "@ingroup FieldManip" ) @@ -1224,6 +1228,7 @@ DefineConsoleFunction( getWordCount, S32, ( const char* text ),, "@tsexample\n" "getWordCount( \"a b c d e\" ) // Returns 5\n" "@endtsexample\n\n" + "@see getTokenCount\n" "@see getFieldCount\n" "@see getRecordCount\n" "@ingroup FieldManip" ) @@ -1597,6 +1602,114 @@ DefineConsoleFunction( nextToken, const char*, ( const char* str1, const char* t return ret; } +//----------------------------------------------------------------------------- + +DefineConsoleFunction( getToken, const char*, ( const char* text, const char* delimiters, S32 index ),, + "Extract the substring at the given @a index in the @a delimiters separated list in @a text.\n" + "@param text A @a delimiters list of substrings.\n" + "@param delimiters Character or characters that separate the list of substrings in @a text.\n" + "@param index The zero-based index of the substring to extract.\n" + "@return The substring at the given index or \"\" if the index is out of range.\n\n" + "@tsexample\n" + "getToken( \"a b c d\", \" \", 2 ) // Returns \"c\"\n" + "@endtsexample\n\n" + "@see getTokens\n" + "@see getTokenCount\n" + "@see getWord\n" + "@see getField\n" + "@see getRecord\n" + "@ingroup FieldManip" ) +{ + return Con::getReturnBuffer( StringUnit::getUnit(text, index, delimiters)); +} + +//----------------------------------------------------------------------------- + +DefineConsoleFunction( getTokens, const char*, ( const char* text, const char* delimiters, S32 startIndex, S32 endIndex ), ( -1 ), + "Extract a range of substrings separated by @a delimiters at the given @a startIndex onwards thru @a endIndex.\n" + "@param text A @a delimiters list of substrings.\n" + "@param delimiters Character or characters that separate the list of substrings in @a text.\n" + "@param startIndex The zero-based index of the first substring to extract from @a text.\n" + "@param endIndex The zero-based index of the last substring to extract from @a text. If this is -1, all words beginning " + "with @a startIndex are extracted from @a text.\n" + "@return A string containing the specified range of substrings from @a text or \"\" if @a startIndex " + "is out of range or greater than @a endIndex.\n\n" + "@tsexample\n" + "getTokens( \"a b c d\", \" \", 1, 2, ) // Returns \"b c\"\n" + "@endtsexample\n\n" + "@see getToken\n" + "@see getTokenCount\n" + "@see getWords\n" + "@see getFields\n" + "@see getRecords\n" + "@ingroup FieldManip" ) +{ + if( endIndex < 0 ) + endIndex = 1000000; + + return Con::getReturnBuffer( StringUnit::getUnits( text, startIndex, endIndex, delimiters ) ); +} + +//----------------------------------------------------------------------------- + +DefineConsoleFunction( setToken, const char*, ( const char* text, const char* delimiters, S32 index, const char* replacement ),, + "Replace the substring in @a text separated by @a delimiters at the given @a index with @a replacement.\n" + "@param text A @a delimiters list of substrings.\n" + "@param delimiters Character or characters that separate the list of substrings in @a text.\n" + "@param index The zero-based index of the substring to replace.\n" + "@param replacement The string with which to replace the substring.\n" + "@return A new string with the substring at the given @a index replaced by @a replacement or the original " + "string if @a index is out of range.\n\n" + "@tsexample\n" + "setToken( \"a b c d\", \" \", 2, \"f\" ) // Returns \"a b f d\"\n" + "@endtsexample\n\n" + "@see getToken\n" + "@see setWord\n" + "@see setField\n" + "@see setRecord\n" + "@ingroup FieldManip" ) +{ + return Con::getReturnBuffer( StringUnit::setUnit( text, index, replacement, delimiters) ); +} + +//----------------------------------------------------------------------------- + +DefineConsoleFunction( removeToken, const char*, ( const char* text, const char* delimiters, S32 index ),, + "Remove the substring in @a text separated by @a delimiters at the given @a index.\n" + "@param text A @a delimiters list of substrings.\n" + "@param delimiters Character or characters that separate the list of substrings in @a text.\n" + "@param index The zero-based index of the word in @a text.\n" + "@return A new string with the substring at the given index removed or the original string if @a index is " + "out of range.\n\n" + "@tsexample\n" + "removeToken( \"a b c d\", \" \", 2 ) // Returns \"a b d\"\n" + "@endtsexample\n\n" + "@see removeWord\n" + "@see removeField\n" + "@see removeRecord\n" + "@ingroup FieldManip" ) +{ + return Con::getReturnBuffer( StringUnit::removeUnit( text, index, delimiters ) ); +} + +//----------------------------------------------------------------------------- + +DefineConsoleFunction( getTokenCount, S32, ( const char* text, const char* delimiters),, + "Return the number of @a delimiters substrings in @a text.\n" + "@param text A @a delimiters list of substrings.\n" + "@param delimiters Character or characters that separate the list of substrings in @a text.\n" + "@return The number of @a delimiters substrings in @a text.\n\n" + "@tsexample\n" + "getTokenCount( \"a b c d e\", \" \" ) // Returns 5\n" + "@endtsexample\n\n" + "@see getWordCount\n" + "@see getFieldCount\n" + "@see getRecordCount\n" + "@ingroup FieldManip" ) +{ + return StringUnit::getUnitCount( text, delimiters ); +} + //============================================================================= // Tagged Strings. //============================================================================= From 5cfcb0cd4547a183f5f5924a3cc9c0f7dc19f8a4 Mon Sep 17 00:00:00 2001 From: Nathan Bowhay Date: Thu, 29 Jan 2015 11:59:06 -0800 Subject: [PATCH 06/14] fixed comment added path param documentation to display splash window. --- Engine/source/console/consoleFunctions.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/Engine/source/console/consoleFunctions.cpp b/Engine/source/console/consoleFunctions.cpp index f7680a82c..c15c6c944 100644 --- a/Engine/source/console/consoleFunctions.cpp +++ b/Engine/source/console/consoleFunctions.cpp @@ -2062,6 +2062,7 @@ DefineEngineFunction( gotoWebPage, void, ( const char* address ),, DefineEngineFunction( displaySplashWindow, bool, (const char* path), ("art/gui/splash.bmp"), "Display a startup splash window suitable for showing while the engine still starts up.\n\n" "@note This is currently only implemented on Windows.\n\n" + "@param path relative path to splash screen image to display.\n" "@return True if the splash window could be successfully initialized.\n\n" "@ingroup Platform" ) { From df2ca75b13d3edc9a3fa180ce0c96b30675005a9 Mon Sep 17 00:00:00 2001 From: Nathan Bowhay Date: Thu, 29 Jan 2015 12:02:40 -0800 Subject: [PATCH 07/14] get max dynamic verts in script you can now get the max dynamic vertices in script. --- Engine/source/console/consoleFunctions.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Engine/source/console/consoleFunctions.cpp b/Engine/source/console/consoleFunctions.cpp index c15c6c944..1d5403818 100644 --- a/Engine/source/console/consoleFunctions.cpp +++ b/Engine/source/console/consoleFunctions.cpp @@ -37,6 +37,7 @@ #include "console/compiler.h" #include "platform/platformInput.h" #include "core/util/journal/journal.h" +#include "gfx/gfxEnums.h" #include "core/util/uuid.h" #ifdef TORQUE_DEMO_PURCHASE @@ -3065,3 +3066,10 @@ DefineEngineFunction( isToolBuild, bool, (),, return false; #endif } + +DefineEngineFunction( getMaxDynamicVerts, S32, (),, + "Get max number of allowable dynamic vertices in a single vertex buffer.\n\n" + "@return the max number of allowable dynamic vertices in a single vertex buffer" ) +{ + return MAX_DYNAMIC_VERTS / 2; +} \ No newline at end of file From c98e95e6ff9f0a22adee57544805701a81b71c39 Mon Sep 17 00:00:00 2001 From: Nathan Bowhay Date: Thu, 29 Jan 2015 15:09:41 -0800 Subject: [PATCH 08/14] Forgot supporting method Added string manipulation functions upload, requires these changes to compile. --- Engine/source/core/util/str.cpp | 100 ++++++++++++++++++++++++++++++++ Engine/source/core/util/str.h | 1 + 2 files changed, 101 insertions(+) diff --git a/Engine/source/core/util/str.cpp b/Engine/source/core/util/str.cpp index 3f4f3d8f9..59fd0ab46 100644 --- a/Engine/source/core/util/str.cpp +++ b/Engine/source/core/util/str.cpp @@ -1624,3 +1624,103 @@ String String::GetTrailingNumber(const char* str, S32& number) return base.substr(0, p - base.c_str()); } + +String String::GetFirstNumber(const char* str, U32& startPos, U32& endPos) +{ + // Check for trivial strings + if (!str || !str[0]) + return String::EmptyString; + + // Find the number at the end of the string + String base(str); + const char* p = base.c_str(); + const char* end = base.c_str() + base.length() - 1; + bool dec = false; + startPos = 0; + + //Check if we are just a digit + if(p == end && isdigit(*p)) + return base; + + //Look for the first digit + while ((p != end) && (dIsspace(*p) || !isdigit(*p))) + { + p++; + startPos++; + } + + //Handle if we are at the end and found nothing + if(p == end && !isdigit(*p)) + return ""; + + //update our end position at least to the start of our number + endPos = startPos; + + //Backup our ptr + const char* backup = p; + + //Check for any negative or decimal values + if(startPos > 0) + { + p--; + startPos--; + if(*p == '.') + { + dec = true; + + //ignore any duplicate periods + while ((p != base.c_str()) && (*p == '.')) + { + p--; + startPos--; + } + + //Found a decimal lets still check for negative sign + if(startPos > 0) + { + p--; + startPos--; + if((*p != '-') && (*p != '_')) + { + startPos++; + p++; + } + } + } + else if((*p != '-') && (*p != '_')) + { + //go back to where we where cause no decimal or negative sign found + startPos++; + p++; + } + } + + //Restore where we were + p = backup; + + //look for the end of the digits + bool justFoundDec = false; + while (p != end) + { + if(*p == '.') + { + if(dec && !justFoundDec) + break; + else + { + dec = true; + justFoundDec = true; + } + } + else if(!isdigit(*p)) + break; + else if(justFoundDec) + justFoundDec = false; + + p++; + endPos++; + } + + U32 len = (!isdigit(*p)) ? endPos - startPos : (endPos + 1) - startPos; + return base.substr(startPos, len); +} diff --git a/Engine/source/core/util/str.h b/Engine/source/core/util/str.h index 009484451..33fdb819d 100644 --- a/Engine/source/core/util/str.h +++ b/Engine/source/core/util/str.h @@ -190,6 +190,7 @@ public: static String ToUpper(const String &string); static String GetTrailingNumber(const char* str, S32& number); + static String GetFirstNumber(const char* str, U32& startPos, U32& endPos); /// @} From 2efe1a9c0a0b420a10742235c7bdcfe7b599cbc1 Mon Sep 17 00:00:00 2001 From: Nathan Bowhay Date: Tue, 3 Feb 2015 10:59:32 -0800 Subject: [PATCH 09/14] Added More Vector math functions Added VectorMul (vector multiply), VectorDiv (vector divide, and VectorMidPoint to find the midpoint of two vectors. --- Engine/source/math/mathTypes.cpp | 88 ++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/Engine/source/math/mathTypes.cpp b/Engine/source/math/mathTypes.cpp index ce0f198d1..807e3bc60 100644 --- a/Engine/source/math/mathTypes.cpp +++ b/Engine/source/math/mathTypes.cpp @@ -659,6 +659,66 @@ DefineConsoleFunction( VectorScale, VectorF, ( VectorF a, F32 scalar ),, { return a * scalar; } +DefineConsoleFunction( VectorMul, VectorF, ( VectorF a, VectorF b ),, + "Multiplies two vectors.\n" + "@param a The first vector.\n" + "@param b The second vector.\n" + "@return The vector @a a * @a b.\n\n" + "@tsexample\n" + "//-----------------------------------------------------------------------------\n" + "//\n" + "// VectorMul( %a, %b );\n" + "//\n" + "// The multiplication of vector a, (ax, ay, az), and vector b, (bx, by, bz) is:\n" + "//\n" + "// a * b = ( ax * bx, ay * by, az * bz )\n" + "//\n" + "//-----------------------------------------------------------------------------\n\n" + + "%a = \"1 0 0\";\n" + "%b = \"0 1 0\";\n\n" + + "// %r = \"( 1 * 0, 0 * 1, 0 * 0 )\";\n" + "// %r = \"0 0 0\";\n" + "%r = VectorMul( %a, %b );\n" + "@endtsexample\n\n" + "@ingroup Vectors" ) +{ + return a * b; +} + +DefineConsoleFunction( VectorDiv, VectorF, ( VectorF a, VectorF b ),, + "Divide two vectors.\n" + "@param a The first vector.\n" + "@param b The second vector.\n" + "@return The vector @a a / @a b.\n\n" + "@tsexample\n" + "//-----------------------------------------------------------------------------\n" + "//\n" + "// VectorDiv( %a, %b );\n" + "//\n" + "// The division of vector a, (ax, ay, az), and vector b, (bx, by, bz) is:\n" + "//\n" + "// a * b = ( ax / bx, ay / by, az / bz )\n" + "//\n" + "//-----------------------------------------------------------------------------\n\n" + + "%a = \"1 1 1\";\n" + "%b = \"2 2 2\";\n\n" + + "// %r = \"( 1 / 2, 1 / 2, 1 / 2 )\";\n" + "// %r = \"0.5 0.5 0.5\";\n" + "%r = VectorDiv( %a, %b );\n" + "@endtsexample\n\n" + "@ingroup Vectors" ) +{ + //this is kind of bad, but so is dividing by 0 + if(b.x == 0) b.x = 0.000001f; + if(b.y == 0) b.y = 0.000001f; + if(b.z == 0) b.z = 0.000001f; + + return a / b; +} //----------------------------------------------------------------------------- @@ -789,6 +849,34 @@ DefineConsoleFunction( VectorDist, F32, ( VectorF a, VectorF b ),, //----------------------------------------------------------------------------- +DefineConsoleFunction( VectorMidPoint, VectorF, ( VectorF a, VectorF b ),, + "Gets the midpoint between the two vectors.\n" + "@param a The first vector.\n" + "@param b The second vector.\n" + "@return The vector (@a a + @a b) / 2.\n\n" + "@tsexample\n" + "//-----------------------------------------------------------------------------\n" + "//\n" + "// VectorMidPoint( %a, %b );\n" + "//\n" + "// The midpoint of vector a, (ax, ay, az), and vector b, (bx, by, bz) is:\n" + "//\n" + "// (a + b)/2 = ( (ax + bx) /2, ay + by) /2, (az + bz) /2 )\n" + "//\n" + "//-----------------------------------------------------------------------------\n" +// "%a = \"1 0 0\";\n" +// "%b = \"0 1 0\";\n\n" +// "// %r = \"( 1 + 0, 0 + 1, 0 + 0 )\";\n" +// "// %r = \"1 1 0\";\n" +// "%r = VectorAdd( %a, %b );\n" + "@endtsexample\n\n" + "@ingroup Vectors") +{ + return (a + b)/2.0f; +} + +//----------------------------------------------------------------------------- + DefineConsoleFunction( VectorLen, F32, ( VectorF v ),, "Calculate the magnitude of the given vector.\n" "@param v A vector.\n" From 686b9fced931833fc30e043472e92a6709cdccbf Mon Sep 17 00:00:00 2001 From: Nathan Bowhay Date: Tue, 3 Feb 2015 11:52:06 -0800 Subject: [PATCH 10/14] Round function can now round to number of digits Changed round function to support optional n parameter to round to that many digits. --- Engine/source/math/mConsoleFunctions.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Engine/source/math/mConsoleFunctions.cpp b/Engine/source/math/mConsoleFunctions.cpp index 1a11fe23e..f9b352c28 100644 --- a/Engine/source/math/mConsoleFunctions.cpp +++ b/Engine/source/math/mConsoleFunctions.cpp @@ -94,13 +94,18 @@ DefineConsoleFunction( mFloor, S32, ( F32 v ),, return (S32)mFloor( v ); } -DefineConsoleFunction( mRound, S32, ( F32 v ),, - "Round v to the nth decimal place or the nearest whole number by default." - "@param v Value to roundn" - "@return The rounded value as a S32." - "@ingroup Math" ) + +DefineConsoleFunction( mRound, F32, ( F32 v, S32 n ), (0), + "Round v to the nth decimal place or the nearest whole number by default." + "@param v Value to round\n" + "@param n Number of decimal places to round to, 0 by default\n" + "@return The rounded value as a S32." + "@ingroup Math" ) { - return mRound(v); + if(n <= 0) + return mRound(v); + else + return mRound(v, n); } DefineConsoleFunction( mCeil, S32, ( F32 v ),, From 7ef3a6495784c19824fbb0e414a54dd2ba54f761 Mon Sep 17 00:00:00 2001 From: Nathan Bowhay Date: Thu, 5 Feb 2015 11:28:19 -0800 Subject: [PATCH 11/14] Added getLocalTime console function Add getLocalTime console function so that script has access to the local time. --- Engine/source/app/game.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Engine/source/app/game.cpp b/Engine/source/app/game.cpp index 4579b4819..2f0e30c77 100644 --- a/Engine/source/app/game.cpp +++ b/Engine/source/app/game.cpp @@ -202,6 +202,26 @@ DefineConsoleFunction( getRealTime, S32, (), , "()" return Platform::getRealMilliseconds(); } +ConsoleFunction( getLocalTime, const char *, 1, 1, "Return the current local time as: weekday month day year hour min sec.\n\n" + "Local time is platform defined.") +{ + Platform::LocalTime lt; + Platform::getLocalTime(lt); + + static const U32 bufSize = 128; + char *retBuffer = Con::getReturnBuffer(bufSize); + dSprintf(retBuffer, bufSize, "%d %d %d %d %02d %02d %02d", + lt.weekday, + lt.month + 1, + lt.monthday, + lt.year + 1900, + lt.hour, + lt.min, + lt.sec); + + return retBuffer; +} + ConsoleFunctionGroupEnd(Platform); //----------------------------------------------------------------------------- From 02f859c150d69e6339a59bd41a6035115a66cdb7 Mon Sep 17 00:00:00 2001 From: Nathan Bowhay Date: Wed, 11 Feb 2015 10:53:34 -0800 Subject: [PATCH 12/14] Fixed spacing to fit GG standards. Fixed tabs to 3 spaces. --- Engine/source/console/consoleFunctions.cpp | 364 ++++++++++----------- Engine/source/core/util/str.cpp | 166 +++++----- Engine/source/math/mConsoleFunctions.cpp | 18 +- 3 files changed, 274 insertions(+), 274 deletions(-) diff --git a/Engine/source/console/consoleFunctions.cpp b/Engine/source/console/consoleFunctions.cpp index 1d5403818..4c12f2550 100644 --- a/Engine/source/console/consoleFunctions.cpp +++ b/Engine/source/console/consoleFunctions.cpp @@ -53,129 +53,129 @@ static char scriptFilenameBuffer[1024]; bool isInt(const char* str) { - int len = dStrlen(str); - if(len <= 0) - return false; + int len = dStrlen(str); + if(len <= 0) + return false; - // Ingore whitespace - int start = 0; - for(int i = start; i < len; i++) - if(str[i] != ' ') - { - start = i; - break; - } + // Ignore whitespace + int start = 0; + for(int i = start; i < len; i++) + if(str[i] != ' ') + { + start = i; + break; + } - for(int i = start; i < len; i++) - switch(str[i]) - { - case '+': case '-': - if(i != 0) - return false; - break; - case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0': - break; - case ' ': // ignore whitespace - for(int j = i+1; j < len; j++) - if(str[j] != ' ') - return false; - return true; - break; - default: - return false; - } - return true; + for(int i = start; i < len; i++) + switch(str[i]) + { + case '+': case '-': + if(i != 0) + return false; + break; + case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0': + break; + case ' ': // ignore whitespace + for(int j = i+1; j < len; j++) + if(str[j] != ' ') + return false; + return true; + break; + default: + return false; + } + return true; } bool isFloat(const char* str, bool sciOk = false) { - int len = dStrlen(str); - if(len <= 0) - return false; + int len = dStrlen(str); + if(len <= 0) + return false; - // Ingore whitespace - int start = 0; - for(int i = start; i < len; i++) - if(str[i] != ' ') - { - start = i; - break; - } + // Ingore whitespace + int start = 0; + for(int i = start; i < len; i++) + if(str[i] != ' ') + { + start = i; + break; + } - bool seenDot = false; - int eLoc = -1; - for(int i = 0; i < len; i++) - switch(str[i]) - { - case '+': case '-': - if(sciOk) - { - //Haven't found e or scientific notation symbol - if(eLoc == -1) - { - //only allowed in beginning - if(i != 0) - return false; - } - else - { - //if not right after the e - if(i != (eLoc + 1)) - return false; - } - } - else - { - //only allowed in beginning - if(i != 0) - return false; - } - break; - case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0': - break; - case 'e': case 'E': - if(!sciOk) - return false; - else - { - //already saw it so can't have 2 - if(eLoc != -1) - return false; + bool seenDot = false; + int eLoc = -1; + for(int i = 0; i < len; i++) + switch(str[i]) + { + case '+': case '-': + if(sciOk) + { + //Haven't found e or scientific notation symbol + if(eLoc == -1) + { + //only allowed in beginning + if(i != 0) + return false; + } + else + { + //if not right after the e + if(i != (eLoc + 1)) + return false; + } + } + else + { + //only allowed in beginning + if(i != 0) + return false; + } + break; + case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0': + break; + case 'e': case 'E': + if(!sciOk) + return false; + else + { + //already saw it so can't have 2 + if(eLoc != -1) + return false; - eLoc = i; - } - break; - case '.': - if(seenDot | (sciOk && eLoc != -1)) - return false; - seenDot = true; - break; - case ' ': // ignore whitespace - for(int j = i+1; j < len; j++) - if(str[j] != ' ') - return false; - return true; - break; - default: - return false; - } - return true; + eLoc = i; + } + break; + case '.': + if(seenDot | (sciOk && eLoc != -1)) + return false; + seenDot = true; + break; + case ' ': // ignore whitespace + for(int j = i+1; j < len; j++) + if(str[j] != ' ') + return false; + return true; + break; + default: + return false; + } + return true; } bool isValidIP(const char* ip) { - unsigned b1, b2, b3, b4; - unsigned char c; - int rc = dSscanf(ip, "%3u.%3u.%3u.%3u%c", &b1, &b2, &b3, &b4, &c); - if (rc != 4 && rc != 5) return false; - if ((b1 | b2 | b3 | b4) > 255) return false; - if (dStrspn(ip, "0123456789.") < dStrlen(ip)) return false; - return true; + unsigned b1, b2, b3, b4; + unsigned char c; + int rc = dSscanf(ip, "%3u.%3u.%3u.%3u%c", &b1, &b2, &b3, &b4, &c); + if (rc != 4 && rc != 5) return false; + if ((b1 | b2 | b3 | b4) > 255) return false; + if (dStrspn(ip, "0123456789.") < dStrlen(ip)) return false; + return true; } bool isValidPort(U16 port) { - return (port >= 0 && port <=65535); + return (port >= 0 && port <=65535); } //============================================================================= @@ -380,29 +380,29 @@ DefineConsoleFunction( strlenskip, S32, ( const char* str, const char* first, co "@return The length of the given string skipping blocks of text between characters.\n" "@ingroup Strings" ) { - const UTF8* pos = str; - U32 size = 0; - U32 length = dStrlen(str); - bool count = true; + const UTF8* pos = str; + U32 size = 0; + U32 length = dStrlen(str); + bool count = true; - //loop through each character counting each character, skipping tags (anything with < followed by >) - for(U32 i = 0; i < length; i++, pos++) - { - if(count) - { - if(*pos == first[0]) - count = false; - else - size++; - } - else - { - if(*pos == last[0]) - count = true; - } - } + //loop through each character counting each character, skipping tags (anything with < followed by >) + for(U32 i = 0; i < length; i++, pos++) + { + if(count) + { + if(*pos == first[0]) + count = false; + else + size++; + } + else + { + if(*pos == last[0]) + count = true; + } + } - return S32(size); + return S32(size); } //----------------------------------------------------------------------------- @@ -831,9 +831,9 @@ DefineConsoleFunction( getFirstNumber, String, ( const char* str ),, "@param str The string from which to read out the first number.\n" "@return String representation of the number or "" if no number.\n\n") { - U32 start; - U32 end; - return String::GetFirstNumber(str, start, end); + U32 start; + U32 end; + return String::GetFirstNumber(str, start, end); } //---------------------------------------------------------------- @@ -1031,17 +1031,17 @@ DefineConsoleFunction( strToggleCaseToWords, const char*, ( const char* str ),, "@endtsexample\n" "@ingroup Strings" ) { - String newStr; - for(S32 i = 0; str[i]; i++) - { - //If capitol add a space - if(i != 0 && str[i] >= 65 && str[i] <= 90) - newStr += " "; + String newStr; + for(S32 i = 0; str[i]; i++) + { + //If capitol add a space + if(i != 0 && str[i] >= 65 && str[i] <= 90) + newStr += " "; - newStr += str[i]; - } + newStr += str[i]; + } - return Con::getReturnBuffer(newStr); + return Con::getReturnBuffer(newStr); } //---------------------------------------------------------------- @@ -1056,7 +1056,7 @@ DefineConsoleFunction( isInt, bool, ( const char* str),, "@endtsexample\n" "@ingroup Strings" ) { - return isInt(str); + return isInt(str); } //---------------------------------------------------------------- @@ -1071,7 +1071,7 @@ DefineConsoleFunction( isFloat, bool, ( const char* str, bool sciOk), (false), "@endtsexample\n" "@ingroup Strings" ) { - return isFloat(str, sciOk); + return isFloat(str, sciOk); } //---------------------------------------------------------------- @@ -1085,13 +1085,13 @@ DefineConsoleFunction( isValidPort, bool, ( const char* str),, "@endtsexample\n" "@ingroup Strings" ) { - if(isInt(str)) - { - U16 port = dAtous(str); - return isValidPort(port); - } - else - return false; + if(isInt(str)) + { + U16 port = dAtous(str); + return isValidPort(port); + } + else + return false; } //---------------------------------------------------------------- @@ -1105,12 +1105,12 @@ DefineConsoleFunction( isValidIP, bool, ( const char* str),, "@endtsexample\n" "@ingroup Strings" ) { - if(dStrcmp(str, "localhost") == 0) - { - return true; - } - else - return isValidIP(str); + if(dStrcmp(str, "localhost") == 0) + { + return true; + } + else + return isValidIP(str); } //---------------------------------------------------------------- @@ -1244,22 +1244,22 @@ DefineEngineFunction( monthNumToStr, String, ( S32 num, bool abbreviate ), (fals "@return month as a word given a number or \"\" if number is bad" "@ingroup FileSystem") { - switch(num) - { - case 1: return abbreviate ? "Jan" : "January"; break; - case 2: return abbreviate ? "Feb" : "February"; break; - case 3: return abbreviate ? "Mar" : "March"; break; - case 4: return abbreviate ? "Apr" : "April"; break; - case 5: return "May"; break; - case 6: return abbreviate ? "Jun" : "June"; break; - case 7: return abbreviate ? "Jul" : "July"; break; - case 8: return abbreviate ? "Aug" : "August"; break; - case 9: return abbreviate ? "Sep" : "September"; break; - case 10: return abbreviate ? "Oct" : "October"; break; - case 11: return abbreviate ? "Nov" : "November"; break; - case 12: return abbreviate ? "Dec" : "December"; break; - default: return ""; - } + switch(num) + { + case 1: return abbreviate ? "Jan" : "January"; break; + case 2: return abbreviate ? "Feb" : "February"; break; + case 3: return abbreviate ? "Mar" : "March"; break; + case 4: return abbreviate ? "Apr" : "April"; break; + case 5: return "May"; break; + case 6: return abbreviate ? "Jun" : "June"; break; + case 7: return abbreviate ? "Jul" : "July"; break; + case 8: return abbreviate ? "Aug" : "August"; break; + case 9: return abbreviate ? "Sep" : "September"; break; + case 10: return abbreviate ? "Oct" : "October"; break; + case 11: return abbreviate ? "Nov" : "November"; break; + case 12: return abbreviate ? "Dec" : "December"; break; + default: return ""; + } } DefineEngineFunction( weekdayNumToStr, String, ( S32 num, bool abbreviate ), (false), @@ -1267,17 +1267,17 @@ DefineEngineFunction( weekdayNumToStr, String, ( S32 num, bool abbreviate ), (fa "@return weekday as a word given a number or \"\" if number is bad" "@ingroup FileSystem") { - switch(num) - { - case 0: return abbreviate ? "Sun" : "Sunday"; break; - case 1: return abbreviate ? "Mon" : "Monday"; break; - case 2: return abbreviate ? "Tue" : "Tuesday"; break; - case 3: return abbreviate ? "Wed" : "Wednesday"; break; - case 4: return abbreviate ? "Thu" : "Thursday"; break; - case 5: return abbreviate ? "Fri" : "Friday"; break; - case 6: return abbreviate ? "Sat" : "Saturday"; break; - default: return ""; - } + switch(num) + { + case 0: return abbreviate ? "Sun" : "Sunday"; break; + case 1: return abbreviate ? "Mon" : "Monday"; break; + case 2: return abbreviate ? "Tue" : "Tuesday"; break; + case 3: return abbreviate ? "Wed" : "Wednesday"; break; + case 4: return abbreviate ? "Thu" : "Thursday"; break; + case 5: return abbreviate ? "Fri" : "Friday"; break; + case 6: return abbreviate ? "Sat" : "Saturday"; break; + default: return ""; + } } //----------------------------------------------------------------------------- @@ -3071,5 +3071,5 @@ DefineEngineFunction( getMaxDynamicVerts, S32, (),, "Get max number of allowable dynamic vertices in a single vertex buffer.\n\n" "@return the max number of allowable dynamic vertices in a single vertex buffer" ) { - return MAX_DYNAMIC_VERTS / 2; + return MAX_DYNAMIC_VERTS / 2; } \ No newline at end of file diff --git a/Engine/source/core/util/str.cpp b/Engine/source/core/util/str.cpp index 59fd0ab46..f97aa08fa 100644 --- a/Engine/source/core/util/str.cpp +++ b/Engine/source/core/util/str.cpp @@ -1627,100 +1627,100 @@ String String::GetTrailingNumber(const char* str, S32& number) String String::GetFirstNumber(const char* str, U32& startPos, U32& endPos) { - // Check for trivial strings - if (!str || !str[0]) - return String::EmptyString; + // Check for trivial strings + if (!str || !str[0]) + return String::EmptyString; - // Find the number at the end of the string - String base(str); - const char* p = base.c_str(); - const char* end = base.c_str() + base.length() - 1; - bool dec = false; - startPos = 0; + // Find the number at the end of the string + String base(str); + const char* p = base.c_str(); + const char* end = base.c_str() + base.length() - 1; + bool dec = false; + startPos = 0; - //Check if we are just a digit - if(p == end && isdigit(*p)) - return base; + //Check if we are just a digit + if(p == end && isdigit(*p)) + return base; - //Look for the first digit - while ((p != end) && (dIsspace(*p) || !isdigit(*p))) - { - p++; - startPos++; - } + //Look for the first digit + while ((p != end) && (dIsspace(*p) || !isdigit(*p))) + { + p++; + startPos++; + } - //Handle if we are at the end and found nothing - if(p == end && !isdigit(*p)) - return ""; + //Handle if we are at the end and found nothing + if(p == end && !isdigit(*p)) + return ""; - //update our end position at least to the start of our number - endPos = startPos; + //update our end position at least to the start of our number + endPos = startPos; - //Backup our ptr - const char* backup = p; + //Backup our ptr + const char* backup = p; - //Check for any negative or decimal values - if(startPos > 0) - { - p--; - startPos--; - if(*p == '.') - { - dec = true; + //Check for any negative or decimal values + if(startPos > 0) + { + p--; + startPos--; + if(*p == '.') + { + dec = true; - //ignore any duplicate periods - while ((p != base.c_str()) && (*p == '.')) - { - p--; - startPos--; - } + //ignore any duplicate periods + while ((p != base.c_str()) && (*p == '.')) + { + p--; + startPos--; + } - //Found a decimal lets still check for negative sign - if(startPos > 0) - { - p--; - startPos--; - if((*p != '-') && (*p != '_')) - { - startPos++; - p++; - } - } - } - else if((*p != '-') && (*p != '_')) - { - //go back to where we where cause no decimal or negative sign found - startPos++; - p++; - } - } + //Found a decimal lets still check for negative sign + if(startPos > 0) + { + p--; + startPos--; + if((*p != '-') && (*p != '_')) + { + startPos++; + p++; + } + } + } + else if((*p != '-') && (*p != '_')) + { + //go back to where we where cause no decimal or negative sign found + startPos++; + p++; + } + } - //Restore where we were - p = backup; + //Restore where we were + p = backup; - //look for the end of the digits - bool justFoundDec = false; - while (p != end) - { - if(*p == '.') - { - if(dec && !justFoundDec) - break; - else - { - dec = true; - justFoundDec = true; - } - } - else if(!isdigit(*p)) - break; - else if(justFoundDec) - justFoundDec = false; + //look for the end of the digits + bool justFoundDec = false; + while (p != end) + { + if(*p == '.') + { + if(dec && !justFoundDec) + break; + else + { + dec = true; + justFoundDec = true; + } + } + else if(!isdigit(*p)) + break; + else if(justFoundDec) + justFoundDec = false; - p++; - endPos++; - } + p++; + endPos++; + } - U32 len = (!isdigit(*p)) ? endPos - startPos : (endPos + 1) - startPos; - return base.substr(startPos, len); + U32 len = (!isdigit(*p)) ? endPos - startPos : (endPos + 1) - startPos; + return base.substr(startPos, len); } diff --git a/Engine/source/math/mConsoleFunctions.cpp b/Engine/source/math/mConsoleFunctions.cpp index f9b352c28..679dad3e2 100644 --- a/Engine/source/math/mConsoleFunctions.cpp +++ b/Engine/source/math/mConsoleFunctions.cpp @@ -96,16 +96,16 @@ DefineConsoleFunction( mFloor, S32, ( F32 v ),, DefineConsoleFunction( mRound, F32, ( F32 v, S32 n ), (0), - "Round v to the nth decimal place or the nearest whole number by default." - "@param v Value to round\n" - "@param n Number of decimal places to round to, 0 by default\n" - "@return The rounded value as a S32." - "@ingroup Math" ) + "Round v to the nth decimal place or the nearest whole number by default." + "@param v Value to round\n" + "@param n Number of decimal places to round to, 0 by default\n" + "@return The rounded value as a S32." + "@ingroup Math" ) { - if(n <= 0) - return mRound(v); - else - return mRound(v, n); + if(n <= 0) + return mRound(v); + else + return mRound(v, n); } DefineConsoleFunction( mCeil, S32, ( F32 v ),, From 2e7018bf681e8c5733cab28cac11f7e2de16e703 Mon Sep 17 00:00:00 2001 From: Nathan Bowhay Date: Wed, 11 Feb 2015 10:55:30 -0800 Subject: [PATCH 13/14] Added type conversions to fix compile errors Added two type conversions that were missing in order to fix some compile errors. --- Engine/source/core/strings/stringFunctions.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Engine/source/core/strings/stringFunctions.h b/Engine/source/core/strings/stringFunctions.h index 0c11d73ed..89601319a 100644 --- a/Engine/source/core/strings/stringFunctions.h +++ b/Engine/source/core/strings/stringFunctions.h @@ -150,11 +150,20 @@ inline U32 dAtoui(const char *str, U32 base = 10) return strtoul(str, NULL, base); } +inline U16 dAtous(const char *str, U32 base = 10) +{ + return strtoul(str, NULL, base); +} + inline F32 dAtof(const char *str) { return strtof(str, NULL); } +inline F64 dAtod(const char *str) +{ + return strtod(str, NULL); +} inline char dToupper(const char c) { From 0fb62de4b8faa711d8fe544516fed653cb409701 Mon Sep 17 00:00:00 2001 From: "Anis A. Hireche" Date: Fri, 26 Feb 2016 18:41:29 +0100 Subject: [PATCH 14/14] restored old signature of mRound as it's used from scripts. --- Engine/source/math/mConsoleFunctions.cpp | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/Engine/source/math/mConsoleFunctions.cpp b/Engine/source/math/mConsoleFunctions.cpp index 679dad3e2..1a11fe23e 100644 --- a/Engine/source/math/mConsoleFunctions.cpp +++ b/Engine/source/math/mConsoleFunctions.cpp @@ -94,18 +94,13 @@ DefineConsoleFunction( mFloor, S32, ( F32 v ),, return (S32)mFloor( v ); } - -DefineConsoleFunction( mRound, F32, ( F32 v, S32 n ), (0), +DefineConsoleFunction( mRound, S32, ( F32 v ),, "Round v to the nth decimal place or the nearest whole number by default." - "@param v Value to round\n" - "@param n Number of decimal places to round to, 0 by default\n" - "@return The rounded value as a S32." - "@ingroup Math" ) + "@param v Value to roundn" + "@return The rounded value as a S32." + "@ingroup Math" ) { - if(n <= 0) - return mRound(v); - else - return mRound(v, n); + return mRound(v); } DefineConsoleFunction( mCeil, S32, ( F32 v ),,