Config.cpp 5.3 KB

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