Project.cpp 6.4 KB

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