ProjectFile.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. SharedPtr<File> file(new File(context_, fullpath, FILE_WRITE));
  46. jsonFile->Save(*file, String(" "));
  47. file->Close();
  48. }
  49. void ProjectFile::Save(Project* project)
  50. {
  51. project_ = project;
  52. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  53. String fullpath = project->GetProjectFilePath();
  54. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  55. JSONValue& root = jsonFile->GetRoot();
  56. root.Set("version", PROJECTFILE_VERSION);
  57. // project object
  58. JSONValue jproject;
  59. jproject.Set("version", project_->GetVersion());
  60. root.Set("project", jproject);
  61. // Save to file
  62. SharedPtr<File> file(new File(context_, fullpath, FILE_WRITE));
  63. jsonFile->Save(*file, String(" "));
  64. file->Close();
  65. }
  66. bool ProjectFile::Load(Project* project)
  67. {
  68. project_ = project;
  69. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  70. String fullpath = project->GetProjectFilePath();
  71. SharedPtr<File> file(new File(context_, fullpath, FILE_READ));
  72. if (file->GetSize() != 0)
  73. {
  74. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  75. if (!jsonFile->BeginLoad(*file))
  76. return false;
  77. JSONValue& root = jsonFile->GetRoot();
  78. int version = root.Get("version").GetInt();
  79. if (version != PROJECTFILE_VERSION)
  80. return false;
  81. // project object
  82. JSONValue jproject = root.Get("project");
  83. if (jproject.IsObject())
  84. {
  85. String pversion = jproject.Get("version").GetString();
  86. project_->SetVersion(pversion);
  87. }
  88. }
  89. return true;
  90. }
  91. }