TamperMachine.cs 6.2 KB

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