AEEditorMode.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. playerBroker_->PostMessage(E_IPCINITIALIZE, startupData);
  61. SendEvent(E_EDITORPLAYERSTARTED);
  62. playerEnabled_ = true;
  63. }
  64. void EditorMode::HandleIPCWorkerExit(StringHash eventType, VariantMap& eventData)
  65. {
  66. //SendEvent(E_EDITORPLAYSTOP);
  67. if (eventData[IPCWorkerExit::P_BROKER] == playerBroker_)
  68. {
  69. playerBroker_ = 0;
  70. playerEnabled_ = false;
  71. SendEvent(E_EDITORPLAYERSTOPPED);
  72. }
  73. }
  74. void EditorMode::HandleIPCWorkerLog(StringHash eventType, VariantMap& eventData)
  75. {
  76. using namespace IPCWorkerLog;
  77. // convert to a player log
  78. VariantMap playerLogData;
  79. playerLogData["message"] = eventData[P_MESSAGE].GetString();
  80. playerLogData["level"] = eventData[P_LEVEL].GetInt();
  81. SendEvent("EditorPlayerLog", playerLogData);
  82. }
  83. void EditorMode::HandleIPCJSError(StringHash eventType, VariantMap& eventData)
  84. {
  85. }
  86. bool EditorMode::PlayProject(String addArgs, bool debug)
  87. {
  88. ToolEnvironment* env = GetSubsystem<ToolEnvironment>();
  89. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  90. const String& editorBinary = env->GetEditorBinary();
  91. Project* project = tsystem->GetProject();
  92. if (!project)
  93. return false;
  94. Vector<String> paths;
  95. paths.Push(env->GetCoreDataDir());
  96. paths.Push(env->GetPlayerDataDir());
  97. paths.Push(project->GetResourcePath());
  98. // fixme: this is for loading from cache
  99. paths.Push(project->GetProjectPath());
  100. paths.Push(project->GetProjectPath() + "Cache");
  101. String resourcePaths;
  102. resourcePaths.Join(paths, "!");
  103. Vector<String> vargs;
  104. String args = ToString("--player --project \"%s\"", AddTrailingSlash(project->GetProjectPath()).CString());
  105. vargs = args.Split(' ');
  106. if (debug)
  107. vargs.Insert(0, "--debug");
  108. if (addArgs.Length() > 0)
  109. vargs.Insert(0, addArgs.Split(' '));
  110. String dump;
  111. dump.Join(vargs, " ");
  112. LOGINFOF("Launching Broker %s %s", editorBinary.CString(), dump.CString());
  113. IPC* ipc = GetSubsystem<IPC>();
  114. playerBroker_ = ipc->SpawnWorker(editorBinary, vargs);
  115. if (playerBroker_)
  116. {
  117. SubscribeToEvent(playerBroker_, E_IPCJSERROR, HANDLER(EditorMode, HandleIPCJSError));
  118. SubscribeToEvent(playerBroker_, E_IPCWORKEREXIT, HANDLER(EditorMode, HandleIPCWorkerExit));
  119. SubscribeToEvent(playerBroker_, E_IPCWORKERLOG, HANDLER(EditorMode, HandleIPCWorkerLog));
  120. }
  121. return playerBroker_.NotNull();
  122. }
  123. void EditorMode::HandleIPCPlayerPauseResumeRequest(StringHash eventType, VariantMap& eventData)
  124. {
  125. if (!playerBroker_) return;
  126. VariantMap noEventData;
  127. playerBroker_->PostMessage(E_PAUSERESUMEREQUESTED, noEventData);
  128. }
  129. void EditorMode::HandleIPCPlayerUpdatesPausedResumed(StringHash eventType, VariantMap& eventData)
  130. {
  131. using namespace UpdatesPaused;
  132. bool paused = eventData[P_PAUSED].GetBool();
  133. if (paused)
  134. SendEvent(E_EDITORPLAYERPAUSED);
  135. else
  136. SendEvent(E_EDITORPLAYERRESUMED);
  137. }
  138. void EditorMode::HandleIPCPlayerPauseStepRequest(StringHash eventType, VariantMap& eventData)
  139. {
  140. if (!playerBroker_) return;
  141. VariantMap noEventData;
  142. playerBroker_->PostMessage(E_PAUSESTEPREQUESTED, noEventData);
  143. }
  144. void EditorMode::HandleIPCPlayerExitRequest(StringHash eventType, VariantMap& eventData)
  145. {
  146. if (!playerBroker_) return;
  147. VariantMap noEventData;
  148. playerBroker_->PostMessage(E_EXITREQUESTED, noEventData);
  149. }
  150. bool EditorMode::IsPlayerEnabled()
  151. {
  152. return playerEnabled_;
  153. }
  154. }