Config.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include <cpp3ds/System/FileSystem.hpp>
  2. #include <fstream>
  3. #include <rapidjson/ostreamwrapper.h>
  4. #include <rapidjson/writer.h>
  5. #include <cpp3ds/System/FileInputStream.hpp>
  6. #include "Config.hpp"
  7. #include "Util.hpp"
  8. namespace {
  9. // Matches with Config::Key enum in Config.hpp
  10. const char *keyStrings[] = {
  11. "cache_version",
  12. "trigger_update",
  13. "show_news",
  14. // Filter
  15. "filter_region",
  16. "filter_genre",
  17. "filter_language",
  18. "filter_platform",
  19. // Sort
  20. // Update
  21. "auto-update",
  22. "last_updated",
  23. "download_title_keys",
  24. "key_urls",
  25. // Download
  26. "download_timeout",
  27. "download_buffer_size",
  28. "play_sound_after_download",
  29. "power_off_after_download",
  30. "power_off_time",
  31. // Other
  32. "sleep_mode",
  33. "language",
  34. };
  35. }
  36. namespace FreeShop {
  37. Config::Config()
  38. {
  39. static_assert(KEY_COUNT == sizeof(keyStrings)/sizeof(*keyStrings), "Key string count must match the enum count.");
  40. loadDefaults();
  41. }
  42. Config &Config::getInstance()
  43. {
  44. static Config config;
  45. return config;
  46. }
  47. bool Config::loadFromFile(const std::string &filename)
  48. {
  49. rapidjson::Document &json = getInstance().m_json;
  50. std::string path = cpp3ds::FileSystem::getFilePath(filename);
  51. std::string jsonString;
  52. cpp3ds::FileInputStream file;
  53. if (!file.open(filename))
  54. return false;
  55. jsonString.resize(file.getSize());
  56. file.read(&jsonString[0], jsonString.size());
  57. json.Parse(jsonString.c_str());
  58. getInstance().loadDefaults();
  59. return !json.HasParseError();
  60. }
  61. void Config::saveToFile(const std::string &filename)
  62. {
  63. std::string path = cpp3ds::FileSystem::getFilePath(filename);
  64. std::ofstream file(path);
  65. rapidjson::OStreamWrapper osw(file);
  66. rapidjson::Writer<rapidjson::OStreamWrapper> writer(osw);
  67. getInstance().m_json.Accept(writer);
  68. }
  69. bool Config::keyExists(const char *key)
  70. {
  71. return getInstance().m_json.HasMember(key);
  72. }
  73. const rapidjson::Value &Config::get(Key key)
  74. {
  75. return getInstance().m_json[keyStrings[key]];
  76. }
  77. #define ADD_DEFAULT(key, val) \
  78. if (!m_json.HasMember(keyStrings[key])) \
  79. m_json.AddMember(rapidjson::StringRef(keyStrings[key]), val, m_json.GetAllocator());
  80. void Config::loadDefaults()
  81. {
  82. if (!m_json.IsObject())
  83. m_json.SetObject();
  84. ADD_DEFAULT(CacheVersion, "");
  85. ADD_DEFAULT(TriggerUpdateFlag, false);
  86. ADD_DEFAULT(ShowNews, true);
  87. // Filter
  88. ADD_DEFAULT(FilterRegion, rapidjson::kArrayType);
  89. ADD_DEFAULT(FilterGenre, rapidjson::kArrayType);
  90. ADD_DEFAULT(FilterLanguage, rapidjson::kArrayType);
  91. ADD_DEFAULT(FilterPlatform, rapidjson::kArrayType);
  92. // Update
  93. ADD_DEFAULT(AutoUpdate, true);
  94. ADD_DEFAULT(LastUpdatedTime, 0);
  95. ADD_DEFAULT(DownloadTitleKeys, false);
  96. ADD_DEFAULT(KeyURLs, rapidjson::kArrayType);
  97. // Download
  98. ADD_DEFAULT(DownloadTimeout, 6.f);
  99. ADD_DEFAULT(DownloadBufferSize, 128u);
  100. ADD_DEFAULT(PlaySoundAfterDownload, true);
  101. ADD_DEFAULT(PowerOffAfterDownload, false);
  102. ADD_DEFAULT(PowerOffTime, 120);
  103. // Other
  104. ADD_DEFAULT(SleepMode, true);
  105. ADD_DEFAULT(Language, "auto");
  106. }
  107. void Config::set(Key key, const char *val)
  108. {
  109. rapidjson::Value v(val, getInstance().m_json.GetAllocator());
  110. set(key, v);
  111. }
  112. void Config::set(Key key, rapidjson::Value &val)
  113. {
  114. const char *keyStr = keyStrings[key];
  115. if (keyExists(keyStr))
  116. getInstance().m_json[keyStr] = val;
  117. else
  118. getInstance().m_json.AddMember(rapidjson::StringRef(keyStr), val, getInstance().m_json.GetAllocator());
  119. }
  120. rapidjson::Document::AllocatorType &Config::getAllocator()
  121. {
  122. return getInstance().m_json.GetAllocator();
  123. }
  124. } // namespace FreeShop