cfg.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include "common/log.h"
  6. #include "common/make_unique.h"
  7. #include "core/file_sys/archive_systemsavedata.h"
  8. #include "core/hle/service/cfg/cfg.h"
  9. namespace Service {
  10. namespace CFG {
  11. const u64 CFG_SAVE_ID = 0x00010017;
  12. const u64 CONSOLE_UNIQUE_ID = 0xDEADC0DE;
  13. const ConsoleModelInfo CONSOLE_MODEL = { NINTENDO_3DS_XL, 0, 0, 0 };
  14. const u8 CONSOLE_LANGUAGE = LANGUAGE_EN;
  15. const char CONSOLE_USERNAME[0x14] = "CITRA";
  16. /// This will be initialized in CFGInit, and will be used when creating the block
  17. UsernameBlock CONSOLE_USERNAME_BLOCK;
  18. /// TODO(Subv): Find out what this actually is
  19. const u8 SOUND_OUTPUT_MODE = 2;
  20. const u8 UNITED_STATES_COUNTRY_ID = 49;
  21. /// TODO(Subv): Find what the other bytes are
  22. const ConsoleCountryInfo COUNTRY_INFO = { 0, 0, 0, UNITED_STATES_COUNTRY_ID };
  23. /**
  24. * TODO(Subv): Find out what this actually is, these values fix some NaN uniforms in some games,
  25. * for example Nintendo Zone
  26. * Thanks Normmatt for providing this information
  27. */
  28. const std::array<float, 8> STEREO_CAMERA_SETTINGS = {
  29. 62.0f, 289.0f, 76.80000305175781f, 46.08000183105469f,
  30. 10.0f, 5.0f, 55.58000183105469f, 21.56999969482422f
  31. };
  32. static const u32 CONFIG_SAVEFILE_SIZE = 0x8000;
  33. static std::array<u8, CONFIG_SAVEFILE_SIZE> cfg_config_file_buffer;
  34. static std::unique_ptr<FileSys::Archive_SystemSaveData> cfg_system_save_data;
  35. ResultCode GetConfigInfoBlock(u32 block_id, u32 size, u32 flag, u8* output) {
  36. // Read the header
  37. SaveFileConfig* config = reinterpret_cast<SaveFileConfig*>(cfg_config_file_buffer.data());
  38. auto itr = std::find_if(std::begin(config->block_entries), std::end(config->block_entries),
  39. [&](const SaveConfigBlockEntry& entry) {
  40. return entry.block_id == block_id && entry.size == size && (entry.flags & flag);
  41. });
  42. if (itr == std::end(config->block_entries)) {
  43. LOG_ERROR(Service_CFG, "Config block %u with size %u and flags %u not found", block_id, size, flag);
  44. return ResultCode(-1); // TODO(Subv): Find the correct error code
  45. }
  46. // The data is located in the block header itself if the size is less than 4 bytes
  47. if (itr->size <= 4)
  48. memcpy(output, &itr->offset_or_data, itr->size);
  49. else
  50. memcpy(output, &cfg_config_file_buffer[itr->offset_or_data], itr->size);
  51. return RESULT_SUCCESS;
  52. }
  53. ResultCode CreateConfigInfoBlk(u32 block_id, u32 size, u32 flags, const u8* data) {
  54. SaveFileConfig* config = reinterpret_cast<SaveFileConfig*>(cfg_config_file_buffer.data());
  55. if (config->total_entries >= CONFIG_FILE_MAX_BLOCK_ENTRIES)
  56. return ResultCode(-1); // TODO(Subv): Find the right error code
  57. // Insert the block header with offset 0 for now
  58. config->block_entries[config->total_entries] = { block_id, 0, size, flags };
  59. if (size > 4) {
  60. u32 offset = config->data_entries_offset;
  61. // Perform a search to locate the next offset for the new data
  62. // use the offset and size of the previous block to determine the new position
  63. for (int i = config->total_entries - 1; i >= 0; --i) {
  64. // Ignore the blocks that don't have a separate data offset
  65. if (config->block_entries[i].size > 4) {
  66. offset = config->block_entries[i].offset_or_data +
  67. config->block_entries[i].size;
  68. break;
  69. }
  70. }
  71. config->block_entries[config->total_entries].offset_or_data = offset;
  72. // Write the data at the new offset
  73. memcpy(&cfg_config_file_buffer[offset], data, size);
  74. }
  75. else {
  76. // The offset_or_data field in the header contains the data itself if it's 4 bytes or less
  77. memcpy(&config->block_entries[config->total_entries].offset_or_data, data, size);
  78. }
  79. ++config->total_entries;
  80. return RESULT_SUCCESS;
  81. }
  82. ResultCode DeleteConfigNANDSaveFile() {
  83. FileSys::Path path("config");
  84. if (cfg_system_save_data->DeleteFile(path))
  85. return RESULT_SUCCESS;
  86. return ResultCode(-1); // TODO(Subv): Find the right error code
  87. }
  88. ResultCode UpdateConfigNANDSavegame() {
  89. FileSys::Mode mode = {};
  90. mode.write_flag = 1;
  91. mode.create_flag = 1;
  92. FileSys::Path path("config");
  93. auto file = cfg_system_save_data->OpenFile(path, mode);
  94. _assert_msg_(Service_CFG, file != nullptr, "could not open file");
  95. file->Write(0, CONFIG_SAVEFILE_SIZE, 1, cfg_config_file_buffer.data());
  96. return RESULT_SUCCESS;
  97. }
  98. ResultCode FormatConfig() {
  99. ResultCode res = DeleteConfigNANDSaveFile();
  100. if (!res.IsSuccess())
  101. return res;
  102. // Delete the old data
  103. cfg_config_file_buffer.fill(0);
  104. // Create the header
  105. SaveFileConfig* config = reinterpret_cast<SaveFileConfig*>(cfg_config_file_buffer.data());
  106. // This value is hardcoded, taken from 3dbrew, verified by hardware, it's always the same value
  107. config->data_entries_offset = 0x455C;
  108. // Insert the default blocks
  109. res = CreateConfigInfoBlk(0x00050005, sizeof(STEREO_CAMERA_SETTINGS), 0xE,
  110. reinterpret_cast<const u8*>(STEREO_CAMERA_SETTINGS.data()));
  111. if (!res.IsSuccess())
  112. return res;
  113. res = CreateConfigInfoBlk(0x00090001, sizeof(CONSOLE_UNIQUE_ID), 0xE,
  114. reinterpret_cast<const u8*>(&CONSOLE_UNIQUE_ID));
  115. if (!res.IsSuccess())
  116. return res;
  117. res = CreateConfigInfoBlk(0x000F0004, sizeof(CONSOLE_MODEL), 0x8,
  118. reinterpret_cast<const u8*>(&CONSOLE_MODEL));
  119. if (!res.IsSuccess())
  120. return res;
  121. res = CreateConfigInfoBlk(0x000A0002, sizeof(CONSOLE_LANGUAGE), 0xA, &CONSOLE_LANGUAGE);
  122. if (!res.IsSuccess())
  123. return res;
  124. res = CreateConfigInfoBlk(0x00070001, sizeof(SOUND_OUTPUT_MODE), 0xE, &SOUND_OUTPUT_MODE);
  125. if (!res.IsSuccess())
  126. return res;
  127. res = CreateConfigInfoBlk(0x000B0000, sizeof(COUNTRY_INFO), 0xE,
  128. reinterpret_cast<const u8*>(&COUNTRY_INFO));
  129. if (!res.IsSuccess())
  130. return res;
  131. res = CreateConfigInfoBlk(0x000A0000, sizeof(CONSOLE_USERNAME_BLOCK), 0xE,
  132. reinterpret_cast<const u8*>(&CONSOLE_USERNAME_BLOCK));
  133. if (!res.IsSuccess())
  134. return res;
  135. // Save the buffer to the file
  136. res = UpdateConfigNANDSavegame();
  137. if (!res.IsSuccess())
  138. return res;
  139. return RESULT_SUCCESS;
  140. }
  141. void CFGInit() {
  142. // TODO(Subv): In the future we should use the FS service to query this archive,
  143. // currently it is not possible because you can only have one open archive of the same type at any time
  144. std::string syssavedata_directory = FileUtil::GetUserPath(D_SYSSAVEDATA_IDX);
  145. cfg_system_save_data = Common::make_unique<FileSys::Archive_SystemSaveData>(
  146. syssavedata_directory, CFG_SAVE_ID);
  147. if (!cfg_system_save_data->Initialize()) {
  148. LOG_CRITICAL(Service_CFG, "Could not initialize SystemSaveData archive for the CFG:U service");
  149. return;
  150. }
  151. // TODO(Subv): All this code should be moved to cfg:i,
  152. // it's only here because we do not currently emulate the lower level code that uses that service
  153. // Try to open the file in read-only mode to check its existence
  154. FileSys::Mode mode = {};
  155. mode.read_flag = 1;
  156. FileSys::Path path("config");
  157. auto file = cfg_system_save_data->OpenFile(path, mode);
  158. // Load the config if it already exists
  159. if (file != nullptr) {
  160. file->Read(0, CONFIG_SAVEFILE_SIZE, cfg_config_file_buffer.data());
  161. return;
  162. }
  163. // Initialize the Username block
  164. // TODO(Subv): Initialize this directly in the variable when MSVC supports char16_t string literals
  165. CONSOLE_USERNAME_BLOCK.ng_word = 0;
  166. CONSOLE_USERNAME_BLOCK.zero = 0;
  167. // Copy string to buffer and pad with zeros at the end
  168. auto size = Common::UTF8ToUTF16(CONSOLE_USERNAME).copy(CONSOLE_USERNAME_BLOCK.username, 0x14);
  169. std::fill(std::begin(CONSOLE_USERNAME_BLOCK.username) + size,
  170. std::end(CONSOLE_USERNAME_BLOCK.username), 0);
  171. FormatConfig();
  172. }
  173. void CFGShutdown() {
  174. }
  175. } // namespace CFG
  176. } // namespace Service