prevent dependencies from being recomputed

This commit is contained in:
Bryan Bishop 2012-04-27 13:41:44 -05:00
parent ce35b436ed
commit 39a71c55c7
1 changed files with 63 additions and 15 deletions

View File

@ -1256,6 +1256,7 @@ class PointerLabelParam(MultiByteParam):
debug = False debug = False
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.dependencies = None
#bank can be overriden #bank can be overriden
if "bank" in kwargs.keys(): if "bank" in kwargs.keys():
if kwargs["bank"] != False and kwargs["bank"] != None and kwargs["bank"] in [True, "reverse"]: if kwargs["bank"] != False and kwargs["bank"] != None and kwargs["bank"] in [True, "reverse"]:
@ -1273,16 +1274,19 @@ class PointerLabelParam(MultiByteParam):
self.parsed_address = calculate_pointer_from_bytes_at(self.address, bank=self.bank) self.parsed_address = calculate_pointer_from_bytes_at(self.address, bank=self.bank)
MultiByteParam.parse(self) MultiByteParam.parse(self)
def get_dependencies(self): def get_dependencies(self, recompute=False):
dependencies = [] dependencies = []
if self.parsed_address == self.address: if self.parsed_address == self.address:
return dependencies return dependencies
if self.dependencies != None and not recompute:
return self.dependencies
thing = script_parse_table[self.parsed_address] thing = script_parse_table[self.parsed_address]
if thing and thing.address == self.parsed_address and not (thing is self): if thing and thing.address == self.parsed_address and not (thing is self):
if self.debug: if self.debug:
print "parsed address is: " + hex(self.parsed_address) + " with label: " + thing.label.name + " of type: " + str(thing.__class__) print "parsed address is: " + hex(self.parsed_address) + " with label: " + thing.label.name + " of type: " + str(thing.__class__)
dependencies.append(thing) dependencies.append(thing)
dependencies.extend(thing.get_dependencies()) dependencies.extend(thing.get_dependencies())
self.dependencies = dependencies
return dependencies return dependencies
def to_asm(self): def to_asm(self):
@ -1525,6 +1529,7 @@ class Command:
self.label = Label(name=label, address=address, object=self) self.label = Label(name=label, address=address, object=self)
#params are where this command's byte parameters are stored #params are where this command's byte parameters are stored
self.params = {} self.params = {}
self.dependencies = None
#override default settings #override default settings
defaults.update(kwargs) defaults.update(kwargs)
#set everything #set everything
@ -1535,13 +1540,16 @@ class Command:
#start parsing this command's parameter bytes #start parsing this command's parameter bytes
self.parse() self.parse()
def get_dependencies(self): def get_dependencies(self, recompute=False):
dependencies = [] dependencies = []
if self.dependencies != None and not recompute:
return self.dependencies
for (key, param) in self.params.items(): for (key, param) in self.params.items():
if hasattr(param, "get_dependencies") and param != self: if hasattr(param, "get_dependencies") and param != self:
deps = param.get_dependencies() deps = param.get_dependencies()
if deps != None and not self in deps: if deps != None and not self in deps:
dependencies.extend(deps) dependencies.extend(deps)
self.dependencies = dependencies
return dependencies return dependencies
def to_asm(self): def to_asm(self):
@ -1957,6 +1965,7 @@ class Script:
self.address = address self.address = address
elif len(args) > 1: elif len(args) > 1:
raise Exception, "don't know what to do with second (or later) positional arguments" raise Exception, "don't know what to do with second (or later) positional arguments"
self.dependencies = None
if "label" in kwargs.keys(): if "label" in kwargs.keys():
label = kwargs["label"] label = kwargs["label"]
else: else:
@ -2055,8 +2064,8 @@ class Script:
self.commands = commands self.commands = commands
return commands return commands
def get_dependencies(self): def get_dependencies(self, recompute=False):
if self.dependencies != None: if self.dependencies != None and not recompute:
return self.dependencies return self.dependencies
dependencies = [] dependencies = []
for command in self.commands: for command in self.commands:
@ -2188,13 +2197,17 @@ class XYTrigger(Command):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.id = kwargs["id"] self.id = kwargs["id"]
self.dependencies = None
Command.__init__(self, *args, **kwargs) Command.__init__(self, *args, **kwargs)
def get_dependencies(self): def get_dependencies(self, recompute=False):
dependencies = [] dependencies = []
if self.dependencies != None and not recompute:
return self.dependencies
thing = script_parse_table[self.params[4].parsed_address] thing = script_parse_table[self.params[4].parsed_address]
if thing and thing != self.params[4]: if thing and thing != self.params[4]:
dependencies.append(thing) dependencies.append(thing)
self.dependencies = dependencies
return dependencies return dependencies
all_xy_triggers = [] all_xy_triggers = []
@ -2264,6 +2277,7 @@ class ItemFragment(Command):
self.map_id = map_id self.map_id = map_id
self.debug = debug self.debug = debug
self.params = {} self.params = {}
self.dependencies = None
self.args = {"debug": debug, "map_group": map_group, "map_id": map_id, "bank": bank} self.args = {"debug": debug, "map_group": map_group, "map_id": map_id, "bank": bank}
script_parse_table[self.address : self.last_address] = self script_parse_table[self.address : self.last_address] = self
self.parse() self.parse()
@ -2330,11 +2344,14 @@ class TrainerFragment(Command):
self.include_in_asm = False self.include_in_asm = False
return return
script_parse_table[self.address : self.last_address] = self script_parse_table[self.address : self.last_address] = self
self.dependencies = None
Command.__init__(self, *args, **kwargs) Command.__init__(self, *args, **kwargs)
def get_dependencies(self): def get_dependencies(self, recompute=False):
deps = [] deps = []
if not is_valid_address(self.address): return deps if not is_valid_address(self.address): return deps
if self.dependencies != None and not recompute:
return self.dependencies
deps.append(self.params[3]) deps.append(self.params[3])
deps.extend(self.params[3].get_dependencies()) deps.extend(self.params[3].get_dependencies())
deps.append(self.params[4]) deps.append(self.params[4])
@ -2343,6 +2360,7 @@ class TrainerFragment(Command):
deps.extend(self.params[5].get_dependencies()) deps.extend(self.params[5].get_dependencies())
deps.append(self.params[6]) deps.append(self.params[6])
deps.extend(self.params[6].get_dependencies()) deps.extend(self.params[6].get_dependencies())
self.dependencies = dep
return deps return deps
def to_asm(self): def to_asm(self):
@ -2374,11 +2392,14 @@ class TrainerFragmentParam(PointerLabelParam):
self.dependencies = [trainerfrag] self.dependencies = [trainerfrag]
PointerLabelParam.parse(self) PointerLabelParam.parse(self)
def get_dependencies(self): def get_dependencies(self, recompute=False):
deps = [] deps = []
if self.dependencies != None and not recompute:
return self.dependencies
deps.extend(self.dependencies) deps.extend(self.dependencies)
if len(self.dependencies) > 0: if len(self.dependencies) > 0:
deps.extend(self.dependencies[0].get_dependencies()) deps.extend(self.dependencies[0].get_dependencies())
self.dependencies = deps
return deps return deps
class PeopleEvent(Command): class PeopleEvent(Command):
@ -2421,6 +2442,7 @@ class PeopleEvent(Command):
self.debug = debug self.debug = debug
self.force = force self.force = force
self.params = {} self.params = {}
self.dependencies = None
#PeopleEvent should probably not be in the global script_parse_table #PeopleEvent should probably not be in the global script_parse_table
#script_parse_table[self.address : self.last_address] = self #script_parse_table[self.address : self.last_address] = self
self.parse() self.parse()
@ -2631,13 +2653,17 @@ class SignpostRemoteBase:
if not label: if not label:
label = self.base_label + hex(address) label = self.base_label + hex(address)
self.label = Label(name=label, address=address, object=self) self.label = Label(name=label, address=address, object=self)
self.dependencies = None
self.parse() self.parse()
def get_dependencies(self): def get_dependencies(self, recompute=False):
dependencies = [] dependencies = []
if self.dependencies != None and not recompute:
return self.dependencies
for p in self.params: for p in self.params:
deps = p.get_dependencies() deps = p.get_dependencies()
dependencies.extend(deps) dependencies.extend(deps)
self.dependencies = dependencies
return dependencies return dependencies
def to_asm(self): def to_asm(self):
@ -2767,6 +2793,7 @@ class Signpost(Command):
#script_parse_table[self.address : self.last_address] = self #script_parse_table[self.address : self.last_address] = self
self.remotes = [] self.remotes = []
self.params = [] self.params = []
self.dependencies = None
self.parse() self.parse()
def parse(self): def parse(self):
@ -2873,10 +2900,13 @@ class Signpost(Command):
else: else:
raise Exception, "unknown signpost type byte="+hex(func) + " signpost@"+hex(self.address) raise Exception, "unknown signpost type byte="+hex(func) + " signpost@"+hex(self.address)
def get_dependencies(self): def get_dependencies(self, recompute=False):
dependencies = [] dependencies = []
if self.dependencies != None and not recompute:
return self.dependencies
for p in self.params: for p in self.params:
dependencies.extend(p.get_dependencies()) dependencies.extend(p.get_dependencies())
self.dependencies = dependencies
return dependencies return dependencies
def to_asm(self): def to_asm(self):
@ -2969,6 +2999,7 @@ class MapHeader:
self.map_id = map_id self.map_id = map_id
self.bank = bank self.bank = bank
self.debug = debug self.debug = debug
self.dependencies = None
if not label: if not label:
label = self.base_label + hex(address) label = self.base_label + hex(address)
self.label = Label(name=label, address=address, object=self) self.label = Label(name=label, address=address, object=self)
@ -2991,9 +3022,12 @@ class MapHeader:
self.time_of_day = DecimalParam(address=address+7) self.time_of_day = DecimalParam(address=address+7)
self.fishing_group = DecimalParam(address=address+8) self.fishing_group = DecimalParam(address=address+8)
def get_dependencies(self): def get_dependencies(self, recompute=False):
if self.dependencies != None and not recompute:
return self.dependencies
dependencies = [self.second_map_header] dependencies = [self.second_map_header]
dependencies.append(self.second_map_header.get_dependencies()) dependencies.append(self.second_map_header.get_dependencies())
self.dependencies = dependencies
return dependencies return dependencies
def to_asm(self): def to_asm(self):
@ -3058,6 +3092,7 @@ class SecondMapHeader:
self.map_id = map_id self.map_id = map_id
self.debug = debug self.debug = debug
self.bank = bank self.bank = bank
self.dependencies = None
if not label: if not label:
label = self.base_label + hex(address) label = self.base_label + hex(address)
self.label = Label(name=label, address=address, object=self) self.label = Label(name=label, address=address, object=self)
@ -3123,10 +3158,13 @@ class SecondMapHeader:
return True return True
def get_dependencies(self): def get_dependencies(self, recompute=False):
if self.dependencies != None and not recompute:
return self.dependencies
dependencies = [self.script_header, self.event_header, self.blockdata] dependencies = [self.script_header, self.event_header, self.blockdata]
dependencies.append(self.script_header.get_dependencies()) dependencies.append(self.script_header.get_dependencies())
dependencies.append(self.event_header.get_dependencies()) dependencies.append(self.event_header.get_dependencies())
self.dependencies = dependencies
return dependencies return dependencies
def to_asm(self): def to_asm(self):
@ -3241,6 +3279,7 @@ class MapEventHeader:
self.map_id = map_id self.map_id = map_id
self.debug = debug self.debug = debug
self.bank = bank self.bank = bank
self.dependencies = None
if not label: if not label:
label = self.base_label + hex(address) label = self.base_label + hex(address)
self.label = Label(name=label, address=address, object=self) self.label = Label(name=label, address=address, object=self)
@ -3297,7 +3336,9 @@ class MapEventHeader:
self.last_address = after_signposts+1 self.last_address = after_signposts+1
return True return True
def get_dependencies(self): def get_dependencies(self, recompute=False):
if self.dependencies != None and not recompute:
return self.dependencies
bases = [] bases = []
bases += self.people_events bases += self.people_events
bases += self.signposts bases += self.signposts
@ -3307,6 +3348,7 @@ class MapEventHeader:
dependencies = [] dependencies = []
for p in bases: for p in bases:
dependencies.extend(p.get_dependencies()) dependencies.extend(p.get_dependencies())
self.dependencies = dependencies
return dependencies return dependencies
def to_asm(self): def to_asm(self):
@ -3459,6 +3501,7 @@ class MapScriptHeader:
self.map_id = map_id self.map_id = map_id
self.debug = debug self.debug = debug
self.bank = bank self.bank = bank
self.dependencies = None
if not label: if not label:
label = self.base_label + hex(address) label = self.base_label + hex(address)
self.label = Label(name=label, address=address, object=self) self.label = Label(name=label, address=address, object=self)
@ -3499,7 +3542,9 @@ class MapScriptHeader:
print "done parsing a MapScriptHeader map_group="+str(map_group)+" map_id="+str(map_id) print "done parsing a MapScriptHeader map_group="+str(map_group)+" map_id="+str(map_id)
return True return True
def get_dependencies(self): def get_dependencies(self, recompute=False):
if self.dependencies != None and not recompute:
return self.dependencies
dependencies = [] dependencies = []
for p in list(self.triggers): for p in list(self.triggers):
#dependencies.append(p[0]) #dependencies.append(p[0])
@ -3507,6 +3552,7 @@ class MapScriptHeader:
for callback in self.callbacks: for callback in self.callbacks:
dependencies.append(callback["callback"]) dependencies.append(callback["callback"])
dependencies.extend(callback["callback"].get_dependencies()) dependencies.extend(callback["callback"].get_dependencies())
self.dependencies = dependencies
return dependencies return dependencies
def to_asm(self): def to_asm(self):
@ -4332,7 +4378,7 @@ def flatten(x):
"flattens a list of sublists into just one list" "flattens a list of sublists into just one list"
return list(flattener(x)) return list(flattener(x))
def get_dependencies_for(some_object): def get_dependencies_for(some_object, recompute=False):
""" """
calculates which labels need to be satisfied for an object calculates which labels need to be satisfied for an object
to be inserted into the asm and compile successfully. to be inserted into the asm and compile successfully.
@ -4343,7 +4389,9 @@ def get_dependencies_for(some_object):
""" """
if isinstance(some_object, int): if isinstance(some_object, int):
some_object = script_parse_table[some_object] some_object = script_parse_table[some_object]
deps = some_object.get_dependencies() if some_object.dependencies != None and not recompute:
return list(flatten(some_object.dependencies))
deps = some_object.get_dependencies(recompute=recompute)
return list(flatten(deps)) return list(flatten(deps))
def isolate_incbins(): def isolate_incbins():