Skip to content

Instantly share code, notes, and snippets.

@zombie110year
Created March 11, 2024 07:12
Show Gist options
  • Select an option

  • Save zombie110year/75b25e4d9325b79dc2e77256d28d8ac6 to your computer and use it in GitHub Desktop.

Select an option

Save zombie110year/75b25e4d9325b79dc2e77256d28d8ac6 to your computer and use it in GitHub Desktop.
把求生之路三方图拆分成两部分,一部分包含地图本体,另一部分包含材质,方便服务器、客户端两用。
# 将地图文件拆分成两份(vtf 或 非 vtf)
# 地图文件需要提前解包并重命名
# 依赖工具:
# 1. https://github.com/ValvePython/vpk vpk 解包
# 2. Left 4 Dead 2 Authoring Tools vpk.exe 打包
from argparse import ArgumentParser
from pathlib import Path
from subprocess import run
from vpk import open as open_vpk
VPKEXE = "D:/SteamLibrary/steamapps/common/Left 4 Dead 2/bin/vpk.exe"
def parser():
p = ArgumentParser("splitmap")
p.add_argument("vpk", help="要拆分的vpk文件")
return p
def main():
args = parser().parse_args()
src = Path(args.vpk)
dest_texture = src.parent.joinpath(f"{src.stem}_textures")
dest_base = src.parent.joinpath(f"{src.stem}_base")
# 自动解包
vpkfile = open_vpk(src)
# 收集文件路径
files_inside = [p for p in vpkfile]
outpaths = []
for p in files_inside:
if p.endswith(".vtf"):
outpaths.append(dest_texture.joinpath(p))
else:
outpaths.append(dest_base.joinpath(p))
dirs2mk = list(set([p.parent for p in outpaths]))
# 提前创建文件夹
for dir_ in dirs2mk:
dir_.mkdir(parents=True, exist_ok=True)
# 写文件
for filepath, outpath in zip(files_inside, outpaths):
fileobj = vpkfile.get_file(filepath)
content = fileobj.read()
outpath.write_bytes(content)
print(f"extract {filepath} -> {outpath}")
input("完成手工作业后,按任意键继续...")
a = run([VPKEXE, dest_base])
b = run([VPKEXE, dest_texture])
a.check_returncode()
b.check_returncode()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment