-
Notifications
You must be signed in to change notification settings - Fork 0
/
avg_parser.py
191 lines (159 loc) · 6.33 KB
/
avg_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import keyword
import re
import patch
import tool.bgm_path_resolver as bgmr
from profiles import IMG # !!!Run profiles_gen.py once to generate profiles.py
import ast_rpy as ast
def is_codename(name): # TODO
if len(name) == 0:
return False
if name[0] != '_' or re.match('', name[0]) is None:
return False
if name in ['+', '-', '*', '/', '\\']:
return False
codename_map = str.maketrans(m := r'\\ ~#%&*.{}/:;()<>?|"\-\[\]\'', '_' * len(m))
def to_codename(origin_name: str):
if origin_name.isspace():
origin_name = f'SPACE_{origin_name.count(" ")}'
name = origin_name.translate(codename_map)
if keyword.iskeyword(name) or name in dir(__builtins__):
name = f'{name}_{str(hash(origin_name)).replace("-", "_")}'
return name
debug_sound_fx = []
debug_bgm = []
debug_names = []
debug_chars = []
def s_apd(value, list_: list):
"""
Append value to list when not exist
:param value:
:param list_:
:return:
"""
if value not in list_:
list_.append(value)
def parser(source, label=None):
lines = source.splitlines()
if '' in lines:
lines.remove('')
name_cname = {}
cname_name = {}
speaker_status = {}
ast_map = [ast.Statement(['stop', 'sound'])]
ast_top = []
for line in lines:
speakers, tags = parse_tags(ast_top, ast_map, cname_name, name_cname, line)
text = line[line.find(':') + 1:]
# TODO Character Portrait
# Convert color tag
for r in re.finditer('<color=#(\\S+?)>', text := text.replace('</color>', '{/color}')):
text = text.replace(r.group(), f'{{color=#{r.groups()[0]}}}')
if len(speakers) > 1:
combined_spk = '_'.join(speakers)
combined_spk_display = ' & '.join([cname_name[x] for x in speakers])
ast_top.append(ast.Assign(combined_spk, 'define', ast.Func('Character', combined_spk_display)))
s_apd(combined_spk, debug_names)
else:
combined_spk = None
# Text
for text_unit in text.split('+'):
if len(speakers) == 1:
ast_map.append(ast.Text(text_unit, character=speakers[0]))
elif len(speakers) > 1:
ast_map.append(ast.Text(text_unit, character=combined_spk))
else:
ast_map.append(ast.Text(text_unit))
ast_top.append(ast.Block('label', label, ast_map))
result = ast.ast2rpy(ast_top)
return result
# NPC-Kalin(1)<Speaker>格琳</Speaker>||:“选择我们,加入我们!格里芬私人军事承包商,更新世界的锋芒!”+没错,从今天开始,您就是格里芬旗下的战术指挥官啦!
def parse_tags(ast_top, ast_map, cname_name, name_cname, line):
head = line[:line.find(':')]
head2 = re.sub('<(\\S+?)>(.+?)</\\1>', '', head)
# Parse All tag by regex match
full_tags = re.finditer('<(\\S+?)>(.+?)</\\1>', head)
half_tags = re.finditer('<(\\S+?)>', head2)
tags = [{'key': x.groups()[0], 'value': x.groups()[1], 'close': True} for x in full_tags]
tags.extend([{'key': x.groups()[0], 'value': None, 'close': False} for x in half_tags])
speakers = []
for tag in tags:
match tag['key']:
case 'BGM':
path = bgmr.get_bgm(tag['value'])
ast_map.append(ast.Statement(['play', 'music'], ast.Str(path)))
case 'SE1' | 'SE2':
sound_fx = tag['value']
s_apd(sound_fx, debug_sound_fx)
path = f'audio/{sound_fx}.wav'
ast_map.append(ast.Statement(['play', 'sound'], ast.Str(path)))
case 'BIN':
bg_result = tag['value']
bg_img = IMG[int(bg_result.strip(' '))]
bg_code_name = f'i_{to_codename(bg_img)}'
ast_map.append(ast.Assign(f'i_{to_codename(bg_img)}', 'image', f'images/{bg_img}.png'))
ast_map.append(ast.Statement(['scene'], bg_code_name))
case 'Speaker':
name = tag['value']
if name is None or name.isspace():
break
code_name = to_codename(name)
if name not in name_cname.keys():
name_cname[name] = code_name
cname_name[code_name] = name
s_apd(name, debug_names)
ast_top.append(ast.Assign(code_name, 'define', ast.Func('Character', name)))
speakers.append(code_name)
# TODO
case '通讯框':
pass
case 'Grey':
pass
case 'Shake':
pass
case 'Position':
pass
case '回忆':
pass
case '关闭蒙版':
pass
case '名单' | '名单2':
pass
return speakers, tags
if __name__ == '__main__':
# import time
# t = time.time()
mp = ast.Block('label', 'start', calls := [])
debug = True
def generate_avg_level(chapter, level, part):
file_name = f'{chapter}-{level}-{part}'
with open(f'avgtxt_main/{file_name}.bytes', 'r', encoding='utf-8') as file:
s = file.read()
label_name = 's' + file_name.replace('-', '_')
avg_rpy_text = parser(s, label_name)
with open(f'rpy/{label_name}.rpy', 'w', encoding='utf-8') as file:
file.write(avg_rpy_text)
print(avg_rpy_text)
calls.append(ast.Statement('call', label_name))
# Chapter 1~12, level 1~6, part 1~2
chapter_, level_, part_ = 1, 1, 1
while True:
generate_avg_level(chapter_, level_, part_)
if (part_ := part_ + 1) > 2:
part_ = 1
if (level_ := level_ + 1) > 6:
level_ = 1
if (chapter_ := chapter_ + 1) > 12:
break
with open('rpy/script.rpy', 'w') as f:
f.write(ast.ast2rpy(mp))
if debug:
# Write debug info
with open('debug/bgm.txt', 'w') as f:
f.write('\n'.join(debug_bgm))
with open('debug/sound_fx.txt', 'w') as f:
f.write('\n'.join(debug_sound_fx))
with open('debug/names.txt', 'w', encoding='utf-8') as f:
f.write('\n'.join(debug_names))
with open('debug/chars.txt', 'w', encoding='utf-8') as f:
f.write('\n'.join(debug_chars))
# print(time.time() - t)