TamperMachine.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.Exceptions;
  3. using Ryujinx.HLE.HOS.Kernel;
  4. using Ryujinx.HLE.HOS.Kernel.Process;
  5. using Ryujinx.HLE.HOS.Services.Hid;
  6. using Ryujinx.HLE.HOS.Tamper;
  7. using System;
  8. using System.Collections.Concurrent;
  9. using System.Collections.Generic;
  10. using System.Threading;
  11. namespace Ryujinx.HLE.HOS
  12. {
  13. public class TamperMachine
  14. {
  15. // Atmosphere specifies a delay of 83 milliseconds between the execution of the last
  16. // cheat and the re-execution of the first one.
  17. private const int TamperMachineSleepMs = 1000 / 12;
  18. private Thread _tamperThread = null;
  19. private ConcurrentQueue<ITamperProgram> _programs = new ConcurrentQueue<ITamperProgram>();
  20. private long _pressedKeys = 0;
  21. private Dictionary<string, ITamperProgram> _programDictionary = new Dictionary<string, ITamperProgram>();
  22. private void Activate()
  23. {
  24. if (_tamperThread == null || !_tamperThread.IsAlive)
  25. {
  26. _tamperThread = new Thread(this.TamperRunner);
  27. _tamperThread.Name = "HLE.TamperMachine";
  28. _tamperThread.Start();
  29. }
  30. }
  31. internal void InstallAtmosphereCheat(string name, string buildId, IEnumerable<string> rawInstructions, ProcessTamperInfo info, ulong exeAddress)
  32. {
  33. if (!CanInstallOnPid(info.Process.Pid))
  34. {
  35. return;
  36. }
  37. ITamperedProcess tamperedProcess = new TamperedKProcess(info.Process);
  38. AtmosphereCompiler compiler = new AtmosphereCompiler(exeAddress, info.HeapAddress, info.AliasAddress, info.AslrAddress, tamperedProcess);
  39. ITamperProgram program = compiler.Compile(name, rawInstructions);
  40. if (program != null)
  41. {
  42. program.TampersCodeMemory = false;
  43. _programs.Enqueue(program);
  44. _programDictionary.TryAdd($"{buildId}-{name}", program);
  45. }
  46. Activate();
  47. }
  48. private bool CanInstallOnPid(long pid)
  49. {
  50. // Do not allow tampering of kernel processes.
  51. if (pid < KernelConstants.InitialProcessId)
  52. {
  53. Logger.Warning?.Print(LogClass.TamperMachine, $"Refusing to tamper kernel process {pid}");
  54. return false;
  55. }
  56. return true;
  57. }
  58. public void EnableCheats(string[] enabledCheats)
  59. {
  60. foreach (var program in _programDictionary.Values)
  61. {
  62. program.IsEnabled = false;
  63. }
  64. foreach (var cheat in enabledCheats)
  65. {
  66. if (_programDictionary.TryGetValue(cheat, out var program))
  67. {
  68. program.IsEnabled = true;
  69. }
  70. }
  71. }
  72. private bool IsProcessValid(ITamperedProcess process)
  73. {
  74. return process.State != ProcessState.Crashed && process.State != ProcessState.Exiting && process.State != ProcessState.Exited;
  75. }
  76. private void TamperRunner()
  77. {
  78. Logger.Info?.Print(LogClass.TamperMachine, "TamperMachine thread running");
  79. int sleepCounter = 0;
  80. while (true)
  81. {
  82. // Sleep to not consume too much CPU.
  83. if (sleepCounter == 0)
  84. {
  85. sleepCounter = _programs.Count;
  86. Thread.Sleep(TamperMachineSleepMs);
  87. }
  88. else
  89. {
  90. sleepCounter--;
  91. }
  92. if (!AdvanceTamperingsQueue())
  93. {
  94. // No more work to be done.
  95. Logger.Info?.Print(LogClass.TamperMachine, "TamperMachine thread exiting");
  96. return;
  97. }
  98. }
  99. }
  100. private bool AdvanceTamperingsQueue()
  101. {
  102. if (!_programs.TryDequeue(out ITamperProgram program))
  103. {
  104. // No more programs in the queue.
  105. _programDictionary.Clear();
  106. return false;
  107. }
  108. // Check if the process is still suitable for running the tamper program.
  109. if (!IsProcessValid(program.Process))
  110. {
  111. // Exit without re-enqueuing the program because the process is no longer valid.
  112. return true;
  113. }
  114. // Re-enqueue the tampering program because the process is still valid.
  115. _programs.Enqueue(program);
  116. Logger.Debug?.Print(LogClass.TamperMachine, $"Running tampering program {program.Name}");
  117. try
  118. {
  119. ControllerKeys pressedKeys = (ControllerKeys)Thread.VolatileRead(ref _pressedKeys);
  120. program.Process.TamperedCodeMemory = false;
  121. program.Execute(pressedKeys);
  122. // Detect the first attempt to tamper memory and log it.
  123. if (!program.TampersCodeMemory && program.Process.TamperedCodeMemory)
  124. {
  125. program.TampersCodeMemory = true;
  126. Logger.Warning?.Print(LogClass.TamperMachine, $"Tampering program {program.Name} modifies code memory so it may not work properly");
  127. }
  128. }
  129. catch (Exception ex)
  130. {
  131. Logger.Debug?.Print(LogClass.TamperMachine, $"The tampering program {program.Name} crashed, this can happen while the game is starting");
  132. if (!string.IsNullOrEmpty(ex.Message))
  133. {
  134. Logger.Debug?.Print(LogClass.TamperMachine, ex.Message);
  135. }
  136. }
  137. return true;
  138. }
  139. public void UpdateInput(List<GamepadInput> gamepadInputs)
  140. {
  141. // Look for the input of the player one or the handheld.
  142. foreach (GamepadInput input in gamepadInputs)
  143. {
  144. if (input.PlayerId == PlayerIndex.Player1 || input.PlayerId == PlayerIndex.Handheld)
  145. {
  146. Thread.VolatileWrite(ref _pressedKeys, (long)input.Buttons);
  147. return;
  148. }
  149. }
  150. // Clear the input because player one is not conected.
  151. Thread.VolatileWrite(ref _pressedKeys, 0);
  152. }
  153. }
  154. }