DistributedObject.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. """DistributedObject module: contains the DistributedObject class"""
  2. from PandaObject import *
  3. from DirectNotifyGlobal import *
  4. # Values for DistributedObject.activeState
  5. ESNew = 1
  6. ESDeleted = 2
  7. ESDisabling = 3
  8. ESDisabled = 4 # values here and lower are considered "disabled"
  9. ESGenerating = 5 # values here and greater are considered "generated"
  10. ESGenerated = 6
  11. class DistributedObject(PandaObject):
  12. """Distributed Object class:"""
  13. notify = directNotify.newCategory("DistributedObject")
  14. # A few objects will set neverDisable to 1... Examples are
  15. # localToon, and anything that lives in the UberZone. This
  16. # keeps them from being disabled when you change zones,
  17. # even to the quiet zone.
  18. neverDisable = 0
  19. def __init__(self, cr):
  20. try:
  21. self.DistributedObject_initialized
  22. except:
  23. self.DistributedObject_initialized = 1
  24. self.cr = cr
  25. # Most DistributedObjects are simple and require no real
  26. # effort to load. Some, particularly actors, may take
  27. # some significant time to load; these we can optimize by
  28. # caching them when they go away instead of necessarily
  29. # deleting them. The object should set cacheable to 1 if
  30. # it needs to be optimized in this way.
  31. self.setCacheable(0)
  32. # This count tells whether the object can be deleted right away,
  33. # or not.
  34. self.delayDeleteCount = 0
  35. # This flag tells whether a delete has been requested on this
  36. # object.
  37. self.deleteImminent = 0
  38. # Keep track of our state as a distributed object. This
  39. # is only trustworthy if the inheriting class properly
  40. # calls up the chain for disable() and generate().
  41. self.activeState = ESNew
  42. return None
  43. #def __del__(self):
  44. # """
  45. # For debugging purposes, this just prints out what got deleted
  46. # """
  47. # DistributedObject.notify.debug("Destructing: " + self.__class__.__name__ +
  48. # " id: " + str(self.doId))
  49. # PandaObject.__del__(self)
  50. def setNeverDisable(self, bool):
  51. assert((bool == 1) or (bool == 0))
  52. self.neverDisable = bool
  53. return None
  54. def getNeverDisable(self):
  55. return self.neverDisable
  56. def setCacheable(self, bool):
  57. assert((bool == 1) or (bool == 0))
  58. self.cacheable = bool
  59. return None
  60. def getCacheable(self):
  61. return self.cacheable
  62. def deleteOrDelay(self):
  63. if self.delayDeleteCount > 0:
  64. self.deleteImminent = 1
  65. else:
  66. self.disableAnnounceAndDelete()
  67. return None
  68. def delayDelete(self, flag):
  69. # Flag should be 0 or 1, meaning increment or decrement count
  70. # Also see DelayDelete.py
  71. if (flag == 1):
  72. self.delayDeleteCount += 1
  73. elif (flag == 0):
  74. self.delayDeleteCount -= 1
  75. else:
  76. self.notify.error("Invalid flag passed to delayDelete: " + str(flag))
  77. if (self.delayDeleteCount < 0):
  78. self.notify.error("Somebody decremented delayDelete for doId %s without incrementing"
  79. % (self.doId))
  80. elif (self.delayDeleteCount == 0):
  81. self.notify.debug("delayDeleteCount for doId %s now 0" % (self.doId))
  82. if self.deleteImminent:
  83. self.notify.debug("delayDeleteCount for doId %s -- deleteImminent"
  84. % (self.doId))
  85. self.disableAnnounceAndDelete()
  86. else:
  87. self.notify.debug("delayDeleteCount for doId %s now %s"
  88. % (self.doId, self.delayDeleteCount))
  89. # Return the count just for kicks
  90. return self.delayDeleteCount
  91. def disableAnnounceAndDelete(self):
  92. self.disableAndAnnounce()
  93. self.delete()
  94. return None
  95. def disableAndAnnounce(self):
  96. """disableAndAnnounce(self)
  97. Inheritors should *not* redefine this function.
  98. """
  99. # We must send the disable announce message *before* we
  100. # actually disable the object. That way, the various cleanup
  101. # tasks can run first and take care of restoring the object to
  102. # a normal, nondisabled state; and *then* the disable function
  103. # can properly disable it (for instance, by parenting it to
  104. # hidden).
  105. self.activeState = ESDisabling
  106. messenger.send(self.uniqueName("disable"))
  107. self.disable()
  108. return None
  109. def announceGenerate(self):
  110. """announceGenerate(self)
  111. Sends a message to the world after the object has been
  112. generated and all of its required fields filled in.
  113. """
  114. self.activeState = ESGenerated
  115. messenger.send(self.uniqueName("generate"), [self])
  116. def disable(self):
  117. """disable(self)
  118. Inheritors should redefine this to take appropriate action on disable
  119. """
  120. self.activeState = ESDisabled
  121. def isDisabled(self):
  122. """isDisabled(self)
  123. Returns true if the object has been disabled and/or deleted,
  124. or if it is brand new and hasn't yet been generated.
  125. """
  126. return (self.activeState < ESGenerating)
  127. def delete(self):
  128. """delete(self)
  129. Inheritors should redefine this to take appropriate action on delete
  130. """
  131. try:
  132. self.DistributedObject_deleted
  133. except:
  134. self.DistributedObject_deleted = 1
  135. del self.cr
  136. return
  137. def generate(self):
  138. """generate(self)
  139. Inheritors should redefine this to take appropriate action on generate
  140. """
  141. self.activeState = ESGenerating
  142. def generateInit(self):
  143. """generateInit(self)
  144. This method is called when the DistributedObject is first introduced
  145. to the world... Not when it is pulled from the cache.
  146. """
  147. self.activeState = ESGenerating
  148. def getDoId(self):
  149. """getDoId(self)
  150. Return the distributed object id
  151. """
  152. return self.doId
  153. def updateRequiredFields(self, cdc, di):
  154. for i in cdc.broadcastRequiredCDU:
  155. i.updateField(cdc, self, di)
  156. def updateAllRequiredFields(self, cdc, di):
  157. for i in cdc.allRequiredCDU:
  158. i.updateField(cdc, self, di)
  159. def updateRequiredOtherFields(self, cdc, di):
  160. # First, update the required fields
  161. for i in cdc.broadcastRequiredCDU:
  162. i.updateField(cdc, self, di)
  163. # Determine how many other fields there are
  164. numberOfOtherFields = di.getArg(STUint16)
  165. # Update each of the other fields
  166. for i in range(numberOfOtherFields):
  167. cdc.updateField(self, di)
  168. return None
  169. def sendUpdate(self, fieldName, args = [], sendToId = None):
  170. self.cr.sendUpdate(self, fieldName, args, sendToId)
  171. def taskName(self, taskString):
  172. return (taskString + "-" + str(self.getDoId()))
  173. def uniqueName(self, idString):
  174. return (idString + "-" + str(self.getDoId()))
  175. def isLocal(self):
  176. # This returns true if the distributed object is "local,"
  177. # which means the client created it instead of the AI, and it
  178. # gets some other special handling. Normally, only the local
  179. # avatar class overrides this to return true.
  180. return 0