Project.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include <rapidjson/document.h>
  5. #include <rapidjson/filestream.h>
  6. #include <rapidjson/prettywriter.h>
  7. #include <Atomic/IO/Log.h>
  8. #include <Atomic/IO/File.h>
  9. #include <Atomic/Resource/JSONFile.h>
  10. #include "../ToolSystem.h"
  11. #include "../ToolEvents.h"
  12. #include "../Platform/Platform.h"
  13. #include "ProjectFile.h"
  14. #include "ProjectBuildSettings.h"
  15. #include "ProjectUserPrefs.h"
  16. #include "Project.h"
  17. using namespace rapidjson;
  18. namespace ToolCore
  19. {
  20. Project::Project(Context* context) :
  21. Object(context),
  22. loading_(false),
  23. dirty_(false)
  24. {
  25. version_ = "1.0.0";
  26. userPrefs_ = new ProjectUserPrefs(context_);
  27. buildSettings_ = new ProjectBuildSettings(context_);
  28. }
  29. Project::~Project()
  30. {
  31. }
  32. void Project::SaveUserPrefs()
  33. {
  34. String path = GetProjectPath() + "UserPrefs.json";
  35. userPrefs_->Save(path);
  36. }
  37. bool Project::LoadUserPrefs()
  38. {
  39. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  40. String path = GetProjectPath() + "UserPrefs.json";
  41. userPrefs_->Load(path);
  42. // If we're in CLI mode, the Build folder is always relative to project
  43. if (tsystem->IsCLI())
  44. {
  45. String path = GetPath(projectFilePath_) + "Build";
  46. userPrefs_->SetLastBuildPath(path);
  47. }
  48. return true;
  49. }
  50. void Project::SaveBuildSettings()
  51. {
  52. buildSettings_->Save(GetProjectPath() + "BuildSettings.json");
  53. }
  54. bool Project::LoadBuildSettings()
  55. {
  56. buildSettings_->Load(GetProjectPath() + "BuildSettings.json");
  57. return true;
  58. }
  59. void Project::AddPlatform(PlatformID platformID)
  60. {
  61. if (ContainsPlatform(platformID))
  62. return;
  63. SetDirty();
  64. platforms_.Push(platformID);
  65. }
  66. void Project::RemovePlatform(PlatformID platformID)
  67. {
  68. if (!ContainsPlatform(platformID))
  69. return;
  70. }
  71. bool Project::ContainsPlatform(PlatformID platformID)
  72. {
  73. for (List<PlatformID>::ConstIterator i = platforms_.Begin(); i != platforms_.End(); ++i)
  74. {
  75. if ((*i) == platformID)
  76. return true;
  77. }
  78. return false;
  79. }
  80. bool Project::Load(const String& fullpath)
  81. {
  82. loading_ = true;
  83. if (fullpath.Contains(".atomic")) {
  84. projectPath_ = AddTrailingSlash(GetPath(fullpath));
  85. projectFilePath_ = fullpath;
  86. }
  87. else
  88. {
  89. projectPath_ = AddTrailingSlash(fullpath);
  90. projectFilePath_ = projectPath_ + GetFileName(RemoveTrailingSlash(projectPath_)) + ".atomic";
  91. }
  92. SharedPtr<ProjectFile> pfile(new ProjectFile(context_));
  93. bool result = pfile->Load(this);
  94. loading_ = false;
  95. LoadBuildSettings();
  96. LoadUserPrefs();
  97. if ( true /*result*/) {
  98. VariantMap data;
  99. data[ProjectLoaded::P_PROJECTPATH] = projectFilePath_;
  100. SendEvent(E_PROJECTLOADED, data);
  101. }
  102. return result;
  103. }
  104. String Project::GetBuildSettingsFullPath()
  105. {
  106. String path = GetPath(projectFilePath_);
  107. String filename = GetFileName(projectFilePath_);
  108. String buildSettingsPath = path + filename + ".buildsettings";
  109. return buildSettingsPath;
  110. }
  111. String Project::GetUserPrefsFullPath()
  112. {
  113. String path = GetPath(projectFilePath_);
  114. String filename = GetFileName(projectFilePath_);
  115. String prefsPath = path + filename + ".userprefs";
  116. return prefsPath;
  117. }
  118. void Project::Save(const String& fullpath)
  119. {
  120. SharedPtr<ProjectFile> pfile(new ProjectFile(context_));
  121. pfile->Save(this);
  122. dirty_ = false;
  123. }
  124. bool Project::IsComponentsDirOrFile(const String& fullPath)
  125. {
  126. String pathName;
  127. String fileName;
  128. String extension;
  129. SplitPath(fullPath, pathName, fileName, extension);
  130. if (extension.Length() && extension != ".js")
  131. return false;
  132. if (IsAbsoluteParentPath(componentsPath_, pathName))
  133. return true;
  134. return false;
  135. }
  136. bool Project::IsScriptsDirOrFile(const String& fullPath)
  137. {
  138. String pathName;
  139. String fileName;
  140. String extension;
  141. SplitPath(fullPath, pathName, fileName, extension);
  142. if (extension.Length() && extension != ".js")
  143. return false;
  144. if (IsAbsoluteParentPath(scriptsPath_, pathName))
  145. return true;
  146. return false;
  147. }
  148. bool Project::IsModulesDirOrFile(const String& fullPath)
  149. {
  150. String pathName;
  151. String fileName;
  152. String extension;
  153. SplitPath(fullPath, pathName, fileName, extension);
  154. if (extension.Length() && extension != ".js")
  155. return false;
  156. if (IsAbsoluteParentPath(modulesPath_, pathName))
  157. return true;
  158. return false;
  159. }
  160. }