CheatWindow.axaml.cs 3.8 KB

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