Project.cpp 6.8 KB

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