ProjectFile.cpp 3.6 KB

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