4
1

BuildModFiles.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python3
  2. import os
  3. import fileinput
  4. import re
  5. import sys
  6. modTag = "mod"
  7. file_path_html = 'glife.html'
  8. file_path_mod = 'mods'
  9. file_path_mod_online = 'mods'
  10. data = ''
  11. modData = {}
  12. scripts = {}
  13. dataWithoutModStuff = ''
  14. with open(file_path_html, 'r') as file:
  15. data = file.read()
  16. dataWithoutModStuff = data
  17. i = 0
  18. regex = r"""<tw-passagedata pid="(\d+)" name="([\s\w]+)" tags="(.*?)" (?:position="\d+,\d+" )?(?:size="\d+,\d+")?>(.*?)</tw-passagedata>"""
  19. for match in re.finditer(regex,data,re.S):
  20. tags = match.group(3).split()
  21. if(modTag in tags):
  22. modId = 'default'
  23. for tag in tags:
  24. if tag.startswith('mod_'):
  25. modId = tag[4:]
  26. if modId not in modData:
  27. modData[modId] = ''
  28. modData[modId] += match.group(0)+'\n'
  29. dataWithoutModStuff = dataWithoutModStuff.replace(match.group(0),'')
  30. i += 1
  31. if(i % 100 == 0):
  32. print(i)
  33. regex = r"""/\*\s*Start Mod:\s*"([^"]*)"\s*\*/(.*?)/\*\s*End Mod:\s*"Mod"\s*\*/"""
  34. for match in re.finditer(regex,data,re.S):
  35. modId = match.group(1).strip()
  36. if not modId:
  37. modId = 'default'
  38. if modId not in scripts:
  39. scripts[modId] = ''
  40. scripts[modId] += match.group(2)
  41. dataWithoutModStuff = dataWithoutModStuff.replace(match.group(0),'')
  42. for modId, scriptContent in scripts.items():
  43. if modId not in modData:
  44. modData[modId] = ''
  45. modData[modId] += "\n.-.-.-. STORYDATA / SCRIPT .-.-.-.\n"
  46. modData[modId] += scriptContent
  47. for modId, modContent in modData.items():
  48. modPath = os.path.join(file_path_mod,modId+'.html')
  49. with open(modPath, 'w') as file:
  50. file.write(modContent)
  51. for modId, modContent in modData.items():
  52. modPath = os.path.join(file_path_mod_online,modId+'.php')
  53. with open(modPath, 'w') as file:
  54. file.write(f'<?php header("Access-Control-Allow-Origin: *"); header(\'Content-type: text/javascript\'); echo($_GET[\'callback\']);?>(`{modContent}\n`);')
  55. with open(file_path_html, 'w') as file:
  56. file.write(dataWithoutModStuff)
  57. print('Done')