root/projects/2008/pyogp/pyogp.lib.base/trunk/pyogp/lib/base/message/data_packer.py

Revision 2493, 3.8 kB (checked in by joshua.linden, 5 months ago)

* Use the PCode enum instead of magic numbers
* Watch out for "None" values when applying ObjectUpdates? to agent
* Add AgentDynamicsUpdate? app event
* Handle 48-, 32-, and 16-byte ObjectUpdates?
* Add basic sit/stand/fly methods to agent, with associated enums

Not reviewed

Line 
1 # standrad python libraries
2 import struct
3
4 # pyogp
5 from pyogp.lib.base.datatypes import UUID, Vector3, Quaternion
6
7 # pyogp messaging
8 from types import MsgType, EndianType
9 from pyogp.lib.base.utilities.helpers import Helpers
10
11 class DataPacker(object):
12     def __init__(self):
13         self.packer = {}
14         self.packer[MsgType.MVT_FIXED]          = ('>',self.__pack_string)  #:DE 23oct2008 added handler for MVT_FIXED
15         self.packer[MsgType.MVT_VARIABLE]       = ('>',self.__pack_string)
16         self.packer[MsgType.MVT_S8]             = ('>','b')
17         self.packer[MsgType.MVT_U8]             = ('>','B')
18         self.packer[MsgType.MVT_BOOL]           = ('>','B')
19         self.packer[MsgType.MVT_LLUUID]         = ('>',self.__pack_uuid)
20         self.packer[MsgType.MVT_IP_ADDR]        = ('>',self.__pack_string)
21         self.packer[MsgType.MVT_IP_PORT]        = ('>','H')
22         self.packer[MsgType.MVT_U16]            = ('<','H')
23         self.packer[MsgType.MVT_U32]            = ('<','I')
24         self.packer[MsgType.MVT_U64]            = ('<','Q')
25         self.packer[MsgType.MVT_S16]            = ('<','h')
26         self.packer[MsgType.MVT_S32]            = ('<','i')
27         self.packer[MsgType.MVT_S64]            = ('<','q')
28         self.packer[MsgType.MVT_F32]            = ('<','f')
29         self.packer[MsgType.MVT_F64]            = ('<','d')
30         self.packer[MsgType.MVT_LLVector3]      = ('<',self.__pack_vector3)
31         self.packer[MsgType.MVT_LLVector3d]     = ('<',self.__pack_vector3d)
32         self.packer[MsgType.MVT_LLVector4]      = ('<',self.__pack_vector4)
33         self.packer[MsgType.MVT_LLQuaternion]   = ('<',self.__pack_quat)
34
35     def pack_data(self, data, data_type, endian_type=EndianType.NONE):
36         if data_type in self.packer:
37             endian, pack = self.packer[data_type]
38
39             #override endian           
40             if endian_type != EndianType.NONE:
41                 endian = endian_type
42
43             if callable(pack):
44                 return pack(endian, data)
45             else:
46                 return struct.pack(endian + pack, data)
47
48         return None
49
50     def __pack_tuple(self, endian, tup, tp):
51         size = len(tup)
52         return struct.pack(endian + str(size) + tp, *tup)
53
54     def __pack_vector3(self, endian, vec):       
55         if isinstance(vec, Vector3):
56             vec = vec() # convert to tuple
57         return self.__pack_tuple(endian, vec, 'f')
58
59     def __pack_vector3d(self, endian, vec):
60         return self.__pack_tuple(endian, vec, 'd')
61
62     def __pack_vector4(self, endian, vec):
63         return self.__pack_tuple(endian, vec, 'f')
64
65     def __pack_quat(self, endian, quat):
66         if isinstance(quat, Quaternion):
67             quat = quat() # convert to tuple
68         vec = Helpers.pack_quaternion_to_vector3(quat)
69         return self.__pack_tuple(endian, vec, 'f')
70
71     def __pack_uuid(self, endian, uuid):
72
73         if isinstance(uuid, UUID):
74             return uuid.get_bytes()
75         else:
76             return uuid.bytes
77
78     def __pack_string(self, endian, pack_string):
79         """Return the string UTF-8 encoded and null terminated."""
80         if pack_string == None:
81             return '\x00'
82         elif isinstance(pack_string,unicode):
83             return pack_string.encode('utf-8') + '\x00'
84         else:
85             return pack_string + '\x00'
86
87 """
88 Contributors can be viewed at:
89 http://svn.secondlife.com/svn/linden/projects/2008/pyogp/CONTRIBUTORS.txt
90
91 $LicenseInfo:firstyear=2008&license=apachev2$
92
93 Copyright 2009, Linden Research, Inc.
94
95 Licensed under the Apache License, Version 2.0 (the "License").
96 You may obtain a copy of the License at:
97     http://www.apache.org/licenses/LICENSE-2.0
98 or in
99     http://svn.secondlife.com/svn/linden/projects/2008/pyogp/LICENSE.txt
100
101 $/LicenseInfo$
102 """
103
Note: See TracBrowser for help on using the browser.