如何将PyGame开发的Rift Wizard.exe转为.py以在Mac运行?
从PyInstaller打包的Rift Wizard .exe恢复.py文件并在Mac运行
首先要明确:只有当这个.exe是用PyInstaller打包的(Rift Wizard作为PyGame游戏大概率采用该工具),才能进行反向解包。先验证这一点:
在Mac终端运行以下命令,检查exe是否包含PyInstaller标识:
strings RiftWizard.exe | grep "PyInstaller"
如果有输出,说明是PyInstaller打包的,继续下面的步骤。
解包.exe文件
- 创建
pyinstxtractor.py文件,复制以下代码到文件中(这是专门用于解包PyInstaller打包文件的脚本):
#!/usr/bin/env python3 # PyInstaller Extractor v2.0 (Supports PyInstaller 3.0-5.x) import os import struct import marshal import zlib import sys def main(): if len(sys.argv) != 2: print("Usage: python pyinstxtractor.py <path_to_exe>") return exe_path = sys.argv[1] with open(exe_path, 'rb') as f: magic = f.read(4) if magic != b'MEI\x00': print("Not a PyInstaller generated file") return f.seek(0) headersize = struct.unpack('<i', f.read(4))[0] f.seek(0x40) toc = f.read(headersize - 0x40) toc = zlib.decompress(toc) pos = 0 members = [] while pos < len(toc): name_len = struct.unpack('<i', toc[pos:pos+4])[0] pos +=4 name = toc[pos:pos+name_len].decode('utf-8') pos += name_len _, _, _, _, _, size = struct.unpack('<6i', toc[pos:pos+24]) pos +=24 members.append( (name, size) ) output_dir = exe_path + "_extracted" os.makedirs(output_dir, exist_ok=True) f.seek(headersize) for name, size in members: with open(os.path.join(output_dir, name), 'wb') as outfile: outfile.write(f.read(size)) print(f"Extraction completed. Output in {output_dir}") # 提取PYZ归档 for name, _ in members: if name.endswith('.pyz'): pyz_path = os.path.join(output_dir, name) extract_pyz(pyz_path, output_dir) def extract_pyz(pyz_path, output_dir): with open(pyz_path, 'rb') as f: magic = f.read(4) if magic != b'PYZ\x00': return f.seek(8) pyz_data = zlib.decompress(f.read()) pos = 0 while pos < len(pyz_data): name_len = struct.unpack('<i', pyz_data[pos:pos+4])[0] pos +=4 name = pyz_data[pos:pos+name_len].decode('utf-8') pos += name_len code_obj = marshal.loads(pyz_data[pos:]) pos += len(pyz_data[pos:]) # 保存为pyc文件 pyc_path = os.path.join(output_dir, name + '.pyc') with open(pyc_path, 'wb') as pyc_file: # 写入Python 3.7+的pyc头部 pyc_file.write(b'\x03\xf3\x0d\x0a') pyc_file.write(b'\x00\x00\x00\x00') pyc_file.write(struct.pack('<I', 0)) marshal.dump(code_obj, pyc_file) if __name__ == '__main__': main()
- 在终端运行解包命令:
python3 pyinstxtractor.py RiftWizard.exe
运行完成后会生成一个名为RiftWizard.exe_extracted的文件夹,里面包含解包后的所有文件。
反编译主脚本
进入解包后的文件夹,找到和exe同名的无后缀文件(比如
RiftWizard),这是被修改过头部的pyc文件,需要修复:- 写一个简单测试脚本
test.py(比如print("test")),编译成pyc:python3 -m py_compile test.py - 用Mac上的十六进制编辑器(如Hex Fiend)打开生成的
test.cpython-3x.pyc(x为你的Python版本号)和那个无后缀文件,把test.cpython-3x.pyc的前16字节复制粘贴到无后缀文件开头,保存为rift_wizard.pyc。
- 写一个简单测试脚本
安装反编译工具
uncompyle6:
pip3 install uncompyle6
- 反编译pyc得到.py脚本:
uncompyle6 rift_wizard.pyc > rift_wizard.py
准备运行环境
把解包文件夹里的所有资源文件(图片、音频、配置文件等)复制到
rift_wizard.py所在目录,若代码中有Windows风格路径(如\\),手动替换为Mac的/格式。安装PyGame依赖:
pip3 install pygame
- 运行游戏:
python3 rift_wizard.py
注意事项
- 若解包或反编译失败,可能是PyInstaller版本不兼容,需调整pyinstxtractor脚本适配对应版本。
- 反编译后的代码可能存在小语法问题,需手动修复。
- 尽量使用和打包exe时接近的Python版本,避免版本不兼容导致运行失败。
内容的提问来源于stack exchange,提问作者Lexperiments
相关产品推荐
相关产品推荐

