ToolEnvironment.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // before resource system exists so use rapidjson directly
  2. #include <rapidjson/document.h>
  3. #include <rapidjson/prettywriter.h>
  4. #include <rapidjson/filestream.h>
  5. #include <Atomic/IO/Log.h>
  6. #include <Atomic/IO/FileSystem.h>
  7. #include <Atomic/IO/File.h>
  8. #include "ToolEnvironment.h"
  9. using namespace rapidjson;
  10. namespace ToolCore
  11. {
  12. ToolEnvironment::ToolEnvironment(Context* context) : Object(context)
  13. {
  14. }
  15. ToolEnvironment::~ToolEnvironment()
  16. {
  17. }
  18. bool ToolEnvironment::InitFromPackage()
  19. {
  20. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  21. #ifdef ATOMIC_PLATFORM_WINDOWS
  22. editorBinary_ = fileSystem->GetProgramDir() + "AtomicEditor.exe";
  23. String resourcesDir = fileSystem->GetProgramDir() + "Resources/";
  24. #else
  25. editorBinary_ = fileSystem->GetProgramDir() + "AtomicEditor";
  26. String resourcesDir = GetPath(RemoveTrailingSlash(fileSystem->GetProgramDir())) + "Resources/";
  27. #endif
  28. toolDataDir_ = resourcesDir + "ToolData/";
  29. return true;
  30. }
  31. bool ToolEnvironment::InitFromJSON(bool atomicTool)
  32. {
  33. // make sure config path is initialized
  34. GetDevConfigFilename();
  35. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  36. if (atomicTool || !fileSystem->FileExists(devConfigFilename_))
  37. {
  38. // default to build directories
  39. SetRootSourceDir(ATOMIC_ROOT_SOURCE_DIR);
  40. SetRootBuildDir(ATOMIC_ROOT_BUILD_DIR, true);
  41. return true;
  42. }
  43. File jsonFile(context_, devConfigFilename_);
  44. if (!jsonFile.IsOpen())
  45. return false;
  46. String json;
  47. jsonFile.ReadText(json);
  48. if (!json.Length())
  49. return false;
  50. rapidjson::Document document;
  51. if (document.Parse<0>(json.CString()).HasParseError())
  52. {
  53. return false;
  54. }
  55. const Value::Member* rootSourceDir = document.FindMember("rootSourceDir");
  56. if (rootSourceDir && rootSourceDir->value.IsString())
  57. SetRootSourceDir(rootSourceDir->value.GetString());
  58. else
  59. return false;
  60. const Value::Member* rootBuildDir = document.FindMember("rootBuildDir");
  61. if (rootBuildDir && rootBuildDir->value.IsString())
  62. SetRootBuildDir(rootBuildDir->value.GetString(), true);
  63. else
  64. return false;
  65. return true;
  66. }
  67. const String& ToolEnvironment::GetDevConfigFilename()
  68. {
  69. if (devConfigFilename_.Length())
  70. return devConfigFilename_;
  71. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  72. #ifdef ATOMIC_PLATFORM_OSX
  73. devConfigFilename_ = fileSystem->GetUserDocumentsDir() + ".atomicgameengine/toolEnv.json";
  74. #elif ATOMIC_PLATFORM_WINDOWS
  75. devConfigFilename_ = fileSystem->GetUserDocumentsDir() + "AtomicGameEngine/toolEnv.json";
  76. #endif
  77. return devConfigFilename_;
  78. }
  79. void ToolEnvironment::SetRootSourceDir(const String& sourceDir)
  80. {
  81. rootSourceDir_ = AddTrailingSlash(sourceDir);
  82. resourceCoreDataDir_ = rootSourceDir_ + "Resources/CoreData";
  83. resourcePlayerDataDir_ = rootSourceDir_ + "Resources/PlayerData";
  84. resourceEditorDataDir_ = rootSourceDir_ + "Resources/EditorData";
  85. toolDataDir_ = rootSourceDir_ + "Data/AtomicEditor/";
  86. }
  87. void ToolEnvironment::SetRootBuildDir(const String& buildDir, bool setBinaryPaths)
  88. {
  89. rootBuildDir_ = AddTrailingSlash(buildDir);
  90. if (setBinaryPaths)
  91. {
  92. #ifdef ATOMIC_PLATFORM_WINDOWS
  93. #ifdef _DEBUG
  94. playerBinary_ = rootBuildDir_ + "Source/AtomicPlayer/Debug/AtomicPlayer.exe";
  95. editorBinary_ = rootBuildDir_ + "Source/AtomicEditor/Debug/AtomicEditor.exe";
  96. #else
  97. playerBinary_ = rootBuildDir_ + "Source/AtomicPlayer/Release/AtomicPlayer.exe";
  98. editorBinary_ = rootBuildDir_ + "Source/AtomicEditor/Release/AtomicEditor.exe";
  99. #endif
  100. #elif ATOMIC_PLATFORM_OSX
  101. #ifdef ATOMIC_XCODE
  102. playerBinary_ = rootBuildDir_ + "Source/AtomicPlayer/" + CMAKE_INTDIR + "/AtomicPlayer.app/Contents/MacOS/AtomicPlayer";
  103. editorBinary_ = rootBuildDir_ + "Source/AtomicEditor/" + CMAKE_INTDIR + "/AtomicEditor.app/Contents/MacOS/AtomicEditor";
  104. #else
  105. playerBinary_ = rootBuildDir_ + "Source/AtomicPlayer/AtomicPlayer.app/Contents/MacOS/AtomicPlayer";
  106. editorBinary_ = rootBuildDir_ + "Source/AtomicEditor/AtomicEditor.app/Contents/MacOS/AtomicEditor";
  107. #endif
  108. #endif
  109. }
  110. }
  111. void ToolEnvironment::Dump()
  112. {
  113. LOGINFOF("Root Source Dir: %s", rootSourceDir_.CString());
  114. LOGINFOF("Root Build Dir: %s", rootBuildDir_.CString());
  115. LOGINFOF("Core Resource Dir: %s", resourceCoreDataDir_.CString());
  116. LOGINFOF("Player Resource Dir: %s", resourcePlayerDataDir_.CString());
  117. LOGINFOF("Editor Resource Dir: %s", resourceEditorDataDir_.CString());
  118. LOGINFOF("Editor Binary: %s", editorBinary_.CString());
  119. LOGINFOF("Player Binary: %s", playerBinary_.CString());
  120. LOGINFOF("Tool Binary: %s", toolBinary_.CString());
  121. LOGINFOF("Tool Data Dir: %s", toolDataDir_.CString());
  122. LOGINFOF("Deployment Data Dir: %s", deploymentDataDir_.CString());
  123. LOGINFOF("Dev Config File: %s", devConfigFilename_.CString());
  124. }
  125. }