4
1

npcs_convert.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.ts'
  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("""/// <reference path="../../../npcs/_system/NPC.ts" />\n\n""")
  16. identifier = 'none'
  17. npc_id = ''
  18. npc_subobjects = {}
  19. npc_defaults = []
  20. counter = 0
  21. for line in lines:
  22. line = line.strip()
  23. if match := re.match(r"""<<set\s+\$npctemp\s*=\s*(\d+)>>""",line):
  24. if len(npc_id) > 0:
  25. for npc_subobject_id in npc_subobjects:
  26. npc_subobject = npc_subobjects[npc_subobject_id]
  27. file.write('\t'+npc_subobject_id+':{\n')
  28. for valueKey in npc_subobject:
  29. val = npc_subobject[valueKey]
  30. file.write('\t\t'+valueKey+':'+val+',\n')
  31. file.write('\t},\n')
  32. if len(npc_defaults) > 0:
  33. file.write('\tdefaults:[')
  34. file.write(','.join(npc_defaults))
  35. file.write('],\n')
  36. file.write('}\n')
  37. npc_id = 'A'+match.group(1)
  38. npc_subobjects = {}
  39. npc_defaults = []
  40. file.write('setup.npcs["'+npc_id+'"] = {\n')
  41. elif len(npc_id) > 0:
  42. if match := re.match(r"""<<set(?:init)?\s+\$(npc_)?(\w*?)\[\s*'A'\s*\+\s*\$npctemp\s*(?:\+\s*'(\w+)')?\]\s*=\s*(.*?)\s*>>""",line):
  43. if match.group(3):
  44. subObjectName = match.group(3)
  45. if subObjectName.startswith('_'):
  46. subObjectName = subObjectName[1:]
  47. if not match.group(2) in npc_subobjects:
  48. npc_subobjects[match.group(2)] = {}
  49. npc_subobjects[match.group(2)][subObjectName] = match.group(4)
  50. else:
  51. v = match.group(4)
  52. if v.startswith('$') or v.startswith('(') or v[0].isalpha():
  53. v="'Incorrect Initialization: "+v+"'"
  54. file.write('\t')
  55. file.write(match.group(2))
  56. file.write(':')
  57. file.write(v)
  58. file.write(',\n')
  59. elif match := re.match(r"""<<run \$npcs\.set\('A' \+ \$npctemp,'([^']+)',(.*)\)>>""",line): # Plain values
  60. field = match.group(1)
  61. v = match.group(2)
  62. if "rand(" in v or v[0] == '$':
  63. v = "'Incorrect Initialization: "+v+"'"
  64. if field == 'firstname':
  65. firstname = v
  66. if field == 'usedname' and v == firstname:
  67. continue
  68. file.write('\t')
  69. file.write(field)
  70. file.write(':')
  71. file.write(v)
  72. file.write(',\n')
  73. elif match := re.match(r"""<<run \$npcs\.set\('A'\+\$npctemp\+'(\w+)','(\w+)',([^>]+)\)>>""",line): # Subarrays, such as preferences
  74. subObjectName = match.group(1)
  75. if subObjectName.startswith('_'):
  76. subObjectName = subObjectName[1:]
  77. if not match.group(2) in npc_subobjects:
  78. npc_subobjects[match.group(2)] = {}
  79. npc_subobjects[match.group(2)][subObjectName] = match.group(3)
  80. elif match := re.match(r"""<<setinit \$pc\.hotcat_rating\['A' \+ \$npctemp\] = (\d+)>>""",line): # Hotcat
  81. v = match.group(1)
  82. file.write('\t')
  83. file.write('hotcat_rating:')
  84. file.write(v)
  85. file.write(',\n')
  86. elif match:= re.match(r"""<<gs 'npcstaticdefaults'\s*(.*?)>>""",line,re.I):
  87. npc_defaults.append(match.group(1).replace("' '",'_'))
  88. for npc_subobject_id in npc_subobjects:
  89. npc_subobject = npc_subobjects[npc_subobject_id]
  90. file.write('\t'+npc_subobject_id+':{\n')
  91. for valueKey in npc_subobject:
  92. val = npc_subobject[valueKey]
  93. file.write('\t\t'+valueKey+':'+val+',\n')
  94. file.write('\t},\n')
  95. if len(npc_defaults) > 0:
  96. file.write('\tdefaults:[')
  97. file.write(','.join(npc_defaults))
  98. file.write('],\n')
  99. file.write('}\n')
  100. tw_sources_path = os.path.join("sugarcube","src","npcs","_npcstatic")
  101. tw_destination_path = os.path.join(tw_sources_path,"compiled")
  102. os.makedirs(tw_destination_path,exist_ok=True)
  103. filesNames = [y for x in os.walk(tw_sources_path) for y in glob(os.path.join(x[0], '*.tw'))]
  104. #print(filesNames)
  105. for file in filesNames:
  106. #print(file)
  107. file_convert(os.path.basename(file).split('.')[0])