Advertisement
ipsBruno

(Pawn) icmd versão 6

Dec 21st, 2012
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.52 KB | None | 0 0
  1. //=================================================================
  2. //
  3. // Criado por Bruno da Silva
  4. // sscanf de Y_less
  5. // Testes por ReDKiiL e Thiago Street
  6. //
  7. // www.ips-team.forumeiros.com
  8. // www.brunodasilva.com
  9. //
  10. //=================================================================
  11.  
  12.  
  13.  
  14. #if defined _icmd_included
  15.  
  16. #endinput
  17.  
  18. #endif
  19.  
  20.  
  21.  
  22. #define _icmd_included
  23.  
  24.  
  25. #if defined FILTERSCRIPT
  26.  
  27. public OnFilterScriptInit() {
  28.  
  29.     OniPsCommandsInit ();
  30.  
  31.     return CallLocalFunction("ExOnFilterScriptInit", "");
  32. }
  33.  
  34. #if defined _ALS_OnFilterScriptInit
  35.  
  36. #undef OnFilterScriptInit
  37.  
  38. #else
  39.  
  40. #define _ALS_OnFilterScriptInit
  41.  
  42. #endif
  43.  
  44. forward ExOnFilterScriptInit();
  45.  
  46. #define OnFilterScriptInit ExOnFilterScriptInit
  47.  
  48.  
  49.  
  50. #else
  51.  
  52.  
  53. public OnGameModeInit() {
  54.  
  55.     OniPsCommandsInit ();
  56.  
  57.     return CallLocalFunction("ExOnGameModeInit", "");
  58. }
  59.  
  60. #if defined _ALS_OnGameModeInit
  61.  
  62. #undef OnGameModeInit
  63.  
  64. #else
  65.  
  66. #define _ALS_OnGameModeInit
  67.  
  68. #endif
  69.  
  70. forward ExOnGameModeInit();
  71.  
  72. #define OnGameModeInit ExOnGameModeInit
  73.  
  74.  
  75. #endif
  76.  
  77.  
  78. new bool: InformarComando;
  79.  
  80. public OnPlayerCommandText( playerid, cmdtext[] ) {
  81.  
  82.     if(!CallLocalFunction("OnPlayerCallCommand", "is", playerid, cmdtext)) return true;
  83.  
  84.     ////////////////////////////////////
  85.     //
  86.     // Preparar prefixo do comando
  87.     //
  88.     ///////////////////////////////////
  89.  
  90.     cmdtext[0] = '_';
  91.  
  92.     static comando[32];
  93.     comando = "ips";
  94.  
  95.     strcat(comando, cmdtext);
  96.  
  97.  
  98.  
  99.     ///////////////////////////////////////////
  100.     //
  101.     //  Localizar local para pegar parametro
  102.     //
  103.     /////////////////////////////////////////
  104.  
  105.  
  106.  
  107.     static c ;
  108.  
  109.     if((c = strfind(cmdtext," ")) != -1 && strlen(cmdtext) - c > 1 ) {
  110.  
  111.         ///////////////////////////////////////////////////
  112.         //
  113.         //  Caso comando tiver parametro, processa-los
  114.         //
  115.         ///////////////////////////////////////////////////
  116.  
  117.  
  118.         comando[ c + 3] = EOS;
  119.  
  120.         if( InformarComando ) {
  121.             if(funcidx(comando) != -1)
  122.                 CallLocalFunction("OnPlayerProcessCommand","dsd",playerid,comando[4], CallLocalFunction(comando, "is", playerid, cmdtext[c + 1]));
  123.         }
  124.         else {
  125.             if(funcidx(comando) != -1)
  126.                 CallLocalFunction(comando, "is", playerid, cmdtext[c + 1]);
  127.         }
  128.     }
  129.  
  130.  
  131.     ///////////////////////////////////////////
  132.     //
  133.     // Caso não tiver, apenas chamar o comando
  134.     //
  135.     ////////////////////////////////////////////
  136.  
  137.     else {
  138.         if( InformarComando ) {
  139.             CallLocalFunction("OnPlayerProcessCommand","dsd",playerid,comando[4], CallLocalFunction(comando, "is", playerid, "\1"));
  140.         }
  141.         else {
  142.             CallLocalFunction(comando, "is", playerid, "\1");
  143.         }
  144.     }
  145.  
  146.     return true;
  147. }
  148.  
  149.  
  150.  
  151. OniPsCommandsInit() {
  152.  
  153.     print("\n\n\n |---------------------------------- \n |");
  154.     print(" | ICMD Versão 6 Iniciado");
  155.     print(" | Criado por Bruno da Silva");
  156.     print(" |\n |—————————————————————————————————— \n\n\n");
  157.  
  158.     if(funcidx("OnPlayerProcessCommand") != -1) {
  159.         InformarComando = true;
  160.     }
  161.  
  162.  
  163.     return true;
  164. }
  165.  
  166.  
  167.  
  168. #if defined _ALS_OnPlayerCommandText
  169.  
  170. #undef OnPlayerCommandText
  171.  
  172. #else
  173.  
  174. #define _ALS_OnPlayerCommandText
  175.  
  176. #endif
  177.  
  178.  
  179. forward ExOnPlayerCommandText(playerid, cmdtext[]);
  180.  
  181. #define OnPlayerCommandText ExOnPlayerCommandText
  182.  
  183. /////////////////////////////////////////////////////////////////////////////////
  184. //
  185. // SSCANF by Y_less
  186. //
  187. //////////////////////////////////////////////////////////////////////////////////
  188.  
  189. stock sscanf_ylessicmd(string[], format[], {Float,_}:...)
  190. {
  191.     #if defined isnull
  192.     if (isnull(string))
  193.     #else
  194.     if (string[0] == 0 || (string[0] == 1 && string[1] == 0))
  195.     #endif
  196.     {
  197.  
  198.         return format[0];
  199.     }
  200.     #pragma tabsize 4
  201.     new
  202.     formatPos = 0,
  203.     stringPos = 0,
  204.     paramPos = 2,
  205.     paramCount = numargs(),
  206.     delim = ' ';
  207.     while (string[stringPos] && string[stringPos] <= ' ')
  208.     {
  209.  
  210.         stringPos++;
  211.     }
  212.     while (paramPos < paramCount && string[stringPos])
  213.     {
  214.  
  215.         switch (format[formatPos++])
  216.         {
  217.  
  218.             case '\0':
  219.             {
  220.  
  221.                 return 0;
  222.             }
  223.             case 'i', 'd':
  224.             {
  225.  
  226.                 new
  227.                 neg = 1,
  228.                 num = 0,
  229.                 ch = string[stringPos];
  230.                 if (ch == '-')
  231.                 {
  232.  
  233.                     neg = -1;
  234.                     ch = string[++stringPos];
  235.                 }
  236.                 do
  237.                 {
  238.  
  239.                     stringPos++;
  240.                     if ('0' <= ch <= '9')
  241.                     {
  242.  
  243.                         num = (num * 10) + (ch - '0');
  244.                     }
  245.                     else
  246.                     {
  247.  
  248.                         return -1;
  249.                     }
  250.                 }
  251.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  252.                 setarg(paramPos, 0, num * neg);
  253.             }
  254.             case 'h', 'x':
  255.             {
  256.  
  257.                 new
  258.                 num = 0,
  259.                 ch = string[stringPos];
  260.                 do
  261.                 {
  262.  
  263.                     stringPos++;
  264.                     switch (ch)
  265.                     {
  266.  
  267.                         case 'x', 'X':
  268.                         {
  269.  
  270.                             num = 0;
  271.                             continue;
  272.                         }
  273.                         case '0' .. '9':
  274.                         {
  275.  
  276.                             num = (num << 4) | (ch - '0');
  277.                         }
  278.                         case 'a' .. 'f':
  279.                         {
  280.  
  281.                             num = (num << 4) | (ch - ('a' - 10));
  282.                         }
  283.                         case 'A' .. 'F':
  284.                         {
  285.  
  286.                             num = (num << 4) | (ch - ('A' - 10));
  287.                         }
  288.                         default:
  289.                         {
  290.  
  291.                             return -1;
  292.                         }
  293.                     }
  294.                 }
  295.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  296.                 setarg(paramPos, 0, num);
  297.             }
  298.             case 'c':
  299.             {
  300.  
  301.                 setarg(paramPos, 0, string[stringPos++]);
  302.             }
  303.             case 'f':
  304.             {
  305.  
  306.  
  307.                 new changestr[16], changepos = 0, strpos = stringPos;
  308.                 while(changepos < 16 && string[strpos] && string[strpos] != delim)
  309.                 {
  310.  
  311.                     changestr[changepos++] = string[strpos++];
  312.                 }
  313.                 changestr[changepos] = '\0';
  314.                 setarg(paramPos,0,_:floatstr(changestr));
  315.             }
  316.             case 'p':
  317.             {
  318.  
  319.                 delim = format[formatPos++];
  320.                 continue;
  321.             }
  322.             case '\'':
  323.             {
  324.  
  325.                 new
  326.                 end = formatPos - 1,
  327.                 ch;
  328.                 while ((ch = format[++end]) && ch != '\'') {}
  329.                 if (!ch)
  330.                 {
  331.  
  332.                     return -1;
  333.                 }
  334.                 format[end] = '\0';
  335.                 if ((ch = strfind(string, format[formatPos], false, stringPos)) == -1)
  336.                 {
  337.  
  338.                     if (format[end + 1])
  339.                     {
  340.  
  341.                         return -1;
  342.                     }
  343.                     return 0;
  344.                 }
  345.                 format[end] = '\'';
  346.                 stringPos = ch + (end - formatPos);
  347.                 formatPos = end + 1;
  348.             }
  349.             case 'u':
  350.             {
  351.  
  352.                 new
  353.                 end = stringPos - 1,
  354.                 id = 0,
  355.                 bool:num = true,
  356.                 ch;
  357.                 while ((ch = string[++end]) && ch != delim)
  358.                 {
  359.  
  360.                     if (num)
  361.                     {
  362.  
  363.                         if ('0' <= ch <= '9')
  364.                         {
  365.  
  366.                             id = (id * 10) + (ch - '0');
  367.                         }
  368.                         else
  369.                         {
  370.  
  371.                             num = false;
  372.                         }
  373.                     }
  374.                 }
  375.                 if (num && IsPlayerConnected(id))
  376.                 {
  377.  
  378.                     setarg(paramPos, 0, id);
  379.                 }
  380.                 else
  381.                 {
  382.  
  383.                     #if !defined foreach
  384.                     #define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
  385.                     #define __SSCANF_FOREACH__
  386.                     #endif
  387.                     string[end] = '\0';
  388.                     num = false;
  389.                     new
  390.                     name[MAX_PLAYER_NAME];
  391.                     id = end - stringPos;
  392.                     foreach (Player, playerid)
  393.                     {
  394.  
  395.                         GetPlayerName(playerid, name, sizeof (name));
  396.                         if (!strcmp(name, string[stringPos], true, id))
  397.                         {
  398.  
  399.                             setarg(paramPos, 0, playerid);
  400.                             num = true;
  401.                             break;
  402.                         }
  403.                     }
  404.                     if (!num)
  405.                     {
  406.  
  407.                         setarg(paramPos, 0, INVALID_PLAYER_ID);
  408.                     }
  409.                     string[end] = ch;
  410.                     #if defined __SSCANF_FOREACH__
  411.                     #undef foreach
  412.                     #undef __SSCANF_FOREACH__
  413.                     #endif
  414.                 }
  415.                 stringPos = end;
  416.             }
  417.             case 's', 'z':
  418.             {
  419.  
  420.                 new
  421.                 i = 0,
  422.                 ch;
  423.                 if (format[formatPos])
  424.                 {
  425.  
  426.                     while ((ch = string[stringPos++]) && ch != delim)
  427.                     {
  428.  
  429.                         setarg(paramPos, i++, ch);
  430.                     }
  431.                     if (!i)
  432.                     {
  433.  
  434.                         return -1;
  435.                     }
  436.                 }
  437.                 else
  438.                 {
  439.  
  440.                     while ((ch = string[stringPos++]))
  441.                     {
  442.  
  443.                         setarg(paramPos, i++, ch);
  444.                     }
  445.                 }
  446.                 stringPos--;
  447.                 setarg(paramPos, i, '\0');
  448.             }
  449.             default:
  450.             {
  451.  
  452.                 continue;
  453.             }
  454.         }
  455.         while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
  456.         {
  457.  
  458.             stringPos++;
  459.         }
  460.         while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
  461.         {
  462.  
  463.             stringPos++;
  464.         }
  465.         paramPos++;
  466.     }
  467.     do
  468.     {
  469.  
  470.         if ((delim = format[formatPos++]) > ' ')
  471.         {
  472.  
  473.             if (delim == '\'')
  474.             {
  475.  
  476.                 while ((delim = format[formatPos++]) && delim != '\'') {}
  477.             }
  478.             else if (delim != 'z')
  479.             {
  480.  
  481.                 return delim;
  482.             }
  483.         }
  484.     }
  485.     while (delim > ' ');
  486.     return 0;
  487. }
  488.  
  489.  
  490.  
  491.  
  492. #define cmd(%1,%2,%3)\
  493.     forward ips_%1(%2,%3); public ips_%1(%2,%3)
  494.  
  495. forward OnPlayerProcessCommand(playerid, comando[], sucesso) ;
  496. forward OnPlayerCallCommand(playerid, cmdtext[]);
  497.  
  498. #define CMD:%1(%2) \
  499.         cmd(%1,%2)
  500.  
  501. #define scmd(%1,%3,%6)<%5>scanf(%0,%2) \
  502.         ips_%1(%3,params[]); public ips_%1(%3,params[]){static %5; new %1sss=sscanf_ylessicmd(params,%0,%2);cmd%1(%3,%1sss,%2);} forward cmd%1(%3,%6,%5); public cmd%1(%3,%6,%5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement