makemessage.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from __future__ import with_statement
  4. import argparse
  5. import os
  6. import traceback
  7. import struct
  8. def main():
  9. parser = argparse.ArgumentParser(description = 'Generate a translatable language file that can be used by SDLPAL.')
  10. parser.add_argument('gamepath', help = 'Game path where SSS.MKF & M.MSG & WORD.DAT are located.')
  11. parser.add_argument('outputfile', help = 'Path of the output message file.')
  12. parser.add_argument('encoding', choices = ['gbk', 'big5'], help = 'Text encoding name, should be either gbk or big5.')
  13. parser.add_argument('-w', '--width', dest = 'wordwidth', default = 10, type = int, help = 'Word width in bytes, default is 10')
  14. parser.add_argument("-c", "--comment", action = 'store_true', help = 'Automatically generate comments')
  15. options = parser.parse_args()
  16. if options.gamepath[-1] != '/' and options.gamepath[-1] != '\\':
  17. options.gamepath += '/'
  18. script_bytes = []
  19. index_bytes = []
  20. msg_bytes = []
  21. word_bytes = []
  22. is_msg_group = 0 #是否正在处理文字组的标示。
  23. msg_count = 0
  24. last_index = -1
  25. temp = ""
  26. comment = ""
  27. message = ""
  28. for file_ in os.listdir(options.gamepath):
  29. if file_.lower() == 'sss.mkf':
  30. try:
  31. with open(options.gamepath + file_, 'rb') as f:
  32. f.seek(12, os.SEEK_SET)
  33. offset_begin, script_begin, file_end = struct.unpack('<III', f.read(12))
  34. f.seek(offset_begin, os.SEEK_SET)
  35. index_bytes = f.read(script_begin - offset_begin)
  36. script_bytes = f.read(file_end - script_begin)
  37. except:
  38. traceback.print_exc()
  39. return
  40. elif file_.lower() == 'm.msg':
  41. try:
  42. with open(options.gamepath + file_, 'rb') as f:
  43. msg_bytes = f.read()
  44. except:
  45. traceback.print_exc()
  46. return
  47. elif file_.lower() == 'word.dat':
  48. try:
  49. with open(options.gamepath + file_, 'rb') as f:
  50. data_bytes=f.read()
  51. except:
  52. traceback.print_exc()
  53. return
  54. if len(data_bytes) % options.wordwidth != 0:
  55. data_bytes += [0x20 for i in range(0, options.wordwidth - len(data_bytes) % options.wordwidth)]
  56. output = "# All lines, except those inside [BEIGN MESSAGE] and [END MESSAGE], can be commented by adding the sharp '#' mark at the first of the line.\n\n"
  57. output += "# This section contains the information that will be displayed when a user finishes the game.\n"
  58. output += "# Only the keys listed here are valid. Other keys will be ignored.\n"
  59. output += "[BEGIN CREDITS]\n"
  60. output += "# Place the translated text of 'Classical special build' here in no more than 24 half-wide characters.\n"
  61. output += "1= Classical special build\n"
  62. output += "# Place the translated porting information template at the following two lines. Be aware that each replaced line will be truncated into at most 40 half-wide characters.\n"
  63. output += "6= ${platform} port by ${author}, ${year}.\n"
  64. output += "7=\n"
  65. output += "# Place the translated GNU licensing information at the following three lines. Be aware that each line will be truncated into at most 40 half-wide characters.\n"
  66. output += "8= This is a free software and it is\n"
  67. output += "9= published under GNU General Public\n"
  68. output += "10= License v3.\n"
  69. output += "# Place the translated text at the following line. Be aware that each line will be truncated into at most 40 half-wide characters.\n"
  70. output += "11= ...Press Enter to continue\n"
  71. output += "[END CREDITS]\n\n"
  72. output += "# Each line controls one position value, lines from 1 to 26 is for the equipment menu.\n"
  73. output += "# For each line, the format is 'idx=x,y,flag', while the flag is optional\n"
  74. output += "# If bit 0 of flag is 1, then use 8x8 font; and while bit 1 of flag is 1, then disable shadow\n"
  75. output += "[BEGIN LAYOUT]\n"
  76. output += "# 1 is the position of image box in equip menu\n"
  77. output += "1=8,8\n"
  78. output += "# 2 is the position of role list box in equip menu\n"
  79. output += "2=2,95\n"
  80. output += "# 3 is the position of current equipment's name in equip menu\n"
  81. output += "3=5,70\n"
  82. output += "# 4 is the position of current equipment's amount in equip menu\n"
  83. output += "4=51,57\n"
  84. output += "# 5 .. 10 are the positions of words 566 ... 571 in equip menu\n"
  85. output += "5=92,11\n"
  86. output += "6=92,33\n"
  87. output += "7=92,55\n"
  88. output += "8=92,77\n"
  89. output += "9=92,99\n"
  90. output += "10=92,121\n"
  91. output += "# 11 .. 16 are the positions of equipped equipments in equip menu\n"
  92. output += "11=130,11\n"
  93. output += "12=130,33\n"
  94. output += "13=130,55\n"
  95. output += "14=130,77\n"
  96. output += "15=130,99\n"
  97. output += "16=130,121\n"
  98. output += "# 17 .. 21 are the positions of words 51 ... 55 in equip menu\n"
  99. output += "17=226,10\n"
  100. output += "18=226,32\n"
  101. output += "19=226,54\n"
  102. output += "20=226,76\n"
  103. output += "21=226,98\n"
  104. output += "# 22 .. 26 are the positions of status values in equip menu\n"
  105. output += "22=260,14\n"
  106. output += "23=260,36\n"
  107. output += "24=260,58\n"
  108. output += "25=260,80\n"
  109. output += "26=260,102\n"
  110. output += "[END LAYOUT]\n\n"
  111. output += "# This section contains the words used by the game.\n"
  112. output += "[BEGIN WORDS]\n"
  113. output += "# Each line is a pattern of 'key=value', where key is an integer and value is a string.\n"
  114. for i in range(0, len(data_bytes) / options.wordwidth):
  115. temp = data_bytes[i * options.wordwidth: (i + 1) * options.wordwidth].rstrip('\x20\x00').decode(options.encoding).encode('utf-8')
  116. if options.comment: output += "# Original word: %d=%s\n" % (i, temp)
  117. output += "%d=%s\n" % (i, temp)
  118. output += "566=Headgear\n"
  119. output += "567=Body Gear\n"
  120. output += "568=Clothing\n"
  121. output += "569=Weapon\n"
  122. output += "570=Footwear\n"
  123. output += "571=Accessory\n"
  124. output += "# This is the only addtional word for ATB in SDLPAL. It is not used in classical mode.\n"
  125. output += "65530=Battle Speed\n"
  126. output += "[END WORDS]\n\n"
  127. output += "# The following sections contain dialog/description texts used by the game.\n\n"
  128. print "Now Processing. Please wait..."
  129. for i in range(0, len(script_bytes) / 8):
  130. op, w1, w2, w3 = struct.unpack('<HHHH', script_bytes[i * 8 : (i + 1) * 8])
  131. if op == 0xFFFF:
  132. if is_msg_group == 0:
  133. is_msg_group = 1
  134. message = "%s %d\n" % ('[BEGIN MESSAGE]', w1)
  135. if options.comment: comment = "# Original message: %d\n" % w1
  136. last_index = w1
  137. msg_count += 1
  138. msg_begin, msg_end = struct.unpack("<II",index_bytes[w1 * 4 : (w1 + 2) * 4])
  139. try:
  140. temp = "%s\n" % (msg_bytes[msg_begin : msg_end].decode(options.encoding, 'replace').encode('utf-8'))
  141. message += temp
  142. if options.comment: comment += "# " + temp
  143. except:
  144. traceback.print_exc()
  145. elif op == 0x008E:
  146. if is_msg_group == 1:
  147. temp = "%s\n" % ('[CLEAR MESSAGE]')
  148. message += temp
  149. if options.comment: comment += "# " + temp
  150. else:
  151. if is_msg_group == 1:
  152. is_msg_group = 0
  153. temp = "%s %d\n\n" % ('[END MESSAGE]', last_index)
  154. message += temp
  155. output += comment + message
  156. try:
  157. with open(options.outputfile, "wt") as f:
  158. f.write(output)
  159. except:
  160. traceback.print_exc()
  161. print "OK! Extraction finished!"
  162. print "Original Dialog script count: " + str(msg_count)
  163. if __name__ == '__main__':
  164. main()