Skip to content

Commit b4da277

Browse files
committed
Properly manage unit objects of unknown type.
1 parent 749f6d4 commit b4da277

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

sc2reader/data/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ def __init__(self, build_id, units, abilities):
244244
}
245245

246246
class Unit(object):
247+
name = 'Unknown Unit'
248+
247249
def __init__(self, unit_id):
248250
self.id = unit_id
249251

sc2reader/events.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,15 @@ def load_context(self, replay):
232232

233233
else:
234234
if self.target_type not in replay.datapack.units:
235-
self.target = None
236-
print [hex(key) for key in replay.datapack.units]
237-
print "{0}\t{1}\tMissing unit {2} from {3}".format(self.frame, self.player.name, hex(self.target_type), replay.datapack.id)
235+
self.logger.error("{0}\t{1}\tMissing unit {2} from {3}".format(self.frame, self.player.name, hex(self.target_type), replay.datapack.id))
236+
unit = Unit(self.target_id)
237+
238238
else:
239239
unit_class = replay.datapack.units[self.target_type]
240-
self.target = unit_class(self.target_id)
241-
replay.objects[uid] = self.target
240+
unit = unit_class(self.target_id)
241+
242+
self.target = unit
243+
replay.objects[uid] = unit
242244

243245
def __str__(self):
244246
if self.target:
@@ -307,19 +309,19 @@ def load_context(self, replay):
307309
objects = list()
308310
data = replay.datapack
309311
for (obj_id, obj_type) in self.objects:
310-
if obj_type not in data.units:
311-
msg = "Unit Type {0} not found in {1}"
312-
self.logger.error(msg.format(hex(obj_type), data.__class__.__name__))
313-
print msg.format(hex(obj_type), data.__class__.__name__)
314-
objects.append(Unit(obj_id))
315-
312+
if (obj_id, obj_type) in replay.objects:
313+
obj = replay.objects[(obj_id,obj_type)]
316314
else:
317-
if (obj_id, obj_type) not in replay.objects:
315+
if obj_type in data.units:
318316
obj = data.units[obj_type](obj_id)
319-
replay.objects[(obj_id,obj_type)] = obj
320317
else:
321-
obj = replay.objects[(obj_id,obj_type)]
318+
msg = "Unit Type {0} not found in {1}"
319+
self.logger.error(msg.format(hex(obj_type), data.__class__.__name__))
320+
obj = Unit(obj_id)
321+
322+
replay.objects[(obj_id,obj_type)] = obj
323+
324+
objects.append(obj)
322325

323-
objects.append(obj)
324326

325327
self.objects = objects

0 commit comments

Comments
 (0)