Project.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/IO/FileSystem.h>
  28. #include <Atomic/Resource/JSONFile.h>
  29. #include "../ToolSystem.h"
  30. #include "../ToolEvents.h"
  31. #include "../Platform/Platform.h"
  32. #include "ProjectEvents.h"
  33. #include "ProjectFile.h"
  34. #include "ProjectSettings.h"
  35. #include "ProjectBuildSettings.h"
  36. #include "ProjectUserPrefs.h"
  37. #include "Project.h"
  38. using namespace rapidjson;
  39. namespace ToolCore
  40. {
  41. Project::Project(Context* context) :
  42. Object(context),
  43. loading_(false),
  44. dirty_(false)
  45. {
  46. version_ = "1.0.0";
  47. projectSettings_ = new ProjectSettings(context_);
  48. userPrefs_ = new ProjectUserPrefs(context_);
  49. buildSettings_ = new ProjectBuildSettings(context_);
  50. }
  51. Project::~Project()
  52. {
  53. }
  54. void Project::SaveUserPrefs()
  55. {
  56. String path = GetProjectPath() + "UserPrefs.json";
  57. userPrefs_->Save(path);
  58. }
  59. bool Project::LoadUserPrefs()
  60. {
  61. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  62. String path = GetProjectPath() + "UserPrefs.json";
  63. userPrefs_->Load(path);
  64. // If we're in CLI mode, the Build folder is always relative to project
  65. if (tsystem->IsCLI())
  66. {
  67. String path = GetPath(projectFilePath_) + "Build";
  68. userPrefs_->SetLastBuildPath(path);
  69. }
  70. return true;
  71. }
  72. void Project::SaveBuildSettings()
  73. {
  74. buildSettings_->Save(GetProjectPath() + "BuildSettings.json");
  75. }
  76. bool Project::LoadBuildSettings()
  77. {
  78. buildSettings_->Load(GetProjectPath() + "BuildSettings.json");
  79. return true;
  80. }
  81. bool Project::LoadProjectSettings()
  82. {
  83. projectSettings_->Load(GetProjectPath() + "Settings/Project.json");
  84. return true;
  85. }
  86. bool Project::GetSupportsPlatform(const String& platform) const
  87. {
  88. return projectSettings_->GetSupportsPlatform(platform);
  89. }
  90. bool Project::Load(const String& fullpath)
  91. {
  92. loading_ = true;
  93. if (fullpath.Contains(".atomic")) {
  94. projectPath_ = AddTrailingSlash(GetPath(fullpath));
  95. projectFilePath_ = fullpath;
  96. }
  97. else
  98. {
  99. projectPath_ = AddTrailingSlash(fullpath);
  100. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  101. StringVector results;
  102. fileSystem->ScanDir(results, projectPath_, "*.atomic", SCAN_FILES, false);
  103. if (!results.Size())
  104. {
  105. // no atomic file, so use the parent path name
  106. projectFilePath_ = projectPath_ + GetFileName(RemoveTrailingSlash(projectPath_)) + ".atomic";
  107. }
  108. else
  109. {
  110. String result = projectPath_ + results[0];
  111. if (results.Size() > 1)
  112. {
  113. // multiple *.atomic files, use newest, and report
  114. unsigned newest = 0xFFFFFFFF;
  115. for (unsigned i = 0; i < results.Size(); i++)
  116. {
  117. String atomicFilePath = projectPath_ + results[i];
  118. unsigned modtime = fileSystem->GetLastModifiedTime(atomicFilePath);
  119. if (!modtime)
  120. continue;
  121. if (newest > modtime)
  122. {
  123. newest = modtime;
  124. result = atomicFilePath;
  125. }
  126. }
  127. ATOMIC_LOGERRORF("Project::Load - Multiple .atomic files found in project, selecting newest: %s", result.CString());
  128. }
  129. projectFilePath_ = result;
  130. }
  131. }
  132. SharedPtr<ProjectFile> pfile(new ProjectFile(context_));
  133. bool result = pfile->Load(this);
  134. loading_ = false;
  135. LoadProjectSettings();
  136. LoadBuildSettings();
  137. LoadUserPrefs();
  138. if ( true /*result*/) {
  139. VariantMap data;
  140. data[ProjectLoaded::P_PROJECTPATH] = projectFilePath_;
  141. data[ProjectLoaded::P_PROJECT] = this;
  142. SendEvent(E_PROJECTLOADED, data);
  143. }
  144. return result;
  145. }
  146. String Project::GetBuildSettingsFullPath()
  147. {
  148. String path = GetPath(projectFilePath_);
  149. String filename = GetFileName(projectFilePath_);
  150. String buildSettingsPath = path + filename + ".buildsettings";
  151. return buildSettingsPath;
  152. }
  153. String Project::GetUserPrefsFullPath()
  154. {
  155. String path = GetPath(projectFilePath_);
  156. String filename = GetFileName(projectFilePath_);
  157. String prefsPath = path + filename + ".userprefs";
  158. return prefsPath;
  159. }
  160. void Project::Save(const String& fullpath)
  161. {
  162. SharedPtr<ProjectFile> pfile(new ProjectFile(context_));
  163. pfile->Save(this);
  164. dirty_ = false;
  165. }
  166. bool Project::IsComponentsDirOrFile(const String& fullPath)
  167. {
  168. String pathName;
  169. String fileName;
  170. String extension;
  171. SplitPath(fullPath, pathName, fileName, extension);
  172. if (extension.Length() && extension != ".js")
  173. return false;
  174. if (IsAbsoluteParentPath(componentsPath_, pathName))
  175. return true;
  176. return false;
  177. }
  178. bool Project::IsScriptsDirOrFile(const String& fullPath)
  179. {
  180. String pathName;
  181. String fileName;
  182. String extension;
  183. SplitPath(fullPath, pathName, fileName, extension);
  184. if (extension.Length() && extension != ".js")
  185. return false;
  186. if (IsAbsoluteParentPath(scriptsPath_, pathName))
  187. return true;
  188. return false;
  189. }
  190. bool Project::IsModulesDirOrFile(const String& fullPath)
  191. {
  192. String pathName;
  193. String fileName;
  194. String extension;
  195. SplitPath(fullPath, pathName, fileName, extension);
  196. if (extension.Length() && extension != ".js")
  197. return false;
  198. if (IsAbsoluteParentPath(modulesPath_, pathName))
  199. return true;
  200. return false;
  201. }
  202. }