]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - prvm_cmds.c
count empty packets in rate limiting
[xonotic/darkplaces.git] / prvm_cmds.c
index 3e4a4aff046977c54700765aeaf054f0890315f8..73816935ce56b2e4e5123cc36a501ffad76b7670 100644 (file)
@@ -1754,53 +1754,12 @@ void VM_strdecolorize(void)
 {
        char szNewString[VM_STRINGTEMP_LENGTH];
        const char *szString;
-       size_t nCnt;
-       int nPos;
-       int nFillPos;
-       int bFinished;
-               nPos = 0;
-               nFillPos = 0;
-               nCnt = 0;
-               bFinished = 0;
 
        // Prepare Strings
        VM_SAFEPARMCOUNT(1,VM_strdecolorize);
        szString = PRVM_G_STRING(OFS_PARM0);
 
-       while(!bFinished)
-       { // Traverse through String
-               if( szString[nPos] == '\n' || szString[nPos] == '\r' || szString[nPos] <= 0)
-               { // String End Found
-                       szNewString[nFillPos++] = szString[nPos];
-                       bFinished = 1;
-               }
-               else
-               if( szString[nPos] == STRING_COLOR_TAG)
-               { // Color Code Located
-                       if( szString[nPos + 1] == STRING_COLOR_TAG)
-                       { // Valid Characters to Include
-                               szNewString[nFillPos++] = szString[nPos];
-                               nPos = nPos + 1;
-                               szNewString[nFillPos++] = szString[nPos];
-                       }
-                       else
-                       if( szString[nPos + 1] >= '0' && szString[nPos + 1] <= '9' )
-                       { // Color Code Found; Increment Position
-                               nPos = nPos + 1;
-                       }
-                       else
-                       { // Unknown Color Code; Include
-                               szNewString[nFillPos++] = szString[nPos];
-                               nPos = nPos + 1;
-                       }
-               }
-               else
-                       // Include Character
-                       szNewString[nFillPos++] = szString[nPos];
-
-                       // Increment Position
-                       nPos = nPos + 1;
-       }
+       COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
 
        PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
 }
@@ -1818,53 +1777,14 @@ float   strlennocol(string s)
 void VM_strlennocol(void)
 {
        const char *szString;
-       size_t nCnt;
-       int nPos;
-       int bFinished;
-               nPos = 0;
-               nCnt = 0;
-               bFinished = 0;
+       int nCnt;
 
        VM_SAFEPARMCOUNT(1,VM_strlennocol);
 
        szString = PRVM_G_STRING(OFS_PARM0);
 
-       while(!bFinished)
-       { // Count Characters
-               // SV_BroadcastPrintf("Position '%d'; Character '%c'; Length '%d'\n", nPos, szString[nPos], nCnt);
+       nCnt = COM_StringLengthNoColors(szString, 0, NULL);
 
-               if( szString[nPos] == '\n' || szString[nPos] == '\r' || szString[nPos] <= 0)
-               { // String End Found
-                       // SV_BroadcastPrintf("Found End of String at '%d'\n", nPos);
-                       bFinished = 1;
-               }
-               else
-               if( szString[nPos] == STRING_COLOR_TAG)
-               { // Color Code Located
-                       if( szString[nPos + 1] == STRING_COLOR_TAG)
-                       { // Increment Length; Skip Color Code
-                               nCnt = nCnt + 1;
-                               nPos = nPos + 1;
-                       }
-                       else
-                       if( szString[nPos + 1] >= '0' && szString[nPos + 1] <= '9' )
-                       { // Color Code Found; Increment Position
-                               // SV_BroadcastPrintf("Found Color Codes at '%d'\n", nPos);
-                               nPos = nPos + 1;
-                       }
-                       else
-                       { // Unknown Color Code; Increment Length!
-                               nPos = nPos + 1;
-                               nCnt = nCnt + 1;
-                       }
-               }
-               else
-                       // Increment String Length
-                       nCnt = nCnt + 1;
-
-               // Increment Position
-               nPos = nPos + 1;
-       }
        PRVM_G_FLOAT(OFS_RETURN) = nCnt;
 }
 
