cmd.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright 2010-2025 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include <bx/allocator.h>
  6. #include <bx/commandline.h>
  7. #include <bx/hash.h>
  8. #include <bx/string.h>
  9. #include "dbg.h"
  10. #include "cmd.h"
  11. #include "entry_p.h"
  12. #include <tinystl/allocator.h>
  13. #include <tinystl/string.h>
  14. #include <tinystl/unordered_map.h>
  15. namespace stl = tinystl;
  16. struct CmdContext
  17. {
  18. CmdContext()
  19. {
  20. }
  21. ~CmdContext()
  22. {
  23. }
  24. void add(const char* _name, ConsoleFn _fn, void* _userData)
  25. {
  26. const uint32_t cmd = bx::hash<bx::HashMurmur2A>(_name, (uint32_t)bx::strLen(_name) );
  27. BX_ASSERT(m_lookup.end() == m_lookup.find(cmd), "Command \"%s\" already exist.", _name);
  28. Func fn = { _fn, _userData };
  29. m_lookup.insert(stl::make_pair(cmd, fn) );
  30. }
  31. void remove(const char* _name)
  32. {
  33. const uint32_t cmd = bx::hash<bx::HashMurmur2A>(_name, (uint32_t)bx::strLen(_name) );
  34. CmdLookup::iterator it = m_lookup.find(cmd);
  35. if (it != m_lookup.end() )
  36. {
  37. m_lookup.erase(it);
  38. }
  39. }
  40. void exec(const char* _cmd)
  41. {
  42. for (bx::StringView next(_cmd); !next.isEmpty(); _cmd = next.getPtr() )
  43. {
  44. char commandLine[1024];
  45. uint32_t size = sizeof(commandLine);
  46. int argc;
  47. char* argv[64];
  48. next = bx::tokenizeCommandLine(_cmd, commandLine, size, argc, argv, BX_COUNTOF(argv), '\n');
  49. if (argc > 0)
  50. {
  51. int err = -1;
  52. uint32_t cmd = bx::hash<bx::HashMurmur2A>(argv[0], (uint32_t)bx::strLen(argv[0]) );
  53. CmdLookup::iterator it = m_lookup.find(cmd);
  54. if (it != m_lookup.end() )
  55. {
  56. Func& fn = it->second;
  57. err = fn.m_fn(this, fn.m_userData, argc, argv);
  58. }
  59. switch (err)
  60. {
  61. case 0:
  62. break;
  63. case -1:
  64. {
  65. stl::string tmp(_cmd, next.getPtr()-_cmd - (next.isEmpty() ? 0 : 1) );
  66. DBG("Command '%s' doesn't exist.", tmp.c_str() );
  67. }
  68. break;
  69. default:
  70. {
  71. stl::string tmp(_cmd, next.getPtr()-_cmd - (next.isEmpty() ? 0 : 1) );
  72. DBG("Failed '%s' err: %d.", tmp.c_str(), err);
  73. }
  74. break;
  75. }
  76. }
  77. }
  78. }
  79. struct Func
  80. {
  81. ConsoleFn m_fn;
  82. void* m_userData;
  83. };
  84. typedef stl::unordered_map<uint32_t, Func> CmdLookup;
  85. CmdLookup m_lookup;
  86. };
  87. static CmdContext* s_cmdContext;
  88. void cmdInit()
  89. {
  90. s_cmdContext = BX_NEW(entry::getAllocator(), CmdContext);
  91. }
  92. void cmdShutdown()
  93. {
  94. bx::deleteObject(entry::getAllocator(), s_cmdContext);
  95. }
  96. void cmdAdd(const char* _name, ConsoleFn _fn, void* _userData)
  97. {
  98. s_cmdContext->add(_name, _fn, _userData);
  99. }
  100. void cmdRemove(const char* _name)
  101. {
  102. s_cmdContext->remove(_name);
  103. }
  104. void cmdExec(const char* _format, ...)
  105. {
  106. char tmp[2048];
  107. va_list argList;
  108. va_start(argList, _format);
  109. bx::vsnprintf(tmp, BX_COUNTOF(tmp), _format, argList);
  110. va_end(argList);
  111. s_cmdContext->exec(tmp);
  112. }