AEEditorCommon.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. #include <Atomic/Engine/Engine.h>
  8. #include <Atomic/Input/Input.h>
  9. #include <Atomic/IPC/IPC.h>
  10. // Move me to Engine
  11. #include <Atomic/Environment/Environment.h>
  12. #include <Atomic/Script/ScriptSystem.h>
  13. #include <AtomicJS/Javascript/Javascript.h>
  14. #include <ToolCore/ToolSystem.h>
  15. #include <ToolCore/ToolEnvironment.h>
  16. #include <Atomic/IO/File.h>
  17. #ifdef ATOMIC_DOTNET
  18. #include <AtomicNET/NETCore/NETHost.h>
  19. #include <AtomicNET/NETCore/NETCore.h>
  20. #include <AtomicNET/NETScript/NETScript.h>
  21. #endif
  22. #include "../Components/EditorComponents.h"
  23. #include "AEEditorCommon.h"
  24. namespace Atomic
  25. {
  26. void jsapi_init_atomicnet(JSVM* vm);
  27. }
  28. using namespace ToolCore;
  29. namespace ToolCore
  30. {
  31. extern void jsapi_init_toolcore(JSVM* vm);
  32. }
  33. namespace AtomicEditor
  34. {
  35. AEEditorCommon::AEEditorCommon(Context* context) :
  36. Application(context)
  37. {
  38. }
  39. void AEEditorCommon::Start()
  40. {
  41. Input* input = GetSubsystem<Input>();
  42. input->SetMouseVisible(true);
  43. Javascript* javascript = GetSubsystem<Javascript>();
  44. vm_ = javascript->InstantiateVM("MainVM");
  45. vm_->InitJSContext();
  46. jsapi_init_toolcore(vm_);
  47. #ifdef ATOMIC_DOTNET
  48. jsapi_init_atomicnet(vm_);
  49. #endif
  50. }
  51. void AEEditorCommon::Setup()
  52. {
  53. #ifdef ATOMIC_3D
  54. RegisterEnvironmentLibrary(context_);
  55. #endif
  56. RegisterEditorComponentLibrary(context_);
  57. #ifdef ATOMIC_DOTNET
  58. RegisterNETScriptLibrary(context_);
  59. #endif
  60. // Register IPC system
  61. context_->RegisterSubsystem(new IPC(context_));
  62. context_->RegisterSubsystem(new ScriptSystem(context_));
  63. // Instantiate and register the Javascript subsystem
  64. Javascript* javascript = new Javascript(context_);
  65. context_->RegisterSubsystem(javascript);
  66. ToolEnvironment* env = new ToolEnvironment(context_);
  67. context_->RegisterSubsystem(env);
  68. #ifdef ATOMIC_DEV_BUILD
  69. if (!env->InitFromJSON())
  70. {
  71. ErrorExit(ToString("Unable to initialize tool environment from %s", env->GetDevConfigFilename().CString()));
  72. return;
  73. }
  74. #else
  75. env->InitFromPackage();
  76. #endif
  77. #ifdef ATOMIC_DOTNET
  78. // Instantiate and register the AtomicNET subsystem
  79. SharedPtr<NETCore> netCore (new NETCore(context_));
  80. context_->RegisterSubsystem(netCore);
  81. String netCoreErrorMsg;
  82. NETHost::SetCoreCLRFilesAbsPath(env->GetNETCoreCLRAbsPath());
  83. NETHost::SetCoreCLRTPAPaths(env->GetNETTPAPaths());
  84. NETHost::SetCoreCLRAssemblyLoadPaths(env->GetNETAssemblyLoadPaths());
  85. if (!netCore->Initialize(netCoreErrorMsg))
  86. {
  87. LOGERRORF("NetCore: Unable to initialize! %s", netCoreErrorMsg.CString());
  88. context_->RemoveSubsystem(NETCore::GetTypeStatic());
  89. }
  90. else
  91. {
  92. }
  93. #endif
  94. ToolSystem* system = new ToolSystem(context_);
  95. context_->RegisterSubsystem(system);
  96. }
  97. void AEEditorCommon::Stop()
  98. {
  99. context_->RemoveSubsystem<IPC>();
  100. vm_ = 0;
  101. context_->RemoveSubsystem<Javascript>();
  102. // make sure JSVM is really down and no outstanding refs
  103. // as if not, will hold on engine subsystems, which is bad
  104. assert(!JSVM::GetJSVM(0));
  105. #ifdef ATOMIC_DOTNET
  106. NETCore* netCore = GetSubsystem<NETCore>();
  107. if (netCore)
  108. {
  109. netCore->Shutdown();
  110. context_->RemoveSubsystem<NETCore>();
  111. }
  112. #endif
  113. }
  114. bool AEEditorCommon::CreateDefaultPreferences(String& path, JSONValue& prefs)
  115. {
  116. // Note there is some duplication here with the editor's
  117. // TypeScript preference code, this is due to the preferences for
  118. // the editor window needing to be available at window creation time
  119. // It could be better to split this all out to a native, scriptable
  120. // preferences object
  121. LOGINFOF("Creating default Atomic Editor preferences: %s", path.CString());
  122. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  123. JSONValue& root = jsonFile->GetRoot();
  124. root.Clear();
  125. root["recentProjects"] = JSONArray();
  126. JSONValue editorWindow;
  127. editorWindow["x"] = 0;
  128. editorWindow["y"] = 0;
  129. editorWindow["width"] = 0;
  130. editorWindow["height"] = 0;
  131. editorWindow["monitor"] = 0;
  132. editorWindow["maximized"] = true;
  133. editorWindow["centered"] = false;
  134. JSONValue playerWindow;
  135. playerWindow["x"] = 0;
  136. playerWindow["y"] = 0;
  137. playerWindow["width"] = 1280;
  138. playerWindow["height"] = 720;
  139. playerWindow["monitor"] = 0;
  140. playerWindow["maximized"] = false;
  141. playerWindow["centered"] = true;
  142. root["editorWindow"] = editorWindow;
  143. root["playerWindow"] = playerWindow;
  144. SharedPtr<File> file(new File(context_, path, FILE_WRITE));
  145. if (!file->IsOpen())
  146. {
  147. LOGERRORF("Unable to open Atomic Editor preferences for writing: %s", path.CString());
  148. return false;
  149. }
  150. jsonFile->Save(*file, " ");
  151. prefs = root;
  152. return true;
  153. }
  154. bool AEEditorCommon::ReadPreferences()
  155. {
  156. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  157. String path = fileSystem->GetAppPreferencesDir("AtomicEditor", "Preferences");
  158. path += "prefs.json";
  159. JSONValue prefs;
  160. if (!fileSystem->FileExists(path))
  161. {
  162. if (!CreateDefaultPreferences(path, prefs))
  163. return false;
  164. }
  165. else
  166. {
  167. SharedPtr<File> file(new File(context_, path, FILE_READ));
  168. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  169. if (!jsonFile->BeginLoad(*file))
  170. {
  171. file->Close();
  172. if (!CreateDefaultPreferences(path, prefs))
  173. return false;
  174. }
  175. else
  176. {
  177. prefs = jsonFile->GetRoot();
  178. }
  179. }
  180. if (!prefs.IsObject() || !prefs["editorWindow"].IsObject())
  181. {
  182. if (!CreateDefaultPreferences(path, prefs))
  183. return false;
  184. }
  185. JSONValue& editorWindow = prefs["editorWindow"];
  186. engineParameters_["WindowPositionX"] = editorWindow["x"].GetUInt();
  187. engineParameters_["WindowPositionY"] = editorWindow["y"].GetUInt();
  188. engineParameters_["WindowWidth"] = editorWindow["width"].GetUInt();
  189. engineParameters_["WindowHeight"] = editorWindow["height"].GetUInt();
  190. engineParameters_["WindowMaximized"] = editorWindow["maximized"].GetBool();
  191. return true;
  192. }
  193. }