outfits_convert.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/usr/bin/env python3
  2. import os
  3. from glob import glob
  4. import fileinput
  5. import re
  6. import sys
  7. varTranslates = {
  8. 'CoatQuality': 'quality',
  9. 'CoatWarm':'warm',
  10. 'coat_description': 'description',
  11. "CloQuality": 'quality',
  12. "CloThinness": 'thinness',
  13. "CloTopCut": 'topcut',
  14. 'CloBra': 'includesBra',
  15. "CloPanties": 'includesPanties',
  16. "CloOnePiece": 'isOnepiece',
  17. "CloInhibit": 'inhib',
  18. "CloPantsShortness": "pantsShortness",
  19. "CloSkirtShortness": 'skirtShortness',
  20. "CloDress": 'isDress',
  21. "CloBimbo":'bimbo',
  22. "Clostyle": 'style',
  23. "CloStyle": 'style',
  24. "CloStyle2":'style2',
  25. "CloStyle3":'style3',
  26. "CloCoverTop":"coverTop",
  27. "CloCoverBack": "coverBack",
  28. "CloCoverFront": "coverFront",
  29. "CloCoverfront": "coverFront",
  30. "swimwear_description": 'description',
  31. "ShoStyle": "style",
  32. "ShoQuality": 'quality',
  33. "ShoCut": 'cut',
  34. "ShoHeels": 'heels',
  35. "BraQuality": 'quality',
  36. "BraType": 'subtype',
  37. "BraFun": 'fun',
  38. "BraCover": 'cover',
  39. "BraThinness": 'thinness',
  40. "BraMaterial": 'material',
  41. "PanType": 'subtype',
  42. "PanFun": 'fun',
  43. "PanMaterial": 'material',
  44. "PanQuality": 'quality',
  45. "PanThinness": 'thinness',
  46. 'PanCoverBack':'coverBack',
  47. 'PanCoverFront':'coverFront',
  48. 'CloExhibit':'//CloExhibit'
  49. }
  50. def file_convert(filename):
  51. shoeHeightRandomizer = 0
  52. global tw_sources_path, tw_destination_path
  53. source_filepath= os.path.join(tw_sources_path,filename)+'.tw'
  54. destination_filepath= os.path.join(tw_destination_path,filename)+'compiled.ts'
  55. with open(source_filepath, 'r') as file:
  56. lines = [line.rstrip() for line in file]
  57. with open(destination_filepath, 'w') as file:
  58. #file.write("if(!setup.outfits)\n\tsetup.outfits = {};\n")
  59. identifier = 'none'
  60. counter = 0
  61. fieldsAlreadySeen = {}
  62. for line in lines:
  63. if match := re.match(r"""^\s*::.*attributes_(.*?)\s*$""",line):
  64. identifier = match.group(1)
  65. #file.write("setup.outfits['"+identifier+"'] = {\n")
  66. #file.write("Object.assign(setup.outfits,{\n")
  67. file.write("""/// <reference path="../../wardrobe/wardrobe.ts" />\n""")
  68. continue
  69. if match := re.match(r"""^\s*<<set\s+\$(.*?)\s*=\s*(.*?)>>\s*$""",line):
  70. varname = match.group(1)
  71. if(varname in varTranslates):
  72. varname = varTranslates[varname]
  73. val = match.group(2)
  74. if submatch := re.match(r"""\s*ERROR: FAILED TO CONVERT LITERAL: "{3}'(.*)"{3}\s*""",val):
  75. val = f"'{submatch.group(1)}'"
  76. if len(val) > 2 and val.startswith("'") and val.endswith("'"):
  77. #It's a string
  78. val = val[1:-1]
  79. val = val.replace("'",r"\'")
  80. val = f"'{val}'"
  81. if varname == 'heels':
  82. shoeHeightRandomizer += 1
  83. if val == '0':
  84. val = 0
  85. elif val == '1':
  86. val = 0
  87. elif val == '2':
  88. val = 1 + shoeHeightRandomizer % 2
  89. elif val == '3':
  90. val = 3
  91. elif val == '4':
  92. val = 4 + shoeHeightRandomizer % 2
  93. elif val == '5':
  94. val = 6 + shoeHeightRandomizer % 2
  95. elif val == '6':
  96. val = 8
  97. if counter > 0:
  98. if(varname in fieldsAlreadySeen):
  99. continue
  100. file.write(f"""\t\t{varname}: {val},\n""")
  101. fieldsAlreadySeen[varname] = True
  102. continue
  103. if match := re.match(r"""^\s*<<(?:else)?if\s+\$location_var\[\$here\]\[1\]\s*==\s*(\d+)\s*>>\s*$""",line):
  104. id = identifier+'_'+match.group(1)
  105. idParts = identifier.split('_')
  106. vendor = idParts[0]
  107. itemtype = 'error'
  108. index = match.group(1)
  109. subtype = None
  110. if vendor == 'school' or vendor == 'office':
  111. subtype = vendor
  112. vendor = 'gm'
  113. itemtype = 'clothes'
  114. elif vendor == 'market':
  115. itemtype = 'clothes'
  116. subtype = 'outfit'
  117. else:
  118. if(len(idParts) < 2):
  119. print("Error: "+destination_filepath)
  120. else:
  121. itemtype = idParts[1]
  122. if itemtype == 'coats':
  123. itemtype = 'coat'
  124. id = id.replace('coats','coat')
  125. elif itemtype == 'swim':
  126. itemtype = 'swimwear'
  127. if idParts[2] == 'one':
  128. subtype = 'swimsuit'
  129. else:
  130. subtype = 'bikini'
  131. elif itemtype == 'maid':
  132. itemtype = 'clothes'
  133. subtype = 'maid'
  134. elif itemtype in ['outfits']:
  135. subtype = itemtype[:-1]
  136. id = id.replace(itemtype,itemtype[:-1])
  137. itemtype = 'clothes'
  138. elif itemtype in ['dress','strip','burlesque','cosplay','gown','server']:
  139. subtype = itemtype
  140. itemtype = 'clothes'
  141. if counter > 0:
  142. file.write("\t},\n")
  143. pass
  144. file.write("\tsetup.outfits."+id+" = {\n")
  145. file.write("\t\tvendor:'"+vendor+"',\n")
  146. file.write("\t\ttype:'"+itemtype+"',\n")
  147. if subtype:
  148. file.write("\t\tsubtype:'"+subtype+"',\n")
  149. file.write("\t\tindex:"+index+",\n")
  150. counter += 1
  151. fieldsAlreadySeen = {}
  152. file.write("\t}")
  153. #file.write("\t}\n});")
  154. tw_sources_path = os.path.join("sugarcube","src","outfits")
  155. tw_destination_path = os.path.join(tw_sources_path,"compiled")
  156. os.makedirs(tw_destination_path,exist_ok=True)
  157. filesNames = [y for x in os.walk(tw_sources_path) for y in glob(os.path.join(x[0], '*.tw'))]
  158. #print(filesNames)
  159. for file in filesNames:
  160. print(file)
  161. file_convert(os.path.basename(file).split('.')[0])