AEPreferences.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include "AtomicEditor.h"
  5. #include <rapidjson/document.h>
  6. #include "rapidjson/prettywriter.h"
  7. #include "rapidjson/filestream.h"
  8. #include <Atomic/Core/Context.h>
  9. #include <Atomic/IO/FileSystem.h>
  10. #include <Atomic/IO/Log.h>
  11. #include <Atomic/IO/File.h>
  12. #include "AEPreferences.h"
  13. using namespace rapidjson;
  14. namespace AtomicEditor
  15. {
  16. AEPreferences::AEPreferences(Context* context) :
  17. Object(context)
  18. {
  19. context->RegisterSubsystem(this);
  20. Read();
  21. }
  22. AEPreferences::~AEPreferences()
  23. {
  24. }
  25. String AEPreferences::GetPreferencesFullPath()
  26. {
  27. FileSystem* fs = GetSubsystem<FileSystem>();
  28. String filepath = fs->GetAppPreferencesDir("AtomicEditor", "Preferences");
  29. filepath += "prefs.json";
  30. return filepath;
  31. }
  32. void AEPreferences::Clear()
  33. {
  34. recentProjects_.Clear();
  35. }
  36. void AEPreferences::Read()
  37. {
  38. rapidjson::Document document;
  39. String filepath = GetPreferencesFullPath();
  40. File jsonFile(context_, filepath);
  41. if (!jsonFile.IsOpen())
  42. return;
  43. String json;
  44. jsonFile.ReadText(json);
  45. if (!json.Length())
  46. return;
  47. if (document.Parse<0>(json.CString()).HasParseError())
  48. {
  49. LOGERRORF("Could not parse JSON data from %s", filepath.CString());
  50. return;
  51. }
  52. Clear();
  53. const Value::Member* recent_files = document.FindMember("recent_files");
  54. if (recent_files && recent_files->value.IsArray())
  55. {
  56. for (Value::ConstValueIterator itr = recent_files->value.Begin(); itr != recent_files->value.End(); itr++)
  57. {
  58. if (!(*itr).IsString())
  59. continue;
  60. String path(itr->GetString());
  61. recentProjects_.Push(path.CString());
  62. }
  63. }
  64. const Value::Member* android_sdk_path = document.FindMember("android_sdk_path");
  65. if (android_sdk_path && android_sdk_path->value.IsString())
  66. androidSDKPath_ = android_sdk_path->value.GetString();
  67. }
  68. void AEPreferences::Write()
  69. {
  70. String filepath = GetPreferencesFullPath();
  71. FILE* file = fopen(filepath.CString(), "w");
  72. if (!file)
  73. return;
  74. rapidjson::FileStream s(file);
  75. rapidjson::PrettyWriter<rapidjson::FileStream> writer(s);
  76. writer.StartObject();
  77. // recent files
  78. writer.String("recent_files");
  79. writer.StartArray();
  80. for (unsigned i = 0; i < recentProjects_.Size(); i++)
  81. writer.String(recentProjects_[i].CString());
  82. writer.EndArray();
  83. writer.String("android_sdk_path");
  84. writer.String(androidSDKPath_.CString());
  85. writer.EndObject();
  86. fclose(file);
  87. }
  88. void AEPreferences::RegisterRecentProject(const String& fullpath)
  89. {
  90. if (recentProjects_.Contains(fullpath))
  91. {
  92. recentProjects_.Remove(fullpath);
  93. }
  94. recentProjects_.Insert(0, fullpath);
  95. Write();
  96. }
  97. }