Config.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. "version",
  12. "cache_version",
  13. "trigger_update",
  14. "show_news",
  15. // Filter
  16. "filter_region",
  17. "filter_genre",
  18. "filter_language",
  19. "filter_platform",
  20. // Sort
  21. // Update
  22. "auto-update",
  23. "last_updated",
  24. "download_title_keys",
  25. "key_urls",
  26. // Download
  27. "download_timeout",
  28. "download_buffer_size",
  29. "play_sound_after_download",
  30. "power_off_after_download",
  31. "power_off_time",
  32. // Music
  33. "music_mode",
  34. "music_filename",
  35. "music_off_low_slider",
  36. // Locales
  37. "language",
  38. "keyboard",
  39. "use_system_keyboard",
  40. // Notifiers
  41. "led_startup",
  42. "led_on_download_finish",
  43. "led_on_download_error",
  44. "news_download_finish",
  45. "news_no_led",
  46. // Inactivity
  47. "sleep_mode",
  48. "sleep_mode_bottom",
  49. "dim_leds",
  50. "sound_on_inactivity",
  51. "music_on_inactivity",
  52. "inactivity_seconds",
  53. // Other
  54. "title_id",
  55. "skiddo",
  56. "show_battery_percentage",
  57. "show_game_counter",
  58. // Internals
  59. "reset_eshop_music",
  60. "clean_exit",
  61. };
  62. }
  63. namespace FreeShop {
  64. Config::Config()
  65. {
  66. static_assert(KEY_COUNT == sizeof(keyStrings)/sizeof(*keyStrings), "Key string count must match the enum count.");
  67. loadDefaults();
  68. }
  69. Config &Config::getInstance()
  70. {
  71. static Config config;
  72. return config;
  73. }
  74. bool Config::loadFromFile(const std::string &filename)
  75. {
  76. rapidjson::Document &json = getInstance().m_json;
  77. std::string path = cpp3ds::FileSystem::getFilePath(filename);
  78. std::string jsonString;
  79. cpp3ds::FileInputStream file;
  80. if (!file.open(filename))
  81. return false;
  82. jsonString.resize(file.getSize());
  83. file.read(&jsonString[0], jsonString.size());
  84. json.Parse(jsonString.c_str());
  85. getInstance().loadDefaults();
  86. return !json.HasParseError();
  87. }
  88. void Config::saveToFile(const std::string &filename)
  89. {
  90. std::string path = cpp3ds::FileSystem::getFilePath(filename);
  91. std::ofstream file(path);
  92. rapidjson::OStreamWrapper osw(file);
  93. rapidjson::Writer<rapidjson::OStreamWrapper> writer(osw);
  94. getInstance().m_json.Accept(writer);
  95. }
  96. bool Config::keyExists(const char *key)
  97. {
  98. return getInstance().m_json.HasMember(key);
  99. }
  100. const rapidjson::Value &Config::get(Key key)
  101. {
  102. return getInstance().m_json[keyStrings[key]];
  103. }
  104. #define ADD_DEFAULT(key, val) \
  105. if (!m_json.HasMember(keyStrings[key])) \
  106. m_json.AddMember(rapidjson::StringRef(keyStrings[key]), val, m_json.GetAllocator());
  107. void Config::loadDefaults()
  108. {
  109. if (!m_json.IsObject())
  110. m_json.SetObject();
  111. ADD_DEFAULT(Version, "");
  112. ADD_DEFAULT(CacheVersion, "");
  113. ADD_DEFAULT(TriggerUpdateFlag, false);
  114. ADD_DEFAULT(ShowNews, true);
  115. // Filter
  116. ADD_DEFAULT(FilterRegion, rapidjson::kArrayType);
  117. ADD_DEFAULT(FilterGenre, rapidjson::kArrayType);
  118. ADD_DEFAULT(FilterLanguage, rapidjson::kArrayType);
  119. ADD_DEFAULT(FilterPlatform, rapidjson::kArrayType);
  120. // Update
  121. ADD_DEFAULT(AutoUpdate, true);
  122. ADD_DEFAULT(LastUpdatedTime, 0);
  123. ADD_DEFAULT(DownloadTitleKeys, false);
  124. ADD_DEFAULT(KeyURLs, rapidjson::kArrayType);
  125. // Download
  126. ADD_DEFAULT(DownloadTimeout, 6.f);
  127. ADD_DEFAULT(DownloadBufferSize, 128u);
  128. ADD_DEFAULT(PlaySoundAfterDownload, true);
  129. ADD_DEFAULT(PowerOffAfterDownload, false);
  130. ADD_DEFAULT(PowerOffTime, 120);
  131. // Music
  132. ADD_DEFAULT(MusicMode, "eshop");
  133. ADD_DEFAULT(MusicFilename, "");
  134. ADD_DEFAULT(MusicTurnOffSlider, true);
  135. // Locales
  136. ADD_DEFAULT(Language, "auto");
  137. ADD_DEFAULT(Keyboard, "qwerty");
  138. ADD_DEFAULT(SystemKeyboard, false);
  139. // Notifiers
  140. ADD_DEFAULT(LEDStartup, true);
  141. ADD_DEFAULT(LEDDownloadFinished, true);
  142. ADD_DEFAULT(LEDDownloadError, true);
  143. ADD_DEFAULT(NEWSDownloadFinished, false);
  144. ADD_DEFAULT(NEWSNoLED, false);
  145. // Inactivity
  146. ADD_DEFAULT(SleepMode, true);
  147. ADD_DEFAULT(SleepModeBottom, false);
  148. ADD_DEFAULT(DimLEDs, false);
  149. ADD_DEFAULT(SoundOnInactivity, true);
  150. ADD_DEFAULT(MusicOnInactivity, false);
  151. ADD_DEFAULT(InactivitySeconds, 60.f);
  152. // Other
  153. ADD_DEFAULT(TitleID, false);
  154. ADD_DEFAULT(Skiddo, false);
  155. ADD_DEFAULT(ShowBattery, false);
  156. ADD_DEFAULT(ShowGameCounter, true);
  157. // Internals
  158. ADD_DEFAULT(ResetEshopMusic, false);
  159. ADD_DEFAULT(CleanExit, true);
  160. }
  161. void Config::set(Key key, const char *val)
  162. {
  163. rapidjson::Value v(val, getInstance().m_json.GetAllocator());
  164. set(key, v);
  165. }
  166. void Config::set(Key key, rapidjson::Value &val)
  167. {
  168. const char *keyStr = keyStrings[key];
  169. if (keyExists(keyStr))
  170. getInstance().m_json[keyStr] = val;
  171. else
  172. getInstance().m_json.AddMember(rapidjson::StringRef(keyStr), val, getInstance().m_json.GetAllocator());
  173. }
  174. rapidjson::Document::AllocatorType &Config::getAllocator()
  175. {
  176. return getInstance().m_json.GetAllocator();
  177. }
  178. } // namespace FreeShop