-
Notifications
You must be signed in to change notification settings - Fork 1
/
fushigi_tools.py
107 lines (86 loc) · 3.97 KB
/
fushigi_tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python3
import argparse, os
import logging as log
from fushigi import *
log.basicConfig(level=log.WARNING) # change to warning when finished
# Some of the variable naming in this is a bit fucky, since I didn't initially realize that Him5 included a hashmap
# Config
argparser = argparse.ArgumentParser(description='Tools for ふしぎ電車 archive files')
argparser.add_argument('command', help='command to perform on the targets',
choices=['format', 'metadata', 'unpack', 'repack'])
argparser.add_argument('files', metavar='target', nargs='+', help='source file or directory to act upon')
argparser.add_argument('--output', dest='output', default='./asset_dump', help='directory containing output files, defaults to ./asset_dump')
args = argparser.parse_args()
# Body
def process_file(file_path):
log.info('Processing file: ' + file_path)
basename = os.path.basename(file_path)
name, extension = os.path.splitext(basename)
if extension.upper() == '.HXP':
print('Processing: ' + basename)
with open(file_path, 'rb') as main_file:
file_format, metadata = parser.file_info(main_file)
command = args.command
if command == 'format':
print('File Format:', file_format)
elif command == 'metadata':
util.print_list(metadata)
elif command == 'unpack':
asset_dir = os.path.join(args.output, basename + ';' + file_format)
util.ensure_dir_exists(asset_dir)
if len(os.listdir(asset_dir)) > 0:
response = input('Directory %s already exists. Empty and re-unpack? (y/N)'.format(asset_dir))
if response.lower() in ('y', 'no'):
util.clear_dir_contents(asset_dir)
else:
return
if file_format == 'Him4':
for index, offset in enumerate(metadata):
unpacker.him4(offset, main_file, asset_dir, index)
elif file_format == 'Him5':
for folder in metadata:
for file in folder['files']:
unpacker.him5(file, main_file, asset_dir)
else:
log.error('unsupported extension: %s', extension)
return
def process_folder(path):
log.info('Processing folder: ' + path)
# this is gross and bad i'm sorry
input_dir, basename = os.path.split(path[:-1])
filename, file_format = basename.split(';')
name, extension = os.path.splitext(filename)
if extension == '.HXP':
print('Processing: ' + basename)
output_path = os.path.join(args.output, filename)
if os.path.exists(output_path):
response = input('File %s already exists. Remove and re-pack? (y/N)'.format(output_path))
if response.lower() in ('y', 'no'):
os.remove(output_path)
else:
return
contents = sorted([os.path.join(path, f) for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))])
if file_format == 'Him4':
repacker.him4(contents, output_path)
else:
repacker.him5(contents, output_path)
else:
log.error('unsupported extension: %s', extension)
return
for path in args.files:
is_file = os.path.isfile(path)
is_dir = os.path.isdir(path)
if not (is_file or is_dir):
log.warn('%s does not exist; skipping', path)
elif is_file:
if args.command in ('format', 'metadata', 'unpack'):
process_file(path)
else:
log.error('%s is a file; please specify a directory to repack')
else: # is_dir
if args.command in ('format', 'metadata', 'unpack'):
files = [os.path.join(path, f) for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
for filename in files:
process_file(filename)
else:
process_folder(path)