CheatWindow.axaml.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using Avalonia;
  2. using Avalonia.Collections;
  3. using Ryujinx.Ava.Common.Locale;
  4. using Ryujinx.Ava.Ui.Models;
  5. using Ryujinx.HLE.FileSystem;
  6. using Ryujinx.HLE.HOS;
  7. using System.Collections.Generic;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Linq;
  11. namespace Ryujinx.Ava.Ui.Windows
  12. {
  13. public partial class CheatWindow : StyleableWindow
  14. {
  15. private readonly string _enabledCheatsPath;
  16. public bool NoCheatsFound { get; }
  17. private AvaloniaList<CheatsList> LoadedCheats { get; }
  18. public string Heading { get; }
  19. public CheatWindow()
  20. {
  21. DataContext = this;
  22. InitializeComponent();
  23. Title = $"Ryujinx {Program.Version} - " + LocaleManager.Instance["CheatWindowTitle"];
  24. }
  25. public CheatWindow(VirtualFileSystem virtualFileSystem, string titleId, string titleName)
  26. {
  27. LoadedCheats = new AvaloniaList<CheatsList>();
  28. Heading = string.Format(LocaleManager.Instance["CheatWindowHeading"], titleName, titleId.ToUpper());
  29. InitializeComponent();
  30. string modsBasePath = virtualFileSystem.ModLoader.GetModsBasePath();
  31. string titleModsPath = virtualFileSystem.ModLoader.GetTitleDir(modsBasePath, titleId);
  32. ulong titleIdValue = ulong.Parse(titleId, System.Globalization.NumberStyles.HexNumber);
  33. _enabledCheatsPath = Path.Combine(titleModsPath, "cheats", "enabled.txt");
  34. string[] enabled = { };
  35. if (File.Exists(_enabledCheatsPath))
  36. {
  37. enabled = File.ReadAllLines(_enabledCheatsPath);
  38. }
  39. int cheatAdded = 0;
  40. var mods = new ModLoader.ModCache();
  41. ModLoader.QueryContentsDir(mods, new DirectoryInfo(Path.Combine(modsBasePath, "contents")), titleIdValue);
  42. string currentCheatFile = string.Empty;
  43. string buildId = string.Empty;
  44. string parentPath = string.Empty;
  45. CheatsList currentGroup = null;
  46. foreach (var cheat in mods.Cheats)
  47. {
  48. if (cheat.Path.FullName != currentCheatFile)
  49. {
  50. currentCheatFile = cheat.Path.FullName;
  51. parentPath = currentCheatFile.Replace(titleModsPath, "");
  52. buildId = Path.GetFileNameWithoutExtension(currentCheatFile).ToUpper();
  53. currentGroup = new CheatsList(buildId, parentPath);
  54. LoadedCheats.Add(currentGroup);
  55. }
  56. var model = new CheatModel(cheat.Name, buildId, enabled.Contains($"{buildId}-{cheat.Name}"));
  57. currentGroup?.Add(model);
  58. cheatAdded++;
  59. }
  60. if (cheatAdded == 0)
  61. {
  62. NoCheatsFound = true;
  63. }
  64. DataContext = this;
  65. Title = $"Ryujinx {Program.Version} - " + LocaleManager.Instance["CheatWindowTitle"];
  66. }
  67. public void Save()
  68. {
  69. if (NoCheatsFound)
  70. {
  71. return;
  72. }
  73. List<string> enabledCheats = new List<string>();
  74. foreach (var cheats in LoadedCheats)
  75. {
  76. foreach (var cheat in cheats)
  77. {
  78. if (cheat.IsEnabled)
  79. {
  80. enabledCheats.Add(cheat.BuildIdKey);
  81. }
  82. }
  83. }
  84. Directory.CreateDirectory(Path.GetDirectoryName(_enabledCheatsPath));
  85. File.WriteAllLines(_enabledCheatsPath, enabledCheats);
  86. Close();
  87. }
  88. }
  89. }