-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfind_files.py
258 lines (210 loc) · 5.35 KB
/
find_files.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
253
254
255
256
257
258
import os
from script import is_label
import versions
g = ''
def makedirs(*args, **kwargs):
try:
os.makedirs(*args, **kwargs)
except OSError:
pass
def split_stuff(root, writes):
lines = open(root).readlines()
for start, end, filename in writes:
makedirs(os.path.dirname(filename))
open(filename, 'w').write(''.join(lines[start:end]))
i = 0
new_lines = []
lines = open(root).readlines()
for start, end, filename in writes:
new_lines += lines[i:start]
new_lines += ['\t.include "{}"\n'.format(filename)]
i = end
new_lines += lines[i:]
open(root, 'w').write(''.join(new_lines))
def get_label(line):
if '.global' in line:
return line.split(' ')[1].strip()
if is_label(line):
return line.split(':')[0].strip()
def split_map_scripts(version):
root = 'data/data1.s'
seen = []
def get_filename():
return 'data/maps/scripts/{}.inc'.format(seen[-1])
writes = []
def write(start, end):
if start is not None:
writes.append((start, end, get_filename()))
start = None
for i, line in enumerate(open(root)):
label = get_label(line)
if label:
if label.endswith('_MapScripts'):
name = label[len(g):-len('_MapScripts')]
if name not in seen:
write(start, i)
seen += [name]
start = i
elif (
'_EventScript_' not in label
and '_MapScript1_' not in label
and '_MapScript2_' not in label
and ((not seen) or (seen[-1] + '_' not in label))
):
write(start, i)
start = None
# arbitrary stopping point
if version['version'] == 'emerald':
if '0x271315' in line:
break
split_stuff(root, writes)
def split_map_text(version):
if version['version'] == 'emerald':
return
root = 'data/data1.s'
seen = []
def get_filename():
return 'data/maps/text/{}.s'.format(seen[-1])
writes = []
def write(start, end):
if start is not None:
writes.append((start, end, get_filename()))
start = None
for i, line in enumerate(open(root)):
label = get_label(line)
if label:
if '_Text_' in label:
name = label[len(g):label.find('_Text_')]
if name not in seen:
write(start, i)
seen += [name]
start = i
elif seen and name != seen[-1]:
write(start, i)
start = None
else:
write(start, i)
start = None
if version['version'] == 'ruby':
if '0x19f7de' in line:
write(start, i)
start = None
#break
split_stuff(root, writes)
def split_map_assets():
root = 'data/data2.s'
start = None
enders = ('_MapBorder', '_MapBlockdata', '_MapAttributes')
for i, line in enumerate(open(root)):
label = get_label(line)
if label:
if any(map(label.endswith, enders)):
if start is None: start = i
elif start:
break
end = i
split_stuff(root, [(start, end, 'data/maps/_assets.inc')])
def split_map_events():
root = 'data/data2.s'
seen = []
def get_filename():
return 'data/maps/events/{}.inc'.format(seen[-1])
writes = []
def write(start, end):
if start is not None:
writes.append((start, end, get_filename()))
start = None
enders = ('_MapObjects', '_MapWarps', '_MapCoordEvents', '_MapBGEvents', '_MapEvents')
for i, line in enumerate(open(root)):
label = get_label(line)
if label:
for ender in enders:
if label.endswith(ender):
name = label.split(ender)[0][len(g):]
if name not in seen:
write(start, i)
seen += [name]
start = i
break
# dont absorb incbins
if 'base_emerald' in line and 'incbin' in line:
write(start, i)
start = None
split_stuff(root, writes)
def split_map_headers(version):
root = 'data/data2.s'
seen = []
def get_filename():
return 'data/maps/{}/header.inc'.format(seen[-1])
writes = []
def write(start, end):
if start is not None:
writes.append((start, end, get_filename()))
start = None
for i, line in enumerate(open(root)):
label = get_label(line)
if label:
name = label[len(g):]
if name in version['map_names']:
if name not in seen:
write(start, i)
seen += [name]
start = i
else:
write(start, i)
start = None
# dont absorb incbins
if 'base_emerald' in line and 'incbin' in line:
write(start, i)
start = None
split_stuff(root, writes)
def split_map_groups():
root = 'data/data2.s'
start = None
for i, line in enumerate(open(root)):
label = get_label(line)
if label:
if label.startswith('gMapGroup'):
if start is None: start = i
elif start is not None:
break
end = i
split_stuff(root, [(start, end, 'data/maps/_groups.inc')])
def split_map_connections():
root = 'data/data2.s'
seen = []
def get_filename():
return 'data/maps/{}/connections.inc'.format(seen[-1])
writes = []
def write(start, end):
if start is not None:
writes.append((start, end, get_filename()))
start = None
ender = '_MapConnectionsList'
for i, line in enumerate(open(root)):
label = get_label(line)
if label:
if label.endswith(ender):
name = label.split(ender)[0][len(g):]
if name not in seen:
write(start, i)
seen += [name]
start = i
elif not label.endswith('_MapConnections'):
write(start, i)
start = None
# dont absorb incbins
if 'base_emerald' in line and 'incbin' in line:
write(start, i)
start = None
split_stuff(root, writes)
def main(version):
split_map_scripts(version)
split_map_assets()
split_map_events()
split_map_headers(version)
split_map_groups()
split_map_connections()
split_map_text(version)
if __name__ == '__main__':
main(versions.ruby)