AEEditorMode.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 <Atomic/IO/Log.h>
  23. #include <Atomic/IPC/IPC.h>
  24. #include <Atomic/IPC/IPCEvents.h>
  25. #include <Atomic/IPC/IPCBroker.h>
  26. #include <Atomic/Input/InputEvents.h>
  27. #include <ToolCore/ToolEnvironment.h>
  28. #include <ToolCore/ToolSystem.h>
  29. #include <ToolCore/License/LicenseSystem.h>
  30. #include <ToolCore/Project/Project.h>
  31. #include <AtomicJS/Javascript/JSIPCEvents.h>
  32. #include <Atomic/UI/SystemUI/DebugHud.h>
  33. #include "../PlayerMode/AEPlayerEvents.h"
  34. #include "AEEditorMode.h"
  35. using namespace ToolCore;
  36. namespace AtomicEditor
  37. {
  38. EditorMode::EditorMode(Context* context) :
  39. Object(context)
  40. {
  41. SubscribeToEvent(E_IPCWORKERSTART, HANDLER(EditorMode, HandleIPCWorkerStarted));
  42. SubscribeToEvent(E_IPCPLAYEREXITREQUEST, HANDLER(EditorMode, HandleIPCPlayerExitRequest));
  43. }
  44. EditorMode::~EditorMode()
  45. {
  46. }
  47. void EditorMode::HandleIPCWorkerStarted(StringHash eventType, VariantMap& eventData)
  48. {
  49. LicenseSystem* licenseSystem = GetSubsystem<LicenseSystem>();
  50. VariantMap startupData;
  51. SystemUI::DebugHud* debugHud = GetSubsystem<SystemUI::DebugHud>();
  52. // BEGIN LICENSE MANAGEMENT
  53. startupData["license3D"] = licenseSystem->GetLicenseModule3D();
  54. // END LICENSE MANAGEMENT
  55. startupData["debugHudMode"] = debugHud ? debugHud->GetMode() : (unsigned) 0;
  56. playerBroker_->PostMessage(E_IPCINITIALIZE, startupData);
  57. SendEvent(E_EDITORPLAYERSTARTED);
  58. playerEnabled_ = true;
  59. }
  60. void EditorMode::HandleIPCWorkerExit(StringHash eventType, VariantMap& eventData)
  61. {
  62. //SendEvent(E_EDITORPLAYSTOP);
  63. if (eventData[IPCWorkerExit::P_BROKER] == playerBroker_)
  64. {
  65. playerBroker_ = 0;
  66. playerEnabled_ = false;
  67. SendEvent(E_EDITORPLAYERSTOPPED);
  68. }
  69. }
  70. void EditorMode::HandleIPCWorkerLog(StringHash eventType, VariantMap& eventData)
  71. {
  72. using namespace IPCWorkerLog;
  73. // convert to a player log
  74. VariantMap playerLogData;
  75. playerLogData["message"] = eventData[P_MESSAGE].GetString();
  76. playerLogData["level"] = eventData[P_LEVEL].GetInt();
  77. SendEvent("EditorPlayerLog", playerLogData);
  78. }
  79. void EditorMode::HandleIPCJSError(StringHash eventType, VariantMap& eventData)
  80. {
  81. }
  82. bool EditorMode::PlayProject(String addArgs, bool debug)
  83. {
  84. ToolEnvironment* env = GetSubsystem<ToolEnvironment>();
  85. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  86. const String& editorBinary = env->GetEditorBinary();
  87. Project* project = tsystem->GetProject();
  88. if (!project)
  89. return false;
  90. Vector<String> paths;
  91. paths.Push(env->GetCoreDataDir());
  92. paths.Push(env->GetPlayerDataDir());
  93. paths.Push(project->GetResourcePath());
  94. // fixme: this is for loading from cache
  95. paths.Push(project->GetProjectPath());
  96. paths.Push(project->GetProjectPath() + "Cache");
  97. String resourcePaths;
  98. resourcePaths.Join(paths, "!");
  99. Vector<String> vargs;
  100. String args = ToString("--player --project \"%s\"", AddTrailingSlash(project->GetProjectPath()).CString());
  101. vargs = args.Split(' ');
  102. if (debug)
  103. vargs.Insert(0, "--debug");
  104. if (addArgs.Length() > 0)
  105. vargs.Insert(0, addArgs.Split(' '));
  106. String dump;
  107. dump.Join(vargs, " ");
  108. LOGINFOF("Launching Broker %s %s", editorBinary.CString(), dump.CString());
  109. IPC* ipc = GetSubsystem<IPC>();
  110. playerBroker_ = ipc->SpawnWorker(editorBinary, vargs);
  111. if (playerBroker_)
  112. {
  113. SubscribeToEvent(playerBroker_, E_IPCJSERROR, HANDLER(EditorMode, HandleIPCJSError));
  114. SubscribeToEvent(playerBroker_, E_IPCWORKEREXIT, HANDLER(EditorMode, HandleIPCWorkerExit));
  115. SubscribeToEvent(playerBroker_, E_IPCWORKERLOG, HANDLER(EditorMode, HandleIPCWorkerLog));
  116. }
  117. return playerBroker_.NotNull();
  118. }
  119. void EditorMode::HandleIPCPlayerExitRequest(StringHash eventType, VariantMap& eventData)
  120. {
  121. if (!playerBroker_) return;
  122. VariantMap noEventData;
  123. playerBroker_->PostMessage(E_EXITREQUESTED, noEventData);
  124. }
  125. bool EditorMode::IsPlayerEnabled()
  126. {
  127. return playerEnabled_;
  128. }
  129. }