PlayCmd.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Poco/Process.h>
  23. #include <Atomic/Core/StringUtils.h>
  24. #include <Atomic/IO/Log.h>
  25. #include <Atomic/IO/File.h>
  26. #include "../ToolSystem.h"
  27. #include "../ToolEnvironment.h"
  28. #include "../Project/Project.h"
  29. #include "../Build/BuildEvents.h"
  30. #include "../Build/BuildSystem.h"
  31. #include "PlayCmd.h"
  32. namespace ToolCore
  33. {
  34. PlayCmd::PlayCmd(Context* context) : Command(context)
  35. {
  36. }
  37. PlayCmd::~PlayCmd()
  38. {
  39. }
  40. bool PlayCmd::ParseInternal(const Vector<String>& arguments, unsigned startIndex, String& errorMsg)
  41. {
  42. String argument = arguments[startIndex].ToLower();
  43. String value = startIndex + 1 < arguments.Size() ? arguments[startIndex + 1] : String::EMPTY;
  44. if (argument != "play")
  45. {
  46. errorMsg = "Unable to parse play command";
  47. return false;
  48. }
  49. return true;
  50. }
  51. bool PlayCmd::LaunchPlayerProcess(const String& command, const Vector<String>& args, const String& initialDirectory)
  52. {
  53. Poco::Process::Args pargs;
  54. for (unsigned i = 0; i < args.Size(); i++)
  55. pargs.push_back(args[i].CString());
  56. std::string pcommand = command.CString();
  57. std::string pinitialDirectory = initialDirectory.CString();
  58. // this can take an ENV as well, may come in useful
  59. Poco::ProcessHandle handle(Poco::Process::launch(pcommand, pargs, pinitialDirectory));
  60. if (!Poco::Process::isRunning(handle))
  61. {
  62. Error(ToString("Unable to launch player process: %s", command.CString()));
  63. return false;
  64. }
  65. handle.wait();
  66. return true;
  67. }
  68. void PlayCmd::Run()
  69. {
  70. ATOMIC_LOGINFOF("Playing project");
  71. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  72. ToolEnvironment* env = GetSubsystem<ToolCore::ToolEnvironment>();
  73. Project* project = tsystem->GetProject();
  74. const String& editorBinary = env->GetEditorBinary();
  75. Vector<String> paths;
  76. paths.Push(env->GetCoreDataDir());
  77. paths.Push(env->GetPlayerDataDir());
  78. paths.Push(project->GetResourcePath());
  79. // fixme: this is for loading from cache
  80. paths.Push(project->GetProjectPath());
  81. paths.Push(project->GetProjectPath() + "Cache");
  82. String resourcePaths;
  83. resourcePaths.Join(paths, "!");
  84. Vector<String> vargs;
  85. String args = ToString("--player --project \"%s\"", AddTrailingSlash(project->GetProjectPath()).CString());
  86. vargs = args.Split(' ');
  87. //vargs.Insert(0, "--player");
  88. // TODO: use IPC (maybe before this set log location/access the log and output it, we need access to errors)
  89. LaunchPlayerProcess(editorBinary, vargs, "");
  90. Finished();
  91. }
  92. }