AEEditorCommon.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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/Graphics/Graphics.h>
  10. #include <Atomic/IPC/IPC.h>
  11. // Move me to Engine
  12. #include <Atomic/Environment/Environment.h>
  13. #include <Atomic/Script/ScriptSystem.h>
  14. #include <AtomicJS/Javascript/Javascript.h>
  15. #include <ToolCore/ToolSystem.h>
  16. #include <ToolCore/ToolEnvironment.h>
  17. #include <Atomic/IO/File.h>
  18. #ifdef ATOMIC_DOTNET
  19. #include <AtomicNET/NETCore/NETHost.h>
  20. #include <AtomicNET/NETCore/NETCore.h>
  21. #include <AtomicNET/NETScript/NETScript.h>
  22. #endif
  23. #include "../Components/EditorComponents.h"
  24. #include "AEEditorCommon.h"
  25. namespace Atomic
  26. {
  27. void jsapi_init_atomicnet(JSVM* vm);
  28. }
  29. using namespace ToolCore;
  30. namespace ToolCore
  31. {
  32. extern void jsapi_init_toolcore(JSVM* vm);
  33. }
  34. namespace AtomicEditor
  35. {
  36. AEEditorCommon::AEEditorCommon(Context* context) :
  37. Application(context)
  38. {
  39. }
  40. void AEEditorCommon::Start()
  41. {
  42. ValidateWindow();
  43. Input* input = GetSubsystem<Input>();
  44. input->SetMouseVisible(true);
  45. Javascript* javascript = GetSubsystem<Javascript>();
  46. vm_ = javascript->InstantiateVM("MainVM");
  47. vm_->InitJSContext();
  48. jsapi_init_toolcore(vm_);
  49. #ifdef ATOMIC_DOTNET
  50. jsapi_init_atomicnet(vm_);
  51. #endif
  52. }
  53. void AEEditorCommon::Setup()
  54. {
  55. #ifdef ATOMIC_3D
  56. RegisterEnvironmentLibrary(context_);
  57. #endif
  58. RegisterEditorComponentLibrary(context_);
  59. #ifdef ATOMIC_DOTNET
  60. RegisterNETScriptLibrary(context_);
  61. #endif
  62. // Register IPC system
  63. context_->RegisterSubsystem(new IPC(context_));
  64. context_->RegisterSubsystem(new ScriptSystem(context_));
  65. // Instantiate and register the Javascript subsystem
  66. Javascript* javascript = new Javascript(context_);
  67. context_->RegisterSubsystem(javascript);
  68. ToolEnvironment* env = new ToolEnvironment(context_);
  69. context_->RegisterSubsystem(env);
  70. #ifdef ATOMIC_DEV_BUILD
  71. if (!env->InitFromJSON())
  72. {
  73. ErrorExit(ToString("Unable to initialize tool environment from %s", env->GetDevConfigFilename().CString()));
  74. return;
  75. }
  76. #else
  77. env->InitFromPackage();
  78. #endif
  79. #ifdef ATOMIC_DOTNET
  80. // Instantiate and register the AtomicNET subsystem
  81. SharedPtr<NETCore> netCore (new NETCore(context_));
  82. context_->RegisterSubsystem(netCore);
  83. String netCoreErrorMsg;
  84. NETHost::SetCoreCLRFilesAbsPath(env->GetNETCoreCLRAbsPath());
  85. NETHost::SetCoreCLRTPAPaths(env->GetNETTPAPaths());
  86. NETHost::SetCoreCLRAssemblyLoadPaths(env->GetNETAssemblyLoadPaths());
  87. if (!netCore->Initialize(netCoreErrorMsg))
  88. {
  89. LOGERRORF("NetCore: Unable to initialize! %s", netCoreErrorMsg.CString());
  90. context_->RemoveSubsystem(NETCore::GetTypeStatic());
  91. }
  92. else
  93. {
  94. }
  95. #endif
  96. ToolSystem* system = new ToolSystem(context_);
  97. context_->RegisterSubsystem(system);
  98. }
  99. void AEEditorCommon::Stop()
  100. {
  101. context_->RemoveSubsystem<IPC>();
  102. vm_ = 0;
  103. context_->RemoveSubsystem<Javascript>();
  104. // make sure JSVM is really down and no outstanding refs
  105. // as if not, will hold on engine subsystems, which is bad
  106. assert(!JSVM::GetJSVM(0));
  107. #ifdef ATOMIC_DOTNET
  108. NETCore* netCore = GetSubsystem<NETCore>();
  109. if (netCore)
  110. {
  111. netCore->Shutdown();
  112. context_->RemoveSubsystem<NETCore>();
  113. }
  114. #endif
  115. }
  116. bool AEEditorCommon::CreateDefaultPreferences(String& path, JSONValue& prefs)
  117. {
  118. // Note there is some duplication here with the editor's
  119. // TypeScript preference code, this is due to the preferences for
  120. // the editor window needing to be available at window creation time
  121. // It could be better to split this all out to a native, scriptable
  122. // preferences object
  123. LOGINFOF("Creating default Atomic Editor preferences: %s", path.CString());
  124. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  125. JSONValue& root = jsonFile->GetRoot();
  126. root.Clear();
  127. root["recentProjects"] = JSONArray();
  128. JSONValue editorWindow;
  129. GetDefaultWindowPreferences(editorWindow);
  130. JSONValue playerWindow;
  131. GetDefaultWindowPreferences(playerWindow);
  132. root["editorWindow"] = editorWindow;
  133. root["playerWindow"] = playerWindow;
  134. prefs = root;
  135. SavePreferences(prefs);
  136. return true;
  137. }
  138. bool AEEditorCommon::ReadPreferences()
  139. {
  140. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  141. String path = GetPreferencesPath();
  142. JSONValue prefs;
  143. LoadPreferences(prefs);
  144. if (!prefs.IsObject() || !prefs["editorWindow"].IsObject())
  145. {
  146. if (!CreateDefaultPreferences(path, prefs))
  147. return false;
  148. }
  149. JSONValue& editorWindow = prefs["editorWindow"];
  150. engineParameters_["WindowPositionX"] = editorWindow["x"].GetUInt();
  151. engineParameters_["WindowPositionY"] = editorWindow["y"].GetUInt();
  152. engineParameters_["WindowWidth"] = editorWindow["width"].GetUInt();
  153. engineParameters_["WindowHeight"] = editorWindow["height"].GetUInt();
  154. engineParameters_["WindowMaximized"] = editorWindow["maximized"].GetBool();
  155. return true;
  156. }
  157. void AEEditorCommon::ValidateWindow()
  158. {
  159. Graphics* graphics = GetSubsystem<Graphics>();
  160. IntVector2 windowPosition = graphics->GetWindowPosition();
  161. int monitors = graphics->GetNumMonitors();
  162. IntVector2 maxResolution;
  163. for (int i = 0; i < monitors; i++)
  164. {
  165. IntVector2 monitorResolution = graphics->GetMonitorResolution(i);
  166. maxResolution += monitorResolution;
  167. }
  168. if (windowPosition.x_ >= maxResolution.x_ || windowPosition.y_ >= maxResolution.y_ || windowPosition.x_ < 0 || windowPosition.y_ < 0)
  169. {
  170. JSONValue prefs;
  171. if (!LoadPreferences(prefs))
  172. return;
  173. JSONValue window;
  174. GetDefaultWindowPreferences(window);
  175. prefs[context_->GetEditorContext() ? "editorWindow" : "playerWindow"] = window;
  176. graphics->SetMode(0, 0);
  177. graphics->CenterWindow();
  178. if (context_->GetEditorContext())
  179. {
  180. graphics->Maximize();
  181. }
  182. SavePreferences(prefs);
  183. }
  184. }
  185. void AEEditorCommon::GetDefaultWindowPreferences(JSONValue& windowPrefs)
  186. {
  187. windowPrefs["x"] = 0;
  188. windowPrefs["y"] = 0;
  189. windowPrefs["width"] = 0;
  190. windowPrefs["height"] = 0;
  191. windowPrefs["monitor"] = 0;
  192. windowPrefs["maximized"] = context_->GetEditorContext() ? true : false;
  193. }
  194. String AEEditorCommon::GetPreferencesPath()
  195. {
  196. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  197. String path = fileSystem->GetAppPreferencesDir("AtomicEditor", "Preferences");
  198. path += "prefs.json";
  199. return path;
  200. }
  201. bool AEEditorCommon::LoadPreferences(JSONValue& prefs)
  202. {
  203. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  204. String path = GetPreferencesPath();
  205. if (!fileSystem->FileExists(path))
  206. {
  207. if (!CreateDefaultPreferences(path, prefs))
  208. return false;
  209. }
  210. else
  211. {
  212. SharedPtr<File> file(new File(context_, path, FILE_READ));
  213. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  214. if (!jsonFile->BeginLoad(*file))
  215. {
  216. file->Close();
  217. if (!CreateDefaultPreferences(path, prefs))
  218. return false;
  219. }
  220. else
  221. {
  222. prefs = jsonFile->GetRoot();
  223. }
  224. file->Close();
  225. }
  226. return true;
  227. }
  228. bool AEEditorCommon::SavePreferences(JSONValue& prefs)
  229. {
  230. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  231. String path = GetPreferencesPath();
  232. SharedPtr<File> file(new File(context_, path, FILE_WRITE));
  233. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  234. jsonFile->GetRoot() = prefs;
  235. if (!file->IsOpen())
  236. {
  237. LOGERRORF("Unable to open Atomic Editor preferences for writing: %s", path.CString());
  238. return false;
  239. }
  240. jsonFile->Save(*file, " ");
  241. file->Close();
  242. return true;
  243. }
  244. }