ProjectFile.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include <Atomic/IO/Log.h>
  2. #include <Atomic/IO/File.h>
  3. #include <Atomic/Resource/JSONFile.h>
  4. #include "../ToolSystem.h"
  5. #include "Project.h"
  6. #include "ProjectFile.h"
  7. namespace ToolCore
  8. {
  9. ProjectFile::ProjectFile(Context* context) : Object(context)
  10. {
  11. }
  12. ProjectFile::~ProjectFile()
  13. {
  14. }
  15. void ProjectFile::WriteNewProject(const String& fullpath)
  16. {
  17. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  18. JSONValue root = jsonFile->GetRoot();
  19. root.Set("version", PROJECTFILE_VERSION);
  20. // project object
  21. JSONValue jproject;
  22. jproject.Set("version", "1.0.0");
  23. root.Set("project", jproject);
  24. // platforms
  25. JSONValue platforms(JSONValue::emptyArray);
  26. root.Set("platforms", platforms);
  27. SharedPtr<File> file(new File(context_, fullpath, FILE_WRITE));
  28. jsonFile->Save(*file, String(" "));
  29. file->Close();
  30. }
  31. void ProjectFile::Save(Project* project)
  32. {
  33. project_ = project;
  34. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  35. String fullpath = project->GetProjectFilePath();
  36. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  37. JSONValue root = jsonFile->GetRoot();
  38. root.Set("version", PROJECTFILE_VERSION);
  39. // project object
  40. JSONValue jproject;
  41. jproject.Set("version", project_->GetVersion());
  42. root.Set("project", jproject);
  43. // platforms
  44. JSONArray platforms;
  45. for (List<PlatformID>::ConstIterator i = project_->platforms_.Begin(); i != project_->platforms_.End(); ++i)
  46. {
  47. Platform* platform = tsystem->GetPlatformByID(*i);
  48. if (platform)
  49. {
  50. platforms.Push(JSONValue(platform->GetName().ToLower()));
  51. }
  52. }
  53. root.Set("platforms", platforms);
  54. // Save to file
  55. SharedPtr<File> file(new File(context_, fullpath, FILE_WRITE));
  56. jsonFile->Save(*file, String(" "));
  57. file->Close();
  58. }
  59. bool ProjectFile::Load(Project* project)
  60. {
  61. project_ = project;
  62. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  63. String fullpath = project->GetProjectFilePath();
  64. SharedPtr<File> file(new File(context_, fullpath, FILE_READ));
  65. if (file->GetSize() != 0)
  66. {
  67. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  68. if (!jsonFile->BeginLoad(*file))
  69. return false;
  70. JSONValue root = jsonFile->GetRoot();
  71. int version = root.Get("version").GetInt();
  72. if (version != PROJECTFILE_VERSION)
  73. return false;
  74. // project object
  75. JSONValue jproject = root.Get("project");
  76. if (jproject.IsObject())
  77. {
  78. String pversion = jproject.Get("version").GetString();
  79. project_->SetVersion(pversion);
  80. }
  81. JSONValue platforms = root.Get("platforms");
  82. if (!platforms.IsArray())
  83. return false;
  84. }
  85. // for now, every project gets all platforms
  86. project_->AddPlatform(PLATFORMID_WINDOWS);
  87. project_->AddPlatform(PLATFORMID_MAC);
  88. project_->AddPlatform(PLATFORMID_ANDROID);
  89. project_->AddPlatform(PLATFORMID_IOS);
  90. project_->AddPlatform(PLATFORMID_WEB);
  91. /*
  92. for (unsigned i = 0; i < platforms.GetSize(); i++)
  93. {
  94. String jplatform = platforms.GetString(i);
  95. Platform* platform = tsystem->GetPlatformByName(jplatform);
  96. if (platform)
  97. project_->AddPlatform(platform->GetPlatformID());
  98. }
  99. */
  100. return true;
  101. }
  102. }