AEEditorMode.cpp 6.1 KB

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