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 "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. u32 offset = config->data_entries_offset;
  60. // Perform a search to locate the next offset for the new data
  61. // use the offset and size of the previous block to determine the new position
  62. for (int i = config->total_entries - 1; i >= 0; --i) {
  63. // Ignore the blocks that don't have a separate data offset
  64. if (config->block_entries[i].size > 4) {
  65. offset = config->block_entries[i].offset_or_data +
  66. config->block_entries[i].size;
  67. break;
  68. }
  69. }
  70. config->block_entries[config->total_entries].offset_or_data = offset;
  71. // Write the data at the new offset
  72. memcpy(&cfg_config_file_buffer[offset], data, size);
  73. }
  74. else {
  75. // The offset_or_data field in the header contains the data itself if it's 4 bytes or less
  76. memcpy(&config->block_entries[config->total_entries].offset_or_data, data, size);
  77. }
  78. ++config->total_entries;
  79. return RESULT_SUCCESS;
  80. }
  81. ResultCode DeleteConfigNANDSaveFile() {
  82. FileSys::Path path("config");
  83. if (cfg_system_save_data->DeleteFile(path))
  84. return RESULT_SUCCESS;
  85. return ResultCode(-1); // TODO(Subv): Find the right error code
  86. }
  87. ResultCode UpdateConfigNANDSavegame() {
  88. FileSys::Mode mode = {};
  89. mode.write_flag = 1;
  90. mode.create_flag = 1;
  91. FileSys::Path path("config");
  92. auto file = cfg_system_save_data->OpenFile(path, mode);
  93. _assert_msg_(Service_CFG, file != nullptr, "could not open file");
  94. file->Write(0, CONFIG_SAVEFILE_SIZE, 1, cfg_config_file_buffer.data());
  95. return RESULT_SUCCESS;
  96. }
  97. ResultCode FormatConfig() {
  98. ResultCode res = DeleteConfigNANDSaveFile();
  99. if (!res.IsSuccess())
  100. return res;
  101. // Delete the old data
  102. std::fill(cfg_config_file_buffer.begin(), cfg_config_file_buffer.end(), 0);
  103. // Create the header
  104. SaveFileConfig* config = reinterpret_cast<SaveFileConfig*>(cfg_config_file_buffer.data());
  105. // This value is hardcoded, taken from 3dbrew, verified by hardware, it's always the same value
  106. config->data_entries_offset = 0x455C;
  107. // Insert the default blocks
  108. res = CreateConfigInfoBlk(0x00050005, sizeof(STEREO_CAMERA_SETTINGS), 0xE,
  109. reinterpret_cast<const u8*>(STEREO_CAMERA_SETTINGS.data()));
  110. if (!res.IsSuccess())
  111. return res;
  112. res = CreateConfigInfoBlk(0x00090001, sizeof(CONSOLE_UNIQUE_ID), 0xE,
  113. reinterpret_cast<const u8*>(&CONSOLE_UNIQUE_ID));
  114. if (!res.IsSuccess())
  115. return res;
  116. res = CreateConfigInfoBlk(0x000F0004, sizeof(CONSOLE_MODEL), 0x8,
  117. reinterpret_cast<const u8*>(&CONSOLE_MODEL));
  118. if (!res.IsSuccess())
  119. return res;
  120. res = CreateConfigInfoBlk(0x000A0002, sizeof(CONSOLE_LANGUAGE), 0xA, &CONSOLE_LANGUAGE);
  121. if (!res.IsSuccess())
  122. return res;
  123. res = CreateConfigInfoBlk(0x00070001, sizeof(SOUND_OUTPUT_MODE), 0xE, &SOUND_OUTPUT_MODE);
  124. if (!res.IsSuccess())
  125. return res;
  126. res = CreateConfigInfoBlk(0x000B0000, sizeof(COUNTRY_INFO), 0xE,
  127. reinterpret_cast<const u8*>(&COUNTRY_INFO));
  128. if (!res.IsSuccess())
  129. return res;
  130. res = CreateConfigInfoBlk(0x000A0000, sizeof(CONSOLE_USERNAME_BLOCK), 0xE,
  131. reinterpret_cast<const u8*>(&CONSOLE_USERNAME_BLOCK));
  132. if (!res.IsSuccess())
  133. return res;
  134. // Save the buffer to the file
  135. res = UpdateConfigNANDSavegame();
  136. if (!res.IsSuccess())
  137. return res;
  138. return RESULT_SUCCESS;
  139. }
  140. void CFGInit() {
  141. // TODO(Subv): In the future we should use the FS service to query this archive,
  142. // currently it is not possible because you can only have one open archive of the same type at any time
  143. using Common::make_unique;
  144. std::string syssavedata_directory = FileUtil::GetUserPath(D_SYSSAVEDATA_IDX);
  145. cfg_system_save_data = 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