Project.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <rapidjson/document.h>
  23. #include <rapidjson/filestream.h>
  24. #include <rapidjson/prettywriter.h>
  25. #include <Atomic/IO/Log.h>
  26. #include <Atomic/IO/File.h>
  27. #include <Atomic/Resource/JSONFile.h>
  28. #include "../ToolSystem.h"
  29. #include "../ToolEvents.h"
  30. #include "../Platform/Platform.h"
  31. #include "ProjectEvents.h"
  32. #include "ProjectFile.h"
  33. #include "ProjectBuildSettings.h"
  34. #include "ProjectUserPrefs.h"
  35. #include "Project.h"
  36. using namespace rapidjson;
  37. namespace ToolCore
  38. {
  39. Project::Project(Context* context) :
  40. Object(context),
  41. loading_(false),
  42. dirty_(false)
  43. {
  44. version_ = "1.0.0";
  45. userPrefs_ = new ProjectUserPrefs(context_);
  46. buildSettings_ = new ProjectBuildSettings(context_);
  47. }
  48. Project::~Project()
  49. {
  50. }
  51. void Project::SaveUserPrefs()
  52. {
  53. String path = GetProjectPath() + "UserPrefs.json";
  54. userPrefs_->Save(path);
  55. }
  56. bool Project::LoadUserPrefs()
  57. {
  58. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  59. String path = GetProjectPath() + "UserPrefs.json";
  60. userPrefs_->Load(path);
  61. // If we're in CLI mode, the Build folder is always relative to project
  62. if (tsystem->IsCLI())
  63. {
  64. String path = GetPath(projectFilePath_) + "Build";
  65. userPrefs_->SetLastBuildPath(path);
  66. }
  67. return true;
  68. }
  69. void Project::SaveBuildSettings()
  70. {
  71. buildSettings_->Save(GetProjectPath() + "BuildSettings.json");
  72. }
  73. bool Project::LoadBuildSettings()
  74. {
  75. buildSettings_->Load(GetProjectPath() + "BuildSettings.json");
  76. return true;
  77. }
  78. void Project::AddPlatform(PlatformID platformID)
  79. {
  80. if (ContainsPlatform(platformID))
  81. return;
  82. SetDirty();
  83. platforms_.Push(platformID);
  84. }
  85. void Project::RemovePlatform(PlatformID platformID)
  86. {
  87. if (!ContainsPlatform(platformID))
  88. return;
  89. }
  90. bool Project::ContainsPlatform(PlatformID platformID)
  91. {
  92. for (List<PlatformID>::ConstIterator i = platforms_.Begin(); i != platforms_.End(); ++i)
  93. {
  94. if ((*i) == platformID)
  95. return true;
  96. }
  97. return false;
  98. }
  99. bool Project::Load(const String& fullpath)
  100. {
  101. loading_ = true;
  102. if (fullpath.Contains(".atomic")) {
  103. projectPath_ = AddTrailingSlash(GetPath(fullpath));
  104. projectFilePath_ = fullpath;
  105. }
  106. else
  107. {
  108. projectPath_ = AddTrailingSlash(fullpath);
  109. projectFilePath_ = projectPath_ + GetFileName(RemoveTrailingSlash(projectPath_)) + ".atomic";
  110. }
  111. SharedPtr<ProjectFile> pfile(new ProjectFile(context_));
  112. bool result = pfile->Load(this);
  113. loading_ = false;
  114. LoadBuildSettings();
  115. LoadUserPrefs();
  116. if ( true /*result*/) {
  117. VariantMap data;
  118. data[ProjectLoaded::P_PROJECTPATH] = projectFilePath_;
  119. SendEvent(E_PROJECTLOADED, data);
  120. }
  121. return result;
  122. }
  123. String Project::GetBuildSettingsFullPath()
  124. {
  125. String path = GetPath(projectFilePath_);
  126. String filename = GetFileName(projectFilePath_);
  127. String buildSettingsPath = path + filename + ".buildsettings";
  128. return buildSettingsPath;
  129. }
  130. String Project::GetUserPrefsFullPath()
  131. {
  132. String path = GetPath(projectFilePath_);
  133. String filename = GetFileName(projectFilePath_);
  134. String prefsPath = path + filename + ".userprefs";
  135. return prefsPath;
  136. }
  137. void Project::Save(const String& fullpath)
  138. {
  139. SharedPtr<ProjectFile> pfile(new ProjectFile(context_));
  140. pfile->Save(this);
  141. dirty_ = false;
  142. }
  143. bool Project::IsComponentsDirOrFile(const String& fullPath)
  144. {
  145. String pathName;
  146. String fileName;
  147. String extension;
  148. SplitPath(fullPath, pathName, fileName, extension);
  149. if (extension.Length() && extension != ".js")
  150. return false;
  151. if (IsAbsoluteParentPath(componentsPath_, pathName))
  152. return true;
  153. return false;
  154. }
  155. bool Project::IsScriptsDirOrFile(const String& fullPath)
  156. {
  157. String pathName;
  158. String fileName;
  159. String extension;
  160. SplitPath(fullPath, pathName, fileName, extension);
  161. if (extension.Length() && extension != ".js")
  162. return false;
  163. if (IsAbsoluteParentPath(scriptsPath_, pathName))
  164. return true;
  165. return false;
  166. }
  167. bool Project::IsModulesDirOrFile(const String& fullPath)
  168. {
  169. String pathName;
  170. String fileName;
  171. String extension;
  172. SplitPath(fullPath, pathName, fileName, extension);
  173. if (extension.Length() && extension != ".js")
  174. return false;
  175. if (IsAbsoluteParentPath(modulesPath_, pathName))
  176. return true;
  177. return false;
  178. }
  179. }