CheatWindow.axaml.cs 3.9 KB

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