fork download
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <map>
  4. #include <string>
  5.  
  6. #define PLUGIN_EXPORT
  7. #define PLUGIN_CALL
  8.  
  9. //begin command library
  10. class Command
  11. {
  12. public:
  13. virtual bool do_command(int playerid, std::string params) = 0;
  14. };
  15. class CommandCollection
  16. {
  17. public:
  18. std::map<std::string, Command*> m_command_map;
  19. void register_command(Command* cmd, std::string name)
  20. {
  21. m_command_map[name] = cmd;
  22. }
  23. };
  24.  
  25. CommandCollection Instance;
  26.  
  27. /*
  28.  * Concatenate preprocessor tokens A and B without expanding macro definitions
  29.  * (however, if invoked from a macro, macro arguments are expanded).
  30.  */
  31. #define PPCAT_NX(A, B) A ## B
  32. /*
  33.  * Concatenate preprocessor tokens A and B after macro-expanding them.
  34.  */
  35. #define PPCAT(A, B) PPCAT_NX(A, B)
  36.  
  37. #define CMD(name) \
  38. class PPCAT(cmd,name) : public Command\
  39. {\
  40. public:\
  41. PPCAT(cmd,name)() { Instance.register_command(this, "/"#name); }\
  42. bool do_command(int playerid, std::string params)
  43. #define CMDEND(name) };PPCAT(cmd,name) PPCAT(ffx_cmd_instance_,name);
  44.  
  45. PLUGIN_EXPORT bool PLUGIN_CALL OnPlayerCommandText(int playerid, const char *cmdtext)
  46. {
  47. std::string main(cmdtext);
  48. std::string command;
  49. std::string parameters;
  50.  
  51. size_t space = main.find(' ');
  52.  
  53. if(space == std::string::npos)
  54. {
  55. space = main.length();
  56. command.assign(main);
  57. }
  58. else
  59. {
  60. command.assign(main.begin(),main.begin()+space);
  61. parameters.assign(main.begin()+(space+1),main.end());
  62. }
  63.  
  64. if(Instance.m_command_map.find(command) != Instance.m_command_map.end())
  65. return Instance.m_command_map[command]->do_command(playerid,parameters);
  66. printf("Unknown command: %s\n",command.c_str());
  67. return false;
  68. }
  69. //end command library
  70.  
  71. //commands
  72.  
  73. CMD(start)
  74. {
  75. printf("start issued, params: '%s'\n",params.c_str());
  76. return true;
  77. }
  78. CMDEND(start);
  79.  
  80. CMD(end)
  81. {
  82. printf("end issued, params: '%s'",params.c_str());
  83. return false;
  84. }
  85. CMDEND(end);
  86.  
  87. //end commands
  88.  
  89. //test run
  90. int main()
  91. {
  92. OnPlayerCommandText(0, "/start");
  93. OnPlayerCommandText(0, "/start ello lololol");
  94. OnPlayerCommandText(0, "/kill");
  95. OnPlayerCommandText(0, "/end");
  96. OnPlayerCommandText(0, "/end 1234");
  97. return 0;
  98. }
Success #stdin #stdout 0s 2992KB
stdin
Standard input is empty
stdout
start issued, params: ''
start issued, params: 'ello lololol'
Unknown command: /kill
end issued, params: ''end issued, params: '1234'