文章目录
- 场景
- 查找
- 修改
- 补充
- 字节
- to_bytes
场景
某些游戏数值(攻击力、射程、速度…)被写在exe之类的文件里
要先查找游戏数值,然后修改
查找
首先,要查找数值,大数重复较少,建议从大数找起
F = '游戏原件.exe'
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_sister1(133)
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 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()
replace2(1600, 27935) # 离子HP
replace1(72, 439952) # 离子A
replace1(28, 440055) # 风暴A
replace2(660, 376339) # 大喷火HP
replace2(1120, 376402) # 美洲狮HP
replace2(560, 376366) # 重激光HP
replace1(23, 393697) # 小喷火V
replace1(20, 393726) # 中喷火V
replace1(17, 393759) # 大喷火V
replace1(24, 393977) # 阿基里斯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.
函数示例
>>> a = 256
>>> a.to_bytes(2, byteorder='little')
b'\x00\x01'
>>> hex(a)
'0x100'