cmd.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright 2010-2018 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  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. uint32_t cmd = bx::hash<bx::HashMurmur2A>(_name, (uint32_t)bx::strLen(_name) );
  27. BX_CHECK(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 exec(const char* _cmd)
  32. {
  33. for (bx::StringView next(_cmd); !next.isEmpty(); _cmd = next.getPtr() )
  34. {
  35. char commandLine[1024];
  36. uint32_t size = sizeof(commandLine);
  37. int argc;
  38. char* argv[64];
  39. next = bx::tokenizeCommandLine(_cmd, commandLine, size, argc, argv, BX_COUNTOF(argv), '\n');
  40. if (argc > 0)
  41. {
  42. int err = -1;
  43. uint32_t cmd = bx::hash<bx::HashMurmur2A>(argv[0], (uint32_t)bx::strLen(argv[0]) );
  44. CmdLookup::iterator it = m_lookup.find(cmd);
  45. if (it != m_lookup.end() )
  46. {
  47. Func& fn = it->second;
  48. err = fn.m_fn(this, fn.m_userData, argc, argv);
  49. }
  50. switch (err)
  51. {
  52. case 0:
  53. break;
  54. case -1:
  55. {
  56. stl::string tmp(_cmd, next.getPtr()-_cmd - (next.isEmpty() ? 0 : 1) );
  57. DBG("Command '%s' doesn't exist.", tmp.c_str() );
  58. }
  59. break;
  60. default:
  61. {
  62. stl::string tmp(_cmd, next.getPtr()-_cmd - (next.isEmpty() ? 0 : 1) );
  63. DBG("Failed '%s' err: %d.", tmp.c_str(), err);
  64. }
  65. break;
  66. }
  67. }
  68. }
  69. }
  70. struct Func
  71. {
  72. ConsoleFn m_fn;
  73. void* m_userData;
  74. };
  75. typedef stl::unordered_map<uint32_t, Func> CmdLookup;
  76. CmdLookup m_lookup;
  77. };
  78. static CmdContext* s_cmdContext;
  79. void cmdInit()
  80. {
  81. s_cmdContext = BX_NEW(entry::getAllocator(), CmdContext);
  82. }
  83. void cmdShutdown()
  84. {
  85. BX_DELETE(entry::getAllocator(), s_cmdContext);
  86. }
  87. void cmdAdd(const char* _name, ConsoleFn _fn, void* _userData)
  88. {
  89. s_cmdContext->add(_name, _fn, _userData);
  90. }
  91. void cmdExec(const char* _format, ...)
  92. {
  93. char tmp[2048];
  94. va_list argList;
  95. va_start(argList, _format);
  96. bx::vsnprintf(tmp, BX_COUNTOF(tmp), _format, argList);
  97. va_end(argList);
  98. s_cmdContext->exec(tmp);
  99. }