Config.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. const char *keyStrings[] = {
  10. "cache_version",
  11. "auto-update",
  12. "trigger_update",
  13. "last_updated",
  14. "download_title_keys",
  15. "key_urls",
  16. "sleep_mode",
  17. };
  18. }
  19. namespace FreeShop {
  20. Config::Config()
  21. {
  22. static_assert(KEY_COUNT == sizeof(keyStrings)/sizeof(*keyStrings), "Key string count much match the enum count.");
  23. loadDefaults();
  24. }
  25. Config &Config::getInstance()
  26. {
  27. static Config config;
  28. return config;
  29. }
  30. bool Config::loadFromFile(const std::string &filename)
  31. {
  32. rapidjson::Document &json = getInstance().m_json;
  33. std::string path = cpp3ds::FileSystem::getFilePath(filename);
  34. std::string jsonString;
  35. cpp3ds::FileInputStream file;
  36. if (!file.open(filename))
  37. return false;
  38. jsonString.resize(file.getSize());
  39. file.read(&jsonString[0], jsonString.size());
  40. json.Parse(jsonString.c_str());
  41. getInstance().loadDefaults();
  42. return !json.HasParseError();
  43. }
  44. void Config::saveToFile(const std::string &filename)
  45. {
  46. std::string path = cpp3ds::FileSystem::getFilePath(filename);
  47. std::ofstream file(path);
  48. rapidjson::OStreamWrapper osw(file);
  49. rapidjson::Writer<rapidjson::OStreamWrapper> writer(osw);
  50. getInstance().m_json.Accept(writer);
  51. }
  52. bool Config::keyExists(const char *key)
  53. {
  54. return getInstance().m_json.HasMember(key);
  55. }
  56. const rapidjson::Value &Config::get(Key key)
  57. {
  58. return getInstance().m_json[keyStrings[key]];
  59. }
  60. #define ADD_DEFAULT(key, val) \
  61. if (!m_json.HasMember(keyStrings[key])) \
  62. m_json.AddMember(rapidjson::StringRef(keyStrings[key]), val, m_json.GetAllocator());
  63. void Config::loadDefaults()
  64. {
  65. if (!m_json.IsObject())
  66. m_json.SetObject();
  67. ADD_DEFAULT(CacheVersion, "");
  68. // Update settings
  69. ADD_DEFAULT(AutoUpdate, true);
  70. ADD_DEFAULT(TriggerUpdateFlag, false);
  71. ADD_DEFAULT(LastUpdatedTime, 0);
  72. ADD_DEFAULT(DownloadTitleKeys, false);
  73. ADD_DEFAULT(KeyURLs, rapidjson::kArrayType);
  74. // Other settings
  75. ADD_DEFAULT(SleepMode, false);
  76. }
  77. void Config::set(Key key, const char *val)
  78. {
  79. rapidjson::Value v(val, getInstance().m_json.GetAllocator());
  80. set(key, v);
  81. }
  82. void Config::set(Key key, rapidjson::Value &val)
  83. {
  84. const char *keyStr = keyStrings[key];
  85. if (keyExists(keyStr))
  86. getInstance().m_json[keyStr] = val;
  87. else
  88. getInstance().m_json.AddMember(rapidjson::StringRef(keyStr), val, getInstance().m_json.GetAllocator());
  89. }
  90. rapidjson::Document::AllocatorType &Config::getAllocator()
  91. {
  92. return getInstance().m_json.GetAllocator();
  93. }
  94. } // namespace FreeShop