Project.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 <rapidjson/document.h>
  8. #include <rapidjson/filestream.h>
  9. #include <rapidjson/prettywriter.h>
  10. #include <Atomic/IO/Log.h>
  11. #include <Atomic/IO/File.h>
  12. #include <Atomic/Resource/JSONFile.h>
  13. #include "../ToolSystem.h"
  14. #include "../ToolEvents.h"
  15. #include "../Platform/Platform.h"
  16. #include "ProjectFile.h"
  17. #include "ProjectBuildSettings.h"
  18. #include "ProjectUserPrefs.h"
  19. #include "Project.h"
  20. using namespace rapidjson;
  21. namespace ToolCore
  22. {
  23. Project::Project(Context* context) :
  24. Object(context),
  25. loading_(false),
  26. dirty_(false)
  27. {
  28. version_ = "1.0.0";
  29. userPrefs_ = new ProjectUserPrefs(context_);
  30. buildSettings_ = new ProjectBuildSettings(context_);
  31. }
  32. Project::~Project()
  33. {
  34. }
  35. void Project::SaveUserPrefs()
  36. {
  37. String path = GetProjectPath() + "UserPrefs.json";
  38. userPrefs_->Save(path);
  39. }
  40. bool Project::LoadUserPrefs()
  41. {
  42. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  43. String path = GetProjectPath() + "UserPrefs.json";
  44. userPrefs_->Load(path);
  45. // If we're in CLI mode, the Build folder is always relative to project
  46. if (tsystem->IsCLI())
  47. {
  48. String path = GetPath(projectFilePath_) + "Build";
  49. userPrefs_->SetLastBuildPath(path);
  50. }
  51. return true;
  52. }
  53. void Project::SaveBuildSettings()
  54. {
  55. buildSettings_->Save(GetProjectPath() + "BuildSettings.json");
  56. }
  57. bool Project::LoadBuildSettings()
  58. {
  59. buildSettings_->Load(GetProjectPath() + "BuildSettings.json");
  60. return true;
  61. }
  62. void Project::AddPlatform(PlatformID platformID)
  63. {
  64. if (ContainsPlatform(platformID))
  65. return;
  66. SetDirty();
  67. platforms_.Push(platformID);
  68. }
  69. void Project::RemovePlatform(PlatformID platformID)
  70. {
  71. if (!ContainsPlatform(platformID))
  72. return;
  73. }
  74. bool Project::ContainsPlatform(PlatformID platformID)
  75. {
  76. for (List<PlatformID>::ConstIterator i = platforms_.Begin(); i != platforms_.End(); ++i)
  77. {
  78. if ((*i) == platformID)
  79. return true;
  80. }
  81. return false;
  82. }
  83. bool Project::Load(const String& fullpath)
  84. {
  85. loading_ = true;
  86. if (fullpath.Contains(".atomic")) {
  87. projectPath_ = AddTrailingSlash(GetPath(fullpath));
  88. projectFilePath_ = fullpath;
  89. }
  90. else
  91. {
  92. projectPath_ = AddTrailingSlash(fullpath);
  93. projectFilePath_ = projectPath_ + GetFileName(RemoveTrailingSlash(projectPath_)) + ".atomic";
  94. }
  95. SharedPtr<ProjectFile> pfile(new ProjectFile(context_));
  96. bool result = pfile->Load(this);
  97. loading_ = false;
  98. LoadBuildSettings();
  99. LoadUserPrefs();
  100. if ( true /*result*/) {
  101. VariantMap data;
  102. data[ProjectLoaded::P_PROJECTPATH] = projectFilePath_;
  103. SendEvent(E_PROJECTLOADED, data);
  104. }
  105. return result;
  106. }
  107. String Project::GetBuildSettingsFullPath()
  108. {
  109. String path = GetPath(projectFilePath_);
  110. String filename = GetFileName(projectFilePath_);
  111. String buildSettingsPath = path + filename + ".buildsettings";
  112. return buildSettingsPath;
  113. }
  114. String Project::GetUserPrefsFullPath()
  115. {
  116. String path = GetPath(projectFilePath_);
  117. String filename = GetFileName(projectFilePath_);
  118. String prefsPath = path + filename + ".userprefs";
  119. return prefsPath;
  120. }
  121. void Project::Save(const String& fullpath)
  122. {
  123. SharedPtr<ProjectFile> pfile(new ProjectFile(context_));
  124. pfile->Save(this);
  125. dirty_ = false;
  126. }
  127. bool Project::IsComponentsDirOrFile(const String& fullPath)
  128. {
  129. String pathName;
  130. String fileName;
  131. String extension;
  132. SplitPath(fullPath, pathName, fileName, extension);
  133. if (extension.Length() && extension != ".js")
  134. return false;
  135. if (IsAbsoluteParentPath(componentsPath_, pathName))
  136. return true;
  137. return false;
  138. }
  139. bool Project::IsScriptsDirOrFile(const String& fullPath)
  140. {
  141. String pathName;
  142. String fileName;
  143. String extension;
  144. SplitPath(fullPath, pathName, fileName, extension);
  145. if (extension.Length() && extension != ".js")
  146. return false;
  147. if (IsAbsoluteParentPath(scriptsPath_, pathName))
  148. return true;
  149. return false;
  150. }
  151. bool Project::IsModulesDirOrFile(const String& fullPath)
  152. {
  153. String pathName;
  154. String fileName;
  155. String extension;
  156. SplitPath(fullPath, pathName, fileName, extension);
  157. if (extension.Length() && extension != ".js")
  158. return false;
  159. if (IsAbsoluteParentPath(modulesPath_, pathName))
  160. return true;
  161. return false;
  162. }
  163. }