-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseries_maps.py
161 lines (112 loc) · 3.83 KB
/
series_maps.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
import struct, os
def parse_keyvalue(line):
if line.find("//") != -1:
line = line[:line.find("//")]
quotes = [idx for idx, c in enumerate(line) if c == '"']
if len(quotes) < 4:
return None
key = line[quotes[0]+1 : quotes[1]]
value = line[quotes[2]+1 : quotes[3]]
return (key, value)
def parse_ents(path, ent_text):
ents = []
lineNum = 0
lastBracket = -1
ent = None
for line in ent_text.splitlines():
lineNum += 1
if len(line) < 1 or line[0] == '\n':
continue
if line[0] == '{':
if lastBracket == 0:
print("\n%s.bsp ent data (line %d): Unexpected '{'\n" % (path, lineNum));
continue
lastBracket = 0
ent = {}
elif line[0] == '}':
if lastBracket == 1:
print("\n%s.bsp ent data (line %d): Unexpected '}'\n" % (path, lineNum));
lastBracket = 1
if ent == None:
continue
ents.append(ent)
ent = None
# a new ent can start on the same line as the previous one ends
if line.find("{") != -1:
ent = {}
lastBracket = 0
elif lastBracket == 0 and ent != None: # currently defining an entity
keyvalue = parse_keyvalue(line)
if keyvalue:
ent[keyvalue[0]] = keyvalue[1]
return ents
def load_entities(bsp_path):
with open(bsp_path, mode='rb') as f:
bytes = f.read()
version = struct.unpack("i", bytes[:4])
offset = struct.unpack("i", bytes[4:4+4])[0]
length = struct.unpack("i", bytes[8:8+4])[0]
ent_text = bytes[offset:offset+length].decode("ascii", "ignore")
return parse_ents(bsp_path, ent_text)
print("\nFailed to open %s" % bsp_path)
return None
def get_all_maps(maps_dir):
all_maps = []
for file in os.listdir(maps_dir):
if not file.lower().endswith('.bsp'):
continue
if '@' in file:
continue # ignore old/alternate versions of maps (w00tguy's scmapdb content pool)
all_maps.append(os.path.join(maps_dir, file))
return sorted(all_maps, key=lambda v: v.upper())
list_file = 'series_maps.txt'
all_maps = []
for dir in ["../../../../svencoop/maps", "../../../../svencoop_addon/maps", "../../../../svencoop_downloads/maps"]:
if os.path.exists(dir):
all_maps += get_all_maps(dir)
series_ignore = []
with open('series_ignore.txt', mode='r') as f:
for map in f.readlines():
map = map[:map.find('/')]
series_ignore.append(map.lower().strip())
map_changes = {}
all_nextmaps = set({})
last_progress_str = ''
for idx, map_path in enumerate(all_maps):
map_name = os.path.basename(map_path).lower().replace('.bsp', '')
if map_name in series_ignore:
continue
progress_str = "Progress: %s / %s (%s)" % (idx, len(all_maps), map_name)
padded_progress_str = progress_str
if len(progress_str) < len(last_progress_str):
padded_progress_str += ' '*(len(last_progress_str) - len(progress_str))
last_progress_str = progress_str
print(padded_progress_str, end='\r')
all_ents = load_entities(map_path)
for ent in all_ents:
if 'classname' in ent and 'trigger_changelevel' in ent['classname'] and 'map' in ent:
nextmap = ent['map'].lower()
if map_name in map_changes:
map_changes[map_name].append(nextmap)
else:
map_changes[map_name] = [nextmap]
all_nextmaps.add(nextmap)
first_maps = []
for map, nextmap in map_changes.items():
if map not in all_nextmaps:
first_maps.append(map)
with open(list_file, 'w') as f:
for map in first_maps:
if map in map_changes:
maplist = [map]
nextmaps = map_changes[map]
while len(nextmaps):
next_next_maps = []
for nextmap in nextmaps:
if nextmap in maplist:
continue # cyclic map change
maplist.append(nextmap)
if nextmap in map_changes:
next_next_maps += map_changes[nextmap]
nextmaps = next_next_maps
f.write(' '.join(maplist) + '\n')