patcher.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import sys
  2. import os
  3. # Agregar el directorio padre al path para importar config
  4. sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  5. from config import TARGET_PROCESS, UI_MESSAGES
  6. from core.memory_manager import MemoryManager
  7. from patterns.pattern_searcher import StaminaPatternSearcher
  8. from patches.patch_factory import PatchFactory
  9. from utils.helpers import HexDumper, BackupManager, UserInterface
  10. class AdvancedPatcher:
  11. """Clase principal del patcher que coordina todos los módulos (Facade Pattern)"""
  12. def __init__(self):
  13. self.memory_manager = MemoryManager()
  14. self.pattern_searcher = StaminaPatternSearcher()
  15. self.patch_factory = PatchFactory()
  16. self.hex_dumper = HexDumper()
  17. self.backup_manager = BackupManager(self.memory_manager)
  18. self.ui = UserInterface()
  19. self.target_process = TARGET_PROCESS['name']
  20. def apply_stamina_patch(self) -> bool:
  21. """Aplica el parche de stamina usando todos los módulos"""
  22. # Buscar y abrir el proceso
  23. if not self.memory_manager.open_process(self.target_process):
  24. print(UI_MESSAGES['error']['process_not_found'].format(process_name=self.target_process))
  25. return False
  26. print(UI_MESSAGES['info']['searching_stamina'])
  27. # Buscar patrones
  28. found_addresses = self.pattern_searcher.search(self.memory_manager)
  29. if not found_addresses:
  30. print(UI_MESSAGES['error']['no_patterns_found'])
  31. return False
  32. # Mostrar direcciones encontradas
  33. self.ui.print_found_addresses(found_addresses)
  34. # Seleccionar dirección objetivo
  35. target_address = self.ui.select_target_address(found_addresses)
  36. # Crear el parche
  37. patch = self.patch_factory.create_patch('stamina')
  38. if not patch:
  39. print("❌ No se pudo crear el parche de stamina")
  40. return False
  41. # Leer código original
  42. original_size = patch.get_patch_size()
  43. original_code = self.memory_manager.read_memory(target_address, original_size)
  44. if not original_code:
  45. print("❌ No se pudo leer el código original")
  46. return False
  47. # Crear backup
  48. self.backup_manager.backup_original_code(target_address, len(original_code))
  49. # Mostrar código original
  50. print(UI_MESSAGES['info']['original_code'])
  51. self.hex_dumper.print_hex_dump(original_code, target_address)
  52. # Generar datos del parche
  53. patch_data = patch.create_patch(original_code)
  54. # Mostrar código del parche
  55. print(UI_MESSAGES['info']['patch_to_apply'])
  56. self.hex_dumper.print_hex_dump(patch_data)
  57. # Confirmar aplicación
  58. if not self.ui.confirm_patch():
  59. print(UI_MESSAGES['error']['operation_cancelled'])
  60. return False
  61. # Aplicar el parche
  62. print(UI_MESSAGES['info']['applying_patch'])
  63. if self.memory_manager.write_memory(target_address, patch_data):
  64. print(UI_MESSAGES['success']['patch_applied'])
  65. # Verificar que el patch se escribió correctamente
  66. verification_data = self.memory_manager.read_memory(target_address, len(patch_data))
  67. if verification_data == patch_data:
  68. print(UI_MESSAGES['success']['verification_successful'])
  69. return True
  70. else:
  71. print(UI_MESSAGES['error']['verification_failed'])
  72. return False
  73. else:
  74. print(UI_MESSAGES['error']['patch_failed'])
  75. return False
  76. def cleanup(self):
  77. """Limpia los recursos utilizados"""
  78. self.memory_manager.close_process()
  79. def run(self) -> bool:
  80. """Ejecuta el proceso completo de patcheo"""
  81. print(UI_MESSAGES['info']['starting_patch_process'])
  82. success = False
  83. try:
  84. success = self.apply_stamina_patch()
  85. if success:
  86. print(UI_MESSAGES['success']['patching_completed'])
  87. print(UI_MESSAGES['success']['stamina_maintained'])
  88. else:
  89. print(UI_MESSAGES['error']['patching_failed'])
  90. except Exception as e:
  91. print(UI_MESSAGES['error']['patching_error'].format(error=e))
  92. finally:
  93. self.cleanup()
  94. return success