我有一个用C编写的游戏(适用于Windows)。我正在努力寻找一种方法来编写一个读取数据并控制这个游戏的Python应用程序
例如:当我的角色生命值达到50%或更低时,我捕获数据的Python程序会不断发送命令以使用恢复生命值的项目。
Python脚本我可以做得很好,但我不知道从这个游戏中捕获数据的用途。
PS:我不想要鼠标/键盘/图形自动化。我知道有一个程序正是我所描述的。用Python编写,用来控制C中的这个游戏。游戏窗口甚至不需要成为Python程序控制它的焦点。
有没有可以访问其他应用程序,读取和发送命令的Python模块?
修改
我现在可以读取内存(通过CheatEngine获取)。 使用while循环我每秒打印正确数量的生命值(十六进制):
from ctypes import *
from ctypes.wintypes import *
import time
OpenProcess = windll.kernel32.OpenProcess
ReadProcessMemory = windll.kernel32.ReadProcessMemory
CloseHandle = windll.kernel32.CloseHandle
PROCESS_ALL_ACCESS = 0x1F0FFF
pid = 3488 # I assume you have this from somewhere.
address = 0x0019F438 # Likewise; for illustration I'll get the .exe header.
buffer = c_char_p("The data goes here")
bufferSize = len(buffer.value)
bytesRead = c_ulong(0)
processHandle = OpenProcess(PROCESS_ALL_ACCESS, False, pid)
while True:
if ReadProcessMemory(processHandle, address, buffer, bufferSize, byref(bytesRead)):
print "Success:", buffer
else:
print "Failed."
time.sleep(1)
CloseHandle(processHandle)
但很快就开始打印奇怪的值:
Success: c_char_p('\x8e') -> 142 |------------------
Success: c_char_p('\x8e') -> 142 |
Success: c_char_p('\x8e') -> 142 |
Success: c_char_p('\x90') -> 144 |
Success: c_char_p('\x90') -> 144 |
Success: c_char_p('\x90') -> 144 |
Success: c_char_p('\x90') -> 144 | This entire block is correct
Success: c_char_p('\x92') -> 146 | The hitpoints are increasing
Success: c_char_p('\x92') -> 146 |
Success: c_char_p('\x92') -> 146 |
Success: c_char_p('\x92') -> 146 |
Success: c_char_p('\x94') -> 148 |
Success: c_char_p('\x94') -> 148 |
Success: c_char_p('\x94') -> 148 |___________________
Success: c_char_p('\x0e') -> 14 |
Success: c_char_p('\x0e') -> 14 | Here it becomes
Success: c_char_p('\x0e') -> 14 | print weird stuff
Success: c_char_p('\x06\xfa\x82p0') |
Success: c_char_p('\x06\xfa\x82p0') |___________________