@@ -2013,11 +1933,6 @@ int tokens[256];
 void VM_tokenize (void)
 {
        const char *p;
-#if 0
-       size_t pos = 0;
-       char tokenbuf[MAX_INPUTLINE];
-       size_t tokenlen;
-#endif
 
        VM_SAFEPARMCOUNT(1,VM_tokenize);
 
@@ -2028,16 +1943,73 @@ void VM_tokenize (void)
        {
                if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
                        break;
-#if 0
-               tokenlen = strlen(com_token) + 1;
-               if (pos + tokenlen > sizeof(tokenbuf))
-                       break;
-               tokens[num_tokens++] = PRVM_SetEngineString(tokenbuf + pos);
-               memcpy(tokenbuf + pos, com_token, tokenlen);
-               pos += tokenlen;
-#else
                tokens[num_tokens++] = PRVM_SetTempString(com_token);
-#endif
+       }
+
+       PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
+}
+
+/*
+=========
+VM_tokenizebyseparator
+
+float tokenizebyseparator(string s, string separator1, ...)
+=========
+*/
+//float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
+//this function returns the token preceding each instance of a separator (of
+//which there can be multiple), and the text following the last separator
+//useful for parsing certain kinds of data like IP addresses
+//example:
+//numnumbers = tokenizebyseparator("10.1.2.3", ".");
+//returns 4 and the tokens "10" "1" "2" "3".
+void VM_tokenizebyseparator (void)
+{
+       int j, k;
+       int numseparators;
+       int separatorlen[7];
+       const char *separators[7];
+       const char *p;
+       char tokentext[MAX_INPUTLINE];
+
+       VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
+
+       p = PRVM_G_STRING(OFS_PARM0);
+
+       numseparators = 0;;
+       for (j = 1;j < prog->argc;j++)
+       {
+               // skip any blank separator strings
+               if (!PRVM_G_STRING(OFS_PARM0 + j)[0])
+                       continue;
+               separators[numseparators] = PRVM_G_STRING(OFS_PARM0 + j);
+               separatorlen[numseparators] = strlen(separators[numseparators]);
+               numseparators++;
+       }
+
+       num_tokens = 0;
+       for (num_tokens = 0;num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0]));num_tokens++)
+       {
+               while (*p)
+               {
+                       for (k = 0;k < numseparators;k++)
+                       {
+                               if (!strncmp(p, separators[k], separatorlen[k]))
+                               {
+                                       p += separatorlen[k];
+                                       break;
+                               }
+                       }
+                       if (k < numseparators)
+                               break;
+                       if (j < (int)sizeof(tokentext[MAX_INPUTLINE]-1))
+                               tokentext[j++] = *p;
+                       p++;
+               }
+               tokentext[j] = 0;
+               tokens[num_tokens] = PRVM_SetTempString(tokentext);
+               if (!*p)
+                       break;
        }
 
        PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
@@ -2522,7 +2494,7 @@ void VM_drawcharacter(void)
                return;
        }
 
-       DrawQ_String (pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
+       DrawQ_String (pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true);
        PRVM_G_FLOAT(OFS_RETURN) = 1;
 }
 
@@ -2563,7 +2535,7 @@ void VM_drawstring(void)
        if(pos[2] || scale[2])
                Con_Printf("VM_drawstring: z value%s from %s discarded\n",(pos[2] && scale[2]) ? "s" : " ",((pos[2] && scale[2]) ? "pos and scale" : (pos[2] ? "pos" : "scale")));
 
-       DrawQ_String (pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
+       DrawQ_String (pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true);
        PRVM_G_FLOAT(OFS_RETURN) = 1;
 }
 /*
@@ -2641,7 +2613,7 @@ void VM_drawfill(void)
        if(pos[2] || size[2])
                Con_Printf("VM_drawfill: z value%s from %s discarded\n",(pos[2] && size[2]) ? "s" : " ",((pos[2] && size[2]) ? "pos and size" : (pos[2] ? "pos" : "size")));
 
-       DrawQ_Pic(pos[0], pos[1], NULL, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
+       DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
        PRVM_G_FLOAT(OFS_RETURN) = 1;
 }