Skip to content

Commit b54cd4a

Browse files
committed
Consolidate s2gs util functions and clean up a bit.
1 parent 497bd49 commit b54cd4a

File tree

1 file changed

+37
-66
lines changed

1 file changed

+37
-66
lines changed

sc2reader/utils.py

Lines changed: 37 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -728,72 +728,6 @@ def get_files(path, exclude=list(), depth=-1, followlinks=False, extension=None,
728728
depth -= 1
729729

730730

731-
def get_unit(type_int):
732-
"""
733-
Takes an int, i, with (i & 0xff000000) = 0x01000000
734-
and returns the corresponding unit/structure
735-
"""
736-
# Test if we have used data_obj before
737-
try:
738-
data_obj
739-
except:
740-
#Nope, create
741-
data_obj = Data()
742-
743-
# Try to parse a unit
744-
try:
745-
unit = data_obj.type(((type_int & 0xff) << 8) | 0x01)
746-
except:
747-
unit = None
748-
749-
return {
750-
'name': unit.name if unit else "Unknown unit ({})".format(hex(type_int)) ,
751-
'type_int':hex(type_int)
752-
}
753-
def get_research(type_int):
754-
"""
755-
Takes an int, i, with (i & 0xff000000) = 0x02000000
756-
and returns the corresponding research/upgrade
757-
"""
758-
t = ((type_int & 0xff) << 8) | 0x02
759-
return {
760-
'name': BUILD_ORDER_UPGRADES[t] if t in BUILD_ORDER_UPGRADES else "Unknown upgrade ({})".format(hex(t)),
761-
'type_int': hex(type_int)}
762-
763-
def parse_hash(hash_string):
764-
"""
765-
Parse a hash to useful data
766-
{
767-
'server',
768-
'hash',
769-
'type'
770-
}
771-
"""
772-
server = hash_string[6:8]
773-
hash = hash_string[8:]
774-
return {
775-
'server': server,
776-
'hash' : ''.join([('%02x' % ord(x)) for x in hash]),
777-
'type' : hash_string[0:4]
778-
}
779-
def reverse_str(s):
780-
return ''.join(reversed(s))
781-
def flip_int(num, b):
782-
"""
783-
Flips the b first bytes in num
784-
Example:
785-
(0x12345, 3) -> 0x452301
786-
(0x00112233, 4) -> 0x33221100
787-
"""
788-
o = 0
789-
for i in range(b/2):
790-
o |= ((num & (0xff << i*8)) << (b-(2*i+1))*8)
791-
o |= ((num & (0xff << (b-(i+1))*8)) >> (b-(2*i+1)) * 8)
792-
if b % 2 == 1:
793-
o |= (num & (0xff << (b/2)*8))
794-
return o
795-
796-
797731
class Length(timedelta):
798732
"""
799733
Extends the builtin timedelta class. See python docs for more info on
@@ -947,3 +881,40 @@ def get_lobby_properties(data):
947881
player_props[pid] = player
948882

949883
return (lobby_props, player_props)
884+
885+
def get_unit(type_int):
886+
"""
887+
Takes an int, i, with (i & 0xff000000) = 0x01000000
888+
and returns the corresponding unit/structure
889+
"""
890+
# Try to parse a unit
891+
unit_code = ((type_int & 0xff) << 8) | 0x01
892+
if unit_code in Data.types:
893+
unit_name = Data.type(unit_code).name
894+
else:
895+
unit_name = "Unknown Unit ({0:X})".format(type_int)
896+
897+
return dict(name=unit_name, type_int=hex(type_int))
898+
899+
900+
def get_research(type_int):
901+
"""
902+
Takes an int, i, with (i & 0xff000000) = 0x02000000
903+
and returns the corresponding research/upgrade
904+
"""
905+
research_code = ((type_int & 0xff) << 8) | 0x02
906+
if research_code in BUILD_ORDER_UPGRADES:
907+
research_name = BUILD_ORDER_UPGRADES[research_code]
908+
else:
909+
"Unknown upgrade ({0:X})".format(research_code)
910+
911+
return dict(name=research_name, type_int=hex(type_int))
912+
913+
def parse_hash(hash_string):
914+
"""Parse a hash to useful data"""
915+
# TODO: this could be used when processing replays.initData as well
916+
return {
917+
'server': hash_string[4:8].strip(),
918+
'hash' : hash_string[8:].encode('hex'),
919+
'type' : hash_string[0:4]
920+
}

0 commit comments

Comments
 (0)