Python修改exe之类的游戏文件中的数值

场景

某些游戏数值(攻击力、射程、速度…)被写在exe之类的文件里
要先查找游戏数值,然后修改

查找

首先,要查找数值,大数重复较少,建议从大数找起

F = '游戏原件.exe'

def find_your_sister0(a: bytes):
    """查找字符串"""
    length = len(a)
    with open(F, 'rb') as f:
        b = f.read()
    for i in range(len(b) - length + 1):
        if b[i: i + length] == a:
            print(i, b[i - 30: i + length + 30])

def find_your_sister1(a: int):
    """查找0~255的数"""
    with open(F, 'rb') as f:
        b = f.read()  # <class 'bytes'>
    for i in range(len(b)):
        if b[i] == a:
            print(i)

def find_your_sister2(a: int):
    """查找0~65535的数"""
    ab = a.to_bytes(2, byteorder='little')
    print(a, '转字节数组', ab)
    with open(F, 'rb') as f:
        b = f.read()
    for i in range(len(b) - 1):
        if b[i: i + 2] == ab:
            print(i)

if __name__ == '__main__':
    # find_your_sister0(b'fighter')
    # find_your_sister1(11)
    find_your_sister2(13536)

修改

F1 = '游戏原件.exe'
F2 = '游戏魔改文件.exe'

def cp():
    with open(F1, 'rb') as f:
        b = f.read()
    with open(F2, 'wb') as f:
        f.write(b)

def replace(new_data: str, offset: int):
    """修改文本"""
    with open(F2, 'rb') as f:
        b = f.read()
    with open(F2, 'wb') as f:
        f.write(b[:offset])
        for i in new_data:
            f.write(ord(i).to_bytes(1, byteorder='little'))
        f.write(b[offset + len(new_data):])
    print(offset, '偏移量的位置', b[offset], '修改为', new_data)

def replace1(new_data: int, offset: int):
    """修改1个byte"""
    with open(F2, 'rb') as f:
        b = f.read()
    with open(F2, 'wb') as f:
        f.write(b[:offset])
        f.write(new_data.to_bytes(1, byteorder='little'))
        f.write(b[offset + 1:])
    print(offset, '偏移量的位置', b[offset], '修改为', new_data)

def replace2(new_data: int, offset: int):
    """修改2个bytes"""
    nb = new_data.to_bytes(2, byteorder='little')  # 例如:256 --> b'\x00\x01'
    with open(F2, 'rb') as f:
        b = f.read()
    with open(F2, 'wb') as f:
        f.write(b[:offset])
        f.write(nb)
        f.write(b[offset + 2:])
    print(offset, '和', offset + 1, '偏移量的位置', b[offset: offset + 2], '修改为', nb)

if __name__ == '__main__':
    cp()
    replace('menu.mp3', 659824)
    replace2(1600, 27935)  # 离子炮HP
    replace1(76, 439952)  # 离子炮A
    replace1(29, 440055)  # 风暴A
    replace2(630, 376339)  # 大喷火HP
    replace1(23, 393697)  # 小喷火V
    replace1(20, 393726)  # 中喷火V
    replace1(19, 393759)  # 大喷火V
    replace1(24, 393977)  # 阿基里斯V
    replace1(9, 393290)  # 重炮兵V
    replace1(13, 393490)  # 重防空V
    replace2(1100, 439604)  # 阿基里斯A

补充

字节

字节(Byte)是计算机信息技术用于计量存储容量的一种计量单位,储存的数值范围为0~255

WinHex是一款十六进制编辑器,编辑界面如下

to_bytes

函数说明

Return an array of bytes representing an integer.

  length
    Length of bytes object to use.  An OverflowError is raised if the
    integer is not representable with the given number of bytes.
  byteorder
    The byte order used to represent the integer.  If byteorder is 'big',
    the most significant byte is at the beginning of the byte array.  If
    byteorder is 'little', the most significant byte is at the end of the
    byte array.  To request the native byte order of the host system, use
    `sys.byteorder' as the byte order value.
  signed
    Determines whether two's complement is used to represent the integer.
    If signed is False and a negative integer is given, an OverflowError
    is raised.

函数示例(256–>100–>0100–>01,00–>00,01

>>> a = 256

>>> hex(a)
'0x100'

>>> a.to_bytes(2, byteorder='little')
b'\x00\x01'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小基基o_O

您的鼓励是我创作的巨大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值