ProjectFile.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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::Save(Project* project)
  16. {
  17. project_ = project;
  18. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  19. String fullpath = project->GetProjectFilePath();
  20. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  21. JSONValue root = jsonFile->CreateRoot();
  22. root.SetInt("version", PROJECTFILE_VERSION);
  23. // platforms
  24. JSONValue platforms = root.CreateChild("platforms", JSON_ARRAY);
  25. for (List<PlatformID>::ConstIterator i = project_->platforms_.Begin(); i != project_->platforms_.End(); ++i)
  26. {
  27. Platform* platform = tsystem->GetPlatformByID(*i);
  28. if (platform)
  29. {
  30. platforms.AddString(platform->GetName().ToLower());
  31. }
  32. }
  33. // Save to file
  34. SharedPtr<File> file(new File(context_, fullpath, FILE_WRITE));
  35. jsonFile->Save(*file, String(" "));
  36. file->Close();
  37. }
  38. bool ProjectFile::Load(Project* project)
  39. {
  40. project_ = project;
  41. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  42. String fullpath = project->GetProjectFilePath();
  43. SharedPtr<File> file(new File(context_, fullpath, FILE_READ));
  44. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  45. jsonFile->BeginLoad(*file);
  46. JSONValue root = jsonFile->GetRoot();
  47. int version = root.GetInt("version");
  48. if (version != PROJECTFILE_VERSION)
  49. return false;
  50. JSONValue platforms = root.GetChild("platforms");
  51. if (!platforms.IsArray())
  52. return false;
  53. for (unsigned i = 0; i < platforms.GetSize(); i++)
  54. {
  55. String jplatform = platforms.GetString(i);
  56. Platform* platform = tsystem->GetPlatformByName(jplatform);
  57. if (platform)
  58. project_->AddPlatform(platform->GetPlatformID());
  59. }
  60. return true;
  61. }
  62. }