ya2 · news · projects · code · about

renamed lib to ya2
[pmachines.git] / ya2 / tools / fix_mask_texture.py
1 class BlockReplacer:
2
3 def __init__(self, out_lines, start_line, end_line, new_lines):
4 self.out_lines = out_lines
5 self.start_line = start_line
6 self.end_line = end_line
7 self.new_lines = new_lines
8 self.is_replacing = False
9
10 def process_line(self, line):
11 if self.is_replacing and line.rstrip() == self.end_line:
12 self.is_replacing = False
13 return True
14 if line.rstrip() == self.start_line:
15 self.out_lines += self.new_lines
16 self.is_replacing = True
17 return self.is_replacing
18
19
20 class Fixer:
21
22 def __init__(self):
23 self.out_lines = []
24 with open('track.egg') as fin:
25 self.lines = fin.readlines()
26 self.replacers = []
27 new_lines = [
28 '<Texture> MASKOBJTrack {\n',
29 ' "./tex/MASKOBJTrack.jpg"\n',
30 ' <Scalar> combine-rgb { INTERPOLATE }\n',
31 ' <Scalar> combine-rgb-source0 { PREVIOUS }\n',
32 ' <Scalar> combine-rgb-operand0 { SRC-COLOR }\n',
33 ' <Scalar> combine-rgb-source1 { LAST_SAVED_RESULT }\n',
34 ' <Scalar> combine-rgb-operand1 { SRC-COLOR }\n',
35 ' <Scalar> combine-rgb-source2 { TEXTURE }\n',
36 ' <Scalar> combine-rgb-operand2 { SRC-COLOR }\n',
37 ' <Scalar> minfilter { LINEAR_MIPMAP_LINEAR }\n',
38 ' <Scalar> magfilter { LINEAR_MIPMAP_LINEAR }\n',
39 ' <Scalar> wrap { REPEAT }\n',
40 '}\n']
41 rep_str = '<Texture> MASKOBJTrack {'
42 rep = BlockReplacer(self.out_lines, rep_str, '}', new_lines)
43 self.replacers += [rep]
44
45 new_lines = [
46 '<Texture> TEXREPOBJTrack1 {\n',
47 ' "./tex/snowbackground.jpg"\n',
48 ' <Scalar> envtype { MODULATE }\n']
49 rep_str = '<Texture> TEXREPOBJTrack1 {'
50 repl_str = ' <Scalar> envtype { MODULATE }'
51 rep = BlockReplacer(self.out_lines, rep_str, repl_str, new_lines)
52 self.replacers += [rep]
53
54 new_lines = [
55 '<Texture> TEXREPOBJTrack2 {\n',
56 ' "./tex/Tileable ice ground texture.jpg"\n',
57 ' <Scalar> envtype { MODULATE }\n',
58 ' <Scalar> saved-result { 1 }\n']
59 rep_str = '<Texture> TEXREPOBJTrack2 {'
60 repl_str = ' <Scalar> envtype { MODULATE }'
61 rep = BlockReplacer(self.out_lines, rep_str, repl_str, new_lines)
62 self.replacers += [rep]
63
64 new_lines = [
65 ' <TRef> { TEXREPOBJTrack1 }\n',
66 ' <TRef> { MASKOBJTrack }\n']
67 rep = BlockReplacer(
68 self.out_lines,
69 ' <TRef> { MASKOBJTrack }',
70 ' <TRef> { TEXREPOBJTrack1 }',
71 new_lines)
72 self.replacers += [rep]
73
74 for line in self.lines:
75 if not any(rep.process_line(line) for rep in self.replacers):
76 self.out_lines += [line]
77 with open('track_fixed.egg', 'w') as fout:
78 list(map(fout.write, self.out_lines))
79
80
81 Fixer()