2022-12-16 18:40:57 -06:00
|
|
|
from panda3d.core import *
|
2019-11-02 17:27:54 -05:00
|
|
|
from direct.directnotify import DirectNotifyGlobal
|
|
|
|
import types
|
2019-12-30 00:00:16 -06:00
|
|
|
from . import AttribDesc
|
|
|
|
from . import EntityTypeDesc
|
2019-11-02 17:27:54 -05:00
|
|
|
from direct.showbase.PythonUtil import mostDerivedLast
|
|
|
|
import os
|
|
|
|
import string
|
2019-12-30 00:00:16 -06:00
|
|
|
import importlib
|
2019-11-02 17:27:54 -05:00
|
|
|
|
|
|
|
class EntityTypeRegistry:
|
|
|
|
notify = DirectNotifyGlobal.directNotify.newCategory('EntityTypeRegistry')
|
|
|
|
|
|
|
|
def __init__(self, entityTypeModule):
|
|
|
|
self.entTypeModule = entityTypeModule
|
|
|
|
hv = HashVal()
|
2019-12-30 00:00:16 -06:00
|
|
|
from . import EntityTypes
|
|
|
|
importlib.reload(EntityTypes)
|
|
|
|
importlib.reload(self.entTypeModule)
|
2019-11-02 17:27:54 -05:00
|
|
|
|
|
|
|
def getPyExtVersion(filename):
|
|
|
|
base, ext = os.path.splitext(filename)
|
|
|
|
if ext == '.pyc' or ext == '.pyo':
|
|
|
|
filename = base + '.py'
|
|
|
|
return filename
|
|
|
|
|
|
|
|
fileLines = file(getPyExtVersion(EntityTypes.__file__)).readlines()
|
|
|
|
hv.hashString(string.join(fileLines, ''))
|
|
|
|
s = str(hv.asHex())
|
|
|
|
s += '.'
|
|
|
|
fileLines = file(getPyExtVersion(self.entTypeModule.__file__)).readlines()
|
|
|
|
hv.hashString(string.join(fileLines, ''))
|
|
|
|
s += str(hv.asHex())
|
|
|
|
self.hashStr = s
|
|
|
|
getPyExtVersion = None
|
|
|
|
classes = []
|
2019-12-30 00:00:16 -06:00
|
|
|
for key, value in list(entityTypeModule.__dict__.items()):
|
|
|
|
if type(value) is type:
|
2019-11-02 17:27:54 -05:00
|
|
|
if issubclass(value, EntityTypeDesc.EntityTypeDesc):
|
|
|
|
classes.append(value)
|
|
|
|
|
|
|
|
self.entTypeName2typeDesc = {}
|
|
|
|
mostDerivedLast(classes)
|
|
|
|
for c in classes:
|
2019-12-30 00:00:16 -06:00
|
|
|
if 'type' in c.__dict__:
|
|
|
|
if c.type in self.entTypeName2typeDesc:
|
2019-11-02 17:27:54 -05:00
|
|
|
EntityTypeRegistry.notify.debug("replacing %s with %s for entity type '%s'" % (self.entTypeName2typeDesc[c.type].__class__, c, c.type))
|
|
|
|
self.entTypeName2typeDesc[c.type] = c()
|
|
|
|
|
|
|
|
self.output2typeNames = {}
|
2019-12-30 00:00:16 -06:00
|
|
|
for typename, typeDesc in list(self.entTypeName2typeDesc.items()):
|
2019-11-02 17:27:54 -05:00
|
|
|
if typeDesc.isConcrete():
|
|
|
|
if hasattr(typeDesc, 'output'):
|
|
|
|
outputType = typeDesc.output
|
|
|
|
self.output2typeNames.setdefault(outputType, [])
|
|
|
|
self.output2typeNames[outputType].append(typename)
|
|
|
|
|
|
|
|
self.permanentTypeNames = []
|
2019-12-30 00:00:16 -06:00
|
|
|
for typename, typeDesc in list(self.entTypeName2typeDesc.items()):
|
2019-11-02 17:27:54 -05:00
|
|
|
if typeDesc.isPermanent():
|
|
|
|
self.permanentTypeNames.append(typename)
|
|
|
|
|
|
|
|
self.typeName2derivedTypeNames = {}
|
2019-12-30 00:00:16 -06:00
|
|
|
for typename, typeDesc in list(self.entTypeName2typeDesc.items()):
|
2019-11-02 17:27:54 -05:00
|
|
|
typenames = []
|
2019-12-30 00:00:16 -06:00
|
|
|
for tn, td in list(self.entTypeName2typeDesc.items()):
|
2019-11-02 17:27:54 -05:00
|
|
|
if td.isConcrete():
|
|
|
|
if issubclass(td.__class__, typeDesc.__class__):
|
|
|
|
typenames.append(tn)
|
|
|
|
|
|
|
|
self.typeName2derivedTypeNames[typename] = typenames
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
def getAllTypeNames(self):
|
2019-12-30 00:00:16 -06:00
|
|
|
return list(self.entTypeName2typeDesc.keys())
|
2019-11-02 17:27:54 -05:00
|
|
|
|
|
|
|
def getTypeDesc(self, entTypeName):
|
|
|
|
return self.entTypeName2typeDesc[entTypeName]
|
|
|
|
|
|
|
|
def getTypeNamesFromOutputType(self, outputType):
|
|
|
|
return self.output2typeNames.get(outputType, [])
|
|
|
|
|
|
|
|
def getDerivedTypeNames(self, entTypeName):
|
|
|
|
return self.typeName2derivedTypeNames[entTypeName]
|
|
|
|
|
|
|
|
def isDerivedAndBase(self, entType, baseEntType):
|
|
|
|
return entType in self.getDerivedTypeNames(baseEntType)
|
|
|
|
|
|
|
|
def getPermanentTypeNames(self):
|
|
|
|
return self.permanentTypeNames
|
|
|
|
|
|
|
|
def getHashStr(self):
|
|
|
|
return self.hashStr
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash(repr(self))
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return str(self.entTypeName2typeDesc)
|