Project.cpp 5.9 KB

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