DistributedObjectGlobalUD.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from .DistributedObjectUD import DistributedObjectUD
  2. from direct.directnotify.DirectNotifyGlobal import directNotify
  3. import sys
  4. class DistributedObjectGlobalUD(DistributedObjectUD):
  5. notify = directNotify.newCategory('DistributedObjectGlobalUD')
  6. doNotDeallocateChannel = 1
  7. isGlobalDistObj = 1
  8. def __init__(self, air):
  9. DistributedObjectUD.__init__(self, air)
  10. self.ExecNamespace = {"self":self}
  11. def announceGenerate(self):
  12. self.air.registerForChannel(self.doId)
  13. DistributedObjectUD.announceGenerate(self)
  14. def delete(self):
  15. self.air.unregisterForChannel(self.doId)
  16. ## self.air.removeDOFromTables(self)
  17. DistributedObjectUD.delete(self)
  18. def execCommand(self, command, mwMgrId, avId, zoneId):
  19. text = str(self.__execMessage(command))[:config.GetInt("ai-debug-length",300)]
  20. self.notify.info(text)
  21. def __execMessage(self, message):
  22. if not self.ExecNamespace:
  23. # Import some useful variables into the ExecNamespace initially.
  24. import panda3d.core
  25. for key, value in panda3d.core.__dict__.items():
  26. if not key.startswith('__'):
  27. self.ExecNamespace[key] = value
  28. #self.importExecNamespace()
  29. # Now try to evaluate the expression using ChatInputNormal.ExecNamespace as
  30. # the local namespace.
  31. try:
  32. return str(eval(message, globals(), self.ExecNamespace))
  33. except SyntaxError:
  34. # Maybe it's only a statement, like "x = 1", or
  35. # "import math". These aren't expressions, so eval()
  36. # fails, but they can be exec'ed.
  37. try:
  38. exec(message, globals(), self.ExecNamespace)
  39. return 'ok'
  40. except:
  41. exception = sys.exc_info()[0]
  42. extraInfo = sys.exc_info()[1]
  43. if extraInfo:
  44. return str(extraInfo)
  45. else:
  46. return str(exception)
  47. except:
  48. exception = sys.exc_info()[0]
  49. extraInfo = sys.exc_info()[1]
  50. if extraInfo:
  51. return str(extraInfo)
  52. else:
  53. return str(exception)