PlayCmd.cpp 2.7 KB

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