1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- #!/usr/bin/env python3
- import os
- from glob import glob
- import fileinput
- import re
- import sys
- def file_convert(filename):
- global tw_sources_path, tw_destination_path
- source_filepath= os.path.join(tw_sources_path,filename)+'.tw'
- destination_filepath= os.path.join(tw_destination_path,filename)+'compiled.js'
- with open(source_filepath, 'r') as file:
- lines = [line.rstrip() for line in file]
- npccounter = 0
- with open(destination_filepath, 'w') as file:
- file.write("if(!setup.outfits)\n\tsetup.outfits = {};\n")
- identifier = 'none'
- counter = 0
- for line in lines:
- if match := re.match(r"""^\s*::.*attributes_(.*?)\s*$""",line):
- identifier = match.group(1)
- file.write("setup.outfits['"+identifier+"'] = {\n")
- continue
- if match := re.match(r"""^\s*<<set\s+\$(.*?)\s*=\s*(.*?)>>\s*$""",line):
- varname = match.group(1)
- val = match.group(2)
- if submatch := re.match(r"""\s*ERROR: FAILED TO CONVERT LITERAL: "{3}'(.*)"{3}\s*""",val):
- val = f"'{submatch.group(1)}'"
- if len(val) > 2 and val.startswith("'") and val.endswith("'"):
- #It's a string
- val = val[1:-1]
- val = val.replace("'",r"\'")
- val = f"'{val}'"
- if counter > 0:
- file.write(f"""\t\t"{varname}": {val},\n""")
- continue
- if match := re.match(r"""^\s*<<(?:else)?if\s+\$location_var\[\$here\]\[1\]\s*==\s*(\d+)\s*>>\s*$""",line):
- id = match.group(1)
- if counter > 0:
- file.write("\t},\n")
- file.write("\t'"+id+"':{\n")
- counter += 1
- file.write("\t}\n}")
- tw_sources_path = os.path.join("sugarcube","src","autogenerated","outfits")
- tw_destination_path = os.path.join(tw_sources_path,"compiled")
- os.makedirs(tw_destination_path,exist_ok=True)
- filesNames = [y for x in os.walk(tw_sources_path) for y in glob(os.path.join(x[0], '*.tw'))]
- #print(filesNames)
- for file in filesNames:
- #print(file)
- file_convert(os.path.basename(file).split('.')[0])
|