comparison pytouhou/formats/hint.py @ 400:7aa70f0def38

Add support for MoF’s hint format.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 12 Feb 2013 19:27:10 +0100
parents
children 6c0cb3eee33e
comparison
equal deleted inserted replaced
399:1c773544eaeb 400:7aa70f0def38
1 # -*- encoding: utf-8 -*-
2 ##
3 ## Copyright (C) 2013 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
4 ##
5 ## This program is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published
7 ## by the Free Software Foundation; version 3 only.
8 ##
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details.
13 ##
14
15
16 from collections import OrderedDict
17
18
19 def _read_n_int(value):
20 values = value.split(', ')
21 return tuple(int(value) for value in values)
22
23
24 class Stage(list):
25 def __init__(self, number):
26 list.__init__(self)
27 self.number = number
28
29
30 class Hint(object):
31 _fields = {'Stage': int,
32 'Tips': None,
33 'Remain': int,
34 'Text': lambda v: v[1:-1],
35 'Pos': _read_n_int,
36 'Count': int,
37 'Base': str,
38 'Align': str,
39 'Time': int,
40 'Alpha': int,
41 'Color': _read_n_int,
42 'Scale': float,
43 'End': None,
44 'StageEnd': None}
45
46
47 def __init__(self):
48 self.version = 0.0
49 self.stages = []
50
51
52 @classmethod
53 def read(cls, file):
54 tokens = []
55
56 for line in file:
57 line = line.strip()
58
59 if not line:
60 continue
61 if line[0] == '#':
62 continue
63 if line == 'Version = 0.0':
64 continue
65
66 field, _, value = line.partition(':')
67 field = field.rstrip()
68 value = value.lstrip()
69 parser = cls._fields[field]
70
71 if parser:
72 tokens.append((field, parser(value)))
73 else:
74 tokens.append((field, None))
75
76 stage_mode = False
77 tip_mode = False
78 stage = None
79 hints = cls()
80 stages = hints.stages
81
82 for token in tokens:
83 key = token[0]
84 value = token[1]
85
86 if stage_mode:
87 if key != 'StageEnd':
88 if tip_mode:
89 if key != 'End':
90 tip[key] = value
91 else:
92 assert tip_mode == True
93 stage.append(tip)
94 tip_mode = False
95 elif key == 'Tips':
96 assert tip_mode == False
97 tip = OrderedDict()
98 tip_mode = True
99 else:
100 assert stage_mode == True
101 stages.append(stage)
102 stage_mode = False
103 elif key == 'Stage':
104 assert stage_mode == False
105 stage = Stage(value)
106 stage_mode = True
107
108 return hints
109
110
111 def write(self, file):
112 file.write('# Hints file generated with PyTouhou\n\n\n')
113
114 file.write('Version = {}\n\n'.format(self.version))
115
116 for stage in self.stages:
117 file.write('# ================================== \n')
118 file.write('Stage : {}\n\n'.format(stage.number))
119
120 for tip in stage:
121 file.write('Tips\n')
122
123 for key, value in tip.items():
124 if key == 'Text':
125 value = '"{}"'.format(value)
126 elif key == 'Pos':
127 key = 'Pos\t'
128 if isinstance(value, tuple):
129 value = str(value)[1:-1]
130 file.write('\t{}\t: {}\n'.format(key, value))
131
132 file.write('End\n\n')
133
134 file.write('StageEnd\n')