Skip to content

Commit 82714f8

Browse files
committed
More work with MapInfo and MapHeader
1 parent dd7457b commit 82714f8

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

sc2reader/resources.py

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,22 +480,75 @@ class MapInfo(Resource):
480480
#: Name of the Map
481481
map_name = str()
482482

483+
#: Language
484+
language = str()
485+
483486
#: Hash of referenced s2mh file
484487
s2mh_hash = str()
485488

486489
#: URL of referenced s2mh file
487490
s2mh_url = str()
488491

489492
def __init__(self, info_file, filename=None, **options):
490-
super(MapInfo, self).__init__(info_file, filename,**options)
493+
super(MapInfo, self).__init__(info_file, filename, **options)
491494
self.data = utils.ReplayBuffer(info_file).read_data_struct()
492495
self.map_name = self.data[0][7]
493496
self.language = self.data[0][13]
494-
self.s2mh_hash = ''.join([hex(ord(x))[2:] for x in self.data[0][1][8:]])
495-
self.s2mh_url = MapHeader.url_template.format(self.data[0][1][6:8], self.s2mh_hash)
497+
parsed_hash = utils.parse_hash(self.data[0][1])
498+
self.s2mh_hash = parsed_hash['hash']
499+
self.s2mh_url = MapHeader.url_template.format(parsed_hash['server'], self.s2mh_hash)
500+
501+
def __str__(self):
502+
return self.map_name
496503

497504
class MapHeader(Resource):
505+
base_url_template = 'http://{0}.depot.battle.net:1119/{1}.{2}'
498506
url_template = 'http://{0}.depot.battle.net:1119/{1}.s2mh'
507+
image_url_template = 'http://{0}.depot.battle.net:1119/{1}.s2mv'
508+
509+
#: The name of the map
510+
name = str()
511+
512+
#: Hash of map file
513+
map_hash = str()
514+
515+
#: Link to the map file
516+
map_url = str()
517+
518+
#: Hash of the map image
519+
image_hash = str()
520+
521+
#: Link to the image of the map (.s2mv)
522+
image_url = str()
523+
524+
#: Localization dictionary, {language, url}
525+
localization_urls = dict()
526+
527+
#: Blizzard map
528+
blizzard = False
529+
499530
def __init__(self, header_file, filename=None, **options):
500-
super(MapHeader, self).__init__(header_file, filename,**options)
531+
super(MapHeader, self).__init__(header_file, filename, **options)
501532
self.data = utils.ReplayBuffer(header_file).read_data_struct()
533+
534+
# Name
535+
self.name = self.data[0][1]
536+
537+
# Blizzard
538+
self.blizzard = (self.data[0][11] == 'BLIZ')
539+
540+
# Parse image hash
541+
parsed_hash = utils.parse_hash(self.data[0][1])
542+
self.image_hash = parsed_hash['hash']
543+
self.image_url = self.image_url_template.format(parsed_hash['server'], parsed_hash['hash'])
544+
545+
# Parse map hash
546+
parsed_hash = utils.parse_hash(self.data[0][2])
547+
self.map_hash = parsed_hash['hash']
548+
self.map_url = self.base_url_template.format(parsed_hash['server'], parsed_hash['hash'], parsed_hash['type'])
549+
550+
# Parse localization hashes
551+
l18n_struct = self.data[0][4][8]
552+
for l in l18n_struct:
553+
parsed_hash = utils.parse_hash(l[1][0])
554+
self.localization_urls[l[0]] = self.base_url_template.format(parsed_hash['server'], parsed_hash['hash'], parsed_hash['type'])

sc2reader/utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,23 @@ def get_files(path, extensions=['.sc2replay'], exclude=[], depth=-1, followlinks
720720

721721
return files
722722

723+
def parse_hash(hash_string):
724+
"""
725+
Parse a hash to useful data
726+
{
727+
'server',
728+
'hash',
729+
'type'
730+
}
731+
"""
732+
server = hash_string[6:8]
733+
hash = hash_string[8:]
734+
return {
735+
'server': server,
736+
'hash' : ''.join([('%02x' % ord(x)) for x in hash]),
737+
'type' : hash_string[0:4]
738+
}
739+
723740
class Length(timedelta):
724741
"""
725742
Extends the builtin timedelta class. See python docs for more info on

0 commit comments

Comments
 (0)