makemessage.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 += "# This section contains the words used by the game.\n"
  73. output += "[BEGIN WORDS]\n"
  74. output += "# Each line is a pattern of 'key=value', where key is an integer and value is a string.\n"
  75. for i in range(0, len(data_bytes) / options.wordwidth):
  76. temp = data_bytes[i * options.wordwidth: (i + 1) * options.wordwidth].rstrip('\x20\x00').decode(options.encoding).encode('utf-8')
  77. if options.comment: output += "# Original word: %d=%s\n" % (i, temp)
  78. output += "%d=%s\n" % (i, temp)
  79. output += "# This is the only addtional word for ATB in SDLPAL. It is not used in classical mode.\n"
  80. output += "65530=Battle Speed\n"
  81. output += "[END WORDS]\n\n"
  82. output += "# The following sections contain dialog/description texts used by the game.\n\n"
  83. print "Now Processing. Please wait..."
  84. for i in range(0, len(script_bytes) / 8):
  85. op, w1, w2, w3 = struct.unpack('<HHHH', script_bytes[i * 8 : (i + 1) * 8])
  86. if op == 0xFFFF:
  87. if is_msg_group == 0:
  88. is_msg_group = 1
  89. message = "%s %d\n" % ('[BEGIN MESSAGE]', w1)
  90. if options.comment: comment = "# Original message: %d\n" % w1
  91. last_index = w1
  92. msg_count += 1
  93. msg_begin, msg_end = struct.unpack("<II",index_bytes[w1 * 4 : (w1 + 2) * 4])
  94. try:
  95. temp = "%s\n" % (msg_bytes[msg_begin : msg_end].decode(options.encoding, 'replace').encode('utf-8'))
  96. message += temp
  97. if options.comment: comment += "# " + temp
  98. except:
  99. traceback.print_exc()
  100. elif op == 0x008E:
  101. if is_msg_group == 1:
  102. temp = "%s\n" % ('[CLEAR MESSAGE]')
  103. message += temp
  104. if options.comment: comment += "# " + temp
  105. else:
  106. if is_msg_group == 1:
  107. is_msg_group = 0
  108. temp = "%s %d\n\n" % ('[END MESSAGE]', last_index)
  109. message += temp
  110. output += comment + message
  111. try:
  112. with open(options.outputfile, "wt") as f:
  113. f.write(output)
  114. except:
  115. traceback.print_exc()
  116. print "OK! Extraction finished!"
  117. print "Original Dialog script count: " + str(msg_count)
  118. if __name__ == '__main__':
  119. main()