From d24581e411bc110884ebf77eb8e96048e88296e7 Mon Sep 17 00:00:00 2001 From: Darren Ranalli Date: Fri, 3 Oct 2003 23:10:27 +0000 Subject: [PATCH] added attribute checks/fixes --- direct/src/level/LevelSpec.py | 46 +++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/direct/src/level/LevelSpec.py b/direct/src/level/LevelSpec.py index 3182a69b92..6ded3b8cfa 100755 --- a/direct/src/level/LevelSpec.py +++ b/direct/src/level/LevelSpec.py @@ -1,14 +1,18 @@ """LevelSpec module: contains the LevelSpec class""" -from PythonUtil import list2dict +import DirectNotifyGlobal +from PythonUtil import list2dict, uniqueElements import string class LevelSpec: """contains spec data for a level, is responsible for handing the data out upon request, as well as recording changes made during editing, and saving out modified spec data""" - def __init__(self, specDict, scenario=0): + notify = DirectNotifyGlobal.directNotify.newCategory("LevelSpec") + + def __init__(self, specDict, scenario=0, entTypeReg=None): self.specDict = specDict + self.entTypeReg = entTypeReg # this maps an entId to the dict that holds its spec; # entities are either in the global dict or a scenario dict @@ -22,6 +26,44 @@ class LevelSpec: list2dict(self.getScenarioEntIds(i), value=self.privGetScenarioEntityDict(i))) + if __debug__: + # make sure there are no duplicate entIds + entIds = self.getGlobalEntIds() + assert uniqueElements(entIds) + entIds = list2dict(entIds) + for i in range(self.getNumScenarios()): + for id in self.getScenarioEntIds(i): + assert not entIds.has_key(id) + entIds[id] = None + + if self.entTypeReg is not None: + # check each spec + allEntIds = entIds + for entId in allEntIds: + spec = self.getEntitySpec(entId) + + assert spec.has_key('type') + attribNames = entTypeReg.getAttribNames(spec['type']) + attribDescs = entTypeReg.getAttribDescs(spec['type']) + + # are there any unknown attribs in the spec? + for attrib in spec.keys(): + if attrib not in attribNames: + LevelSpec.notify.warning( + "entId %s (%s): unknown attrib '%s', omitting" + % (entId, spec['type'], attrib)) + del spec[attrib] + + # does the spec have all of its attributes? + for attribName in attribNames: + if not spec.has_key(attribName): + default = attribDescs[attribName].getDefaultValue() + LevelSpec.notify.warning( + "entId %s (%s): missing attrib '%s', setting " + "to default (%s)" % (entId, spec['type'], + attribName, repr(default))) + spec[attribName] = default + self.setScenario(scenario) def getNumScenarios(self):