AEProject.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 "AtomicEditor.h"
  8. #include <Atomic/IO/File.h>
  9. #include <Atomic/IO/FileSystem.h>
  10. #include <Atomic/IO/Log.h>
  11. #include <Atomic/Core/Context.h>
  12. #include "../AEEditor.h"
  13. #include "Build/BuildSystem.h"
  14. #include "AEProject.h"
  15. using namespace rapidjson;
  16. namespace AtomicEditor
  17. {
  18. Project::Project(Context* context) :
  19. Object(context),
  20. loading_(false)
  21. {
  22. }
  23. Project::~Project()
  24. {
  25. }
  26. void Project::LoadUserPrefs(const String& fullpath)
  27. {
  28. rapidjson::Document document;
  29. File jsonFile(context_, fullpath);
  30. if (!jsonFile.IsOpen())
  31. return;
  32. String json;
  33. jsonFile.ReadText(json);
  34. if (!json.Length())
  35. return;
  36. if (document.Parse<0>(json.CString()).HasParseError())
  37. {
  38. LOGERRORF("Could not parse Project JSON data from %s", fullpath.CString());
  39. return;
  40. }
  41. const Value::Member* current_platform = document.FindMember("current_platform");
  42. AEEditorPlatform platform = AE_PLATFORM_UNDEFINED;
  43. if (current_platform && current_platform->value.IsString())
  44. {
  45. String splatform = current_platform->value.GetString();
  46. if (splatform == "Windows")
  47. platform = AE_PLATFORM_WINDOWS;
  48. else if (splatform == "Mac")
  49. platform = AE_PLATFORM_MAC;
  50. else if (splatform == "HTML5")
  51. platform = AE_PLATFORM_HTML5;
  52. else if (splatform == "iOS")
  53. platform = AE_PLATFORM_IOS;
  54. else if (splatform == "Android")
  55. platform = AE_PLATFORM_ANDROID;
  56. }
  57. if (platform == AE_PLATFORM_UNDEFINED)
  58. {
  59. #ifdef ATOMIC_PLATFORM_OSX
  60. platform = AE_PLATFORM_MAC;
  61. #else
  62. platform = AE_PLATFORM_WINDOWS;
  63. #endif
  64. }
  65. const Value::Member* last_build_path = document.FindMember("last_build_path");
  66. if (last_build_path && last_build_path->value.IsString())
  67. {
  68. lastBuildPath_ = last_build_path->value.GetString();
  69. }
  70. // probably will want to move this, it will trigger a save (which is guarded with load_)
  71. Editor* editor = GetSubsystem<Editor>();
  72. editor->RequestPlatformChange(platform);
  73. }
  74. void Project::SaveUserPrefs(const String& fullpath)
  75. {
  76. Editor* editor = GetSubsystem<Editor>();
  77. FILE* file = fopen(fullpath.CString(), "w");
  78. if (!file)
  79. return;
  80. rapidjson::FileStream s(file);
  81. rapidjson::PrettyWriter<rapidjson::FileStream> writer(s);
  82. writer.StartObject();
  83. writer.String("version");
  84. writer.Int(1);
  85. writer.String("current_platform");
  86. AEEditorPlatform platform = editor->GetCurrentPlatform();
  87. if (platform == AE_PLATFORM_WINDOWS)
  88. writer.String("Windows");
  89. else if (platform == AE_PLATFORM_MAC)
  90. writer.String("Mac");
  91. else if (platform == AE_PLATFORM_HTML5)
  92. writer.String("HTML5");
  93. else if (platform == AE_PLATFORM_IOS)
  94. writer.String("iOS");
  95. else if (platform == AE_PLATFORM_ANDROID)
  96. writer.String("Android");
  97. writer.String("last_build_path");
  98. writer.String(lastBuildPath_.CString());
  99. writer.EndObject();
  100. fclose(file);
  101. }
  102. void Project::Load(const String& fullpath)
  103. {
  104. loading_ = true;
  105. projectFilePath_ = fullpath;
  106. LoadUserPrefs(GetUserPrefsFullPath(fullpath));
  107. rapidjson::Document document;
  108. File jsonFile(context_, fullpath);
  109. if (!jsonFile.IsOpen())
  110. {
  111. loading_ = false;
  112. return;
  113. }
  114. String json;
  115. jsonFile.ReadText(json);
  116. if (!json.Length())
  117. {
  118. loading_ = false;
  119. return;
  120. }
  121. if (document.Parse<0>(json.CString()).HasParseError())
  122. {
  123. LOGERRORF("Could not parse Project JSON data from %s", fullpath.CString());
  124. loading_ = false;
  125. return;
  126. }
  127. const Value::Member* version = document.FindMember("version");
  128. if (version && version->value.IsInt())
  129. {
  130. }
  131. Value::Member* build_settings = document.FindMember("build_settings");
  132. if (build_settings && build_settings->value.IsObject())
  133. {
  134. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  135. buildSystem->LoadBuildSettings(build_settings);
  136. }
  137. loading_ = false;
  138. }
  139. String Project::GetUserPrefsFullPath(const String& projectPath)
  140. {
  141. String path = GetPath(projectPath);
  142. String filename = GetFileName(projectPath);
  143. String prefsPath = path + "/" + filename + ".atomic.userprefs";
  144. return prefsPath;
  145. }
  146. void Project::Save(const String& fullpath)
  147. {
  148. if (loading_)
  149. return;
  150. if (fullpath.Length())
  151. projectFilePath_ = fullpath;
  152. String path = projectFilePath_;
  153. SaveUserPrefs(GetUserPrefsFullPath(path));
  154. FILE* file = fopen(path.CString(), "w");
  155. if (!file)
  156. return;
  157. rapidjson::FileStream s(file);
  158. rapidjson::PrettyWriter<rapidjson::FileStream> writer(s);
  159. writer.StartObject();
  160. writer.String("version");
  161. writer.Int(1);
  162. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  163. buildSystem->SaveBuildSettings(writer);
  164. writer.EndObject();
  165. fclose(file);
  166. }
  167. bool Project::IsComponentsDirOrFile(const String& fullPath)
  168. {
  169. String pathName;
  170. String fileName;
  171. String extension;
  172. SplitPath(fullPath, pathName, fileName, extension);
  173. if (extension.Length() && extension != ".js")
  174. return false;
  175. if (IsAbsoluteParentPath(componentsPath_, pathName))
  176. return true;
  177. return false;
  178. }
  179. bool Project::IsScriptsDirOrFile(const String& fullPath)
  180. {
  181. String pathName;
  182. String fileName;
  183. String extension;
  184. SplitPath(fullPath, pathName, fileName, extension);
  185. if (extension.Length() && extension != ".js")
  186. return false;
  187. if (IsAbsoluteParentPath(scriptsPath_, pathName))
  188. return true;
  189. return false;
  190. }
  191. bool Project::IsModulesDirOrFile(const String& fullPath)
  192. {
  193. String pathName;
  194. String fileName;
  195. String extension;
  196. SplitPath(fullPath, pathName, fileName, extension);
  197. if (extension.Length() && extension != ".js")
  198. return false;
  199. if (IsAbsoluteParentPath(modulesPath_, pathName))
  200. return true;
  201. return false;
  202. }
  203. }