Y_Less

cmdcmp

Aug 29th, 2010
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 7.52 KB | None | 0 0
  1. // Include the main header.
  2. #include <a_samp>
  3.  
  4. #define _ALS_OnPlayerCommandText
  5.  
  6. // Include the YSI headers.
  7. //#define _DEBUG 5
  8. #define OnPlayerCommandText ycmd_OPCT
  9. forward ycmd_OPCT(playerid, cmdtext[]);
  10. #include <YSI\y_commands>
  11. #include <YSI\y_master>
  12. #undef OnPlayerCommandText
  13.  
  14. // Undefine the macros used by both YSI and zcmd.
  15. #undef CMD
  16. #undef COMMAND
  17.  
  18. // Include the zcmd headers.
  19. #define OnPlayerCommandText zcmd_OPCT
  20. forward zcmd_OPCT(playerid, cmdtext[]);
  21. #include <zcmd>
  22. #undef OnPlayerCommandText
  23.  
  24. // Include the mcmd headers.
  25. #include <mcmd>
  26.  
  27. // Define dcmd.
  28. #define dcmd(%1,%2,%3) if ((strcmp((%3)[1], #%1, true, (%2)) == 0) && ((((%3)[(%2) + 1] == 0) && (dcmd_%1(playerid, "")))||(((%3)[(%2) + 1] == 32) && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1
  29.  
  30. // Define the enum for all commands.
  31. enum E_COMM_PERM
  32. {
  33.     // Command permissions super macro.
  34.     #define COMM(%0) E_COMM_%0,
  35.     #include "cmdcmp_macro1"
  36.     #include "cmdcmp_macro2"
  37.     E_COMM_zzz,
  38.     _E_COMM_END
  39. }
  40.  
  41. // Permissions for playes to use a command.
  42. new
  43.     BitArray:g_perm[E_COMM_PERM]<MAX_PLAYERS>;
  44.  
  45. // Define the commands for zcmd.  Include permission checks as ycmd has it in-
  46. // built, so the others need it explicitly to match.
  47. #define COMM(%0) CMD:%0(playerid,params[]){if(Bit_Get(g_perm[E_COMM_%0], playerid))return 1;return 0;}
  48. #include "cmdcmp_macro1"
  49. #include "cmdcmp_macro2"
  50. CMD:zzz(playerid, params[])
  51. {
  52.     if(Bit_Get(g_perm[E_COMM_zzz], playerid))
  53.     {
  54.         //printf("zcmd zzz called");
  55.         return 1;
  56.     }
  57.     return 0;
  58. }
  59.  
  60. // Define the commands for ycmd.  This has internal permission checks so we
  61. // don't need to include anything in the command itself.
  62. #define COMM(%0) YCMD:%0(playerid,params[],help){return 1;}
  63. #include "cmdcmp_macro1"
  64. #include "cmdcmp_macro2"
  65. YCMD:zzz(playerid, params[], help)
  66. {
  67.     //printf("ycmd zzz called");
  68. }
  69.  
  70. // Define the commands for dcmd.
  71. #define COMM(%0) dcmd_%0(playerid,params[]){if(Bit_Get(g_perm[E_COMM_%0], playerid))return 1;return 0;}
  72. #include "cmdcmp_macro1"
  73. #include "cmdcmp_macro2"
  74. dcmd_zzz(playerid, params[])
  75. {
  76.     if(Bit_Get(g_perm[E_COMM_zzz], playerid))
  77.     {
  78.         //printf("dcmd zzz called");
  79.         return 1;
  80.     }
  81.     return 0;
  82. }
  83.  
  84. // Define the commands for mcmd.  Because this differentiates between commands
  85. // with parameters and commands with none we need to do two sets.
  86. #define COMM(%0) mcmd_%0(playerid,params[]){if(Bit_Get(g_perm[E_COMM_%0], playerid))return 1;return 0;}
  87. #include "cmdcmp_macro1"
  88. #undef COMM
  89. #define COMM(%0) mcmd_%0(playerid){if(Bit_Get(g_perm[E_COMM_%0], playerid))return 1;return 0;}
  90. #include "cmdcmp_macro2"
  91. mcmd_zzz(playerid)
  92. {
  93.     if(Bit_Get(g_perm[E_COMM_zzz], playerid))
  94.     {
  95.         //printf("mcmd zzz called");
  96.         return 1;
  97.     }
  98.     return 0;
  99. }
  100.  
  101. // dcmd
  102. dcmd_OPCT(playerid, cmdtext[])
  103. {
  104.     #define COMM(%0) dcmd(%0,3,cmdtext);
  105.     #include "cmdcmp_macro1"
  106.     #include "cmdcmp_macro2"
  107.     dcmd(zzz, 3, cmdtext);
  108.     return 0;
  109. }
  110.  
  111. // mcmd
  112. mcmd_OPCT(playerid, cmdtext[])
  113. {
  114.     mcmd_init(cmdtext);
  115.     #define COMM(%0) mcmd(%0,cmdtext,3,playerid);
  116.     #include "cmdcmp_macro1"
  117.     #undef COMM
  118.     #define COMM(%0) mcmd2(%0,cmdtext,playerid);
  119.     #include "cmdcmp_macro2"
  120.     mcmd2(zzz, cmdtext, playerid);
  121.     return 0;
  122. }
  123.  
  124. // strcmp
  125. strcmp_OPCT(playerid, cmdtext[])
  126. {
  127.     #define COMM(%0) if (!strcmp(#%0,cmdtext[1],true,3)){if(Bit_Get(g_perm[E_COMM_%0], playerid))return 1;return 0;}
  128.     #include "cmdcmp_macro1"
  129.     #include "cmdcmp_macro2"
  130.     if (!strcmp("zzz", cmdtext[1], true, 3))
  131.     {
  132.         if(Bit_Get(g_perm[E_COMM_zzz], playerid))
  133.         {
  134.             //printf("strcmp zzz called");
  135.             return 1;
  136.         }
  137.         return 0;
  138.     }
  139.     return 0;
  140. }
  141.  
  142. strtok(const string[], &index)
  143. {
  144.     new length = strlen(string);
  145.     while ((index < length) && (string[index] <= ' '))
  146.     {
  147.         index++;
  148.     }
  149.     new offset = index;
  150.     new result[20];
  151.     while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
  152.     {
  153.         result[index - offset] = string[index];
  154.         index++;
  155.     }
  156.     result[index - offset] = EOS;
  157.     return result;
  158. }
  159.  
  160. // strtok
  161. strtok_OPCT(playerid, cmdtext[])
  162. {
  163.     new
  164.         idx,
  165.         cmd[128];
  166.     cmd = strtok(cmdtext, idx);
  167.     #define COMM(%0) if (!strcmp("/"#%0,cmd,true,4)){if(Bit_Get(g_perm[E_COMM_%0], playerid))return 1;return 0;}
  168.     #include "cmdcmp_macro1"
  169.     #include "cmdcmp_macro2"
  170.     if (!strcmp("/zzz", cmd, true, 4))
  171.     {
  172.         if(Bit_Get(g_perm[E_COMM_zzz], playerid))
  173.         {
  174.             //printf("strtok zzz called");
  175.             return 1;
  176.         }
  177.         return 0;
  178.     }
  179.     return 0;
  180. }
  181.  
  182. main()
  183. {
  184.     printf("================================");
  185.     printf(" Command processor comparisons.");
  186.     printf("================================");
  187.     // Disable all ycmd commands by default.
  188.     for (new E_COMM_PERM:i; i != _E_COMM_END; ++i)
  189.     {
  190.         Command_SetPlayer(i, 0, false);
  191.         Command_SetPlayer(i, 499, false);
  192.     }
  193.     Tests();
  194.     // Now enable all the commands and rerun the tests.
  195.     for (new E_COMM_PERM:i; i != _E_COMM_END; ++i)
  196.     {
  197.         // ycmd.
  198.         Command_SetPlayer(i, 0, true);
  199.         Command_SetPlayer(i, 499, true);
  200.         // All others.
  201.         Bit_Set(g_perm[i], 0, true);
  202.         Bit_Set(g_perm[i], 499, true);
  203.     }
  204.     Tests();
  205.     /*mcmd_OPCT(0, "/aaa hello");
  206.     mcmd_OPCT(0, "/azz hello");
  207.     mcmd_OPCT(0, "/zzz hello");
  208.     mcmd_OPCT(0, "/zzz");*/
  209.     return 0;
  210. }
  211.  
  212. Tests()
  213. {
  214.     new
  215.         t0a,
  216.         t0b,
  217.         t1a,
  218.         t1b,
  219.         t2a,
  220.         t2b,
  221.         t3a,
  222.         t3b,
  223.         t4a,
  224.         t4b,
  225.         t5a,
  226.         t5b;
  227.     // Call the ycmd command versions.
  228.     printf("ycmd:");
  229.     // Enable the command for this player so it gets called correctly.
  230.     ycmd_OPCT(0, "/zzz");
  231.     ycmd_OPCT(0, "/zzzz");
  232.     ycmd_OPCT(0, "/zzz ");
  233.     t0a = GetTickCount();
  234.     for (new i = 0; i != 10000; ++i)
  235.     {
  236.         ycmd_OPCT(0, "/aaa hello");
  237.         ycmd_OPCT(499, "/anz hello");
  238.         ycmd_OPCT(0, "/amz");
  239.         ycmd_OPCT(499, "/azz");
  240.     }
  241.     t0b = GetTickCount();
  242.     // Call the zcmd command versions.
  243.     printf("zcmd:");
  244.     zcmd_OPCT(0, "/zzz");
  245.     zcmd_OPCT(0, "/zzzz");
  246.     zcmd_OPCT(0, "/zzz ");
  247.     t1a = GetTickCount();
  248.     for (new i = 0; i != 10000; ++i)
  249.     {
  250.         zcmd_OPCT(0, "/aaa hello");
  251.         zcmd_OPCT(499, "/anz hello");
  252.         zcmd_OPCT(0, "/amz");
  253.         zcmd_OPCT(499, "/azz");
  254.     }
  255.     t1b = GetTickCount();
  256.     // Call the dcmd command versions.
  257.     printf("dcmd:");
  258.     dcmd_OPCT(0, "/zzz");
  259.     dcmd_OPCT(0, "/zzzz");
  260.     dcmd_OPCT(0, "/zzz ");
  261.     t2a = GetTickCount();
  262.     for (new i = 0; i != 10000; ++i)
  263.     {
  264.         dcmd_OPCT(0, "/aaa hello");
  265.         dcmd_OPCT(499, "/anz hello");
  266.         dcmd_OPCT(0, "/amz");
  267.         dcmd_OPCT(499, "/azz");
  268.     }
  269.     t2b = GetTickCount();
  270.     // Call the mcmd command versions.
  271.     printf("mcmd:");
  272.     mcmd_OPCT(0, "/zzz");
  273.     mcmd_OPCT(0, "/zzzz");
  274.     mcmd_OPCT(0, "/zzz ");
  275.     t3a = GetTickCount();
  276.     for (new i = 0; i != 10000; ++i)
  277.     {
  278.         mcmd_OPCT(0, "/aaa hello");
  279.         mcmd_OPCT(499, "/anz hello");
  280.         mcmd_OPCT(0, "/amz");
  281.         mcmd_OPCT(499, "/azz");
  282.     }
  283.     t3b = GetTickCount();
  284.     // Call the strcmp command versions.
  285.     printf("strcmp:");
  286.     strcmp_OPCT(0, "/zzz");
  287.     strcmp_OPCT(0, "/zzzz");
  288.     strcmp_OPCT(0, "/zzz ");
  289.     t4a = GetTickCount();
  290.     for (new i = 0; i != 10000; ++i)
  291.     {
  292.         strcmp_OPCT(0, "/aaa hello");
  293.         strcmp_OPCT(499, "/anz hello");
  294.         strcmp_OPCT(0, "/amz");
  295.         strcmp_OPCT(499, "/azz");
  296.     }
  297.     t4b = GetTickCount();
  298.     // Call the strtok command versions.
  299.     printf("strtok:");
  300.     strtok_OPCT(0, "/zzz");
  301.     strtok_OPCT(0, "/zzzz");
  302.     strtok_OPCT(0, "/zzz ");
  303.     t5a = GetTickCount();
  304.     for (new i = 0; i != 10000; ++i)
  305.     {
  306.         strtok_OPCT(0, "/aaa hello");
  307.         strtok_OPCT(499, "/anz hello");
  308.         strtok_OPCT(0, "/amz");
  309.         strtok_OPCT(499, "/azz");
  310.     }
  311.     t5b = GetTickCount();
  312.     printf("Results:");
  313.     printf("  ycmd: %d", t0b - t0a);
  314.     printf("  zcmd: %d", t1b - t1a);
  315.     printf("  dcmd: %d", t2b - t2a);
  316.     printf("  mcmd: %d", t3b - t3a);
  317.     printf("strcmp: %d", t4b - t4a);
  318.     printf("strtok: %d", t5b - t5a);
  319.     return 0;
  320. }
Add Comment
Please, Sign In to add comment