GameTableContextMenu.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. using Gtk;
  2. using LibHac;
  3. using LibHac.Account;
  4. using LibHac.Common;
  5. using LibHac.Fs;
  6. using LibHac.Fs.Fsa;
  7. using LibHac.Fs.Shim;
  8. using LibHac.FsSystem;
  9. using LibHac.FsSystem.NcaUtils;
  10. using LibHac.Ncm;
  11. using LibHac.Ns;
  12. using Ryujinx.Common.Configuration;
  13. using Ryujinx.Common.Logging;
  14. using Ryujinx.Common.Utilities;
  15. using Ryujinx.HLE.FileSystem;
  16. using Ryujinx.HLE.HOS;
  17. using System;
  18. using System.Buffers;
  19. using System.Collections.Generic;
  20. using System.Diagnostics;
  21. using System.Globalization;
  22. using System.IO;
  23. using System.Reflection;
  24. using System.Threading;
  25. using static LibHac.Fs.ApplicationSaveDataManagement;
  26. namespace Ryujinx.Ui
  27. {
  28. public class GameTableContextMenu : Menu
  29. {
  30. private readonly ListStore _gameTableStore;
  31. private readonly TreeIter _rowIter;
  32. private readonly VirtualFileSystem _virtualFileSystem;
  33. private readonly BlitStruct<ApplicationControlProperty> _controlData;
  34. private MessageDialog _dialog;
  35. private bool _cancel;
  36. public GameTableContextMenu(ListStore gameTableStore, BlitStruct<ApplicationControlProperty> controlData, TreeIter rowIter, VirtualFileSystem virtualFileSystem)
  37. {
  38. _gameTableStore = gameTableStore;
  39. _rowIter = rowIter;
  40. _virtualFileSystem = virtualFileSystem;
  41. _controlData = controlData;
  42. MenuItem openSaveUserDir = new MenuItem("Open User Save Directory")
  43. {
  44. Sensitive = !Utilities.IsEmpty(controlData.ByteSpan) && controlData.Value.UserAccountSaveDataSize > 0,
  45. TooltipText = "Open the directory which contains Application's User Saves."
  46. };
  47. MenuItem openSaveDeviceDir = new MenuItem("Open Device Save Directory")
  48. {
  49. Sensitive = !Utilities.IsEmpty(controlData.ByteSpan) && controlData.Value.DeviceSaveDataSize > 0,
  50. TooltipText = "Open the directory which contains Application's Device Saves."
  51. };
  52. MenuItem openSaveBcatDir = new MenuItem("Open BCAT Save Directory")
  53. {
  54. Sensitive = !Utilities.IsEmpty(controlData.ByteSpan) && controlData.Value.BcatDeliveryCacheStorageSize > 0,
  55. TooltipText = "Open the directory which contains Application's BCAT Saves."
  56. };
  57. MenuItem manageTitleUpdates = new MenuItem("Manage Title Updates")
  58. {
  59. TooltipText = "Open the Title Update management window"
  60. };
  61. MenuItem manageDlc = new MenuItem("Manage DLC")
  62. {
  63. TooltipText = "Open the DLC management window"
  64. };
  65. MenuItem openTitleModDir = new MenuItem("Open Mods Directory")
  66. {
  67. TooltipText = "Open the directory which contains Application's Mods."
  68. };
  69. string ext = System.IO.Path.GetExtension(_gameTableStore.GetValue(_rowIter, 9).ToString()).ToLower();
  70. bool hasNca = ext == ".nca" || ext == ".nsp" || ext == ".pfs0" || ext == ".xci";
  71. MenuItem extractMenu = new MenuItem("Extract Data");
  72. MenuItem extractRomFs = new MenuItem("RomFS")
  73. {
  74. Sensitive = hasNca,
  75. TooltipText = "Extract the RomFS section from Application's current config (including updates)."
  76. };
  77. MenuItem extractExeFs = new MenuItem("ExeFS")
  78. {
  79. Sensitive = hasNca,
  80. TooltipText = "Extract the ExeFS section from Application's current config (including updates)."
  81. };
  82. MenuItem extractLogo = new MenuItem("Logo")
  83. {
  84. Sensitive = hasNca,
  85. TooltipText = "Extract the Logo section from Application's current config (including updates)."
  86. };
  87. Menu extractSubMenu = new Menu();
  88. extractSubMenu.Append(extractExeFs);
  89. extractSubMenu.Append(extractRomFs);
  90. extractSubMenu.Append(extractLogo);
  91. extractMenu.Submenu = extractSubMenu;
  92. MenuItem managePtcMenu = new MenuItem("Cache Management");
  93. MenuItem purgePtcCache = new MenuItem("Purge PPTC cache")
  94. {
  95. TooltipText = "Delete the Application's PPTC cache."
  96. };
  97. MenuItem openPtcDir = new MenuItem("Open PPTC directory")
  98. {
  99. TooltipText = "Open the directory which contains Application's PPTC cache."
  100. };
  101. Menu managePtcSubMenu = new Menu();
  102. managePtcSubMenu.Append(purgePtcCache);
  103. managePtcSubMenu.Append(openPtcDir);
  104. managePtcMenu.Submenu = managePtcSubMenu;
  105. openSaveUserDir.Activated += OpenSaveUserDir_Clicked;
  106. openSaveDeviceDir.Activated += OpenSaveDeviceDir_Clicked;
  107. openSaveBcatDir.Activated += OpenSaveBcatDir_Clicked;
  108. manageTitleUpdates.Activated += ManageTitleUpdates_Clicked;
  109. manageDlc.Activated += ManageDlc_Clicked;
  110. openTitleModDir.Activated += OpenTitleModDir_Clicked;
  111. extractRomFs.Activated += ExtractRomFs_Clicked;
  112. extractExeFs.Activated += ExtractExeFs_Clicked;
  113. extractLogo.Activated += ExtractLogo_Clicked;
  114. purgePtcCache.Activated += PurgePtcCache_Clicked;
  115. openPtcDir.Activated += OpenPtcDir_Clicked;
  116. this.Add(openSaveUserDir);
  117. this.Add(openSaveDeviceDir);
  118. this.Add(openSaveBcatDir);
  119. this.Add(new SeparatorMenuItem());
  120. this.Add(manageTitleUpdates);
  121. this.Add(manageDlc);
  122. this.Add(openTitleModDir);
  123. this.Add(new SeparatorMenuItem());
  124. this.Add(managePtcMenu);
  125. this.Add(extractMenu);
  126. }
  127. private bool TryFindSaveData(string titleName, ulong titleId, BlitStruct<ApplicationControlProperty> controlHolder, SaveDataFilter filter, out ulong saveDataId)
  128. {
  129. saveDataId = default;
  130. Result result = _virtualFileSystem.FsClient.FindSaveDataWithFilter(out SaveDataInfo saveDataInfo, SaveDataSpaceId.User, ref filter);
  131. if (ResultFs.TargetNotFound.Includes(result))
  132. {
  133. // Savedata was not found. Ask the user if they want to create it
  134. using MessageDialog messageDialog = new MessageDialog(null, DialogFlags.Modal, MessageType.Question, ButtonsType.YesNo, null)
  135. {
  136. Title = "Ryujinx",
  137. Icon = new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.assets.Icon.png"),
  138. Text = $"There is no savedata for {titleName} [{titleId:x16}]",
  139. SecondaryText = "Would you like to create savedata for this game?",
  140. WindowPosition = WindowPosition.Center
  141. };
  142. if (messageDialog.Run() != (int)ResponseType.Yes)
  143. {
  144. return false;
  145. }
  146. ref ApplicationControlProperty control = ref controlHolder.Value;
  147. if (LibHac.Utilities.IsEmpty(controlHolder.ByteSpan))
  148. {
  149. // If the current application doesn't have a loaded control property, create a dummy one
  150. // and set the savedata sizes so a user savedata will be created.
  151. control = ref new BlitStruct<ApplicationControlProperty>(1).Value;
  152. // The set sizes don't actually matter as long as they're non-zero because we use directory savedata.
  153. control.UserAccountSaveDataSize = 0x4000;
  154. control.UserAccountSaveDataJournalSize = 0x4000;
  155. Logger.Warning?.Print(LogClass.Application,
  156. "No control file was found for this game. Using a dummy one instead. This may cause inaccuracies in some games.");
  157. }
  158. Uid user = new Uid(1, 0);
  159. result = EnsureApplicationSaveData(_virtualFileSystem.FsClient, out _, new LibHac.Ncm.ApplicationId(titleId), ref control, ref user);
  160. if (result.IsFailure())
  161. {
  162. GtkDialog.CreateErrorDialog($"There was an error creating the specified savedata: {result.ToStringWithName()}");
  163. return false;
  164. }
  165. // Try to find the savedata again after creating it
  166. result = _virtualFileSystem.FsClient.FindSaveDataWithFilter(out saveDataInfo, SaveDataSpaceId.User, ref filter);
  167. }
  168. if (result.IsSuccess())
  169. {
  170. saveDataId = saveDataInfo.SaveDataId;
  171. return true;
  172. }
  173. GtkDialog.CreateErrorDialog($"There was an error finding the specified savedata: {result.ToStringWithName()}");
  174. return false;
  175. }
  176. private string GetSaveDataDirectory(ulong saveDataId)
  177. {
  178. string saveRootPath = System.IO.Path.Combine(_virtualFileSystem.GetNandPath(), $"user/save/{saveDataId:x16}");
  179. if (!Directory.Exists(saveRootPath))
  180. {
  181. // Inconsistent state. Create the directory
  182. Directory.CreateDirectory(saveRootPath);
  183. }
  184. string committedPath = System.IO.Path.Combine(saveRootPath, "0");
  185. string workingPath = System.IO.Path.Combine(saveRootPath, "1");
  186. // If the committed directory exists, that path will be loaded the next time the savedata is mounted
  187. if (Directory.Exists(committedPath))
  188. {
  189. return committedPath;
  190. }
  191. // If the working directory exists and the committed directory doesn't,
  192. // the working directory will be loaded the next time the savedata is mounted
  193. if (!Directory.Exists(workingPath))
  194. {
  195. Directory.CreateDirectory(workingPath);
  196. }
  197. return workingPath;
  198. }
  199. private void ExtractSection(NcaSectionType ncaSectionType, int programIndex = 0)
  200. {
  201. FileChooserDialog fileChooser = new FileChooserDialog("Choose the folder to extract into", null, FileChooserAction.SelectFolder, "Cancel", ResponseType.Cancel, "Extract", ResponseType.Accept);
  202. fileChooser.SetPosition(WindowPosition.Center);
  203. int response = fileChooser.Run();
  204. string destination = fileChooser.Filename;
  205. fileChooser.Dispose();
  206. if (response == (int)ResponseType.Accept)
  207. {
  208. Thread extractorThread = new Thread(() =>
  209. {
  210. string sourceFile = _gameTableStore.GetValue(_rowIter, 9).ToString();
  211. Gtk.Application.Invoke(delegate
  212. {
  213. _dialog = new MessageDialog(null, DialogFlags.DestroyWithParent, MessageType.Info, ButtonsType.Cancel, null)
  214. {
  215. Title = "Ryujinx - NCA Section Extractor",
  216. Icon = new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.assets.Icon.png"),
  217. SecondaryText = $"Extracting {ncaSectionType} section from {System.IO.Path.GetFileName(sourceFile)}...",
  218. WindowPosition = WindowPosition.Center
  219. };
  220. int dialogResponse = _dialog.Run();
  221. if (dialogResponse == (int)ResponseType.Cancel || dialogResponse == (int)ResponseType.DeleteEvent)
  222. {
  223. _cancel = true;
  224. _dialog.Dispose();
  225. }
  226. });
  227. using (FileStream file = new FileStream(sourceFile, FileMode.Open, FileAccess.Read))
  228. {
  229. Nca mainNca = null;
  230. Nca patchNca = null;
  231. if ((System.IO.Path.GetExtension(sourceFile).ToLower() == ".nsp") ||
  232. (System.IO.Path.GetExtension(sourceFile).ToLower() == ".pfs0") ||
  233. (System.IO.Path.GetExtension(sourceFile).ToLower() == ".xci"))
  234. {
  235. PartitionFileSystem pfs;
  236. if (System.IO.Path.GetExtension(sourceFile) == ".xci")
  237. {
  238. Xci xci = new Xci(_virtualFileSystem.KeySet, file.AsStorage());
  239. pfs = xci.OpenPartition(XciPartitionType.Secure);
  240. }
  241. else
  242. {
  243. pfs = new PartitionFileSystem(file.AsStorage());
  244. }
  245. foreach (DirectoryEntryEx fileEntry in pfs.EnumerateEntries("/", "*.nca"))
  246. {
  247. pfs.OpenFile(out IFile ncaFile, fileEntry.FullPath.ToU8Span(), OpenMode.Read).ThrowIfFailure();
  248. Nca nca = new Nca(_virtualFileSystem.KeySet, ncaFile.AsStorage());
  249. if (nca.Header.ContentType == NcaContentType.Program)
  250. {
  251. int dataIndex = Nca.GetSectionIndexFromType(NcaSectionType.Data, NcaContentType.Program);
  252. if (nca.Header.GetFsHeader(dataIndex).IsPatchSection())
  253. {
  254. patchNca = nca;
  255. }
  256. else
  257. {
  258. mainNca = nca;
  259. }
  260. }
  261. }
  262. }
  263. else if (System.IO.Path.GetExtension(sourceFile).ToLower() == ".nca")
  264. {
  265. mainNca = new Nca(_virtualFileSystem.KeySet, file.AsStorage());
  266. }
  267. if (mainNca == null)
  268. {
  269. Logger.Error?.Print(LogClass.Application, "Extraction failed. The main NCA was not present in the selected file.");
  270. Gtk.Application.Invoke(delegate
  271. {
  272. GtkDialog.CreateErrorDialog("Extraction failed. The main NCA was not present in the selected file.");
  273. });
  274. return;
  275. }
  276. (Nca updatePatchNca, _) = ApplicationLoader.GetGameUpdateData(_virtualFileSystem, mainNca.Header.TitleId.ToString("x16"), programIndex, out _);
  277. if (updatePatchNca != null)
  278. {
  279. patchNca = updatePatchNca;
  280. }
  281. int index = Nca.GetSectionIndexFromType(ncaSectionType, mainNca.Header.ContentType);
  282. IFileSystem ncaFileSystem = patchNca != null ? mainNca.OpenFileSystemWithPatch(patchNca, index, IntegrityCheckLevel.ErrorOnInvalid)
  283. : mainNca.OpenFileSystem(index, IntegrityCheckLevel.ErrorOnInvalid);
  284. FileSystemClient fsClient = _virtualFileSystem.FsClient;
  285. string source = DateTime.Now.ToFileTime().ToString().Substring(10);
  286. string output = DateTime.Now.ToFileTime().ToString().Substring(10);
  287. fsClient.Register(source.ToU8Span(), ncaFileSystem);
  288. fsClient.Register(output.ToU8Span(), new LocalFileSystem(destination));
  289. (Result? resultCode, bool canceled) = CopyDirectory(fsClient, $"{source}:/", $"{output}:/");
  290. if (!canceled)
  291. {
  292. if (resultCode.Value.IsFailure())
  293. {
  294. Logger.Error?.Print(LogClass.Application, $"LibHac returned error code: {resultCode.Value.ErrorCode}");
  295. Gtk.Application.Invoke(delegate
  296. {
  297. _dialog?.Dispose();
  298. GtkDialog.CreateErrorDialog("Extraction failed. Read the log file for further information.");
  299. });
  300. }
  301. else if (resultCode.Value.IsSuccess())
  302. {
  303. Gtk.Application.Invoke(delegate
  304. {
  305. _dialog?.Dispose();
  306. MessageDialog dialog = new MessageDialog(null, DialogFlags.DestroyWithParent, MessageType.Info, ButtonsType.Ok, null)
  307. {
  308. Title = "Ryujinx - NCA Section Extractor",
  309. Icon = new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.assets.Icon.png"),
  310. SecondaryText = "Extraction has completed successfully.",
  311. WindowPosition = WindowPosition.Center
  312. };
  313. dialog.Run();
  314. dialog.Dispose();
  315. });
  316. }
  317. }
  318. fsClient.Unmount(source.ToU8Span());
  319. fsClient.Unmount(output.ToU8Span());
  320. }
  321. });
  322. extractorThread.Name = "GUI.NcaSectionExtractorThread";
  323. extractorThread.IsBackground = true;
  324. extractorThread.Start();
  325. }
  326. }
  327. private (Result? result, bool canceled) CopyDirectory(FileSystemClient fs, string sourcePath, string destPath)
  328. {
  329. Result rc = fs.OpenDirectory(out DirectoryHandle sourceHandle, sourcePath.ToU8Span(), OpenDirectoryMode.All);
  330. if (rc.IsFailure()) return (rc, false);
  331. using (sourceHandle)
  332. {
  333. foreach (DirectoryEntryEx entry in fs.EnumerateEntries(sourcePath, "*", SearchOptions.Default))
  334. {
  335. if (_cancel)
  336. {
  337. return (null, true);
  338. }
  339. string subSrcPath = PathTools.Normalize(PathTools.Combine(sourcePath, entry.Name));
  340. string subDstPath = PathTools.Normalize(PathTools.Combine(destPath, entry.Name));
  341. if (entry.Type == DirectoryEntryType.Directory)
  342. {
  343. fs.EnsureDirectoryExists(subDstPath);
  344. (Result? result, bool canceled) = CopyDirectory(fs, subSrcPath, subDstPath);
  345. if (canceled || result.Value.IsFailure())
  346. {
  347. return (result, canceled);
  348. }
  349. }
  350. if (entry.Type == DirectoryEntryType.File)
  351. {
  352. fs.CreateOrOverwriteFile(subDstPath, entry.Size);
  353. rc = CopyFile(fs, subSrcPath, subDstPath);
  354. if (rc.IsFailure()) return (rc, false);
  355. }
  356. }
  357. }
  358. return (Result.Success, false);
  359. }
  360. public Result CopyFile(FileSystemClient fs, string sourcePath, string destPath)
  361. {
  362. Result rc = fs.OpenFile(out FileHandle sourceHandle, sourcePath.ToU8Span(), OpenMode.Read);
  363. if (rc.IsFailure()) return rc;
  364. using (sourceHandle)
  365. {
  366. rc = fs.OpenFile(out FileHandle destHandle, destPath.ToU8Span(), OpenMode.Write | OpenMode.AllowAppend);
  367. if (rc.IsFailure()) return rc;
  368. using (destHandle)
  369. {
  370. const int maxBufferSize = 1024 * 1024;
  371. rc = fs.GetFileSize(out long fileSize, sourceHandle);
  372. if (rc.IsFailure()) return rc;
  373. int bufferSize = (int)Math.Min(maxBufferSize, fileSize);
  374. byte[] buffer = ArrayPool<byte>.Shared.Rent(bufferSize);
  375. try
  376. {
  377. for (long offset = 0; offset < fileSize; offset += bufferSize)
  378. {
  379. int toRead = (int)Math.Min(fileSize - offset, bufferSize);
  380. Span<byte> buf = buffer.AsSpan(0, toRead);
  381. rc = fs.ReadFile(out long _, sourceHandle, offset, buf);
  382. if (rc.IsFailure()) return rc;
  383. rc = fs.WriteFile(destHandle, offset, buf);
  384. if (rc.IsFailure()) return rc;
  385. }
  386. }
  387. finally
  388. {
  389. ArrayPool<byte>.Shared.Return(buffer);
  390. }
  391. rc = fs.FlushFile(destHandle);
  392. if (rc.IsFailure()) return rc;
  393. }
  394. }
  395. return Result.Success;
  396. }
  397. // Events
  398. private void OpenSaveUserDir_Clicked(object sender, EventArgs args)
  399. {
  400. string titleName = _gameTableStore.GetValue(_rowIter, 2).ToString().Split("\n")[0];
  401. string titleId = _gameTableStore.GetValue(_rowIter, 2).ToString().Split("\n")[1].ToLower();
  402. if (!ulong.TryParse(titleId, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out ulong titleIdNumber))
  403. {
  404. GtkDialog.CreateErrorDialog("UI error: The selected game did not have a valid title ID");
  405. return;
  406. }
  407. SaveDataFilter filter = new SaveDataFilter();
  408. filter.SetUserId(new UserId(1, 0));
  409. OpenSaveDir(titleName, titleIdNumber, filter);
  410. }
  411. private void OpenSaveDir(string titleName, ulong titleId, SaveDataFilter filter)
  412. {
  413. filter.SetProgramId(new ProgramId(titleId));
  414. if (!TryFindSaveData(titleName, titleId, _controlData, filter, out ulong saveDataId))
  415. {
  416. return;
  417. }
  418. string saveDir = GetSaveDataDirectory(saveDataId);
  419. Process.Start(new ProcessStartInfo
  420. {
  421. FileName = saveDir,
  422. UseShellExecute = true,
  423. Verb = "open"
  424. });
  425. }
  426. private void OpenSaveDeviceDir_Clicked(object sender, EventArgs args)
  427. {
  428. string titleName = _gameTableStore.GetValue(_rowIter, 2).ToString().Split("\n")[0];
  429. string titleId = _gameTableStore.GetValue(_rowIter, 2).ToString().Split("\n")[1].ToLower();
  430. if (!ulong.TryParse(titleId, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out ulong titleIdNumber))
  431. {
  432. GtkDialog.CreateErrorDialog("UI error: The selected game did not have a valid title ID");
  433. return;
  434. }
  435. SaveDataFilter filter = new SaveDataFilter();
  436. filter.SetSaveDataType(SaveDataType.Device);
  437. OpenSaveDir(titleName, titleIdNumber, filter);
  438. }
  439. private void OpenSaveBcatDir_Clicked(object sender, EventArgs args)
  440. {
  441. string titleName = _gameTableStore.GetValue(_rowIter, 2).ToString().Split("\n")[0];
  442. string titleId = _gameTableStore.GetValue(_rowIter, 2).ToString().Split("\n")[1].ToLower();
  443. if (!ulong.TryParse(titleId, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out ulong titleIdNumber))
  444. {
  445. GtkDialog.CreateErrorDialog("UI error: The selected game did not have a valid title ID");
  446. return;
  447. }
  448. SaveDataFilter filter = new SaveDataFilter();
  449. filter.SetSaveDataType(SaveDataType.Bcat);
  450. OpenSaveDir(titleName, titleIdNumber, filter);
  451. }
  452. private void ManageTitleUpdates_Clicked(object sender, EventArgs args)
  453. {
  454. string titleName = _gameTableStore.GetValue(_rowIter, 2).ToString().Split("\n")[0];
  455. string titleId = _gameTableStore.GetValue(_rowIter, 2).ToString().Split("\n")[1].ToLower();
  456. TitleUpdateWindow titleUpdateWindow = new TitleUpdateWindow(titleId, titleName, _virtualFileSystem);
  457. titleUpdateWindow.Show();
  458. }
  459. private void ManageDlc_Clicked(object sender, EventArgs args)
  460. {
  461. string titleName = _gameTableStore.GetValue(_rowIter, 2).ToString().Split("\n")[0];
  462. string titleId = _gameTableStore.GetValue(_rowIter, 2).ToString().Split("\n")[1].ToLower();
  463. DlcWindow dlcWindow = new DlcWindow(titleId, titleName, _virtualFileSystem);
  464. dlcWindow.Show();
  465. }
  466. private void OpenTitleModDir_Clicked(object sender, EventArgs args)
  467. {
  468. string titleId = _gameTableStore.GetValue(_rowIter, 2).ToString().Split("\n")[1].ToLower();
  469. var modsBasePath = _virtualFileSystem.ModLoader.GetModsBasePath();
  470. var titleModsPath = _virtualFileSystem.ModLoader.GetTitleDir(modsBasePath, titleId);
  471. Process.Start(new ProcessStartInfo
  472. {
  473. FileName = titleModsPath,
  474. UseShellExecute = true,
  475. Verb = "open"
  476. });
  477. }
  478. private void ExtractRomFs_Clicked(object sender, EventArgs args)
  479. {
  480. ExtractSection(NcaSectionType.Data);
  481. }
  482. private void ExtractExeFs_Clicked(object sender, EventArgs args)
  483. {
  484. ExtractSection(NcaSectionType.Code);
  485. }
  486. private void ExtractLogo_Clicked(object sender, EventArgs args)
  487. {
  488. ExtractSection(NcaSectionType.Logo);
  489. }
  490. private void OpenPtcDir_Clicked(object sender, EventArgs args)
  491. {
  492. string titleId = _gameTableStore.GetValue(_rowIter, 2).ToString().Split("\n")[1].ToLower();
  493. string ptcDir = System.IO.Path.Combine(AppDataManager.GamesDirPath, titleId, "cache", "cpu");
  494. string mainPath = System.IO.Path.Combine(ptcDir, "0");
  495. string backupPath = System.IO.Path.Combine(ptcDir, "1");
  496. if (!Directory.Exists(ptcDir))
  497. {
  498. Directory.CreateDirectory(ptcDir);
  499. Directory.CreateDirectory(mainPath);
  500. Directory.CreateDirectory(backupPath);
  501. }
  502. Process.Start(new ProcessStartInfo
  503. {
  504. FileName = ptcDir,
  505. UseShellExecute = true,
  506. Verb = "open"
  507. });
  508. }
  509. private void PurgePtcCache_Clicked(object sender, EventArgs args)
  510. {
  511. string[] tableEntry = _gameTableStore.GetValue(_rowIter, 2).ToString().Split("\n");
  512. string titleId = tableEntry[1].ToLower();
  513. DirectoryInfo mainDir = new DirectoryInfo(System.IO.Path.Combine(AppDataManager.GamesDirPath, titleId, "cache", "cpu", "0"));
  514. DirectoryInfo backupDir = new DirectoryInfo(System.IO.Path.Combine(AppDataManager.GamesDirPath, titleId, "cache", "cpu", "1"));
  515. MessageDialog warningDialog = new MessageDialog(null, DialogFlags.Modal, MessageType.Warning, ButtonsType.YesNo, null)
  516. {
  517. Title = "Ryujinx - Warning",
  518. Text = $"You are about to delete the PPTC cache for '{tableEntry[0]}'. Are you sure you want to proceed?",
  519. WindowPosition = WindowPosition.Center
  520. };
  521. List<FileInfo> cacheFiles = new List<FileInfo>();
  522. if (mainDir.Exists) { cacheFiles.AddRange(mainDir.EnumerateFiles("*.cache")); }
  523. if (backupDir.Exists) { cacheFiles.AddRange(backupDir.EnumerateFiles("*.cache")); }
  524. if (cacheFiles.Count > 0 && warningDialog.Run() == (int)ResponseType.Yes)
  525. {
  526. foreach (FileInfo file in cacheFiles)
  527. {
  528. try
  529. {
  530. file.Delete();
  531. }
  532. catch(Exception e)
  533. {
  534. Logger.Error?.Print(LogClass.Application, $"Error purging PPTC cache {file.Name}: {e}");
  535. }
  536. }
  537. }
  538. warningDialog.Dispose();
  539. }
  540. }
  541. }