mii.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <memory>
  4. #include "common/logging/log.h"
  5. #include "core/hle/service/cmif_serialization.h"
  6. #include "core/hle/service/ipc_helpers.h"
  7. #include "core/hle/service/mii/mii.h"
  8. #include "core/hle/service/mii/mii_manager.h"
  9. #include "core/hle/service/mii/mii_result.h"
  10. #include "core/hle/service/mii/types/char_info.h"
  11. #include "core/hle/service/mii/types/raw_data.h"
  12. #include "core/hle/service/mii/types/store_data.h"
  13. #include "core/hle/service/mii/types/ver3_store_data.h"
  14. #include "core/hle/service/server_manager.h"
  15. #include "core/hle/service/set/system_settings_server.h"
  16. #include "core/hle/service/sm/sm.h"
  17. namespace Service::Mii {
  18. class IDatabaseService final : public ServiceFramework<IDatabaseService> {
  19. public:
  20. explicit IDatabaseService(Core::System& system_, std::shared_ptr<MiiManager> mii_manager,
  21. bool is_system_)
  22. : ServiceFramework{system_, "IDatabaseService"}, manager{mii_manager},
  23. is_system{is_system_} {
  24. // clang-format off
  25. static const FunctionInfo functions[] = {
  26. {0, D<&IDatabaseService::IsUpdated>, "IsUpdated"},
  27. {1, D<&IDatabaseService::IsFullDatabase>, "IsFullDatabase"},
  28. {2, D<&IDatabaseService::GetCount>, "GetCount"},
  29. {3, D<&IDatabaseService::Get>, "Get"},
  30. {4, D<&IDatabaseService::Get1>, "Get1"},
  31. {5, D<&IDatabaseService::UpdateLatest>, "UpdateLatest"},
  32. {6, D<&IDatabaseService::BuildRandom>, "BuildRandom"},
  33. {7, D<&IDatabaseService::BuildDefault>, "BuildDefault"},
  34. {8, D<&IDatabaseService::Get2>, "Get2"},
  35. {9, D<&IDatabaseService::Get3>, "Get3"},
  36. {10, D<&IDatabaseService::UpdateLatest1>, "UpdateLatest1"},
  37. {11, D<&IDatabaseService::FindIndex>, "FindIndex"},
  38. {12, D<&IDatabaseService::Move>, "Move"},
  39. {13, D<&IDatabaseService::AddOrReplace>, "AddOrReplace"},
  40. {14, D<&IDatabaseService::Delete>, "Delete"},
  41. {15, D<&IDatabaseService::DestroyFile>, "DestroyFile"},
  42. {16, D<&IDatabaseService::DeleteFile>, "DeleteFile"},
  43. {17, D<&IDatabaseService::Format>, "Format"},
  44. {18, nullptr, "Import"},
  45. {19, nullptr, "Export"},
  46. {20, D<&IDatabaseService::IsBrokenDatabaseWithClearFlag>, "IsBrokenDatabaseWithClearFlag"},
  47. {21, D<&IDatabaseService::GetIndex>, "GetIndex"},
  48. {22, D<&IDatabaseService::SetInterfaceVersion>, "SetInterfaceVersion"},
  49. {23, D<&IDatabaseService::Convert>, "Convert"},
  50. {24, D<&IDatabaseService::ConvertCoreDataToCharInfo>, "ConvertCoreDataToCharInfo"},
  51. {25, D<&IDatabaseService::ConvertCharInfoToCoreData>, "ConvertCharInfoToCoreData"},
  52. {26, D<&IDatabaseService::Append>, "Append"},
  53. };
  54. // clang-format on
  55. RegisterHandlers(functions);
  56. m_set_sys = system.ServiceManager().GetService<Service::Set::ISystemSettingsServer>(
  57. "set:sys", true);
  58. manager->Initialize(metadata);
  59. }
  60. private:
  61. Result IsUpdated(Out<bool> out_is_updated, SourceFlag source_flag) {
  62. LOG_DEBUG(Service_Mii, "called with source_flag={}", source_flag);
  63. *out_is_updated = manager->IsUpdated(metadata, source_flag);
  64. R_SUCCEED();
  65. }
  66. Result IsFullDatabase(Out<bool> out_is_full_database) {
  67. LOG_DEBUG(Service_Mii, "called");
  68. *out_is_full_database = manager->IsFullDatabase();
  69. R_SUCCEED();
  70. }
  71. Result GetCount(Out<u32> out_mii_count, SourceFlag source_flag) {
  72. *out_mii_count = manager->GetCount(metadata, source_flag);
  73. LOG_DEBUG(Service_Mii, "called with source_flag={}, mii_count={}", source_flag,
  74. *out_mii_count);
  75. R_SUCCEED();
  76. }
  77. Result Get(Out<u32> out_mii_count, SourceFlag source_flag,
  78. OutArray<CharInfoElement, BufferAttr_HipcMapAlias> char_info_element_buffer) {
  79. const auto result =
  80. manager->Get(metadata, char_info_element_buffer, *out_mii_count, source_flag);
  81. LOG_INFO(Service_Mii, "called with source_flag={}, mii_count={}", source_flag,
  82. *out_mii_count);
  83. R_RETURN(result);
  84. }
  85. Result Get1(Out<u32> out_mii_count, SourceFlag source_flag,
  86. OutArray<CharInfo, BufferAttr_HipcMapAlias> char_info_buffer) {
  87. const auto result = manager->Get(metadata, char_info_buffer, *out_mii_count, source_flag);
  88. LOG_INFO(Service_Mii, "called with source_flag={}, mii_count={}", source_flag,
  89. *out_mii_count);
  90. R_RETURN(result);
  91. }
  92. Result UpdateLatest(Out<CharInfo> out_char_info, const CharInfo& char_info,
  93. SourceFlag source_flag) {
  94. LOG_INFO(Service_Mii, "called with source_flag={}", source_flag);
  95. R_RETURN(manager->UpdateLatest(metadata, *out_char_info, char_info, source_flag));
  96. }
  97. Result BuildRandom(Out<CharInfo> out_char_info, Age age, Gender gender, Race race) {
  98. LOG_DEBUG(Service_Mii, "called with age={}, gender={}, race={}", age, gender, race);
  99. R_UNLESS(age <= Age::All, ResultInvalidArgument);
  100. R_UNLESS(gender <= Gender::All, ResultInvalidArgument);
  101. R_UNLESS(race <= Race::All, ResultInvalidArgument);
  102. manager->BuildRandom(*out_char_info, age, gender, race);
  103. R_SUCCEED();
  104. }
  105. Result BuildDefault(Out<CharInfo> out_char_info, s32 index) {
  106. LOG_DEBUG(Service_Mii, "called with index={}", index);
  107. R_UNLESS(index < static_cast<s32>(RawData::DefaultMii.size()), ResultInvalidArgument);
  108. manager->BuildDefault(*out_char_info, index);
  109. R_SUCCEED();
  110. }
  111. Result Get2(Out<u32> out_mii_count, SourceFlag source_flag,
  112. OutArray<StoreDataElement, BufferAttr_HipcMapAlias> store_data_element_buffer) {
  113. const auto result =
  114. manager->Get(metadata, store_data_element_buffer, *out_mii_count, source_flag);
  115. LOG_INFO(Service_Mii, "called with source_flag={}, mii_count={}", source_flag,
  116. *out_mii_count);
  117. R_RETURN(result);
  118. }
  119. Result Get3(Out<u32> out_mii_count, SourceFlag source_flag,
  120. OutArray<StoreData, BufferAttr_HipcMapAlias> store_data_buffer) {
  121. const auto result = manager->Get(metadata, store_data_buffer, *out_mii_count, source_flag);
  122. LOG_INFO(Service_Mii, "called with source_flag={}, mii_count={}", source_flag,
  123. *out_mii_count);
  124. R_RETURN(result);
  125. }
  126. Result UpdateLatest1(Out<StoreData> out_store_data, const StoreData& store_data,
  127. SourceFlag source_flag) {
  128. LOG_INFO(Service_Mii, "called with source_flag={}", source_flag);
  129. R_UNLESS(is_system, ResultPermissionDenied);
  130. R_RETURN(manager->UpdateLatest(metadata, *out_store_data, store_data, source_flag));
  131. }
  132. Result FindIndex(Out<s32> out_index, Common::UUID create_id, bool is_special) {
  133. LOG_INFO(Service_Mii, "called with create_id={}, is_special={}",
  134. create_id.FormattedString(), is_special);
  135. *out_index = manager->FindIndex(create_id, is_special);
  136. R_SUCCEED();
  137. }
  138. Result Move(Common::UUID create_id, s32 new_index) {
  139. LOG_INFO(Service_Mii, "called with create_id={}, new_index={}", create_id.FormattedString(),
  140. new_index);
  141. R_UNLESS(is_system, ResultPermissionDenied);
  142. const u32 count = manager->GetCount(metadata, SourceFlag::Database);
  143. R_UNLESS(new_index >= 0 && new_index < static_cast<s32>(count), ResultInvalidArgument);
  144. R_RETURN(manager->Move(metadata, new_index, create_id));
  145. }
  146. Result AddOrReplace(const StoreData& store_data) {
  147. LOG_INFO(Service_Mii, "called");
  148. R_UNLESS(is_system, ResultPermissionDenied);
  149. const auto result = manager->AddOrReplace(metadata, store_data);
  150. R_RETURN(result);
  151. }
  152. Result Delete(Common::UUID create_id) {
  153. LOG_INFO(Service_Mii, "called, create_id={}", create_id.FormattedString());
  154. R_UNLESS(is_system, ResultPermissionDenied);
  155. R_RETURN(manager->Delete(metadata, create_id));
  156. }
  157. Result DestroyFile() {
  158. bool is_db_test_mode_enabled{};
  159. m_set_sys->GetSettingsItemValueImpl(is_db_test_mode_enabled, "mii",
  160. "is_db_test_mode_enabled");
  161. LOG_INFO(Service_Mii, "called is_db_test_mode_enabled={}", is_db_test_mode_enabled);
  162. R_UNLESS(is_db_test_mode_enabled, ResultTestModeOnly);
  163. R_RETURN(manager->DestroyFile(metadata));
  164. }
  165. Result DeleteFile() {
  166. bool is_db_test_mode_enabled{};
  167. m_set_sys->GetSettingsItemValueImpl(is_db_test_mode_enabled, "mii",
  168. "is_db_test_mode_enabled");
  169. LOG_INFO(Service_Mii, "called is_db_test_mode_enabled={}", is_db_test_mode_enabled);
  170. R_UNLESS(is_db_test_mode_enabled, ResultTestModeOnly);
  171. R_RETURN(manager->DeleteFile());
  172. }
  173. Result Format() {
  174. bool is_db_test_mode_enabled{};
  175. m_set_sys->GetSettingsItemValueImpl(is_db_test_mode_enabled, "mii",
  176. "is_db_test_mode_enabled");
  177. LOG_INFO(Service_Mii, "called is_db_test_mode_enabled={}", is_db_test_mode_enabled);
  178. R_UNLESS(is_db_test_mode_enabled, ResultTestModeOnly);
  179. R_RETURN(manager->Format(metadata));
  180. }
  181. Result IsBrokenDatabaseWithClearFlag(Out<bool> out_is_broken_with_clear_flag) {
  182. LOG_DEBUG(Service_Mii, "called");
  183. R_UNLESS(is_system, ResultPermissionDenied);
  184. *out_is_broken_with_clear_flag = manager->IsBrokenWithClearFlag(metadata);
  185. R_SUCCEED();
  186. }
  187. Result GetIndex(Out<s32> out_index, const CharInfo& char_info) {
  188. LOG_DEBUG(Service_Mii, "called");
  189. R_RETURN(manager->GetIndex(metadata, char_info, *out_index));
  190. }
  191. Result SetInterfaceVersion(u32 interface_version) {
  192. LOG_INFO(Service_Mii, "called, interface_version={:08X}", interface_version);
  193. manager->SetInterfaceVersion(metadata, interface_version);
  194. R_SUCCEED();
  195. }
  196. Result Convert(Out<CharInfo> out_char_info, const Ver3StoreData& mii_v3) {
  197. LOG_INFO(Service_Mii, "called");
  198. R_RETURN(manager->ConvertV3ToCharInfo(*out_char_info, mii_v3));
  199. }
  200. Result ConvertCoreDataToCharInfo(Out<CharInfo> out_char_info, const CoreData& core_data) {
  201. LOG_INFO(Service_Mii, "called");
  202. R_RETURN(manager->ConvertCoreDataToCharInfo(*out_char_info, core_data));
  203. }
  204. Result ConvertCharInfoToCoreData(Out<CoreData> out_core_data, const CharInfo& char_info) {
  205. LOG_INFO(Service_Mii, "called");
  206. R_RETURN(manager->ConvertCharInfoToCoreData(*out_core_data, char_info));
  207. }
  208. Result Append(const CharInfo& char_info) {
  209. LOG_INFO(Service_Mii, "called");
  210. R_RETURN(manager->Append(metadata, char_info));
  211. }
  212. std::shared_ptr<MiiManager> manager = nullptr;
  213. DatabaseSessionMetadata metadata{};
  214. bool is_system{};
  215. std::shared_ptr<Service::Set::ISystemSettingsServer> m_set_sys;
  216. };
  217. IStaticService::IStaticService(Core::System& system_, const char* name_,
  218. std::shared_ptr<MiiManager> mii_manager, bool is_system_)
  219. : ServiceFramework{system_, name_}, manager{mii_manager}, is_system{is_system_} {
  220. // clang-format off
  221. static const FunctionInfo functions[] = {
  222. {0, D<&IStaticService::GetDatabaseService>, "GetDatabaseService"},
  223. };
  224. // clang-format on
  225. RegisterHandlers(functions);
  226. }
  227. IStaticService::~IStaticService() = default;
  228. Result IStaticService::GetDatabaseService(
  229. Out<SharedPointer<IDatabaseService>> out_database_service) {
  230. LOG_DEBUG(Service_Mii, "called");
  231. *out_database_service = std::make_shared<IDatabaseService>(system, manager, is_system);
  232. R_SUCCEED();
  233. }
  234. std::shared_ptr<MiiManager> IStaticService::GetMiiManager() {
  235. return manager;
  236. }
  237. class IImageDatabaseService final : public ServiceFramework<IImageDatabaseService> {
  238. public:
  239. explicit IImageDatabaseService(Core::System& system_) : ServiceFramework{system_, "miiimg"} {
  240. // clang-format off
  241. static const FunctionInfo functions[] = {
  242. {0, D<&IImageDatabaseService::Initialize>, "Initialize"},
  243. {10, nullptr, "Reload"},
  244. {11, D<&IImageDatabaseService::GetCount>, "GetCount"},
  245. {12, nullptr, "IsEmpty"},
  246. {13, nullptr, "IsFull"},
  247. {14, nullptr, "GetAttribute"},
  248. {15, nullptr, "LoadImage"},
  249. {16, nullptr, "AddOrUpdateImage"},
  250. {17, nullptr, "DeleteImages"},
  251. {100, nullptr, "DeleteFile"},
  252. {101, nullptr, "DestroyFile"},
  253. {102, nullptr, "ImportFile"},
  254. {103, nullptr, "ExportFile"},
  255. {104, nullptr, "ForceInitialize"},
  256. };
  257. // clang-format on
  258. RegisterHandlers(functions);
  259. }
  260. private:
  261. Result Initialize() {
  262. LOG_INFO(Service_Mii, "called");
  263. R_SUCCEED();
  264. }
  265. Result GetCount(Out<u32> out_count) {
  266. LOG_DEBUG(Service_Mii, "called");
  267. *out_count = 0;
  268. R_SUCCEED();
  269. }
  270. };
  271. void LoopProcess(Core::System& system) {
  272. auto server_manager = std::make_unique<ServerManager>(system);
  273. std::shared_ptr<MiiManager> manager = std::make_shared<MiiManager>();
  274. server_manager->RegisterNamedService(
  275. "mii:e", std::make_shared<IStaticService>(system, "mii:e", manager, true));
  276. server_manager->RegisterNamedService(
  277. "mii:u", std::make_shared<IStaticService>(system, "mii:u", manager, false));
  278. server_manager->RegisterNamedService("miiimg", std::make_shared<IImageDatabaseService>(system));
  279. ServerManager::RunServer(std::move(server_manager));
  280. }
  281. } // namespace Service::Mii