Project.cpp 6.9 KB

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