outfits_convert.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env python3
  2. import os
  3. from glob import glob
  4. import fileinput
  5. import re
  6. import sys
  7. def file_convert(filename):
  8. global tw_sources_path, tw_destination_path
  9. source_filepath= os.path.join(tw_sources_path,filename)+'.tw'
  10. destination_filepath= os.path.join(tw_destination_path,filename)+'compiled.js'
  11. with open(source_filepath, 'r') as file:
  12. lines = [line.rstrip() for line in file]
  13. npccounter = 0
  14. with open(destination_filepath, 'w') as file:
  15. file.write("if(!setup.outfits)\n\tsetup.outfits = {};\n")
  16. identifier = 'none'
  17. counter = 0
  18. for line in lines:
  19. if match := re.match(r"""^\s*::.*attributes_(.*?)\s*$""",line):
  20. identifier = match.group(1)
  21. file.write("setup.outfits['"+identifier+"'] = {\n")
  22. continue
  23. if match := re.match(r"""^\s*<<set\s+\$(.*?)\s*=\s*(.*?)>>\s*$""",line):
  24. varname = match.group(1)
  25. val = match.group(2)
  26. if submatch := re.match(r"""\s*ERROR: FAILED TO CONVERT LITERAL: "{3}'(.*)"{3}\s*""",val):
  27. val = f"'{submatch.group(1)}'"
  28. if len(val) > 2 and val.startswith("'") and val.endswith("'"):
  29. #It's a string
  30. val = val[1:-1]
  31. val = val.replace("'",r"\'")
  32. val = f"'{val}'"
  33. if counter > 0:
  34. file.write(f"""\t\t"{varname}": {val},\n""")
  35. continue
  36. if match := re.match(r"""^\s*<<(?:else)?if\s+\$location_var\[\$here\]\[1\]\s*==\s*(\d+)\s*>>\s*$""",line):
  37. id = match.group(1)
  38. if counter > 0:
  39. file.write("\t},\n")
  40. file.write("\t'"+id+"':{\n")
  41. counter += 1
  42. file.write("\t}\n}")
  43. tw_sources_path = os.path.join("sugarcube","src","autogenerated","outfits")
  44. tw_destination_path = os.path.join(tw_sources_path,"compiled")
  45. os.makedirs(tw_destination_path,exist_ok=True)
  46. filesNames = [y for x in os.walk(tw_sources_path) for y in glob(os.path.join(x[0], '*.tw'))]
  47. #print(filesNames)
  48. for file in filesNames:
  49. #print(file)
  50. file_convert(os.path.basename(file).split('.')[0])