PlayCmd.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <Poco/Process.h>
  2. #include <Atomic/Core/StringUtils.h>
  3. #include <Atomic/IO/Log.h>
  4. #include <Atomic/IO/File.h>
  5. #include "../ToolSystem.h"
  6. #include "../ToolEnvironment.h"
  7. #include "../Project/Project.h"
  8. #include "../Build/BuildEvents.h"
  9. #include "../Build/BuildSystem.h"
  10. #include "PlayCmd.h"
  11. namespace ToolCore
  12. {
  13. PlayCmd::PlayCmd(Context* context) : Command(context)
  14. {
  15. }
  16. PlayCmd::~PlayCmd()
  17. {
  18. }
  19. bool PlayCmd::Parse(const Vector<String>& arguments, unsigned startIndex, String& errorMsg)
  20. {
  21. String argument = arguments[startIndex].ToLower();
  22. String value = startIndex + 1 < arguments.Size() ? arguments[startIndex + 1] : String::EMPTY;
  23. if (argument != "play")
  24. {
  25. errorMsg = "Unable to parse play command";
  26. return false;
  27. }
  28. return true;
  29. }
  30. bool PlayCmd::LaunchPlayerProcess(const String& command, const Vector<String>& args, const String& initialDirectory)
  31. {
  32. Poco::Process::Args pargs;
  33. for (unsigned i = 0; i < args.Size(); i++)
  34. pargs.push_back(args[i].CString());
  35. std::string pcommand = command.CString();
  36. std::string pinitialDirectory = initialDirectory.CString();
  37. // this can take an ENV as well, may come in useful
  38. Poco::ProcessHandle handle(Poco::Process::launch(pcommand, pargs, pinitialDirectory));
  39. if (!Poco::Process::isRunning(handle))
  40. {
  41. Error(ToString("Unable to launch player process: %s", command.CString()));
  42. return false;
  43. }
  44. handle.wait();
  45. return true;
  46. }
  47. void PlayCmd::Run()
  48. {
  49. LOGINFOF("Playing project");
  50. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  51. ToolEnvironment* env = GetSubsystem<ToolCore::ToolEnvironment>();
  52. Project* project = tsystem->GetProject();
  53. const String& editorBinary = env->GetEditorBinary();
  54. Vector<String> paths;
  55. paths.Push(env->GetCoreDataDir());
  56. paths.Push(env->GetPlayerDataDir());
  57. paths.Push(project->GetResourcePath());
  58. String resourcePaths;
  59. resourcePaths.Join(paths, "!");
  60. Vector<String> vargs;
  61. String args = ToString("--editor-resource-paths \"%s\"", resourcePaths.CString());
  62. vargs = args.Split(' ');
  63. vargs.Insert(0, "--player");
  64. // TODO: use IPC (maybe before this set log location/access the log and output it, we need access to errors)
  65. LaunchPlayerProcess(editorBinary, vargs, "");
  66. Finished();
  67. }
  68. }