npcs_convert.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python3
  2. import os
  3. from glob import glob
  4. import fileinput
  5. import re
  6. import sys
  7. exit(0)
  8. def file_convert(filename):
  9. global tw_sources_path, tw_destination_path
  10. source_filepath= os.path.join(tw_sources_path,filename)+'.tw'
  11. destination_filepath= os.path.join(tw_destination_path,filename)+'compiled.js'
  12. with open(source_filepath, 'r') as file:
  13. lines = [line.rstrip() for line in file]
  14. npccounter = 0
  15. with open(destination_filepath, 'w') as file:
  16. file.write("if(!setup.npcs)\n\tsetup.npcs = {};\n")
  17. identifier = 'none'
  18. npc_id = ''
  19. npc_subobjects = {}
  20. npc_defaults = []
  21. counter = 0
  22. for line in lines:
  23. line = line.strip()
  24. if match := re.match(r"""<<set\s+\$npctemp\s*=\s*(\d+)>>""",line):
  25. if len(npc_id) > 0:
  26. for npc_subobject_id in npc_subobjects:
  27. npc_subobject = npc_subobjects[npc_subobject_id]
  28. file.write('\t'+npc_subobject_id+':{\n')
  29. for valueKey in npc_subobject:
  30. val = npc_subobject[valueKey]
  31. file.write('\t\t'+valueKey+':'+val+',\n')
  32. file.write('\t},\n')
  33. if len(npc_defaults) > 0:
  34. file.write('\tdefaults:[')
  35. file.write(','.join(npc_defaults))
  36. file.write('],\n')
  37. file.write('}\n')
  38. npc_id = 'A'+match.group(1)
  39. npc_subobjects = {}
  40. npc_defaults = []
  41. file.write('setup.npcs["'+npc_id+'"] = {\n')
  42. elif len(npc_id) > 0:
  43. if match := re.match(r"""<<set(?:init)?\s+\$(npc_)?(\w*?)\[\s*'A'\s*\+\s*\$npctemp\s*(?:\+\s*'(\w+)')?\]\s*=\s*(.*?)\s*>>""",line):
  44. if match.group(3):
  45. subObjectName = match.group(3)
  46. if subObjectName.startswith('_'):
  47. subObjectName = subObjectName[1:]
  48. if not match.group(2) in npc_subobjects:
  49. npc_subobjects[match.group(2)] = {}
  50. npc_subobjects[match.group(2)][subObjectName] = match.group(4)
  51. else:
  52. v = match.group(4)
  53. if v.startswith('$') or v.startswith('(') or v[0].isalpha():
  54. v="'Incorrect Initialization: "+v+"'"
  55. file.write('\t')
  56. file.write(match.group(2))
  57. file.write(':')
  58. file.write(v)
  59. file.write(',\n')
  60. elif match:= re.match(r"""<<gs 'npcstaticdefaults'\s*(.*?)>>""",line,re.I):
  61. npc_defaults.append(match.group(1).replace("' '",'_'))
  62. for npc_subobject_id in npc_subobjects:
  63. npc_subobject = npc_subobjects[npc_subobject_id]
  64. file.write('\t'+npc_subobject_id+':{\n')
  65. for valueKey in npc_subobject:
  66. val = npc_subobject[valueKey]
  67. file.write('\t\t'+valueKey+':'+val+',\n')
  68. file.write('\t},\n')
  69. if len(npc_defaults) > 0:
  70. file.write('\tdefaults:[')
  71. file.write(','.join(npc_defaults))
  72. file.write('],\n')
  73. file.write('}\n')
  74. tw_sources_path = os.path.join("sugarcube","src","autogenerated","npcstatic")
  75. tw_destination_path = os.path.join(tw_sources_path,"compiled")
  76. os.makedirs(tw_destination_path,exist_ok=True)
  77. filesNames = [y for x in os.walk(tw_sources_path) for y in glob(os.path.join(x[0], '*.tw'))]
  78. #print(filesNames)
  79. for file in filesNames:
  80. #print(file)
  81. file_convert(os.path.basename(file).split('.')[0])