ProjectFile.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/IO/Log.h>
  23. #include <Atomic/IO/File.h>
  24. #include <Atomic/Resource/JSONFile.h>
  25. #include "../ToolSystem.h"
  26. #include "Project.h"
  27. #include "ProjectFile.h"
  28. namespace ToolCore
  29. {
  30. ProjectFile::ProjectFile(Context* context) : Object(context)
  31. {
  32. }
  33. ProjectFile::~ProjectFile()
  34. {
  35. }
  36. void ProjectFile::WriteNewProject(const String& fullpath)
  37. {
  38. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  39. JSONValue root = jsonFile->GetRoot();
  40. root.Set("version", PROJECTFILE_VERSION);
  41. // project object
  42. JSONValue jproject;
  43. jproject.Set("version", "1.0.0");
  44. root.Set("project", jproject);
  45. // platforms
  46. JSONValue platforms(JSONValue::emptyArray);
  47. root.Set("platforms", platforms);
  48. SharedPtr<File> file(new File(context_, fullpath, FILE_WRITE));
  49. jsonFile->Save(*file, String(" "));
  50. file->Close();
  51. }
  52. void ProjectFile::Save(Project* project)
  53. {
  54. project_ = project;
  55. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  56. String fullpath = project->GetProjectFilePath();
  57. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  58. JSONValue root = jsonFile->GetRoot();
  59. root.Set("version", PROJECTFILE_VERSION);
  60. // project object
  61. JSONValue jproject;
  62. jproject.Set("version", project_->GetVersion());
  63. root.Set("project", jproject);
  64. // platforms
  65. JSONArray platforms;
  66. for (List<PlatformID>::ConstIterator i = project_->platforms_.Begin(); i != project_->platforms_.End(); ++i)
  67. {
  68. Platform* platform = tsystem->GetPlatformByID(*i);
  69. if (platform)
  70. {
  71. platforms.Push(JSONValue(platform->GetName().ToLower()));
  72. }
  73. }
  74. root.Set("platforms", platforms);
  75. // Save to file
  76. SharedPtr<File> file(new File(context_, fullpath, FILE_WRITE));
  77. jsonFile->Save(*file, String(" "));
  78. file->Close();
  79. }
  80. bool ProjectFile::Load(Project* project)
  81. {
  82. project_ = project;
  83. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  84. String fullpath = project->GetProjectFilePath();
  85. SharedPtr<File> file(new File(context_, fullpath, FILE_READ));
  86. if (file->GetSize() != 0)
  87. {
  88. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  89. if (!jsonFile->BeginLoad(*file))
  90. return false;
  91. JSONValue root = jsonFile->GetRoot();
  92. int version = root.Get("version").GetInt();
  93. if (version != PROJECTFILE_VERSION)
  94. return false;
  95. // project object
  96. JSONValue jproject = root.Get("project");
  97. if (jproject.IsObject())
  98. {
  99. String pversion = jproject.Get("version").GetString();
  100. project_->SetVersion(pversion);
  101. }
  102. JSONValue platforms = root.Get("platforms");
  103. if (!platforms.IsArray())
  104. return false;
  105. }
  106. // for now, every project gets all platforms
  107. project_->AddPlatform(PLATFORMID_WINDOWS);
  108. project_->AddPlatform(PLATFORMID_MAC);
  109. project_->AddPlatform(PLATFORMID_ANDROID);
  110. project_->AddPlatform(PLATFORMID_IOS);
  111. project_->AddPlatform(PLATFORMID_WEB);
  112. /*
  113. for (unsigned i = 0; i < platforms.GetSize(); i++)
  114. {
  115. String jplatform = platforms.GetString(i);
  116. Platform* platform = tsystem->GetPlatformByName(jplatform);
  117. if (platform)
  118. project_->AddPlatform(platform->GetPlatformID());
  119. }
  120. */
  121. return true;
  122. }
  123. }