CheatWindow.cs 5.8 KB

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