Project.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. readOnly_(false)
  43. {
  44. version_ = "1.0.0";
  45. projectSettings_ = new ProjectSettings(context_);
  46. userPrefs_ = new ProjectUserPrefs(context_);
  47. buildSettings_ = new ProjectBuildSettings(context_);
  48. }
  49. Project::~Project()
  50. {
  51. }
  52. void Project::SaveUserPrefs()
  53. {
  54. String path = GetProjectPath() + "UserPrefs.json";
  55. userPrefs_->Save(path);
  56. }
  57. bool Project::LoadUserPrefs()
  58. {
  59. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  60. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  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 (tenv->GetCLI())
  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, bool readOnly)
  90. {
  91. loading_ = true;
  92. readOnly_ = readOnly;
  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. VariantMap data;
  133. data[ProjectBeginLoad::P_PROJECTPATH] = projectFilePath_;
  134. data[ProjectBeginLoad::P_PROJECT] = this;
  135. SendEvent(E_PROJECTBEGINLOAD, data);
  136. SharedPtr<ProjectFile> pfile(new ProjectFile(context_));
  137. bool result = pfile->Load(this);
  138. loading_ = false;
  139. LoadProjectSettings();
  140. LoadBuildSettings();
  141. LoadUserPrefs();
  142. if ( true /*result*/) {
  143. VariantMap data;
  144. data[ProjectLoaded::P_PROJECTPATH] = projectFilePath_;
  145. data[ProjectLoaded::P_PROJECT] = this;
  146. data[ProjectLoaded::P_RESULT] = result;
  147. SendEvent(E_PROJECTLOADED, data);
  148. }
  149. return result;
  150. }
  151. String Project::GetBuildSettingsFullPath()
  152. {
  153. String path = GetPath(projectFilePath_);
  154. String filename = GetFileName(projectFilePath_);
  155. String buildSettingsPath = path + filename + ".buildsettings";
  156. return buildSettingsPath;
  157. }
  158. String Project::GetUserPrefsFullPath()
  159. {
  160. String path = GetPath(projectFilePath_);
  161. String filename = GetFileName(projectFilePath_);
  162. String prefsPath = path + filename + ".userprefs";
  163. return prefsPath;
  164. }
  165. void Project::Save(const String& fullpath)
  166. {
  167. SharedPtr<ProjectFile> pfile(new ProjectFile(context_));
  168. pfile->Save(this);
  169. dirty_ = false;
  170. }
  171. bool Project::IsComponentsDirOrFile(const String& fullPath)
  172. {
  173. String pathName;
  174. String fileName;
  175. String extension;
  176. SplitPath(fullPath, pathName, fileName, extension);
  177. if (extension.Length() && extension != ".js")
  178. return false;
  179. if (IsAbsoluteParentPath(componentsPath_, pathName))
  180. return true;
  181. return false;
  182. }
  183. bool Project::IsScriptsDirOrFile(const String& fullPath)
  184. {
  185. String pathName;
  186. String fileName;
  187. String extension;
  188. SplitPath(fullPath, pathName, fileName, extension);
  189. if (extension.Length() && extension != ".js")
  190. return false;
  191. if (IsAbsoluteParentPath(scriptsPath_, pathName))
  192. return true;
  193. return false;
  194. }
  195. bool Project::IsModulesDirOrFile(const String& fullPath)
  196. {
  197. String pathName;
  198. String fileName;
  199. String extension;
  200. SplitPath(fullPath, pathName, fileName, extension);
  201. if (extension.Length() && extension != ".js")
  202. return false;
  203. if (IsAbsoluteParentPath(modulesPath_, pathName))
  204. return true;
  205. return false;
  206. }
  207. }