Skip to content

Instantly share code, notes, and snippets.

@wareya
Last active May 25, 2017 05:47
Show Gist options
  • Select an option

  • Save wareya/f88ed54d51167670d3a7803f5f58008e to your computer and use it in GitHub Desktop.

Select an option

Save wareya/f88ed54d51167670d3a7803f5f58008e to your computer and use it in GitHub Desktop.
script dumper that works for kamimaho (based on kssize.py)
# Modified to spit out main script content in utf-8
# Extract script with xp3viewer
import glob
import os
import re
import sys
# count mode
# not used
COUNT_MODE = 1
def is_iscript(s):
return s.find('@iscript') == 0 or s.find('[iscript]') == 0
def is_endscript(s):
return s.find('@endscript') == 0 or s.find('[endscript]') == 0
def remove_formatting(s):
s = re.sub(r';.*', "", s)
s = re.sub(r'^\*.*', "", s)
s = re.sub(r'^@.*', "", s)
s = re.sub(r'\[ruby text="(.*?)"\](.*?)\[/ruby\]', "〈\2〉《\1》", s)
s = re.sub(r'\[ps\]', "\n", s)
s = re.sub(r'\[[lr]\]', "", s)
s = re.sub(r'\[[^ ]+([ ]+(([^" ]*[ ]*=[ ]*(("[^"]*")|([^ \]]*)))|(\*)|([^"= \]]*)))*[ ]*\]', "", s)
#s = re.sub(r'\[.*?\]', "", s)
return s
outfile = open("out.txt", "w+", encoding="utf-8")
def txtsize(fi):
in_script = False
lines = size = 0
prevline_t = ''
for line in fi.readlines():
line_s = line.strip()
if is_endscript(line_s):
in_script = False
if not in_script:
line_t = remove_formatting(line_s)
if(line_t != ""):
outfile.write("%s\n" % line_t);
if is_iscript(line_s):
in_script = True
prevline_t = line_t
return lines,size
def get_encoding(fname):
data = open(fname,'rb').read(2)
if data == b'\xFF\xFE' or data == b'\xFE\xFF':
return 'utf-16'
else:
return 'cp932'
if __name__ == '__main__':
glines = gsize = 0
for path in sys.argv:
if path == '.':
continue
if path[-3:] != ".ks":
continue
for file in glob.glob(path):
txtsize(open(file,'r',encoding=get_encoding(file)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment