AEEditorMode.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 <Atomic/Graphics/Texture2D.h>
  29. #include <ToolCore/ToolEnvironment.h>
  30. #include <ToolCore/ToolSystem.h>
  31. #include <ToolCore/License/LicenseSystem.h>
  32. #include <ToolCore/Project/Project.h>
  33. #include <AtomicJS/Javascript/JSIPCEvents.h>
  34. #include <Atomic/UI/SystemUI/DebugHud.h>
  35. #include "../PlayerMode/AEPlayerEvents.h"
  36. #include "AEEditorMode.h"
  37. using namespace ToolCore;
  38. namespace AtomicEditor
  39. {
  40. EditorMode::EditorMode(Context* context) :
  41. Object(context)
  42. {
  43. SubscribeToEvent(E_IPCWORKERSTART, HANDLER(EditorMode, HandleIPCWorkerStarted));
  44. SubscribeToEvent(E_IPCPLAYERPAUSERESUMEREQUEST, HANDLER(EditorMode, HandleIPCPlayerPauseResumeRequest));
  45. SubscribeToEvent(E_IPCPLAYERUPDATESPAUSEDRESUMED, HANDLER(EditorMode, HandleIPCPlayerUpdatesPausedResumed));
  46. SubscribeToEvent(E_IPCPLAYERPAUSESTEPREQUEST, HANDLER(EditorMode, HandleIPCPlayerPauseStepRequest));
  47. SubscribeToEvent(E_IPCPLAYEREXITREQUEST, HANDLER(EditorMode, HandleIPCPlayerExitRequest));
  48. SubscribeToEvent(E_IPCPLAYERRENDERTEXTUREINFO, HANDLER(EditorMode, HandleIPCPlayerRenderTextureInfo));
  49. }
  50. EditorMode::~EditorMode()
  51. {
  52. }
  53. void EditorMode::HandleIPCWorkerStarted(StringHash eventType, VariantMap& eventData)
  54. {
  55. LicenseSystem* licenseSystem = GetSubsystem<LicenseSystem>();
  56. VariantMap startupData;
  57. SystemUI::DebugHud* debugHud = GetSubsystem<SystemUI::DebugHud>();
  58. // BEGIN LICENSE MANAGEMENT
  59. startupData["license3D"] = licenseSystem->GetLicenseModule3D();
  60. // END LICENSE MANAGEMENT
  61. startupData["debugHudMode"] = debugHud ? debugHud->GetMode() : (unsigned) 0;
  62. playerBroker_->PostMessage(E_IPCINITIALIZE, startupData);
  63. SendEvent(E_EDITORPLAYERSTARTED);
  64. playerEnabled_ = true;
  65. }
  66. void EditorMode::HandleIPCPlayerRenderTextureInfo(StringHash eventType, VariantMap& eventData)
  67. {
  68. using namespace IPCPlayerRenderTextureInfo;
  69. int width = eventData[P_WIDTH].GetInt();
  70. int height = eventData[P_HEIGHT].GetInt();
  71. unsigned format = eventData[P_FORMAT].GetUInt();
  72. TextureUsage usage = (TextureUsage) eventData[P_USAGE].GetUInt();
  73. unsigned resourceHandle = eventData[P_RESOURCEHANDLE].GetUInt();
  74. renderTexture_ = new Texture2D(context_);
  75. renderTexture_->SetGPUShared(true);
  76. renderTexture_->SetGPUSharedHandle((void*) ((uintptr_t)resourceHandle));
  77. renderTexture_->SetSize(width, height, format, usage);
  78. SendEvent(E_EDITORPLAYERRENDERTEXTUREUPDATED);
  79. }
  80. void EditorMode::HandleIPCWorkerExit(StringHash eventType, VariantMap& eventData)
  81. {
  82. if (eventData[IPCWorkerExit::P_BROKER] == playerBroker_)
  83. {
  84. playerBroker_ = 0;
  85. playerEnabled_ = false;
  86. SendEvent(E_EDITORPLAYERSTOPPED);
  87. }
  88. }
  89. void EditorMode::HandleIPCWorkerLog(StringHash eventType, VariantMap& eventData)
  90. {
  91. using namespace IPCWorkerLog;
  92. // convert to a player log
  93. VariantMap playerLogData;
  94. playerLogData["message"] = eventData[P_MESSAGE].GetString();
  95. playerLogData["level"] = eventData[P_LEVEL].GetInt();
  96. SendEvent("EditorPlayerLog", playerLogData);
  97. }
  98. void EditorMode::HandleIPCJSError(StringHash eventType, VariantMap& eventData)
  99. {
  100. }
  101. bool EditorMode::PlaySetup(Vector<String>& vargs, const String& addArgs, bool debug)
  102. {
  103. ToolEnvironment* env = GetSubsystem<ToolEnvironment>();
  104. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  105. const String& editorBinary = env->GetEditorBinary();
  106. Project* project = tsystem->GetProject();
  107. if (!project)
  108. return false;
  109. Vector<String> paths;
  110. paths.Push(env->GetCoreDataDir());
  111. paths.Push(env->GetPlayerDataDir());
  112. paths.Push(project->GetResourcePath());
  113. // fixme: this is for loading from cache
  114. paths.Push(project->GetProjectPath());
  115. paths.Push(project->GetProjectPath() + "Cache");
  116. String resourcePaths;
  117. resourcePaths.Join(paths, "!");
  118. String args = ToString("--player --project \"%s\"", AddTrailingSlash(project->GetProjectPath()).CString());
  119. vargs = args.Split(' ');
  120. if (debug)
  121. vargs.Insert(0, "--debug");
  122. if (addArgs.Length() > 0)
  123. vargs.Insert(0, addArgs.Split(' '));
  124. return true;
  125. }
  126. bool EditorMode::PlayScene(const String& scenePath, const String& addArgs, bool debug)
  127. {
  128. String _addArgs = "--scene \"" + scenePath +"\" " + addArgs;
  129. Vector<String> vargs;
  130. if (!PlaySetup(vargs, _addArgs, debug))
  131. return false;
  132. ToolEnvironment* env = GetSubsystem<ToolEnvironment>();
  133. const String& editorBinary = env->GetEditorBinary();
  134. String dump;
  135. dump.Join(vargs, " ");
  136. LOGINFOF("Launching Scene Player Broker %s %s", editorBinary.CString(), dump.CString());
  137. IPC* ipc = GetSubsystem<IPC>();
  138. playerBroker_ = ipc->SpawnWorker(editorBinary, vargs);
  139. if (playerBroker_)
  140. {
  141. SubscribeToEvent(playerBroker_, E_IPCJSERROR, HANDLER(EditorMode, HandleIPCJSError));
  142. SubscribeToEvent(playerBroker_, E_IPCWORKEREXIT, HANDLER(EditorMode, HandleIPCWorkerExit));
  143. SubscribeToEvent(playerBroker_, E_IPCWORKERLOG, HANDLER(EditorMode, HandleIPCWorkerLog));
  144. }
  145. return playerBroker_.NotNull();
  146. }
  147. bool EditorMode::PlayProject(const String& addArgs, bool debug)
  148. {
  149. return PlayScene("Scenes/ToonTown.scene", addArgs, debug);
  150. Vector<String> vargs;
  151. if (!PlaySetup(vargs, addArgs, debug))
  152. return false;
  153. ToolEnvironment* env = GetSubsystem<ToolEnvironment>();
  154. const String& editorBinary = env->GetEditorBinary();
  155. String dump;
  156. dump.Join(vargs, " ");
  157. LOGINFOF("Launching Broker %s %s", editorBinary.CString(), dump.CString());
  158. IPC* ipc = GetSubsystem<IPC>();
  159. playerBroker_ = ipc->SpawnWorker(editorBinary, vargs);
  160. if (playerBroker_)
  161. {
  162. SubscribeToEvent(playerBroker_, E_IPCJSERROR, HANDLER(EditorMode, HandleIPCJSError));
  163. SubscribeToEvent(playerBroker_, E_IPCWORKEREXIT, HANDLER(EditorMode, HandleIPCWorkerExit));
  164. SubscribeToEvent(playerBroker_, E_IPCWORKERLOG, HANDLER(EditorMode, HandleIPCWorkerLog));
  165. }
  166. return playerBroker_.NotNull();
  167. }
  168. void EditorMode::HandleIPCPlayerPauseResumeRequest(StringHash eventType, VariantMap& eventData)
  169. {
  170. if (!playerBroker_) return;
  171. VariantMap noEventData;
  172. playerBroker_->PostMessage(E_PAUSERESUMEREQUESTED, noEventData);
  173. }
  174. void EditorMode::HandleIPCPlayerUpdatesPausedResumed(StringHash eventType, VariantMap& eventData)
  175. {
  176. using namespace UpdatesPaused;
  177. bool paused = eventData[P_PAUSED].GetBool();
  178. if (paused)
  179. SendEvent(E_EDITORPLAYERPAUSED);
  180. else
  181. SendEvent(E_EDITORPLAYERRESUMED);
  182. }
  183. void EditorMode::HandleIPCPlayerPauseStepRequest(StringHash eventType, VariantMap& eventData)
  184. {
  185. if (!playerBroker_) return;
  186. VariantMap noEventData;
  187. playerBroker_->PostMessage(E_PAUSESTEPREQUESTED, noEventData);
  188. }
  189. void EditorMode::HandleIPCPlayerExitRequest(StringHash eventType, VariantMap& eventData)
  190. {
  191. if (!playerBroker_) return;
  192. VariantMap noEventData;
  193. playerBroker_->PostMessage(E_EXITREQUESTED, noEventData);
  194. }
  195. bool EditorMode::IsPlayerEnabled()
  196. {
  197. return playerEnabled_;
  198. }
  199. }