forked from OoTRandomizer/OoT-Randomizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLanguage.py
More file actions
569 lines (468 loc) · 17.1 KB
/
Language.py
File metadata and controls
569 lines (468 loc) · 17.1 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
"""
text rule:
[] >> replace using internal dict phrase / expression
{} >> replace using external phrase
"""
from __future__ import annotations
import ast
import json
import os
import re
import unicodedata
from functools import reduce
from typing import TypedDict
from Utils import lang_path
class ItemMessage(TypedDict):
id: int
text: str
def half_to_full_width(s: str) -> str:
out = []
for ch in s:
code = ord(ch)
if code == 0x20:
out.append('\u3000')
elif 0x30 <= code <= 0x39 or 0x41 <= code <= 0x5A or 0x61 <= code <= 0x7A:
out.append(chr(code + 0xFEE0))
elif 0xFF61 <= code <= 0xFF9F:
out.append(unicodedata.normalize('NFKC', ch))
else:
out.append(ch)
return ''.join(out)
_TEMP_LITERAL_NAME_RE = re.compile(r'(None|True|False)\Z')
_METHOD_TOKEN_RE = re.compile(r'([A-Za-z_]\w*)\(\)\Z')
_NAME_RE = re.compile(r'[A-Za-z_]\w*\Z')
_ALLOWED_METHODS = {
"capitalize",
"lower",
"upper",
"title",
"swapcase",
"casefold",
"strip",
"lstrip",
"rstrip",
}
_ALLOWED_FUNCTIONS = {
"format",
}
class _SafeExprEvaluator(ast.NodeVisitor):
def __init__(self, env: dict):
self.env = env
def visit(self, node):
allowed = (
ast.Expression,
ast.Constant,
ast.Name,
ast.Attribute,
ast.Subscript,
ast.Call,
ast.List,
ast.Tuple,
ast.Dict,
ast.Set,
ast.Compare,
ast.BoolOp,
ast.UnaryOp,
ast.IfExp,
ast.Load,
ast.Slice,
ast.Eq,
ast.NotEq,
ast.In,
ast.NotIn,
ast.Is,
ast.IsNot,
ast.Lt,
ast.LtE,
ast.Gt,
ast.GtE,
ast.And,
ast.Or,
ast.Not,
)
if not isinstance(node, allowed):
raise ValueError(f"Unsupported expression node: {type(node).__name__}")
return super().visit(node)
def generic_visit(self, node):
raise ValueError(f"Unsupported expression: {type(node).__name__}")
def visit_Expression(self, node: ast.Expression):
return self.visit(node.body)
def visit_Constant(self, node: ast.Constant):
return node.value
def visit_Name(self, node: ast.Name):
if node.id in self.env:
return self.env[node.id]
raise NameError(f"Unknown name: {node.id}")
def visit_Attribute(self, node: ast.Attribute):
base = self.visit(node.value)
if isinstance(base, dict):
if node.attr in base:
return base[node.attr]
raise KeyError(
f"KeyError: '{node.attr}' not found while resolving attribute. "
f"Available keys: {list(base.keys())}"
)
return getattr(base, node.attr)
def visit_Subscript(self, node: ast.Subscript):
value = self.visit(node.value)
index = self.visit(node.slice)
if isinstance(value, dict) and index is None and "None" in value:
index = "None"
return value[index]
def visit_Slice(self, node: ast.Slice):
return slice(
self.visit(node.lower) if node.lower else None,
self.visit(node.upper) if node.upper else None,
self.visit(node.step) if node.step else None,
)
def visit_List(self, node: ast.List):
return [self.visit(e) for e in node.elts]
def visit_Tuple(self, node: ast.Tuple):
return tuple(self.visit(e) for e in node.elts)
def visit_Set(self, node: ast.Set):
return {self.visit(e) for e in node.elts}
def visit_Dict(self, node: ast.Dict):
return {
self.visit(k): self.visit(v)
for k, v in zip(node.keys, node.values)
}
def visit_BoolOp(self, node: ast.BoolOp):
if isinstance(node.op, ast.And):
result = True
for v in node.values:
result = self.visit(v)
if not result:
return result
return result
if isinstance(node.op, ast.Or):
result = False
for v in node.values:
result = self.visit(v)
if result:
return result
return result
raise ValueError("Unsupported boolean operator")
def visit_UnaryOp(self, node: ast.UnaryOp):
val = self.visit(node.operand)
if isinstance(node.op, ast.Not):
return not val
raise ValueError("Unsupported unary operator")
def visit_IfExp(self, node: ast.IfExp):
return self.visit(node.body) if self.visit(node.test) else self.visit(node.orelse)
def visit_Compare(self, node: ast.Compare):
left = self.visit(node.left)
for op, comp in zip(node.ops, node.comparators):
right = self.visit(comp)
if isinstance(op, ast.Eq):
ok = (left == right)
elif isinstance(op, ast.NotEq):
ok = (left != right)
elif isinstance(op, ast.In):
ok = (left in right)
elif isinstance(op, ast.NotIn):
ok = (left not in right)
elif isinstance(op, ast.Is):
ok = (left is right)
elif isinstance(op, ast.IsNot):
ok = (left is not right)
elif isinstance(op, ast.Lt):
ok = (left < right)
elif isinstance(op, ast.LtE):
ok = (left <= right)
elif isinstance(op, ast.Gt):
ok = (left > right)
elif isinstance(op, ast.GtE):
ok = (left >= right)
else:
raise ValueError(f"Unsupported compare operator: {type(op).__name__}")
if not ok:
return False
left = right
return True
def visit_Call(self, node: ast.Call):
if isinstance(node.func, ast.Attribute):
if node.args or node.keywords:
raise ValueError("Only zero-argument method calls are allowed")
obj = self.visit(node.func.value)
method_name = node.func.attr
if method_name not in _ALLOWED_METHODS:
raise ValueError(f"Method not allowed: {method_name}")
method = getattr(obj, method_name, None)
if method is None or not callable(method):
raise ValueError(f"Object has no callable method: {method_name}")
return method()
if isinstance(node.func, ast.Name):
func_name = node.func.id
if func_name not in _ALLOWED_FUNCTIONS:
raise ValueError(f"Function not allowed: {func_name}")
func = self.env.get(func_name)
if func is None or not callable(func):
raise ValueError(f"Unknown callable: {func_name}")
args = [self.visit(arg) for arg in node.args]
kwargs = {}
for kw in node.keywords:
if kw.arg is None:
raise ValueError("Keyword expansion is not allowed")
kwargs[kw.arg] = self.visit(kw.value)
return func(*args, **kwargs)
raise ValueError("Only method calls or allowed function calls are allowed")
class Language:
def __init__(self, lang: str):
with open(os.path.join(lang_path(lang), "property.json"), mode="r", encoding="utf-8") as f:
message = json.load(f)
self.__dict__.update(message)
self.base = self.lang_property["base"]
extensions = (".bin", ".ia4", ".zobj")
self.path = lang_path(lang)
self.data = {
fname: os.path.join(lang_path(lang), fname)
for fname in os.listdir(lang_path(lang))
if fname.lower().endswith(extensions)
}
def _dict_get(self, obj, key):
if isinstance(obj, (list, tuple)):
if isinstance(key, str):
key = int(key, 0)
return obj[key]
if isinstance(obj, dict):
return obj[key]
return getattr(obj, key)
def _to_output_string(self, value):
s = str(value)
if self.base == "jp" and type(value) is int:
s = half_to_full_width(s)
return s
def _to_expr_literal(self, value):
value = self._coerce_expr_value(value)
if self.base == "jp" and type(value) is int:
return repr(half_to_full_width(str(value)))
return repr(value)
def _coerce_expr_value(self, value):
if isinstance(value, (str, int, float, bool, type(None), list, tuple, dict, set)):
return value
return str(value)
def _format_expr_function(self, value, external=None):
if external is not None and not isinstance(external, dict):
raise ValueError("format(text, external_dict) requires dict or None as second argument")
try:
return self.format_from_text(str(value), external)
except (KeyError, NameError, ValueError, IndexError, AttributeError, SyntaxError) as e:
print("FORMAT FAIL")
print("VALUE =", repr(value))
print("EXTERNAL=", repr(external))
print("ERROR =", repr(e))
return str(value)
def _is_simple_external_key(self, key_text: str) -> bool:
key_text = key_text.strip()
return key_text.isdigit() or _NAME_RE.fullmatch(key_text) is not None
def _build_expr_env(self, external: dict | None):
env = {
k: v
for k, v in self.__dict__.items()
if not k.startswith("_") and not callable(v)
}
env["format"] = self._format_expr_function
if external:
env.update(external)
return env
def _safe_eval_expr(self, expr: str, external: dict | None):
tree = ast.parse(expr, mode="eval")
return _SafeExprEvaluator(self._build_expr_env(external)).visit(tree)
def _read_group(self, text: str, start: int, opener: str, closer: str):
depth = 1
i = start + 1
buff = []
while i < len(text):
ch = text[i]
if ch == "\\" and i + 1 < len(text):
buff.append(text[i:i + 2])
i += 2
continue
if ch == opener:
depth += 1
buff.append(ch)
i += 1
continue
if ch == closer:
depth -= 1
if depth == 0:
return "".join(buff), i + 1
buff.append(ch)
i += 1
continue
buff.append(ch)
i += 1
return None, start + 1
def _split_dot_tokens(self, text: str):
tokens = []
buff = []
brace_depth = 0
paren_depth = 0
quote = None
i = 0
while i < len(text):
ch = text[i]
if quote:
buff.append(ch)
if ch == "\\" and i + 1 < len(text):
buff.append(text[i + 1])
i += 2
continue
if ch == quote:
quote = None
i += 1
continue
if ch in ("'", '"'):
quote = ch
buff.append(ch)
i += 1
continue
if ch == "{":
brace_depth += 1
buff.append(ch)
i += 1
continue
if ch == "}":
brace_depth -= 1
buff.append(ch)
i += 1
continue
if ch == "(":
paren_depth += 1
buff.append(ch)
i += 1
continue
if ch == ")":
paren_depth -= 1
buff.append(ch)
i += 1
continue
if ch == "." and brace_depth == 0 and paren_depth == 0:
token = "".join(buff).strip()
if token:
tokens.append(token)
buff = []
i += 1
continue
buff.append(ch)
i += 1
token = "".join(buff).strip()
if token:
tokens.append(token)
return tokens
def _translate_path_ops_to_python(self, content: str):
tokens = self._split_dot_tokens(content.strip())
if not tokens:
raise ValueError("Empty path")
head = tokens[0]
if not _NAME_RE.fullmatch(head):
raise ValueError("Not a path-op expression")
expr = head
for token in tokens[1:]:
if token.startswith("{") and token.endswith("}"):
inner = token[1:-1].strip()
if inner.isdigit():
expr += f"[{int(inner)}]"
elif _NAME_RE.fullmatch(inner):
expr += f"[{inner}]"
else:
raise ValueError(f"Unsupported external key token: {token}")
continue
if _TEMP_LITERAL_NAME_RE.fullmatch(token):
expr += f"[{token}]"
continue
if token.isdigit():
expr += f"[{int(token)}]"
continue
m = _METHOD_TOKEN_RE.fullmatch(token)
if m:
method_name = m.group(1)
if method_name not in _ALLOWED_METHODS:
raise ValueError(f"Unsupported method: {method_name}")
expr += f".{method_name}()"
continue
if _NAME_RE.fullmatch(token):
expr += f".{token}"
continue
raise ValueError(f"Unsupported token in path-op expression: {token}")
return expr
def _replace_placeholders(self, text: str, external: dict | None, for_expr: bool):
out = []
i = 0
while i < len(text):
ch = text[i]
if ch == "\\" and i + 1 < len(text) and text[i + 1] in "[]{}":
out.append(text[i:i + 2])
i += 2
continue
if ch == "[":
content, nxt = self._read_group(text, i, "[", "]")
if content is None:
out.append(ch)
i += 1
continue
out.append(self._resolve_square(content, external, for_expr))
i = nxt
continue
if ch == "{":
if i != 0 and text[i - 1] == "$":
out.append(ch)
i += 1
continue
content, nxt = self._read_group(text, i, "{", "}")
if content is None:
out.append(ch)
i += 1
continue
if for_expr and not self._is_simple_external_key(content):
out.append("{")
out.append(content)
out.append("}")
i = nxt
continue
if for_expr and not self._is_simple_external_key(content):
out.append("{")
out.append(content)
out.append("}")
i = nxt
continue
out.append(self._resolve_external(content, external, for_expr))
i = nxt
continue
out.append(ch)
i += 1
return "".join(out)
def _resolve_external(self, key_text: str, external: dict | None, for_expr: bool):
key_text = key_text.strip()
real_key = int(key_text) if key_text.isdigit() else key_text
value = (external or {})[real_key]
return self._to_expr_literal(value) if for_expr else self._to_output_string(value)
def _resolve_square(self, content: str, external: dict | None, for_expr: bool):
content = content.strip()
try:
translated = self._translate_path_ops_to_python(content)
except Exception:
translated = None
if translated is not None:
value = self._safe_eval_expr(translated, external)
return self._to_expr_literal(value) if for_expr else self._to_output_string(value)
expr = self._replace_placeholders(content, external, for_expr=True)
value = self._safe_eval_expr(expr, external)
return self._to_expr_literal(value) if for_expr else self._to_output_string(value)
def format_from_text(self, text: str, external: dict | None = None):
get = self._replace_placeholders(text, external or {}, for_expr=False)
get = re.sub(r'\\([\[\]\{\}])', r'\1', get)
for a, k in self.language_specific_replace_table:
get = get.replace(a, k)
return get
def format_from_id(self, id: str, external: dict = None):
keys = id.split('.')
key = keys.pop(0)
base = getattr(self, key)
i = 0
while i < len(keys):
if keys[i].isdigit():
keys[i] = int(keys[i])
i += 1
txt = reduce(self._dict_get, keys, base)
return self.format_from_text(str(txt), external)