2023-10-09 22:09:22 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import os
|
|
|
|
import re
|
2023-10-27 00:00:02 -04:00
|
|
|
import sys
|
2023-10-28 14:31:30 -04:00
|
|
|
import json
|
2023-11-04 00:15:01 -04:00
|
|
|
from os.path import exists
|
2023-10-28 14:31:30 -04:00
|
|
|
|
2023-11-02 23:56:36 -04:00
|
|
|
hl_gameui_whitelist = {
|
|
|
|
"GAMEUI_GAMEMENU_RESUMEGAME",
|
|
|
|
"GAMEUI_SAVEGAME",
|
|
|
|
"GAMEUI_LOADGAME",
|
|
|
|
"GAMEUI_NEWGAME",
|
|
|
|
"GAMEUI_OPTIONS",
|
|
|
|
"GAMEUI_GAMEMENU_QUIT",
|
2023-11-04 00:15:01 -04:00
|
|
|
|
|
|
|
"GAMEUI_SOUNDEFFECTVOLUME",
|
|
|
|
"GAMEUI_MUSICVOLUME",
|
|
|
|
"GAMEUI_SUBTITLESANDSOUNDEFFECTS",
|
|
|
|
}
|
|
|
|
|
|
|
|
language_translations = {
|
|
|
|
'brazilian': 'Brasileiro',
|
|
|
|
'bulgarian': 'Български език',
|
|
|
|
'czech': 'Čeština',
|
|
|
|
'danish': 'Dansk',
|
|
|
|
'german': 'Deutsch',
|
|
|
|
'english': 'English',
|
|
|
|
'spanish': 'Español',
|
|
|
|
'greek': 'Ελληνικά',
|
|
|
|
'french': 'Français',
|
|
|
|
'italian': 'Italiano',
|
|
|
|
'polish': 'Język polski',
|
|
|
|
'latam': 'Latam',
|
|
|
|
'hungarian': 'Magyar nyelv',
|
|
|
|
'dutch': 'Nederlands',
|
|
|
|
'norwegian': 'Norsk',
|
|
|
|
'portuguese': 'Português',
|
|
|
|
'russian': 'Русский язык',
|
|
|
|
'romanian': 'Românește',
|
|
|
|
'finnish': 'Suomi',
|
|
|
|
'swedish': 'Svenska',
|
|
|
|
'turkish': 'Türkçe',
|
|
|
|
'ukrainian': 'Українська мова',
|
2023-11-02 23:56:36 -04:00
|
|
|
}
|
|
|
|
|
2023-10-28 14:31:30 -04:00
|
|
|
def get_supported_characters():
|
|
|
|
with open('assets/fonts/dejavu_sans_book_8.json', 'r') as f:
|
|
|
|
content = json.loads('\n'.join(f.readlines()))
|
|
|
|
|
|
|
|
result = {' ', '\t', '\n', '\r'}
|
|
|
|
|
|
|
|
for symbol in content['symbols']:
|
|
|
|
result.update(chr(symbol['id']))
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
2023-10-27 00:00:02 -04:00
|
|
|
|
|
|
|
def dump_lines(sourcefile_path, lines):
|
|
|
|
if not os.path.exists(os.path.dirname(os.path.abspath(sourcefile_path))):
|
|
|
|
os.makedirs(os.path.dirname(os.path.abspath(sourcefile_path)))
|
|
|
|
|
|
|
|
with open(sourcefile_path, "w") as f:
|
|
|
|
f.writelines(lines)
|
2023-10-09 22:09:22 -04:00
|
|
|
|
|
|
|
def get_caption_keys_values_language(lines):
|
|
|
|
language = "English"
|
|
|
|
keys = []
|
|
|
|
values = []
|
|
|
|
|
|
|
|
found_language = False
|
|
|
|
found_tokens = False
|
|
|
|
for line in lines:
|
|
|
|
if not found_language:
|
|
|
|
if not '"Language"' in line:
|
|
|
|
continue
|
|
|
|
found_language = True
|
|
|
|
line = line.replace('"', "")
|
|
|
|
key, val = line.split()
|
|
|
|
language = val
|
|
|
|
continue
|
|
|
|
if not found_tokens:
|
|
|
|
if not '"Tokens"' in line:
|
|
|
|
continue
|
|
|
|
found_tokens = True
|
|
|
|
continue
|
|
|
|
if ("{" in line) or ("}" in line) :
|
|
|
|
continue
|
2023-11-02 23:56:36 -04:00
|
|
|
keyval= re.split('"\t+"', line)
|
2023-10-09 22:09:22 -04:00
|
|
|
if len(keyval) != 2:
|
|
|
|
keyval= line.split('" "')
|
|
|
|
if len(keyval) != 2:
|
|
|
|
continue
|
2023-10-28 21:43:47 -04:00
|
|
|
if "[english]" in keyval[0] or 'commentary' in keyval[0]:
|
2023-10-09 22:09:22 -04:00
|
|
|
continue
|
|
|
|
key = keyval[0].replace('"', "").replace(".", "_").replace("-", "_").replace('\\', "_").replace('#', "").upper()
|
|
|
|
val = keyval[1].replace('"', "").replace("\n", "").replace("\\", "")
|
|
|
|
val = re.sub(r'\<clr.+\>','',val)
|
|
|
|
val = re.sub(r'\<norepeat.+\>','',val)
|
|
|
|
val = val.replace("<sfx>", "")
|
|
|
|
val = re.sub(r'\<len:.+\>','',val)
|
|
|
|
val = val.replace("<len>", "")
|
|
|
|
keys.append(key)
|
2023-10-28 21:24:21 -04:00
|
|
|
values.append(val)
|
2023-10-09 22:09:22 -04:00
|
|
|
|
|
|
|
return keys, values, language
|
|
|
|
|
2023-10-28 21:43:47 -04:00
|
|
|
def make_overall_subtitles_header(all_header_lines, languages_list, message_count, max_message_length):
|
2023-10-09 22:09:22 -04:00
|
|
|
header_lines = []
|
|
|
|
header_lines.append("#ifndef __SUBTITLES_H__\n")
|
|
|
|
header_lines.append("#define __SUBTITLES_H__\n")
|
|
|
|
header_lines.append("\n")
|
|
|
|
header_lines.append(f"#define NUM_SUBTITLE_LANGUAGES {len(languages_list)}\n")
|
2023-10-28 21:43:47 -04:00
|
|
|
header_lines.append(f"#define NUM_SUBTITLE_MESSAGES {message_count + 1}\n")
|
|
|
|
header_lines.append(f"#define MAX_SUBTITLE_LENGTH {max_message_length}\n")
|
2023-10-27 00:00:02 -04:00
|
|
|
header_lines.append("\n")
|
|
|
|
header_lines.append("struct SubtitleBlock {\n")
|
|
|
|
header_lines.append(" char* romStart;\n")
|
|
|
|
header_lines.append(" char* romEnd;\n")
|
|
|
|
header_lines.append(" char** values;\n")
|
|
|
|
header_lines.append("};\n")
|
2023-10-09 22:09:22 -04:00
|
|
|
header_lines.append("\n")
|
|
|
|
header_lines.append("extern char* SubtitleLanguages[];\n")
|
2023-10-27 00:00:02 -04:00
|
|
|
header_lines.append("extern struct SubtitleBlock SubtitleLanguageBlocks[];\n")
|
2023-10-09 22:09:22 -04:00
|
|
|
header_lines.append("\n")
|
2023-11-04 00:15:01 -04:00
|
|
|
|
|
|
|
for idx, language in enumerate(languages_list):
|
|
|
|
header_lines.append(f"#define LANGUAGE_{language.upper()} {idx}\n")
|
|
|
|
|
|
|
|
header_lines.append("\n")
|
|
|
|
|
|
|
|
|
2023-10-09 22:09:22 -04:00
|
|
|
if len(languages_list) > 0:
|
|
|
|
header_lines.extend(all_header_lines)
|
|
|
|
else:
|
|
|
|
header_lines.append(f"enum SubtitleKey\n")
|
|
|
|
header_lines.append("{\n")
|
|
|
|
header_lines.append(' SubtitleKeyNone,\n')
|
|
|
|
header_lines.append("};\n")
|
|
|
|
header_lines.append("\n")
|
|
|
|
|
|
|
|
header_lines.append("#endif")
|
|
|
|
|
2023-10-27 00:00:02 -04:00
|
|
|
dump_lines("build/src/audio/subtitles.h", header_lines)
|
2023-10-09 22:09:22 -04:00
|
|
|
|
|
|
|
def make_SubtitleKey_headerlines(keys):
|
|
|
|
header_lines = []
|
|
|
|
header_lines.append("\n")
|
|
|
|
header_lines.append(f"enum SubtitleKey\n")
|
|
|
|
header_lines.append("{\n")
|
|
|
|
header_lines.append(' SubtitleKeyNone,\n')
|
|
|
|
for key in keys:
|
|
|
|
header_lines.append(f' {key},\n')
|
|
|
|
header_lines.append("};\n")
|
|
|
|
header_lines.append("\n")
|
|
|
|
return header_lines
|
|
|
|
|
2023-11-02 23:56:36 -04:00
|
|
|
def make_subtitle_for_language(lang_lines, lang_name, keys):
|
2023-10-27 00:00:02 -04:00
|
|
|
lines = []
|
|
|
|
|
2023-10-28 21:43:47 -04:00
|
|
|
lines.append('#include "subtitles.h"')
|
|
|
|
lines.append("\n")
|
2023-10-27 00:32:18 -04:00
|
|
|
lines.append("\n")
|
|
|
|
|
2023-11-04 00:15:01 -04:00
|
|
|
for idx, value in enumerate(lang_lines):
|
2023-11-02 23:56:36 -04:00
|
|
|
lines.append(f'char __translation_{lang_name}_{keys[idx]}[] = "{value}";\n')
|
2023-10-27 00:32:18 -04:00
|
|
|
|
2023-10-27 00:00:02 -04:00
|
|
|
lines.append("\n")
|
2023-10-28 21:43:47 -04:00
|
|
|
lines.append(f"char* gSubtitle{lang_name}[NUM_SUBTITLE_MESSAGES] = {'{'}\n")
|
2023-10-27 00:00:02 -04:00
|
|
|
|
2023-10-27 00:32:18 -04:00
|
|
|
# SubtitleKeyNone
|
|
|
|
lines.append(' "",\n')
|
|
|
|
|
2023-11-04 00:15:01 -04:00
|
|
|
for idx, value in enumerate(lang_lines):
|
2023-11-02 23:56:36 -04:00
|
|
|
lines.append(f' __translation_{lang_name}_{keys[idx]},\n')
|
2023-10-27 00:00:02 -04:00
|
|
|
|
|
|
|
lines.append("};\n")
|
|
|
|
|
|
|
|
dump_lines(f"build/src/audio/subtitles_{lang_name}.c", lines)
|
2023-10-09 22:09:22 -04:00
|
|
|
|
2023-10-28 14:31:30 -04:00
|
|
|
def determine_invalid_characters(lang_name, lang_lines, good_characters):
|
|
|
|
used_characters = set()
|
|
|
|
|
|
|
|
for value in lang_lines:
|
|
|
|
used_characters = used_characters | set(value)
|
|
|
|
|
|
|
|
invalid = used_characters - good_characters
|
|
|
|
|
|
|
|
if len(invalid) == 0:
|
2023-10-28 21:24:21 -04:00
|
|
|
return used_characters
|
2023-10-28 14:31:30 -04:00
|
|
|
|
|
|
|
print(f"{lang_name} has {len(invalid)} invalid charcters\n{''.join(sorted(list(invalid)))}")
|
|
|
|
|
|
|
|
return used_characters
|
|
|
|
|
2023-10-27 00:00:02 -04:00
|
|
|
|
|
|
|
def make_subtitle_ld(languages):
|
|
|
|
lines = []
|
|
|
|
|
|
|
|
for language in languages:
|
|
|
|
language_name = language['name']
|
|
|
|
|
2023-10-27 22:10:35 -04:00
|
|
|
lines.append(f" __romPos = (__romPos + 15) & ~0xF;\n")
|
2023-10-27 00:00:02 -04:00
|
|
|
lines.append(f" BEGIN_SEG(subtitles_{language_name}, 0x04000000)\n")
|
|
|
|
lines.append(" {\n")
|
|
|
|
lines.append(f" build/src/audio/subtitles_{language_name}.o(.data);\n")
|
|
|
|
lines.append(f" build/src/audio/subtitles_{language_name}.o(.bss);\n")
|
|
|
|
lines.append(" }\n")
|
|
|
|
lines.append(f" END_SEG(subtitles_{language_name})\n")
|
|
|
|
lines.append("\n")
|
|
|
|
|
|
|
|
dump_lines('build/subtitles.ld', lines)
|
|
|
|
|
|
|
|
def make_overall_subtitles_sourcefile(language_list):
|
2023-10-09 22:09:22 -04:00
|
|
|
sourcefile_lines = []
|
|
|
|
sourcefile_lines.append('#include "subtitles.h"\n')
|
|
|
|
|
|
|
|
sourcefile_lines.append("\n")
|
|
|
|
sourcefile_lines.append("char* SubtitleLanguages[] =\n")
|
|
|
|
sourcefile_lines.append("{\n")
|
|
|
|
if len(language_list) <= 0:
|
|
|
|
sourcefile_lines.append(f' "",\n')
|
|
|
|
else:
|
|
|
|
for language in language_list:
|
2023-11-04 00:15:01 -04:00
|
|
|
sourcefile_lines.append(f' "{language_translations[language]}",\n')
|
2023-10-09 22:09:22 -04:00
|
|
|
sourcefile_lines.append("};\n")
|
|
|
|
sourcefile_lines.append("\n")
|
|
|
|
|
2023-10-27 00:00:02 -04:00
|
|
|
for language in language_list:
|
|
|
|
sourcefile_lines.append(f"extern char _subtitles_{language}SegmentRomStart[];\n")
|
|
|
|
sourcefile_lines.append(f"extern char _subtitles_{language}SegmentRomEnd[];\n")
|
2023-10-28 21:43:47 -04:00
|
|
|
sourcefile_lines.append(f"extern char* gSubtitle{language}[NUM_SUBTITLE_MESSAGES];\n")
|
2023-10-27 00:00:02 -04:00
|
|
|
sourcefile_lines.append("\n")
|
2023-10-09 22:09:22 -04:00
|
|
|
|
2023-10-27 00:00:02 -04:00
|
|
|
sourcefile_lines.append("struct SubtitleBlock SubtitleLanguageBlocks[] = {\n")
|
|
|
|
|
|
|
|
for language in language_list:
|
|
|
|
sourcefile_lines.append(" {\n")
|
|
|
|
sourcefile_lines.append(f" _subtitles_{language}SegmentRomStart,\n")
|
|
|
|
sourcefile_lines.append(f" _subtitles_{language}SegmentRomEnd,\n")
|
|
|
|
sourcefile_lines.append(f" gSubtitle{language},\n")
|
|
|
|
sourcefile_lines.append(" },\n")
|
|
|
|
|
|
|
|
sourcefile_lines.append("};\n")
|
|
|
|
sourcefile_lines.append("\n")
|
|
|
|
|
|
|
|
dump_lines("build/src/audio/subtitles.c", sourcefile_lines)
|
|
|
|
|
2023-11-02 23:56:36 -04:00
|
|
|
def read_translation_file(filepath):
|
2023-11-04 00:15:01 -04:00
|
|
|
if not exists(filepath):
|
|
|
|
return [], [], ''
|
|
|
|
|
2023-11-02 23:56:36 -04:00
|
|
|
lines = []
|
|
|
|
|
|
|
|
with open(filepath, "r", encoding='utf-16-le') as f:
|
|
|
|
lines = f.readlines()
|
|
|
|
|
|
|
|
new_lines = []
|
|
|
|
for line in lines:
|
|
|
|
line = line.replace("\x00", "")
|
|
|
|
if "\n" != line:
|
|
|
|
new_lines.append(line)
|
|
|
|
|
|
|
|
return get_caption_keys_values_language(new_lines)
|
|
|
|
|
|
|
|
def filter_whitelist(keys, values, whitelist):
|
|
|
|
result_keys = []
|
|
|
|
result = []
|
|
|
|
|
|
|
|
for index in range(len(keys)):
|
|
|
|
if keys[index] in whitelist:
|
|
|
|
result_keys.append(keys[index])
|
|
|
|
result.append(values[index])
|
|
|
|
|
|
|
|
return result_keys, result
|
|
|
|
|
2023-10-27 00:00:02 -04:00
|
|
|
def process_all_closecaption_files(dir, language_names):
|
2023-10-09 22:09:22 -04:00
|
|
|
values_list = []
|
|
|
|
header_lines = []
|
|
|
|
language_list = []
|
2023-10-27 00:00:02 -04:00
|
|
|
language_with_values_list = []
|
2023-11-04 00:15:01 -04:00
|
|
|
key_order = None
|
|
|
|
default_values = None
|
2023-10-27 00:00:02 -04:00
|
|
|
|
|
|
|
for langauge_name in language_names:
|
|
|
|
filename = f"closecaption_{langauge_name}.txt"
|
|
|
|
|
2023-10-09 22:09:22 -04:00
|
|
|
try:
|
|
|
|
filepath = os.path.join(dir, filename)
|
|
|
|
|
2023-11-02 23:56:36 -04:00
|
|
|
k,v,l = read_translation_file(filepath)
|
|
|
|
|
|
|
|
gamepad_k, gamepad_v, _ = read_translation_file(f"vpk/Portal/hl2/resource/gameui_{langauge_name}.txt")
|
|
|
|
|
|
|
|
gamepad_k, gamepad_v = filter_whitelist(gamepad_k, gamepad_v, hl_gameui_whitelist)
|
|
|
|
|
2023-11-04 00:15:01 -04:00
|
|
|
extra_k, extra_v, _ = read_translation_file(f"assets/translations/extra_{langauge_name}.txt")
|
2023-11-02 23:56:36 -04:00
|
|
|
|
2023-11-04 00:15:01 -04:00
|
|
|
k = k + gamepad_k + extra_k
|
|
|
|
v = v + gamepad_v + extra_v
|
|
|
|
|
|
|
|
if not key_order:
|
2023-10-09 22:09:22 -04:00
|
|
|
header_lines = make_SubtitleKey_headerlines(k)
|
2023-11-04 00:15:01 -04:00
|
|
|
key_order = k
|
|
|
|
default_values = v
|
|
|
|
else:
|
|
|
|
index_mapping = {}
|
|
|
|
for idx, x in enumerate(k):
|
|
|
|
index_mapping[x] = idx
|
|
|
|
|
|
|
|
new_values = []
|
|
|
|
|
|
|
|
for idx, key in enumerate(key_order):
|
|
|
|
if key in index_mapping:
|
|
|
|
new_values.append(v[index_mapping[key]])
|
|
|
|
else:
|
|
|
|
new_values.append(default_values[idx])
|
|
|
|
|
|
|
|
v = new_values
|
|
|
|
|
|
|
|
values_list.append(v)
|
2023-10-09 22:09:22 -04:00
|
|
|
language_list.append(l)
|
2023-10-27 00:00:02 -04:00
|
|
|
|
|
|
|
language_with_values_list.append({
|
|
|
|
'value': v,
|
|
|
|
'name': l,
|
|
|
|
})
|
2023-10-09 22:09:22 -04:00
|
|
|
print(filename, " - PASSED")
|
2023-10-28 14:31:30 -04:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2023-10-09 22:09:22 -04:00
|
|
|
print(filename, " - FAILED")
|
2023-11-04 00:15:01 -04:00
|
|
|
raise
|
2023-10-09 22:09:22 -04:00
|
|
|
continue
|
2023-10-27 00:00:02 -04:00
|
|
|
|
2023-10-28 14:31:30 -04:00
|
|
|
good_characters = get_supported_characters()
|
|
|
|
used_characters = set()
|
|
|
|
|
2023-10-28 21:43:47 -04:00
|
|
|
max_message_length = 0
|
|
|
|
|
2023-10-27 00:00:02 -04:00
|
|
|
for language in language_with_values_list:
|
2023-11-04 00:15:01 -04:00
|
|
|
make_subtitle_for_language(language['value'], language['name'], key_order)
|
2023-10-28 14:31:30 -04:00
|
|
|
used_characters = used_characters | determine_invalid_characters(language['name'], language['value'], good_characters)
|
|
|
|
|
2023-10-28 21:43:47 -04:00
|
|
|
for value in language['value']:
|
|
|
|
max_message_length = max(max_message_length, len(value))
|
|
|
|
|
2023-10-28 14:31:30 -04:00
|
|
|
print(f"needed characters\n{''.join(sorted(list(good_characters & used_characters)))}")
|
|
|
|
print(f"unused characters\n{''.join(sorted(list(good_characters - used_characters)))}")
|
|
|
|
print(f"invalid characters\n{''.join(sorted(list(used_characters - good_characters)))}")
|
2023-10-28 21:43:47 -04:00
|
|
|
print(f"max message length\n{max_message_length}")
|
2023-10-28 14:31:30 -04:00
|
|
|
|
2023-10-27 00:00:02 -04:00
|
|
|
make_subtitle_ld(language_with_values_list)
|
|
|
|
|
2023-10-28 21:43:47 -04:00
|
|
|
make_overall_subtitles_header(header_lines, language_list, len(language_with_values_list[0]['value']), max_message_length)
|
2023-10-27 00:00:02 -04:00
|
|
|
make_overall_subtitles_sourcefile(language_list)
|
2023-10-09 22:09:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2023-10-27 00:00:02 -04:00
|
|
|
process_all_closecaption_files("vpk/Portal/portal/resource", sys.argv[1:])
|