cfg.cpp 8.0 KB

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