Skip to content

Instantly share code, notes, and snippets.

@IceflowRE
Created August 7, 2019 22:32
Show Gist options
  • Save IceflowRE/0c2973cfc647d49b2a3d44ceca9d293c to your computer and use it in GitHub Desktop.
Save IceflowRE/0c2973cfc647d49b2a3d44ceca9d293c to your computer and use it in GitHub Desktop.
Trove Development Tools Wrapper
"""
Created by Iceflower S <iceflower@gmx.de> - 2019
Existing files will be overriden!
Usage:
python trove_devtools.py --help
Examples:
python trove_devtools.py --recursive extract .\blueprints\ .\extracted\
python trove_devtools.py --recursive convert blueprint .\extracted\ .\qb-export\
"""
import multiprocessing
import os
import sys
import traceback
from argparse import ArgumentParser
from concurrent.futures import ThreadPoolExecutor, as_completed
from concurrent.futures.process import ProcessPoolExecutor
from pathlib import Path
def exec_trove_tool(trove_app: Path, cmd: str, args: str) -> str:
os.system(f'{str(trove_app)} -tool {cmd} {args}')
return f'{str(trove_app)} -tool {cmd} {args}'
def extract(trove_app: Path, input_path: Path, output_path: Path, recursive: bool):
cmd = "extractarchive"
if not input_path.is_dir():
print("Input is not a directory.")
exit(1)
if recursive:
path_list = input_path.glob('**/*index.tfi')
job_list = []
with ThreadPoolExecutor(max_workers=multiprocessing.cpu_count()) as executor:
for archive in path_list:
cur_output = output_path.joinpath(archive.parent.relative_to(args.input))
cur_output.mkdir(exist_ok=True, parents=True)
job = executor.submit(exec_trove_tool, trove_app, cmd, f'"{str(archive.parent)}" "{str(cur_output)}"')
job_list.append(job)
for res in as_completed(job_list):
print(res.result())
else:
exec_trove_tool(trove_app, cmd, f'"{str(input_path)}" "{str(output_path)}"')
def convert(trove_app: Path, cmd: str, in_end: str, out_end: str, input_path: Path, output_path: Path, recursive: bool):
if input_path.is_file():
output_file = str(output_path.joinpath(input_path.stem)) + "." + out_end
exec_trove_tool(trove_app, cmd, f'"{str(input_path)}" "{str(output_file)}"')
elif input_path.is_dir():
if recursive:
path_list = input_path.glob('**/*.' + in_end)
else:
path_list = input_path.glob('*.' + in_end)
job_list = []
with ProcessPoolExecutor(max_workers=multiprocessing.cpu_count()) as executor:
for file in path_list:
cur_output = output_path.joinpath(file.parent.relative_to(args.input))
cur_output.mkdir(exist_ok=True, parents=True)
cur_output_file = str(cur_output.joinpath(file.stem)) + "." + out_end
job = executor.submit(exec_trove_tool, trove_app, cmd, f'"{str(file)}" "{str(cur_output_file)}"')
job_list.append(job)
for res in as_completed(job_list):
print(res.result())
if __name__ == '__main__':
if sys.version_info[0] < 3 or sys.version_info[1] < 7:
exit('Only Python 3.7 or greater is supported. You are using:' + sys.version)
parser = ArgumentParser(description="Trove Development Tools Wrapper - created by Iceflower S <iceflower@gmx.de> - 2019\nExisting files will be overriden!")
parser.add_argument('-v', '--version', action='version', version='0.1.0')
parser.add_argument('--trove', dest='trove_app', metavar='<trove directory>', type=Path, default=Path("./"),
help='Trove directory.')
parser.add_argument('--recursive', dest='recursive', action='store_true',
help='Do input recursive, if possible.')
subparsers = parser.add_subparsers(required=True, dest='command')
extractor = subparsers.add_parser('extract',
help='Extract blueprint.')
converter = subparsers.add_parser('convert',
help='Convert from qb/blueprint to blueprint/qb.')
converter.add_argument('input_type', type=str, choices=['blueprint', 'qb'],
help='Input type.')
parser.add_argument('input', type=Path,
help='Input file/directory.')
parser.add_argument('output', type=Path,
help='Output directory.')
args = parser.parse_args(sys.argv[1:])
trove_app = args.trove_app.joinpath('Trove.exe')
if not trove_app.exists():
print("Error: Couldn't find Trove.exe")
exit(1)
if not args.input.exists():
print("Input not found.")
exit(1)
if args.input.is_file() and args.recursive:
print("Input is file, ignoring --recursive.")
args.output.mkdir(exist_ok=True, parents=True)
try:
if args.command == 'extract':
extract(trove_app, args.input, args.output, args.recursive)
elif args.command == 'convert':
if args.input_type == 'blueprint':
cmd = "copyblueprint -generatemaps 1"
convert(trove_app, cmd, "blueprint", "qb", args.input, args.output, args.recursive)
elif args.input_type == 'qb':
cmd = "copyblueprint -applystylemap 1 -applytypemap 1 -applyalphamap 1 -applyentitymap 1"
convert(trove_app, cmd, "qb", "blueprint", args.input, args.output, args.recursive)
except Exception as ex:
print('Something went wrong: ' + traceback.format_exc(ex.__traceback__))
exit(1)
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment