WIP 18/2/24

This commit is contained in:
Dusk 2024-02-18 17:11:31 +01:00
parent 6c9dd79880
commit ab35815fc5
4 changed files with 135 additions and 30 deletions

46
src/flow.py Normal file
View File

@ -0,0 +1,46 @@
#!/usr/bin/env python3
onik = {
"gamestart" : [
'onik_000.txt',
'onik_001.txt',
'onik_002.txt',
'onik_003.txt',
'onik_004.txt',
'onik_005.txt',
'onik_009.txt',
'onik_009_02.txt',
'onik_010.txt',
'onik_011.txt',
'onik_012.txt',
'onik_013.txt',
'onik_014.txt',
'onik_014_02.txt',
'onik_015.txt',
'onik_015_02.txt',
'onik_015_03.txt',
],
"Opening" : [ 'onik_op.txt' ],
"Sub_Tips_001" : [ 'onik_tips_01.txt' ],
"Sub_Tips_002" : [ 'onik_tips_02.txt' ],
"Sub_Tips_003" : [ 'onik_tips_03.txt' ],
"Sub_Tips_004" : [ 'onik_tips_04.txt' ],
"Sub_Tips_005" : [ 'onik_tips_05.txt' ],
"Sub_Tips_006" : [ 'onik_tips_06.txt' ],
"Sub_Tips_006" : [ 'onik_tips_06.txt' ],
"Sub_Tips_007" : [ 'onik_tips_07.txt' ],
"Sub_Tips_008" : [ 'onik_tips_08.txt' ],
"Sub_Tips_009" : [ 'onik_tips_09.txt' ],
"Sub_Tips_010" : [ 'onik_tips_10.txt' ],
"Sub_Tips_011" : [ 'onik_tips_11.txt' ],
"Sub_Tips_012" : [ 'onik_tips_12.txt' ],
"Sub_Tips_013" : [ 'onik_tips_13.txt' ],
"Sub_Tips_014" : [ 'onik_tips_14.txt' ],
"Sub_Tips_015" : [ 'onik_tips_15.txt' ],
"Sub_Tips_016" : [ 'onik_tips_16.txt' ],
"Sub_Tips_016" : [ 'onik_tips_16.txt' ],
"Sub_Tips_017" : [ 'onik_tips_17.txt' ],
"Sub_Tips_018" : [ 'onik_tips_18.txt' ],
"Sub_Tips_019" : [ 'onik_tips_19.txt' ],
"Sub_Tips_020" : [ 'onik_tips_20.txt' ],
}

View File

@ -4,7 +4,7 @@ import orig
def main(): def main():
orig.write_translated() orig.process_sections()
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -6,6 +6,7 @@ from unidecode import unidecode
import config import config
import parser import parser
import flow
japanese_ranges = [ japanese_ranges = [
(0x4E00, 0x9FFF), # Kanji (0x4E00, 0x9FFF), # Kanji
@ -16,32 +17,83 @@ japanese_ranges = [
] ]
def write_translated(): def process_sections():
translation = parser.parse_to_tokens()
output_filepath = os.path.join(config.get('output_path'), 'out.txt') output_filepath = os.path.join(config.get('output_path'), 'out.txt')
outfile = open(output_filepath, 'w', encoding='shift_jisx0213') outfile = open(output_filepath, 'w', encoding='shift_jisx0213')
origfile = open(config.get('original_path'), 'r', encoding='shift_jisx0213')
with open(config.get('original_path'), 'r', encoding='shift_jisx0213') as file: sections = dict()
for line in file:
for line in origfile:
if line.startswith('*'):
section_name = line[1:].split(' ', 1)[0].replace('\n','')
outfile.write(line)
if section_name in flow.onik:
print("entering", section_name)
write_translated(
outfile,
origfile,
flow.onik[section_name],
)
else:
outfile.write(line)
outfile.close()
origfile.close()
# Given a set of translation files, the original file and the output file
# replace the japanese lines with the translated ones in a given section.
def write_translated(outfile, origfile, translation_file_paths):
for transfilepath in translation_file_paths:
print(f'- reading "{transfilepath}"')
structure = parser.parse_to_structure(transfilepath)
for i, line in enumerate(origfile):
found = False found = False
for start, end in japanese_ranges: for start, end in japanese_ranges:
if start <= ord(line[0]) <= end: if start <= ord(line[0]) <= end:
found = True found = True
if found and len(translation) > 0: if found:
# outfile.write(unidecode(translation.pop(0)[1]).replace("\\","¥")) # The amount of lines may not coincide because the original
amount = line.count("@") + line.count("¥") # might have one line for what we have multiple lines. Count
# the number of appearances of the end-of-command symbols
# (@ and ¥) to determine how many of the translated lines is
# equivalent to the given original line.
amount = line.count("@") + line.count("¥") + line.count('/')
outfile.write('`') outfile.write('`')
for _ in range(amount): _printed_line = ""
outfile.write( while True:
unidecode(translation.pop(0)[1]).replace("\\", "¥")) if amount <= 0:
break
if structure[0][3] == 'Line_ContinueAfterTyping':
amount += 1
_printed_line += structure[0][2]
outfile.write(
unidecode(structure.pop(0)[1]).replace("\\", "¥")
)
amount -= 1
outfile.write('\n')
print("\n-", transfilepath)
print(">", _printed_line)
print("<", line, end='')
# Used up all of the structures, this chapter has ended.
# Got to the next one
if len(structure) <= 0:
break
if amount > 0:
outfile.write('\n')
else: else:
outfile.write(line) outfile.write(line)
outfile.close()

View File

@ -85,41 +85,48 @@ def get_functions_from_file(filepath: str):
return tokens return tokens
def parse_to_tokens(): def parse_to_structure(filename: str):
scripts_path = config.get("scripts_path") scripts_path = config.get("scripts_path")
tokens = get_functions_from_file( tokens = get_functions_from_file(
os.path.join(scripts_path, "onik_000.txt") os.path.join(scripts_path, filename)
) )
structure = [] structure = []
for token in tokens: for token in tokens:
if token[0] == "OutputLine": function_name = token[0]
dialogue = token[4][1:-1].replace('\\', '')
if token[-1] == "Line_Normal": if function_name == "OutputLine":
text_jp = token[2][1:-1]
text_en = token[4][1:-1]
line_type = token[-1]
dialogue = text_en.replace('\\', '')
if line_type == "Line_Normal":
dialogue += "\\" dialogue += "\\"
elif token[-1] == "Line_WaitForInput": elif line_type == "Line_WaitForInput":
dialogue += "@" dialogue += "@"
elif token[-1] == "Line_ContinueAfterTyping": elif line_type == "Line_ContinueAfterTyping":
pass pass
else: else:
raise Exception("Unhandled output termination") raise Exception("Unhandled output termination")
structure.append(["OutputLine", dialogue]) structure.append([
"OutputLine",
dialogue,
text_jp,
line_type
])
elif ( elif (
token[0] == "OutputLineAll" function_name == "OutputLineAll"
and "Line_ContinueAfterTyping" == token[-1] and "Line_ContinueAfterTyping" == token[-1]
and "\\n" in token[2] and "\\n" in token[2]
): ):
count = token[2].count("\\n") pass
#count = token[2].count("\\n")
# structure.append(["LineBreak", count]) # structure.append(["LineBreak", count])
# for coso in structure:
# if coso[0] == "OutputLine":
# print(coso[1], end="")
# elif coso[0] == "LineBreak":
# print(":".join(["br"] * coso[1]))
return structure return structure