forked from 46OkuMen/appareden
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
252 lines (207 loc) · 7.46 KB
/
utils.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
"""
Minor string utilities for Appareden.
"""
import re
from .rominfo import S_CONTROL_CODES
WAITS = ['[WAIT%s]' % n for n in range(1, 7)]
NAMES = ['Gento', 'Benimaru', 'Haley', 'Tamamo', 'Okitsugu', 'Masamune', 'Flame Dragon',
'Ice Dragon', 'Thunder Dragon', 'Shir[o]', 'Hanz[o]', 'Genpaku',
'Izunokami', 'Gennai', 'Ginpei', 'King Shikai', 'O-Toki', 'Benkei']
def effective_length(s):
"""The length of a string, ignoring the control codes."""
# TODO: Rough so far. Only removes stuff in brackets.
#pattern = rb'\[.*?\]'
#print (s, re.sub(pattern, b'', s))
#return len(re.sub(pattern, b'', s))
original = s
s = s.replace(b'[LN]', b' ')
for w in WAITS:
s = s.replace(bytes(w, encoding='ascii'), b'')
for cc in (b'[o]', b'[O]', b'[u]', b'[U]'):
# Overlined vowels are just one character, not [3]
s = s.replace(cc, b'x')
#print(original, s)
return len(s)
def typeset(s, width=37):
s_safe = s.replace('\u014d', '[o]').replace('\u016b', '[u]')
#print(s_safe)
# SJIS lines, like Haley's, must be split by SJIS spaces
prefix = ''
#print(s_safe)
#print(s_safe.split('[LN]'))
for n in NAMES:
if s_safe.split('[LN]')[0] == n:
if s_safe.split('[LN]')[1] != '':
prefix = n
s_safe = s_safe.lstrip(n)
#print("Prefix")
sjis = s_safe.encode('shift-jis')
#print(sjis.split(b'[LN]'))
if effective_length(sjis) <= width:
# Indent stuff
#s = s.replace('[LN]', '[LN] ')
return s
if b'\x82' in sjis:
space = b'\x81\x40'
width = 40
else:
space = b' '
# LNs are breaks too. Let's replace them with spaces and see what happens
#sjis = sjis.replace(b'[LN]', space)
manual_lines = sjis.split(b'[LN]')
for i, manual_line in enumerate(manual_lines):
words = manual_line.split(space)
lines = []
#print(words)
while words:
#print(words)
#if len(lines) > 0:
# line = b' '
#else:
# Indent non-initial lines (unless they start with a quote)
# Example: *clears throat*/n"The Dragon Gems...
# TODO: Still not working for throat-clearing lines.
#if len(lines) > 0 and not words[0].startswith(b'"'):
if not words[0].startswith(b'"'):
# Yes, it's a space.
line = b' '
else:
line = b''
while effective_length(line) <= width and words:
if effective_length(line + words[0] + space) > width:
break
line += words.pop(0) + space
line = line.rstrip()
# Remove initial spaces from first line.
# Not sure if a good idea?
if len(lines) == 0:
line = line.lstrip()
if len(lines) > 0:
if line == lines[-1]:
print("That line is the same as the last one. Continuing onward")
break
lines.append(line)
lines = [l.decode('shift-jis') for l in lines]
lines = [l.replace('[o]', '\u014d') for l in lines]
lines = [l.replace('[u]', '\u016b') for l in lines]
# Re-join the lines that weren't manually broken
manual_lines[i] = '[LN]'.join(lines)
# Join the segments of the string that were manually broken
return prefix + '[LN] '.join(manual_lines)
def sjis_punctuate(s):
s_safe = s.replace('\u014d', '[o]').replace('\u016b', '[u]')
sjis = s_safe.encode('shift-jis')
if b'\x82' not in sjis:
return s
#print(s)
sjis = sjis.replace(b' ', b'\x81\x40')
#s = s.replace(b'"', b'\x81\x56')
s_safe = sjis.decode('shift-jis')
s = s_safe.replace('[o]', '\u014d')
return s
def shadoff_compress(s):
#print(s)
# Definitely don't compress filenames!
if b'.GEM' in s:
return s
# If it's all spaces, keep it the same
if s.count(b' ') == len(s):
return s
# If it's a fullwidth Latin char SJIS string, keep it the same
if b'\x82' in s:
return s
# Don't further compress the dictionary
if b'\xff' in s:
return s
s = s.decode('shift-jis')
compressed = ''
chars = list(s)
# TODO: Remove the continuous-spaces processing
continuous_spaces = 0
first_number = True
while chars:
c = chars.pop(0)
if c == ' ':
first_number = True
continuous_spaces += 1
if not chars:
compressed += c
elif c.isupper():
#if continuous_spaces > 2:
# compressed += '_' + chr(continuous_spaces)
first_number = True
if continuous_spaces > 0:
compressed += ' '*(continuous_spaces)
continuous_spaces = 0
compressed += '^'
compressed += c
elif c.isdigit():
if first_number:
if len(compressed) > 0:
if compressed[-1] != '&' and compressed[-1] != '}' and compressed[-1] != '0':
compressed += ' '
compressed += c
first_number = False
else:
compressed += c
else:
compressed += c
else:
compressed += c
else:
first_number = True
if continuous_spaces > 0:
compressed += ' '*(continuous_spaces-1)
c = c.upper()
continuous_spaces = 0
compressed += c
#print(bytes(compressed, encoding='shift-jis'))
return bytes(compressed, encoding='shift-jis')
def replace_control_codes(s):
s = s.decode('shift-jis')
cursor = 0
while cursor < len(s):
c = s[cursor]
if c == 'n':
if s[cursor-1] != '>':
s = s[:cursor] + S_CONTROL_CODES['n'] + s[cursor+1:]
if c == 'w':
# TODO: Why am I replacing the >w control codes here as well? Let's try not doing that
#if s[cursor-1] != '>':
s = s[:cursor] + S_CONTROL_CODES['w'] + s[cursor+1:]
if c == 'c':
if s[cursor-1] != '>':
s = s[:cursor] + S_CONTROL_CODES['c'] + s[cursor+1:]
cursor += 1
s = s.encode('shift-jis')
return s
# Deprecated, no longer necessary
#def properly_space_waits(s):
"""
Every [WAIT*] control code interferes with the spacing a bit.
(More accurately, Shadoff compression interferes with their spacing)
Need to add (n-1) spaces after or before every WAIT,
where n = the number of lowercase words that preceded it on the same line.
"""
# No longer necessary after removing Shadoff compression
# return s
"""
result = ''
wait_segments = s.split('[WAIT')
if len(wait_segments) <= 1:
return s
else:
for i, w in enumerate(wait_segments):
if i == len(wait_segments)-1:
result += w
break
ln_segments = w.split('[LN]')
for j, l in enumerate(ln_segments):
if j == len(ln_segments)-1:
words = l.split()
lowercase_count = len([word for word in words if word[0].islower()]) + 1
result += l + ' '*(lowercase_count) + '[WAIT'
else:
result += l + '[LN]'
return result
"""