MiiDatabaseManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. using LibHac;
  2. using LibHac.Common;
  3. using LibHac.Fs;
  4. using LibHac.Fs.Fsa;
  5. using LibHac.Fs.Shim;
  6. using LibHac.Ncm;
  7. using Ryujinx.HLE.HOS.Services.Mii.Types;
  8. using System.Runtime.CompilerServices;
  9. namespace Ryujinx.HLE.HOS.Services.Mii
  10. {
  11. class MiiDatabaseManager
  12. {
  13. private static bool IsTestModeEnabled = false;
  14. private static uint MountCounter = 0;
  15. private const ulong DatabaseTestSaveDataId = 0x8000000000000031;
  16. private const ulong DatabaseSaveDataId = 0x8000000000000030;
  17. private static U8String DatabasePath = new U8String("mii:/MiiDatabase.dat");
  18. private static U8String MountName = new U8String("mii");
  19. private NintendoFigurineDatabase _database;
  20. private bool _isDirty;
  21. private HorizonClient _horizonClient;
  22. protected ulong UpdateCounter { get; private set; }
  23. public MiiDatabaseManager()
  24. {
  25. _database = new NintendoFigurineDatabase();
  26. _isDirty = false;
  27. UpdateCounter = 0;
  28. }
  29. private void ResetDatabase()
  30. {
  31. _database = new NintendoFigurineDatabase();
  32. _database.Format();
  33. }
  34. private void MarkDirty(DatabaseSessionMetadata metadata)
  35. {
  36. _isDirty = true;
  37. UpdateCounter++;
  38. metadata.UpdateCounter = UpdateCounter;
  39. }
  40. private bool GetAtVirtualIndex(int index, out int realIndex, out StoreData storeData)
  41. {
  42. realIndex = -1;
  43. storeData = new StoreData();
  44. int virtualIndex = 0;
  45. for (int i = 0; i < _database.Length; i++)
  46. {
  47. StoreData tmp = _database.Get(i);
  48. if (!tmp.IsSpecial())
  49. {
  50. if (index == virtualIndex)
  51. {
  52. realIndex = i;
  53. storeData = tmp;
  54. return true;
  55. }
  56. virtualIndex++;
  57. }
  58. }
  59. return false;
  60. }
  61. private int ConvertRealIndexToVirtualIndex(int realIndex)
  62. {
  63. int virtualIndex = 0;
  64. for (int i = 0; i < realIndex; i++)
  65. {
  66. StoreData tmp = _database.Get(i);
  67. if (!tmp.IsSpecial())
  68. {
  69. virtualIndex++;
  70. }
  71. }
  72. return virtualIndex;
  73. }
  74. public void InitializeDatabase(HorizonClient horizonClient)
  75. {
  76. _horizonClient = horizonClient;
  77. // Ensure we have valid data in the database
  78. _database.Format();
  79. MountSave();
  80. }
  81. private Result MountSave()
  82. {
  83. if (MountCounter != 0)
  84. {
  85. MountCounter++;
  86. return Result.Success;
  87. }
  88. ulong saveDataId = IsTestModeEnabled ? DatabaseTestSaveDataId : DatabaseSaveDataId;
  89. Result result = _horizonClient.Fs.MountSystemSaveData(MountName, SaveDataSpaceId.System, saveDataId);
  90. if (result.IsFailure())
  91. {
  92. if (!ResultFs.TargetNotFound.Includes(result))
  93. return result;
  94. if (IsTestModeEnabled)
  95. {
  96. result = _horizonClient.Fs.CreateSystemSaveData(saveDataId, 0x10000, 0x10000,
  97. SaveDataFlags.KeepAfterResettingSystemSaveDataWithoutUserSaveData);
  98. if (result.IsFailure()) return result;
  99. }
  100. else
  101. {
  102. result = _horizonClient.Fs.CreateSystemSaveData(saveDataId, SystemProgramId.Ns.Value, 0x10000,
  103. 0x10000, SaveDataFlags.KeepAfterResettingSystemSaveDataWithoutUserSaveData);
  104. if (result.IsFailure()) return result;
  105. }
  106. result = _horizonClient.Fs.MountSystemSaveData(MountName, SaveDataSpaceId.System, saveDataId);
  107. if (result.IsFailure()) return result;
  108. }
  109. if (result == Result.Success)
  110. {
  111. MountCounter++;
  112. }
  113. return result;
  114. }
  115. public ResultCode DeleteFile()
  116. {
  117. ResultCode result = (ResultCode)_horizonClient.Fs.DeleteFile(DatabasePath).Value;
  118. _horizonClient.Fs.Commit(MountName);
  119. return result;
  120. }
  121. public ResultCode LoadFromFile(out bool isBroken)
  122. {
  123. isBroken = false;
  124. if (MountCounter == 0)
  125. {
  126. return ResultCode.InvalidArgument;
  127. }
  128. UpdateCounter++;
  129. ResetDatabase();
  130. Result result = _horizonClient.Fs.OpenFile(out FileHandle handle, DatabasePath, OpenMode.Read);
  131. if (result.IsSuccess())
  132. {
  133. result = _horizonClient.Fs.GetFileSize(out long fileSize, handle);
  134. if (result.IsSuccess())
  135. {
  136. if (fileSize == Unsafe.SizeOf<NintendoFigurineDatabase>())
  137. {
  138. result = _horizonClient.Fs.ReadFile(handle, 0, _database.AsSpan());
  139. if (result.IsSuccess())
  140. {
  141. if (_database.Verify() != ResultCode.Success)
  142. {
  143. ResetDatabase();
  144. isBroken = true;
  145. }
  146. else
  147. {
  148. isBroken = _database.FixDatabase();
  149. }
  150. }
  151. }
  152. else
  153. {
  154. isBroken = true;
  155. }
  156. }
  157. _horizonClient.Fs.CloseFile(handle);
  158. return (ResultCode)result.Value;
  159. }
  160. else if (ResultFs.PathNotFound.Includes(result))
  161. {
  162. return (ResultCode)ForceSaveDatabase().Value;
  163. }
  164. return ResultCode.Success;
  165. }
  166. private Result ForceSaveDatabase()
  167. {
  168. Result result = _horizonClient.Fs.CreateFile(DatabasePath, Unsafe.SizeOf<NintendoFigurineDatabase>());
  169. if (result.IsSuccess() || ResultFs.PathAlreadyExists.Includes(result))
  170. {
  171. result = _horizonClient.Fs.OpenFile(out FileHandle handle, DatabasePath, OpenMode.Write);
  172. if (result.IsSuccess())
  173. {
  174. result = _horizonClient.Fs.GetFileSize(out long fileSize, handle);
  175. if (result.IsSuccess())
  176. {
  177. // If the size doesn't match, recreate the file
  178. if (fileSize != Unsafe.SizeOf<NintendoFigurineDatabase>())
  179. {
  180. _horizonClient.Fs.CloseFile(handle);
  181. result = _horizonClient.Fs.DeleteFile(DatabasePath);
  182. if (result.IsSuccess())
  183. {
  184. result = _horizonClient.Fs.CreateFile(DatabasePath, Unsafe.SizeOf<NintendoFigurineDatabase>());
  185. if (result.IsSuccess())
  186. {
  187. result = _horizonClient.Fs.OpenFile(out handle, DatabasePath, OpenMode.Write);
  188. }
  189. }
  190. if (result.IsFailure())
  191. {
  192. return result;
  193. }
  194. }
  195. result = _horizonClient.Fs.WriteFile(handle, 0, _database.AsReadOnlySpan(), WriteOption.Flush);
  196. }
  197. _horizonClient.Fs.CloseFile(handle);
  198. }
  199. }
  200. if (result.IsSuccess())
  201. {
  202. _isDirty = false;
  203. result = _horizonClient.Fs.Commit(MountName);
  204. }
  205. return result;
  206. }
  207. public DatabaseSessionMetadata CreateSessionMetadata(SpecialMiiKeyCode miiKeyCode)
  208. {
  209. return new DatabaseSessionMetadata(UpdateCounter, miiKeyCode);
  210. }
  211. public void SetInterfaceVersion(DatabaseSessionMetadata metadata, uint interfaceVersion)
  212. {
  213. metadata.InterfaceVersion = interfaceVersion;
  214. }
  215. public bool IsUpdated(DatabaseSessionMetadata metadata)
  216. {
  217. bool result = metadata.UpdateCounter != UpdateCounter;
  218. metadata.UpdateCounter = UpdateCounter;
  219. return result;
  220. }
  221. public int GetCount(DatabaseSessionMetadata metadata)
  222. {
  223. if (!metadata.MiiKeyCode.IsEnabledSpecialMii())
  224. {
  225. int count = 0;
  226. for (int i = 0; i < _database.Length; i++)
  227. {
  228. StoreData tmp = _database.Get(i);
  229. if (!tmp.IsSpecial())
  230. {
  231. count++;
  232. }
  233. }
  234. return count;
  235. }
  236. else
  237. {
  238. return _database.Length;
  239. }
  240. }
  241. public void Get(DatabaseSessionMetadata metadata, int index, out StoreData storeData)
  242. {
  243. if (!metadata.MiiKeyCode.IsEnabledSpecialMii())
  244. {
  245. if (GetAtVirtualIndex(index, out int realIndex, out _))
  246. {
  247. index = realIndex;
  248. }
  249. else
  250. {
  251. index = 0;
  252. }
  253. }
  254. storeData = _database.Get(index);
  255. }
  256. public ResultCode FindIndex(DatabaseSessionMetadata metadata, out int index, CreateId createId)
  257. {
  258. return FindIndex(out index, createId, metadata.MiiKeyCode.IsEnabledSpecialMii());
  259. }
  260. public ResultCode FindIndex(out int index, CreateId createId, bool isSpecial)
  261. {
  262. if (_database.GetIndexByCreatorId(out int realIndex, createId))
  263. {
  264. if (isSpecial)
  265. {
  266. index = realIndex;
  267. return ResultCode.Success;
  268. }
  269. StoreData storeData = _database.Get(realIndex);
  270. if (!storeData.IsSpecial())
  271. {
  272. if (realIndex < 1)
  273. {
  274. index = 0;
  275. }
  276. else
  277. {
  278. index = ConvertRealIndexToVirtualIndex(realIndex);
  279. }
  280. return ResultCode.Success;
  281. }
  282. }
  283. index = -1;
  284. return ResultCode.NotFound;
  285. }
  286. public ResultCode Move(DatabaseSessionMetadata metadata, int newIndex, CreateId createId)
  287. {
  288. if (!metadata.MiiKeyCode.IsEnabledSpecialMii())
  289. {
  290. if (GetAtVirtualIndex(newIndex, out int realIndex, out _))
  291. {
  292. newIndex = realIndex;
  293. }
  294. else
  295. {
  296. newIndex = 0;
  297. }
  298. }
  299. if (_database.GetIndexByCreatorId(out int oldIndex, createId))
  300. {
  301. StoreData realStoreData = _database.Get(oldIndex);
  302. if (!metadata.MiiKeyCode.IsEnabledSpecialMii() && realStoreData.IsSpecial())
  303. {
  304. return ResultCode.InvalidOperationOnSpecialMii;
  305. }
  306. ResultCode result = _database.Move(newIndex, oldIndex);
  307. if (result == ResultCode.Success)
  308. {
  309. MarkDirty(metadata);
  310. }
  311. return result;
  312. }
  313. return ResultCode.NotFound;
  314. }
  315. public ResultCode AddOrReplace(DatabaseSessionMetadata metadata, StoreData storeData)
  316. {
  317. if (!storeData.IsValid())
  318. {
  319. return ResultCode.InvalidStoreData;
  320. }
  321. if (!metadata.MiiKeyCode.IsEnabledSpecialMii() && storeData.IsSpecial())
  322. {
  323. return ResultCode.InvalidOperationOnSpecialMii;
  324. }
  325. if (_database.GetIndexByCreatorId(out int index, storeData.CreateId))
  326. {
  327. StoreData oldStoreData = _database.Get(index);
  328. if (oldStoreData.IsSpecial())
  329. {
  330. return ResultCode.InvalidOperationOnSpecialMii;
  331. }
  332. _database.Replace(index, storeData);
  333. }
  334. else
  335. {
  336. if (_database.IsFull())
  337. {
  338. return ResultCode.DatabaseFull;
  339. }
  340. _database.Add(storeData);
  341. }
  342. MarkDirty(metadata);
  343. return ResultCode.Success;
  344. }
  345. public ResultCode Delete(DatabaseSessionMetadata metadata, CreateId createId)
  346. {
  347. if (!_database.GetIndexByCreatorId(out int index, createId))
  348. {
  349. return ResultCode.NotFound;
  350. }
  351. if (!metadata.MiiKeyCode.IsEnabledSpecialMii())
  352. {
  353. StoreData storeData = _database.Get(index);
  354. if (storeData.IsSpecial())
  355. {
  356. return ResultCode.InvalidOperationOnSpecialMii;
  357. }
  358. }
  359. _database.Delete(index);
  360. MarkDirty(metadata);
  361. return ResultCode.Success;
  362. }
  363. public ResultCode DestroyFile(DatabaseSessionMetadata metadata)
  364. {
  365. _database.CorruptDatabase();
  366. MarkDirty(metadata);
  367. ResultCode result = SaveDatabase();
  368. ResetDatabase();
  369. return result;
  370. }
  371. public ResultCode SaveDatabase()
  372. {
  373. if (_isDirty)
  374. {
  375. return (ResultCode)ForceSaveDatabase().Value;
  376. }
  377. else
  378. {
  379. return ResultCode.NotUpdated;
  380. }
  381. }
  382. public void FormatDatabase(DatabaseSessionMetadata metadata)
  383. {
  384. _database.Format();
  385. MarkDirty(metadata);
  386. }
  387. public bool IsFullDatabase()
  388. {
  389. return _database.IsFull();
  390. }
  391. }
  392. }