home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 April / enter-2004-04.iso / files / EVE_1424_100181.exe / tripickle.py < prev    next >
Encoding:
Text File  |  2004-04-20  |  2.0 KB  |  75 lines

  1. import copy_reg
  2. import blue
  3.  
  4. reducers = {}
  5. constructors = {}
  6. def RegisterReducer(typename,reducer):
  7.     reducers[typename] = reducer
  8.  
  9. def RegisterConstructor(typename,constructor):
  10.     constructors[typename] = constructor
  11.  
  12. def ConstructBlueObject(blueObClass,args):
  13.     if constructors.has_key(blueObClass):
  14.         return apply(constructors[blueObClass],args)
  15.     else:
  16.         memStream = blue.os.CreateInstance("blue.MemStream")
  17.         buff = args
  18.         memStream.Write(buff)
  19.         memStream.Seek(0)
  20.         rot = blue.os.CreateInstance("blue.Rot")
  21.         return rot.GetCopy(memStream)
  22.  
  23.  
  24. def ReduceBlueObject(ob):
  25.     blueObClass = ob.__typename__
  26.     if reducers.has_key(blueObClass):
  27.         return ConstructBlueObject, (blueObClass,) + (reducers[blueObClass](ob),)
  28.     else:
  29.         ret = blue.os.CreateInstance("blue.MemStream")
  30.         ob.SaveTo(ret)
  31.         ret.Seek(0)
  32.         return ConstructBlueObject, (blueObClass,) + (str(ret.Read(ret.size)),)
  33.  
  34. def ReduceTriVector(vec):
  35.     return (vec.x,vec.y,vec.z)
  36. RegisterReducer("TriVector",ReduceTriVector)
  37.  
  38. def ReduceTriQuaternion(q):
  39.     return (q.x,q.y,q.z,q.w)
  40. RegisterReducer("TriQuaternion",ReduceTriQuaternion)
  41.  
  42.  
  43. ##def ConstructTriVector(tuple):
  44. ##    import trinity
  45. ##    return trinity.TriVector(tuple)
  46. try:
  47.     import blue
  48.     test = blue.os.CreateInstance("trinity.")
  49. except:
  50.     pass
  51. import trinity
  52. RegisterConstructor("TriVector",trinity.TriVector)
  53. RegisterConstructor("TriQuaternion",trinity.TriQuaternion)
  54.  
  55. copy_reg.pickle(type(blue.os),ReduceBlueObject,ConstructBlueObject)
  56.  
  57. ##def Test():
  58. ##    import trinity
  59. ##    v = trinity.TriVector(3,2,1)
  60. ##    print 'v = ',v
  61. ##    import cPickle
  62. ##    s = cPickle.dumps(v)
  63. ##    print 'dumps(v) = ',s
  64. ##    l = cPickle.loads(s)
  65. ##    print 'loads(dumps(v)) = ',l
  66. ##    q = trinity.TriQuaternion(1,3,2,1)
  67. ##    print 'q = ',q
  68. ##    s = cPickle.dumps(q)
  69. ##    print 'dumps(q) = ',s
  70. ##    l = cPickle.loads(s)
  71. ##    print 'loads(dumps(q)) = ',l
  72. ##
  73.  
  74.  
  75.