Last active
May 12, 2018 06:05
-
-
Save mjtorn/105c229eff1b4d6071f9d2a9ae842023 to your computer and use it in GitHub Desktop.
If you need to parse Escoria scripts for dialog but aren't far enough in your game to have all the scripts loaded for lockit.gd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
from collections import namedtuple | |
import itertools | |
import operator | |
import re | |
import sys | |
DialogSpec = namedtuple('DialogSpec', ('fname', 'id', 'dialog', 'comment')) | |
def get_dialogs(files): | |
dialogs = {} | |
ok = True | |
seen_dialog = {} | |
for fname in files: | |
with open(fname, 'r') as f: | |
lines = [l.strip() for l in f.readlines()] | |
lines = [l for l in lines if l.startswith('say')] | |
if lines: | |
for line in lines: | |
comment = None | |
dialog = None | |
char = None | |
id_ = None | |
# Basic validation | |
if ':"' not in line: | |
print('Broken line in {}'.format(fname)) | |
print('\t{}'.format(line)) | |
ok = False | |
# Fuck the pain away | |
line = re.sub('\[.+\]', '', line).strip() | |
# Parse comments | |
if '#' in line: | |
line, comment = line.rsplit('#', 1) | |
comment = comment.strip() | |
# Know about the id separator | |
line, dialog = line.rsplit(':', 1) | |
# Smaller tokens | |
split_line = line.split() | |
assert(len(split_line) == 3) | |
assert(split_line[0] == 'say') | |
char = split_line[1] | |
id_ = split_line[2] | |
# Sanity check | |
if id_ in seen_dialog: | |
if seen_dialog[id_] != dialog: | |
print('Duplicate id "{}" for different dialog in file {}:\n\t{} <> {}' | |
''.format(id_, fname, seen_dialog[id_], dialog)) | |
ok = False | |
else: | |
seen_dialog[id_] = dialog | |
dialog_spec = DialogSpec(fname, id_, dialog, comment) | |
dialogs.setdefault(char, []).append(dialog_spec) | |
return dialogs, ok | |
def main(args): | |
files = args | |
dialogs, ok = get_dialogs(files) | |
if ok: | |
for char in dialogs: | |
print('Dealing with {}'.format(char)) | |
char_out_fname = '{}.txt'.format(char) | |
sorted_fnames = sorted(dialogs[char], key=operator.attrgetter('fname')) | |
fname_specs = itertools.groupby(sorted_fnames, operator.attrgetter('fname')) | |
output = "" | |
for fname, specs in fname_specs: | |
specs = tuple(specs) | |
# Click-clack-pow, can't care about the minutiæ of this shit | |
output = "{}\n{}\n{}\n\n".format(output, fname, len(fname) * '=') | |
for spec in specs: | |
output = """{}\ | |
{} | |
{} | |
{} | |
""".format(output, spec.id, len(spec.id) * '-', spec.dialog) | |
if spec.comment: | |
output = '{}({})'.format(output, spec.comment) | |
output = '{}\n\n'.format(output) | |
with open(char_out_fname, 'w') as f: | |
f.write(output) | |
return 0 if ok else 1 | |
if __name__ == '__main__': | |
sys.exit(main(sys.argv[1:])) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment