Add scripts to patch/extract .ks (KiriKiri) file
This commit is contained in:
79
extract_ks.py
Normal file
79
extract_ks.py
Normal file
@@ -0,0 +1,79 @@
|
||||
import re
|
||||
from os.path import splitext
|
||||
from os import listdir
|
||||
import json
|
||||
|
||||
|
||||
def extract_dialogue_from_ks(file_path, encoding='cp932'):
|
||||
# 存储结果
|
||||
dialogues = []
|
||||
|
||||
try:
|
||||
with open(file_path, 'r', encoding=encoding) as file:
|
||||
content = file.read()
|
||||
contents = content.split('\n')
|
||||
block_text = ''
|
||||
block_name = ''
|
||||
for line in contents:
|
||||
if line.endswith('|'):
|
||||
if block_text:
|
||||
if block_name:
|
||||
dialogues.append({
|
||||
'name': block_name,
|
||||
'message': block_text
|
||||
})
|
||||
else:
|
||||
dialogues.append({
|
||||
'message': block_text
|
||||
})
|
||||
block_text = ''
|
||||
block_name = ''
|
||||
continue
|
||||
if line.startswith('@exlink') or line.startswith('@e_xlink'):
|
||||
a = re.search(r'txt="([^"]+)"', line)
|
||||
if a:
|
||||
dialogues.append({
|
||||
'message': a.group(1),
|
||||
})
|
||||
continue
|
||||
if line.startswith('@nm'):
|
||||
a = re.search(r't="([^"]+)"', line)
|
||||
if a:
|
||||
block_name = a.group(1)
|
||||
continue
|
||||
if not (line.endswith('[r]') or line.endswith('[np]')):
|
||||
continue
|
||||
block_text += line.replace('\n', '').replace('[r]', '\n').replace('[np]', '')
|
||||
if block_text:
|
||||
if block_name:
|
||||
dialogues.append({
|
||||
'name': block_name,
|
||||
'message': block_text
|
||||
})
|
||||
else:
|
||||
dialogues.append({
|
||||
'message': block_text
|
||||
})
|
||||
return dialogues
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error processing file: {e}")
|
||||
return []
|
||||
|
||||
|
||||
def get_ks_files(directory):
|
||||
"""
|
||||
获取指定目录下的所有.ks文件
|
||||
:param directory: 目录路径
|
||||
:return: ks文件列表
|
||||
"""
|
||||
return [f for f in listdir(directory) if splitext(f)[1].lower() == '.ks']
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
for f in get_ks_files('.'):
|
||||
print(f"Processing file: {f}")
|
||||
result = extract_dialogue_from_ks(f)
|
||||
print(f"Extracted {len(result)} dialogues from {f}")
|
||||
with open(f"{splitext(f)[0]}.json", 'w', encoding='utf-8') as output_file:
|
||||
json.dump(result, output_file, ensure_ascii=False, indent=4)
|
||||
Reference in New Issue
Block a user