CheatWindow.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using Gtk;
  2. using Ryujinx.HLE.FileSystem;
  3. using Ryujinx.HLE.HOS;
  4. using Ryujinx.Ui.App.Common;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using GUI = Gtk.Builder.ObjectAttribute;
  10. namespace Ryujinx.Ui.Windows
  11. {
  12. public class CheatWindow : Window
  13. {
  14. private readonly string _enabledCheatsPath;
  15. private readonly bool _noCheatsFound;
  16. #pragma warning disable CS0649, IDE0044 // Field is never assigned to, Add readonly modifier
  17. [GUI] Label _baseTitleInfoLabel;
  18. [GUI] TextView _buildIdTextView;
  19. [GUI] TreeView _cheatTreeView;
  20. [GUI] Button _saveButton;
  21. #pragma warning restore CS0649, IDE0044
  22. public CheatWindow(VirtualFileSystem virtualFileSystem, ulong titleId, string titleName, string titlePath) : this(new Builder("Ryujinx.Ui.Windows.CheatWindow.glade"), virtualFileSystem, titleId, titleName, titlePath) { }
  23. private CheatWindow(Builder builder, VirtualFileSystem virtualFileSystem, ulong titleId, string titleName, string titlePath) : base(builder.GetRawOwnedObject("_cheatWindow"))
  24. {
  25. builder.Autoconnect(this);
  26. _baseTitleInfoLabel.Text = $"Cheats Available for {titleName} [{titleId:X16}]";
  27. _buildIdTextView.Buffer.Text = $"BuildId: {ApplicationData.GetApplicationBuildId(virtualFileSystem, titlePath)}";
  28. string modsBasePath = ModLoader.GetModsBasePath();
  29. string titleModsPath = ModLoader.GetTitleDir(modsBasePath, titleId.ToString("X16"));
  30. _enabledCheatsPath = System.IO.Path.Combine(titleModsPath, "cheats", "enabled.txt");
  31. _cheatTreeView.Model = new TreeStore(typeof(bool), typeof(string), typeof(string), typeof(string));
  32. CellRendererToggle enableToggle = new();
  33. enableToggle.Toggled += (sender, args) =>
  34. {
  35. _cheatTreeView.Model.GetIter(out TreeIter treeIter, new TreePath(args.Path));
  36. bool newValue = !(bool)_cheatTreeView.Model.GetValue(treeIter, 0);
  37. _cheatTreeView.Model.SetValue(treeIter, 0, newValue);
  38. if (_cheatTreeView.Model.IterChildren(out TreeIter childIter, treeIter))
  39. {
  40. do
  41. {
  42. _cheatTreeView.Model.SetValue(childIter, 0, newValue);
  43. }
  44. while (_cheatTreeView.Model.IterNext(ref childIter));
  45. }
  46. };
  47. _cheatTreeView.AppendColumn("Enabled", enableToggle, "active", 0);
  48. _cheatTreeView.AppendColumn("Name", new CellRendererText(), "text", 1);
  49. _cheatTreeView.AppendColumn("Path", new CellRendererText(), "text", 2);
  50. var buildIdColumn = _cheatTreeView.AppendColumn("Build Id", new CellRendererText(), "text", 3);
  51. buildIdColumn.Visible = false;
  52. string[] enabled = Array.Empty<string>();
  53. if (File.Exists(_enabledCheatsPath))
  54. {
  55. enabled = File.ReadAllLines(_enabledCheatsPath);
  56. }
  57. int cheatAdded = 0;
  58. var mods = new ModLoader.ModCache();
  59. ModLoader.QueryContentsDir(mods, new DirectoryInfo(System.IO.Path.Combine(modsBasePath, "contents")), titleId);
  60. string currentCheatFile = string.Empty;
  61. string buildId = string.Empty;
  62. TreeIter parentIter = default;
  63. foreach (var cheat in mods.Cheats)
  64. {
  65. if (cheat.Path.FullName != currentCheatFile)
  66. {
  67. currentCheatFile = cheat.Path.FullName;
  68. string parentPath = currentCheatFile.Replace(titleModsPath, "");
  69. buildId = System.IO.Path.GetFileNameWithoutExtension(currentCheatFile).ToUpper();
  70. parentIter = ((TreeStore)_cheatTreeView.Model).AppendValues(false, buildId, parentPath, "");
  71. }
  72. string cleanName = cheat.Name[1..^7];
  73. ((TreeStore)_cheatTreeView.Model).AppendValues(parentIter, enabled.Contains($"{buildId}-{cheat.Name}"), cleanName, "", buildId);
  74. cheatAdded++;
  75. }
  76. if (cheatAdded == 0)
  77. {
  78. ((TreeStore)_cheatTreeView.Model).AppendValues(false, "No Cheats Found", "", "");
  79. _cheatTreeView.GetColumn(0).Visible = false;
  80. _noCheatsFound = true;
  81. _saveButton.Visible = false;
  82. }
  83. _cheatTreeView.ExpandAll();
  84. }
  85. private void SaveButton_Clicked(object sender, EventArgs args)
  86. {
  87. if (_noCheatsFound)
  88. {
  89. return;
  90. }
  91. List<string> enabledCheats = new();
  92. if (_cheatTreeView.Model.GetIterFirst(out TreeIter parentIter))
  93. {
  94. do
  95. {
  96. if (_cheatTreeView.Model.IterChildren(out TreeIter childIter, parentIter))
  97. {
  98. do
  99. {
  100. var enabled = (bool)_cheatTreeView.Model.GetValue(childIter, 0);
  101. if (enabled)
  102. {
  103. var name = _cheatTreeView.Model.GetValue(childIter, 1).ToString();
  104. var buildId = _cheatTreeView.Model.GetValue(childIter, 3).ToString();
  105. enabledCheats.Add($"{buildId}-<{name} Cheat>");
  106. }
  107. }
  108. while (_cheatTreeView.Model.IterNext(ref childIter));
  109. }
  110. }
  111. while (_cheatTreeView.Model.IterNext(ref parentIter));
  112. }
  113. Directory.CreateDirectory(System.IO.Path.GetDirectoryName(_enabledCheatsPath));
  114. File.WriteAllLines(_enabledCheatsPath, enabledCheats);
  115. Dispose();
  116. }
  117. private void CancelButton_Clicked(object sender, EventArgs args)
  118. {
  119. Dispose();
  120. }
  121. }
  122. }