Project.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 "ProjectSettings.h"
  34. #include "ProjectBuildSettings.h"
  35. #include "ProjectUserPrefs.h"
  36. #include "Project.h"
  37. using namespace rapidjson;
  38. namespace ToolCore
  39. {
  40. Project::Project(Context* context) :
  41. Object(context),
  42. loading_(false),
  43. dirty_(false)
  44. {
  45. version_ = "1.0.0";
  46. projectSettings_ = new ProjectSettings(context_);
  47. userPrefs_ = new ProjectUserPrefs(context_);
  48. buildSettings_ = new ProjectBuildSettings(context_);
  49. }
  50. Project::~Project()
  51. {
  52. }
  53. void Project::SaveUserPrefs()
  54. {
  55. String path = GetProjectPath() + "UserPrefs.json";
  56. userPrefs_->Save(path);
  57. }
  58. bool Project::LoadUserPrefs()
  59. {
  60. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  61. String path = GetProjectPath() + "UserPrefs.json";
  62. userPrefs_->Load(path);
  63. // If we're in CLI mode, the Build folder is always relative to project
  64. if (tsystem->IsCLI())
  65. {
  66. String path = GetPath(projectFilePath_) + "Build";
  67. userPrefs_->SetLastBuildPath(path);
  68. }
  69. return true;
  70. }
  71. void Project::SaveBuildSettings()
  72. {
  73. buildSettings_->Save(GetProjectPath() + "BuildSettings.json");
  74. }
  75. bool Project::LoadBuildSettings()
  76. {
  77. buildSettings_->Load(GetProjectPath() + "BuildSettings.json");
  78. return true;
  79. }
  80. bool Project::LoadProjectSettings()
  81. {
  82. projectSettings_->Load(GetProjectPath() + "Settings/Project.json");
  83. return true;
  84. }
  85. bool Project::GetSupportsPlatform(const String& platform) const
  86. {
  87. return projectSettings_->GetSupportsPlatform(platform);
  88. }
  89. bool Project::Load(const String& fullpath)
  90. {
  91. loading_ = true;
  92. if (fullpath.Contains(".atomic")) {
  93. projectPath_ = AddTrailingSlash(GetPath(fullpath));
  94. projectFilePath_ = fullpath;
  95. }
  96. else
  97. {
  98. projectPath_ = AddTrailingSlash(fullpath);
  99. projectFilePath_ = projectPath_ + GetFileName(RemoveTrailingSlash(projectPath_)) + ".atomic";
  100. }
  101. SharedPtr<ProjectFile> pfile(new ProjectFile(context_));
  102. bool result = pfile->Load(this);
  103. loading_ = false;
  104. LoadProjectSettings();
  105. LoadBuildSettings();
  106. LoadUserPrefs();
  107. if ( true /*result*/) {
  108. VariantMap data;
  109. data[ProjectLoaded::P_PROJECTPATH] = projectFilePath_;
  110. data[ProjectLoaded::P_PROJECT] = this;
  111. SendEvent(E_PROJECTLOADED, data);
  112. }
  113. return result;
  114. }
  115. String Project::GetBuildSettingsFullPath()
  116. {
  117. String path = GetPath(projectFilePath_);
  118. String filename = GetFileName(projectFilePath_);
  119. String buildSettingsPath = path + filename + ".buildsettings";
  120. return buildSettingsPath;
  121. }
  122. String Project::GetUserPrefsFullPath()
  123. {
  124. String path = GetPath(projectFilePath_);
  125. String filename = GetFileName(projectFilePath_);
  126. String prefsPath = path + filename + ".userprefs";
  127. return prefsPath;
  128. }
  129. void Project::Save(const String& fullpath)
  130. {
  131. SharedPtr<ProjectFile> pfile(new ProjectFile(context_));
  132. pfile->Save(this);
  133. dirty_ = false;
  134. }
  135. bool Project::IsComponentsDirOrFile(const String& fullPath)
  136. {
  137. String pathName;
  138. String fileName;
  139. String extension;
  140. SplitPath(fullPath, pathName, fileName, extension);
  141. if (extension.Length() && extension != ".js")
  142. return false;
  143. if (IsAbsoluteParentPath(componentsPath_, pathName))
  144. return true;
  145. return false;
  146. }
  147. bool Project::IsScriptsDirOrFile(const String& fullPath)
  148. {
  149. String pathName;
  150. String fileName;
  151. String extension;
  152. SplitPath(fullPath, pathName, fileName, extension);
  153. if (extension.Length() && extension != ".js")
  154. return false;
  155. if (IsAbsoluteParentPath(scriptsPath_, pathName))
  156. return true;
  157. return false;
  158. }
  159. bool Project::IsModulesDirOrFile(const String& fullPath)
  160. {
  161. String pathName;
  162. String fileName;
  163. String extension;
  164. SplitPath(fullPath, pathName, fileName, extension);
  165. if (extension.Length() && extension != ".js")
  166. return false;
  167. if (IsAbsoluteParentPath(modulesPath_, pathName))
  168. return true;
  169. return false;
  170. }
  171. }