AEEditorCommon.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 "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. }
  95. void AEEditorCommon::Stop()
  96. {
  97. context_->RemoveSubsystem<IPC>();
  98. vm_ = 0;
  99. context_->RemoveSubsystem<Javascript>();
  100. // make sure JSVM is really down and no outstanding refs
  101. // as if not, will hold on engine subsystems, which is bad
  102. assert(!JSVM::GetJSVM(0));
  103. #ifdef ATOMIC_DOTNET
  104. NETCore* netCore = GetSubsystem<NETCore>();
  105. if (netCore)
  106. {
  107. netCore->Shutdown();
  108. context_->RemoveSubsystem<NETCore>();
  109. }
  110. #endif
  111. }
  112. bool AEEditorCommon::ReadPreferences(String& path, JSONValue& prefs, const String& propertyName)
  113. {
  114. if (!path.EndsWith(".json"))
  115. path.Append(".json");
  116. SharedPtr<File> file(new File(context_, path, FILE_READ));
  117. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  118. if (!jsonFile->BeginLoad(*file))
  119. return false;
  120. JSONValue root = jsonFile->GetRoot();
  121. if (propertyName.Length() > 0)
  122. {
  123. if (root.Contains(propertyName))
  124. {
  125. prefs = root.Get(propertyName);
  126. }
  127. }
  128. else
  129. {
  130. prefs = root;
  131. }
  132. file->Close();
  133. return true;
  134. }
  135. }