PlayCmd.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 build 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. return false;
  41. return true;
  42. }
  43. void PlayCmd::Run()
  44. {
  45. LOGINFOF("Playing project");
  46. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  47. ToolEnvironment* env = GetSubsystem<ToolCore::ToolEnvironment>();
  48. Project* project = tsystem->GetProject();
  49. const String& editorBinary = env->GetEditorBinary();
  50. Vector<String> paths;
  51. paths.Push(env->GetCoreDataDir());
  52. paths.Push(env->GetPlayerDataDir());
  53. paths.Push(project->GetResourcePath());
  54. String resourcePaths;
  55. resourcePaths.Join(paths, "!");
  56. Vector<String> vargs;
  57. String args = ToString("--editor-resource-paths \"%s\"", resourcePaths.CString());
  58. vargs = args.Split(' ');
  59. vargs.Insert(0, "--player");
  60. // TODO: use IPC (maybe before this set log location/access the log and output it, we need access to errors)
  61. LaunchPlayerProcess(editorBinary, vargs, "");
  62. }
  63. }