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