Skip to content

Instantly share code, notes, and snippets.

@ImanCol
Last active April 6, 2023 01:45
Show Gist options
  • Save ImanCol/a165762b265687f959a18f12eab36601 to your computer and use it in GitHub Desktop.
Save ImanCol/a165762b265687f959a18f12eab36601 to your computer and use it in GitHub Desktop.
Extrae archivos .EXP (Mapa) de los juegos de Vigilante 8
import os
import struct
import progressbar
import tkinter as tk
from tkinter import filedialog
ATTR_QUANTITY = 21
AttrName = ["FORM", "TERRTITL", "TEXT", "XLSC", "HEAD", "XBGM", "SUNA", "COLS", "XBMP", "XTIN", "ZONE", "ZMAP", "AIMP", "RECT", "XWAT", "XRTP", "PLTX", "JUNC", ".", "RSEG", "BSP "]
AttrLength = [4, 8, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1, 4, 4]
AttrQuantity = [0] * ATTR_QUANTITY
def choose_file():
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename(filetypes=[("CFG files", "!archive.cfg")])
return file_path
def ExtractEXP(inFile, outDir, pbar):
pack_list = []
with open(inFile, 'rb') as exp_file:
exp_file.seek(8)
if not os.path.exists(outDir):
os.makedirs(outDir)
file_size = os.fstat(exp_file.fileno()).st_size
exp_file_pos = exp_file.tell()
while exp_file_pos < file_size:
spos = exp_file_pos
for attr in range(ATTR_QUANTITY):
compare = exp_file.read(AttrLength[attr])
if compare == AttrName[attr].encode():
data_size = struct.unpack('>i', exp_file.read(4))[0]
data_size_aligned = (data_size + 1) & 0xfffffffe
buff = exp_file.read(data_size_aligned)
outFile = os.path.join(outDir, AttrName[attr] + '{:04}'.format(AttrQuantity[attr]))
pack_list.append(AttrName[attr] + '{:04}'.format(AttrQuantity[attr]))
with open(outFile, 'wb') as out_file:
out_file.write(buff)
AttrQuantity[attr] += 1
break
else:
exp_file.seek(spos)
exp_file_pos = exp_file.tell()
pbar.update(exp_file_pos / file_size * 100)
with open(os.path.join(outDir, '!archive.cfg'), 'w') as cfg_file:
cfg_file.writelines([line + '\n' for line in pack_list])
def extract_exp_files(path):
i = 0
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith('.EXP'):
inFile = os.path.join(root, file)
outDir = os.path.join(os.path.dirname(inFile), os.path.splitext(file)[0])
print(f"Processing file '{inFile}'...")
with progressbar.ProgressBar(max_value=100) as bar:
ExtractEXP(inFile, outDir, bar)
print(f"Extraction completed. Results saved in '{outDir}' directory.")
i += 1
print(f"Extraction completed for {i} files.")
folder_path = filedialog.askdirectory()
if folder_path:
extract_exp_files(folder_path)
else:
print("No folder selected.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment