Skip to content

Commit 150fd2f

Browse files
committed
Adds experimental map support.
1 parent 803abd2 commit 150fd2f

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

sc2reader/objects.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
from __future__ import absolute_import
22

3+
import urllib2
34
import hashlib
45

6+
from cStringIO import StringIO
7+
58
from collections import namedtuple
69

10+
from mpyq import MPQArchive
11+
712
from sc2reader.constants import *
813
from sc2reader.utils import PersonDict, AttributeDict
914

@@ -210,3 +215,28 @@ def format(self, format_string):
210215

211216
def __repr__(self):
212217
return str(self)
218+
219+
class Map(object):
220+
url_template = 'http://{0}.depot.battle.net:1119/{1}.s2ma'
221+
222+
def __init__(self, gateway, map_hash, map_name=''):
223+
self.hash = map_hash.encode('hex')
224+
self.gateway = gateway
225+
self.url = Map.url_template.format(self.gateway, self.hash)
226+
self.name = map_name
227+
228+
def load(self):
229+
print "Fetching map: {0}".format(self.url);
230+
self.file = urllib2.urlopen(self.url).read()
231+
print "Map Received"
232+
self.archive = MPQArchive(StringIO(self.file))
233+
self.minimap = self.archive.read_file('Minimap.tga')
234+
self.game_strings = self.archive.read_file('enUS.SC2Data\LocalizedData\GameStrings.txt')
235+
for line in self.game_strings.split('\r\n'):
236+
parts = line.split('=')
237+
if parts[0] == 'DocInfo/Name':
238+
self.name = parts[1]
239+
elif parts[0] == 'DocInfo/Author':
240+
self.author = parts[1]
241+
elif parts[0] == 'DocInfo/DescLong':
242+
self.description = parts[1]

sc2reader/replay.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from datetime import datetime
66
from collections import defaultdict
77
from sc2reader.constants import REGIONS, LOCALIZED_RACES, GAME_SPEED_FACTOR
8-
from sc2reader.objects import Player, Observer, Team
8+
from sc2reader.objects import Player, Observer, Team, Map
99

1010
from sc2reader import utils
1111

@@ -42,7 +42,7 @@ class Replay(object):
4242
is_private = bool()
4343

4444
#: The name of the map the game was played on
45-
map = str()
45+
map = None
4646

4747
#: The gateway the game was played on: us, eu, sea, etc
4848
gateway = str()
@@ -151,6 +151,7 @@ def load_details(self):
151151
initData = self.raw_data['replay.initData']
152152
if initData.map_data:
153153
self.gateway = initData.map_data[0].gateway
154+
self.map = Map(self.gateway, initData.map_data[-1].map_hash)
154155

155156
#Expand this special case mapping
156157
if self.gateway == 'sg':
@@ -173,7 +174,8 @@ def load_details(self):
173174
if 'replay.details' in self.raw_data:
174175
details = self.raw_data['replay.details']
175176

176-
self.map = details.map
177+
if self.map:
178+
self.map.name = details.map
177179

178180
self.windows_timestamp = details.file_time-details.utc_adjustment
179181
self.unix_timestamp = utils.windows_to_unix(self.windows_timestamp)
@@ -185,6 +187,8 @@ def load_details(self):
185187
self.start_time = datetime.utcfromtimestamp(self.unix_timestamp-self.real_length.seconds)
186188
self.date = self.end_time #backwards compatibility
187189

190+
def load_map(self):
191+
self.map.load()
188192

189193
def load_players(self):
190194
#If we don't at least have details and attributes_events we can go no further

0 commit comments

Comments
 (0)