CheatWindow.cs 5.7 KB

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