makemessage.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #示例:lscript.py -i /home/user/PAL -e gb2312 -o dialog.txt
  10. parser = argparse.ArgumentParser(description='Output FFFF info of Script file.')
  11. parser.add_argument('-i','--input', dest='inpath', help='Game path')
  12. parser.add_argument('-o','--output', dest='outfile', help='Output file')
  13. parser.add_argument('-w','--width', dest='wordwidth', help='Word width in bytes')
  14. parser.add_argument('-e','--encoding', dest='encoding', help='Encoding name')
  15. options = parser.parse_args()
  16. if options.inpath == None or len(options.inpath) == 0:
  17. print 'Game path must be specified!'
  18. parser.print_help()
  19. return
  20. if options.encoding == None or len(options.encoding) == 0:
  21. print 'Encoding must be specified!'
  22. parser.print_help()
  23. return
  24. if options.outfile == None or len(options.outfile) == 0:
  25. print 'Output file must be specified!'
  26. parser.print_help()
  27. return
  28. if options.inpath[-1] != '/' and options.inpath[-1] != '\\':
  29. options.inpath += '/'
  30. if options.wordwidth == None:
  31. options.wordwidth = 10
  32. else:
  33. options.wordwidth = int(options.wordwidth)
  34. script_bytes = []
  35. index_bytes = []
  36. msg_bytes = []
  37. word_bytes = []
  38. is_msg_group = 0 #是否正在处理文字组的标示。
  39. msg_count = 0
  40. last_index = -1
  41. for file_ in os.listdir(options.inpath):
  42. if file_.lower() == 'sss.mkf':
  43. try:
  44. with open(options.inpath + file_, 'rb') as f:
  45. f.seek(12, os.SEEK_SET)
  46. offset_begin, script_begin, file_end = struct.unpack('<III', f.read(12))
  47. f.seek(offset_begin, os.SEEK_SET)
  48. index_bytes = f.read(script_begin - offset_begin)
  49. script_bytes = f.read(file_end - script_begin)
  50. except:
  51. traceback.print_exc()
  52. return
  53. elif file_.lower() == 'm.msg':
  54. try:
  55. with open(options.inpath + file_, 'rb') as f:
  56. msg_bytes = f.read()
  57. except:
  58. traceback.print_exc()
  59. return
  60. elif file_.lower() == 'word.dat':
  61. try:
  62. with open(options.inpath + file_, 'rb') as f:
  63. data_bytes=f.read()
  64. except:
  65. traceback.print_exc()
  66. return
  67. if len(data_bytes) % options.wordwidth != 0:
  68. data_bytes += [0x20 for i in range(0, options.wordwidth - len(data_bytes) % options.wordwidth)]
  69. 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"
  70. output += "# This section contains the information that will be displayed when a user finishes the game.\n"
  71. output += "# Only the keys listed here are valid. Other keys will be ignored.\n"
  72. output += "[BEGIN CREDITS]\n"
  73. output += "# Place the translated text of 'Classical special build' here in no more than 24 half-wide characters.\n"
  74. output += "1= Classical special build\n"
  75. 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"
  76. output += "6= ${platform} port by ${author}, ${year}.\n"
  77. output += "7=\n"
  78. 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"
  79. output += "8= This is a free software and it is\n"
  80. output += "9= published under GNU General Public\n"
  81. output += "10= License v3.\n"
  82. 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"
  83. output += "11= ...Press Enter to continue\n"
  84. output += "[END CREDITS]\n\n"
  85. output += "# This section contains the words used by the game.\n"
  86. output += "[BEGIN WORDS]\n"
  87. output += "# Each line is a pattern of 'key=value', where key is an integer and value is a string.\n"
  88. for i in range(0, len(data_bytes) / options.wordwidth):
  89. output += "%d=%s\n" % (i, data_bytes[i * options.wordwidth: (i + 1) * options.wordwidth].rstrip('\x20\x00').decode(options.encoding).encode('utf-8'))
  90. output += "[END WORDS]\n\n"
  91. output += "# The following sections contain dialog/description texts used by the game.\n\n"
  92. print "Now Processing. Please wait..."
  93. for i in range(0, len(script_bytes) / 8):
  94. op, w1, w2, w3 = struct.unpack('<HHHH', script_bytes[i * 8 : (i + 1) * 8])
  95. if op == 0xFFFF:
  96. if is_msg_group == 0:
  97. is_msg_group = 1
  98. output += "%s %d\n" % ('[BEGIN MESSAGE]', w1)
  99. last_index = w1
  100. msg_count += 1
  101. msg_begin, msg_end = struct.unpack("<II",index_bytes[w1 * 4 : (w1 + 2) * 4])
  102. try:
  103. output += "%s\n" % (msg_bytes[msg_begin : msg_end].decode(options.encoding, 'replace').encode('utf-8'))
  104. except:
  105. traceback.print_exc()
  106. elif op == 0x008E:
  107. if is_msg_group == 1:
  108. output += "%s\n" % ('[CLEAR MESSAGE]')
  109. else:
  110. if is_msg_group == 1:
  111. is_msg_group = 0
  112. output += "%s %d\n\n" % ('[END MESSAGE]', last_index)
  113. try:
  114. with open(options.outfile, "wt") as f:
  115. f.write(output)
  116. except:
  117. traceback.print_exc()
  118. print "OK! Extraction finished!"
  119. print "Original Dialog script count: " + str(msg_count)
  120. if __name__ == '__main__':
  121. main()