console_api.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2012-2017 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "console_server.h"
  6. #include "data_compiler.h"
  7. #include "device.h"
  8. #include "dynamic_string.h"
  9. #include "json_object.h"
  10. #include "lua_environment.h"
  11. #include "sjson.h"
  12. #include "string_stream.h"
  13. #include "temp_allocator.h"
  14. namespace crown
  15. {
  16. static void console_command_script(ConsoleServer& /*cs*/, TCPSocket /*client*/, const char* json, void* /*user_data*/)
  17. {
  18. TempAllocator4096 ta;
  19. JsonObject obj(ta);
  20. DynamicString script(ta);
  21. sjson::parse(json, obj);
  22. sjson::parse_string(obj["script"], script);
  23. device()->_lua_environment->execute_string(script.c_str());
  24. }
  25. static void console_command_reload(ConsoleServer& /*cs*/, TCPSocket /*client*/, const char* json, void* /*user_data*/)
  26. {
  27. TempAllocator4096 ta;
  28. JsonObject obj(ta);
  29. DynamicString type(ta);
  30. DynamicString name(ta);
  31. sjson::parse(json, obj);
  32. sjson::parse_string(obj["resource_type"], type);
  33. sjson::parse_string(obj["resource_name"], name);
  34. logi("Reloading resource '%s.%s'", name.c_str(), type.c_str());
  35. device()->reload(ResourceId(type.c_str()), ResourceId(name.c_str()));
  36. logi("Reloaded resource '%s.%s'", name.c_str(), type.c_str());
  37. }
  38. static void console_command_pause(ConsoleServer& /*cs*/, TCPSocket /*client*/, const char* /*json*/, void* /*user_data*/)
  39. {
  40. device()->pause();
  41. }
  42. static void console_command_unpause(ConsoleServer& /*cs*/, TCPSocket /*client*/, const char* /*json*/, void* /*user_data*/)
  43. {
  44. device()->unpause();
  45. }
  46. void load_console_api(ConsoleServer& cs)
  47. {
  48. cs.register_command("script", console_command_script, NULL);
  49. cs.register_command("reload", console_command_reload, NULL);
  50. cs.register_command("pause", console_command_pause, NULL);
  51. cs.register_command("unpause", console_command_unpause, NULL);
  52. }
  53. } // namespace crown