Project.cpp 4.6 KB

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