AEEditorCommon.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/Resource/JSONFile.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 "AEEditorCommon.h"
  23. namespace Atomic
  24. {
  25. void jsapi_init_atomicnet(JSVM* vm);
  26. }
  27. using namespace ToolCore;
  28. namespace ToolCore
  29. {
  30. extern void jsapi_init_toolcore(JSVM* vm);
  31. }
  32. namespace AtomicEditor
  33. {
  34. AEEditorCommon::AEEditorCommon(Context* context) :
  35. Application(context)
  36. {
  37. }
  38. void AEEditorCommon::Start()
  39. {
  40. Input* input = GetSubsystem<Input>();
  41. input->SetMouseVisible(true);
  42. Javascript* javascript = GetSubsystem<Javascript>();
  43. vm_ = javascript->InstantiateVM("MainVM");
  44. vm_->InitJSContext();
  45. jsapi_init_toolcore(vm_);
  46. #ifdef ATOMIC_DOTNET
  47. jsapi_init_atomicnet(vm_);
  48. #endif
  49. }
  50. void AEEditorCommon::Setup()
  51. {
  52. #ifdef ATOMIC_3D
  53. RegisterEnvironmentLibrary(context_);
  54. #endif
  55. #ifdef ATOMIC_DOTNET
  56. RegisterNETScriptLibrary(context_);
  57. #endif
  58. // Register IPC system
  59. context_->RegisterSubsystem(new IPC(context_));
  60. context_->RegisterSubsystem(new ScriptSystem(context_));
  61. // Instantiate and register the Javascript subsystem
  62. Javascript* javascript = new Javascript(context_);
  63. context_->RegisterSubsystem(javascript);
  64. ToolEnvironment* env = new ToolEnvironment(context_);
  65. context_->RegisterSubsystem(env);
  66. #ifdef ATOMIC_DEV_BUILD
  67. if (!env->InitFromJSON())
  68. {
  69. ErrorExit(ToString("Unable to initialize tool environment from %s", env->GetDevConfigFilename().CString()));
  70. return;
  71. }
  72. #else
  73. env->InitFromPackage();
  74. #endif
  75. #ifdef ATOMIC_DOTNET
  76. // Instantiate and register the AtomicNET subsystem
  77. SharedPtr<NETCore> netCore (new NETCore(context_));
  78. context_->RegisterSubsystem(netCore);
  79. String netCoreErrorMsg;
  80. NETHost::SetCoreCLRFilesAbsPath(env->GetNETCoreCLRAbsPath());
  81. NETHost::SetCoreCLRTPAPaths(env->GetNETTPAPaths());
  82. NETHost::SetCoreCLRAssemblyLoadPaths(env->GetNETAssemblyLoadPaths());
  83. if (!netCore->Initialize(netCoreErrorMsg))
  84. {
  85. LOGERRORF("NetCore: Unable to initialize! %s", netCoreErrorMsg.CString());
  86. context_->RemoveSubsystem(NETCore::GetTypeStatic());
  87. }
  88. else
  89. {
  90. }
  91. #endif
  92. ToolSystem* system = new ToolSystem(context_);
  93. context_->RegisterSubsystem(system);
  94. ReadPreferences();
  95. }
  96. void AEEditorCommon::Stop()
  97. {
  98. context_->RemoveSubsystem<IPC>();
  99. vm_ = 0;
  100. context_->RemoveSubsystem<Javascript>();
  101. // make sure JSVM is really down and no outstanding refs
  102. // as if not, will hold on engine subsystems, which is bad
  103. assert(!JSVM::GetJSVM(0));
  104. #ifdef ATOMIC_DOTNET
  105. NETCore* netCore = GetSubsystem<NETCore>();
  106. if (netCore)
  107. {
  108. netCore->Shutdown();
  109. context_->RemoveSubsystem<NETCore>();
  110. }
  111. #endif
  112. }
  113. bool AEEditorCommon::ReadPreferences()
  114. {
  115. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  116. String preferencesPath = fileSystem->GetAppPreferencesDir("AtomicEditor", "Preferences");
  117. preferencesPath += "prefs.json";
  118. SharedPtr<File> file(new File(context_, preferencesPath, FILE_READ));
  119. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  120. if (!jsonFile->BeginLoad(*file))
  121. return false;
  122. JSONValue root = jsonFile->GetRoot();
  123. JSONValue editorWindow = root.Get("editorWindow");
  124. if (!editorWindow.IsObject())
  125. return false;
  126. engineParameters_["WindowPositionX"] = editorWindow.Get("x").GetUInt();
  127. engineParameters_["WindowPositionY"] = editorWindow.Get("y").GetUInt();
  128. engineParameters_["WindowWidth"] = editorWindow.Get("width").GetUInt();
  129. engineParameters_["WindowHeight"] = editorWindow.Get("height").GetUInt();
  130. engineParameters_["FullScreen"] = editorWindow.Get("fullscreen").GetBool();
  131. file->Close();
  132. return true;
  133. }
  134. }