CheatWindow.axaml.cs 3.6 KB

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