Skip to content

Instantly share code, notes, and snippets.

@alexparlett
Last active December 19, 2023 18:28
Show Gist options
  • Save alexparlett/0630a7ca60584ae2658714fc44531aee to your computer and use it in GitHub Desktop.
Save alexparlett/0630a7ca60584ae2658714fc44531aee to your computer and use it in GitHub Desktop.
X4 - Cat Unpacker
.\unpack.py "E:\SteamLibrary\steamapps\common\X4 Foundations" "." -f "^.*(xml|xsd|html|js|css)$"
import os
import argparse
import glob
import re
parser = argparse.ArgumentParser()
parser.add_argument(
'sourcedir', help='The directory where the cat files are located')
parser.add_argument(
'destdir', help='The directory where to extract any matching files')
parser.add_argument('-i', '--include', type=list, nargs='*',
help='files to include, by default this is all cat files found in the directory')
parser.add_argument('-f', '--filter', default='^.*(xml|xsd|html|js|css|lua)$',
help='A Regex filter of which embedded files to extract, by default this is xml,xsd,html,js,css,lua')
args = parser.parse_args()
pattern = re.compile(args.filter)
outdir = args.destdir
list_of_files = []
for file in os.listdir(args.sourcedir):
if (not args.include and file.lower().endswith(".cat")) or (args.include and file in args.include):
list_of_files.append(os.path.join(args.sourcedir, file))
for file in list_of_files:
inf = open(file, "r")
inf_data_name = "%s.dat" % file.split(".")[0]
inf_data = open(inf_data_name, "rb")
for line in inf:
obj_data_split = line.split(" ")
filepath = " ".join(obj_data_split[0:len(obj_data_split) - 3])
obj_data = {"hash": obj_data_split[-1],
"modified_epoch": obj_data_split[-2],
"size": obj_data_split[-3],
"filepath": filepath}
obj_data["path"] = os.path.dirname(obj_data["filepath"])
obj_data["filename"] = obj_data["filepath"].split("/")[-1]
if pattern.match(obj_data["filepath"]):
if not os.path.isdir("%s/%s" % (outdir, obj_data["path"])):
os.makedirs("%s/%s" % (outdir, obj_data["path"]))
try:
outf = open("%s/%s/%s" %
(outdir, obj_data["path"], obj_data["filename"]), "wb")
outf.write(inf_data.read(int(obj_data["size"])))
outf.close()
except IOError:
print(("[IOERROR] %s/%s/%s" %
(outdir, obj_data["path"], obj_data["filename"])))
else:
inf_data.read(int(obj_data["size"]))
inf.close()
@nipsy
Copy link

nipsy commented Jun 17, 2023

This can be fixed by either splitting string until last dot or moving cat/dat files to path without dots e.g. /home/user/x4data/src/.

It seems like the proper solution here is to fix the logic in this script to read:

    inf_data_name = "%s.dat" % file.rsplit(".", 1)[0]

This then splits only once from the right to capture the proceeding full path name correctly, regardless of the presence of any other periods (as happens by default for Steam in Linux).

@TrogloGeek
Copy link

It seems like the proper solution here is to fix the logic in this script to read:

    inf_data_name = "%s.dat" % file.rsplit(".", 1)[0]

This then splits only once from the right to capture the proceeding full path name correctly, regardless of the presence of any other periods (as happens by default for Steam in Linux).

If it was not for the -i parameter that allows treating other arbitrary files as being a catalog file, we could do simplier with

inf_data_name = "%s.dat" % file[:-4]

to simply remove the last 4 characters as we would know file would end with .cat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment