CheatWindow.axaml.cs 3.7 KB

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