-
Notifications
You must be signed in to change notification settings - Fork 4
/
text.py
78 lines (62 loc) · 2 KB
/
text.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
# coding: utf-8
from script import *
import charmap
class String(Chunk):
name = '.string'
atomic = True
def parse(self):
Chunk.parse(self)
address = self.address
address = self.rom.find('\xff', address) + 1
self.last_address = address
@property
def bytes(self):
return self.rom[self.address:self.last_address]
@property
def asm(self):
return '"' + charmap.decode(self.bytes, self.version['charmap']) + '"'
def to_asm(self):
newline = '"\n\t{} "'.format(self.name)
asm = self.asm
for newline_token in ('\\l', '\\p', '\\n',):
asm = asm.replace(newline_token, newline_token + newline)
return '\t' + self.name + ' ' + asm
class Text(ParamGroup):
param_classes = [String]
class TextPointer(Pointer):
target = Text
class JPString(String):
@property
def asm(self):
return '"' + charmap.decode(self.bytes, self.version['charmap_jp']) + '"'
class JPText(Text):
param_classes = [JPString]
alphabet = map(chr, xrange(ord('A'), ord('Z')+1))
class BrailleString(String):
name = '.braille'
letters = [ 1, 5, 3, 11, 9, 7, 15, 13, 6, 14, 17, 21, 19, 27, 25, 23, 31, 29, 22, 30, 49, 53, 46, 51, 59, 57 ]
mapping = dict(zip(letters, alphabet))
mapping.update({ 0: ' ', 28: '!', 16: '\'', 4: ',', 48: '-', 44: '.', 52: '?', 8: '"', 0xfe: '\\n', 0xff: '$' })
@property
def asm(self):
return '"' + strmap(self.mapping.get, self.bytes) + '"'
strmap = lambda *args: ''.join(map(*args))
class RawString(String):
"""Just spits out "{0x..}...{0xff}"."""
@property
def asm(self):
return '"' + strmap('{{0x{:02x}}}'.format, self.bytes) + '"'
class Braille(ParamGroup):
param_classes = [Byte, Byte, Byte, Byte, Byte, Byte, ('string', BrailleString)]
class BraillePointer(Pointer):
target = Braille
if __name__ == '__main__':
args = get_args(
'address',
('version', {'nargs': '?', 'default': 'ruby'})
)
insert_recursive(
Text,
int(args.address, 16),
args.version,
)