diff --git a/direct/src/showbase/EventManager.py b/direct/src/showbase/EventManager.py index 7bbb915915..14465120cb 100644 --- a/direct/src/showbase/EventManager.py +++ b/direct/src/showbase/EventManager.py @@ -76,6 +76,8 @@ class EventManager: return eventParameter.getStringValue() elif eventParameter.isWstring(): return eventParameter.getWstringValue() + elif hasattr(eventParameter, 'isBytes') and eventParameter.isBytes(): + return eventParameter.getBytesValue() elif eventParameter.isTypedRefCount(): return eventParameter.getTypedRefCountValue() elif eventParameter.isEmpty(): diff --git a/dtool/src/dtoolbase/pvector.h b/dtool/src/dtoolbase/pvector.h index 12e7c108ef..f8dcb21071 100644 --- a/dtool/src/dtoolbase/pvector.h +++ b/dtool/src/dtoolbase/pvector.h @@ -46,15 +46,15 @@ public: typedef std::vector base_class; typedef typename base_class::size_type size_type; - explicit pvector(TypeHandle type_handle = pvector_type_handle) : base_class(allocator(type_handle)) { } + explicit pvector(TypeHandle type_handle = get_type_handle(pvector)) : base_class(allocator(type_handle)) { } pvector(const pvector ©) : base_class(copy) { } pvector(pvector &&from) noexcept : base_class(std::move(from)) {}; - explicit pvector(size_type n, TypeHandle type_handle = pvector_type_handle) : base_class(n, Type(), allocator(type_handle)) { } - explicit pvector(size_type n, const Type &value, TypeHandle type_handle = pvector_type_handle) : base_class(n, value, allocator(type_handle)) { } - pvector(const Type *begin, const Type *end, TypeHandle type_handle = pvector_type_handle) : base_class(allocator(type_handle)) { + explicit pvector(size_type n, TypeHandle type_handle = get_type_handle(pvector)) : base_class(n, Type(), allocator(type_handle)) { } + explicit pvector(size_type n, const Type &value, TypeHandle type_handle = get_type_handle(pvector)) : base_class(n, value, allocator(type_handle)) { } + pvector(const Type *begin, const Type *end, TypeHandle type_handle = get_type_handle(pvector)) : base_class(allocator(type_handle)) { this->insert(this->end(), begin, end); } - pvector(std::initializer_list init, TypeHandle type_handle = pvector_type_handle) : base_class(std::move(init), allocator(type_handle)) { } + pvector(std::initializer_list init, TypeHandle type_handle = get_type_handle(pvector)) : base_class(std::move(init), allocator(type_handle)) { } pvector &operator =(const pvector ©) { base_class::operator =(copy); diff --git a/dtool/src/dtoolbase/register_type.cxx b/dtool/src/dtoolbase/register_type.cxx index b67475e4f6..649c4ebe11 100644 --- a/dtool/src/dtoolbase/register_type.cxx +++ b/dtool/src/dtoolbase/register_type.cxx @@ -26,6 +26,7 @@ TypeHandle double_type_handle; TypeHandle float_type_handle; TypeHandle string_type_handle; TypeHandle wstring_type_handle; +TypeHandle vector_uchar_type_handle; TypeHandle long_p_type_handle; TypeHandle int_p_type_handle; @@ -59,6 +60,7 @@ void init_system_type_handles() { register_type(float_type_handle, "float"); register_type(string_type_handle, "string"); register_type(wstring_type_handle, "wstring"); + register_type(vector_uchar_type_handle, "vector_uchar"); register_type(int_p_type_handle, "int*"); register_type(short_p_type_handle, "short*"); diff --git a/dtool/src/dtoolbase/register_type.h b/dtool/src/dtoolbase/register_type.h index a15cc9ddb1..95b2c48548 100644 --- a/dtool/src/dtoolbase/register_type.h +++ b/dtool/src/dtoolbase/register_type.h @@ -19,6 +19,9 @@ #include "typeHandle.h" #include "typeRegistry.h" +template +class pvector; + /** * This inline function is just a convenient way to call * TypeRegistry::register_type(), along with zero to four @@ -88,6 +91,7 @@ extern TypeHandle EXPCL_DTOOL_DTOOLBASE double_type_handle; extern TypeHandle EXPCL_DTOOL_DTOOLBASE float_type_handle; extern TypeHandle EXPCL_DTOOL_DTOOLBASE string_type_handle; extern TypeHandle EXPCL_DTOOL_DTOOLBASE wstring_type_handle; +extern TypeHandle EXPCL_DTOOL_DTOOLBASE vector_uchar_type_handle; extern TypeHandle long_p_type_handle; extern TypeHandle int_p_type_handle; @@ -175,6 +179,16 @@ INLINE TypeHandle _get_type_handle(const std::wstring *) { return wstring_type_handle; } +template +INLINE TypeHandle _get_type_handle(const pvector *) { + return pvector_type_handle; +} + +template<> +INLINE TypeHandle _get_type_handle(const pvector *) { + return vector_uchar_type_handle; +} + template<> INLINE TypeHandle _get_type_handle(const long * const *) { return long_p_type_handle; diff --git a/dtool/src/dtoolutil/vector_uchar.cxx b/dtool/src/dtoolutil/vector_uchar.cxx index 78750619bf..2087d20587 100644 --- a/dtool/src/dtoolutil/vector_uchar.cxx +++ b/dtool/src/dtoolutil/vector_uchar.cxx @@ -19,3 +19,25 @@ #define NAME vector_uchar #include "vector_src.cxx" + +/** + * Writes a hex representation of the bytes to the output stream. + */ +std::ostream & +operator << (std::ostream &out, const vector_uchar &data) { + static const char nibble_to_hex[] = "0123456789abcdef"; + + if (data.empty()) { + return out; + } + + out << nibble_to_hex[(data[0] & 0xf0) >> 4]; + out << nibble_to_hex[data[0] & 0xf]; + + for (size_t i = 1; i < data.size(); ++i) { + out << ' '; + out << nibble_to_hex[(data[i] & 0xf0) >> 4]; + out << nibble_to_hex[data[i] & 0xf]; + } + return out; +} diff --git a/dtool/src/dtoolutil/vector_uchar.h b/dtool/src/dtoolutil/vector_uchar.h index 3700b4ba8d..e04fd89289 100644 --- a/dtool/src/dtoolutil/vector_uchar.h +++ b/dtool/src/dtoolutil/vector_uchar.h @@ -30,4 +30,7 @@ #include "vector_src.h" +EXPCL_DTOOL_DTOOLUTIL std::ostream & +operator << (std::ostream &out, const vector_uchar &data); + #endif diff --git a/panda/src/event/eventParameter.I b/panda/src/event/eventParameter.I index 1d3d168344..61cbacb0b8 100644 --- a/panda/src/event/eventParameter.I +++ b/panda/src/event/eventParameter.I @@ -63,6 +63,11 @@ EventParameter(const std::string &value) : _ptr(new EventStoreString(value)) { } INLINE EventParameter:: EventParameter(const std::wstring &value) : _ptr(new EventStoreWstring(value)) { } +/** + * Defines an EventParameter that stores a bytes value. + */ +INLINE EventParameter:: +EventParameter(const vector_uchar &value) : _ptr(new ParamBytes(value)) { } /** * @@ -185,6 +190,28 @@ get_wstring_value() const { return ((const EventStoreWstring *)_ptr.p())->get_value(); } +/** + * Returns true if the EventParameter stores a binary bytes value, false + * otherwise. + */ +INLINE bool EventParameter:: +is_bytes() const { + if (is_empty()) { + return false; + } + return _ptr->is_of_type(ParamBytes::get_class_type()); +} + +/** + * Retrieves the value stored in the EventParameter. It is only valid to call + * this if is_bytes() has already returned true. + */ +INLINE vector_uchar EventParameter:: +get_bytes_value() const { + nassertr(is_bytes(), vector_uchar()); + return ((const ParamBytes *)_ptr.p())->get_value(); +} + /** * Returns true if the EventParameter stores a TypedReferenceCount pointer, * false otherwise. Note that a TypedReferenceCount is not exactly the same diff --git a/panda/src/event/eventParameter.h b/panda/src/event/eventParameter.h index 44e891e3c9..7994796b75 100644 --- a/panda/src/event/eventParameter.h +++ b/panda/src/event/eventParameter.h @@ -41,6 +41,7 @@ PUBLISHED: INLINE EventParameter(double value); INLINE EventParameter(const std::string &value); INLINE EventParameter(const std::wstring &value); + INLINE EventParameter(const vector_uchar &value); INLINE EventParameter(const EventParameter ©); INLINE EventParameter &operator = (const EventParameter ©); @@ -59,6 +60,8 @@ PUBLISHED: INLINE std::string get_string_value() const; INLINE bool is_wstring() const; INLINE std::wstring get_wstring_value() const; + INLINE bool is_bytes() const; + INLINE vector_uchar get_bytes_value() const; INLINE bool is_typed_ref_count() const; INLINE TypedReferenceCount *get_typed_ref_count_value() const; diff --git a/panda/src/putil/config_putil.cxx b/panda/src/putil/config_putil.cxx index 9849cd5b84..c9e8e4b9b1 100644 --- a/panda/src/putil/config_putil.cxx +++ b/panda/src/putil/config_putil.cxx @@ -212,6 +212,7 @@ init_libputil() { FactoryParam::init_type(); Namable::init_type(); NodeCachedReferenceCount::init_type(); + ParamBytes::init_type("ParamBytes"); ParamMatrix3d::init_type("ParamMatrix3d"); ParamMatrix3f::init_type("ParamMatrix3f"); ParamMatrix4d::init_type("ParamMatrix4d"); diff --git a/panda/src/putil/paramValue.cxx b/panda/src/putil/paramValue.cxx index 5ffcf2807c..88add8a9c0 100644 --- a/panda/src/putil/paramValue.cxx +++ b/panda/src/putil/paramValue.cxx @@ -16,6 +16,7 @@ template class ParamValue; template class ParamValue; +template class ParamValue; template class ParamValue; template class ParamValue; diff --git a/panda/src/putil/paramValue.h b/panda/src/putil/paramValue.h index f7e4f207a1..f98e76d3f1 100644 --- a/panda/src/putil/paramValue.h +++ b/panda/src/putil/paramValue.h @@ -22,6 +22,7 @@ #include "bamReader.h" #include "bamWriter.h" #include "luse.h" +#include "vector_uchar.h" /** * A non-template base class of ParamValue (below), which serves mainly to @@ -152,6 +153,7 @@ private: EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, ParamValue); EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, ParamValue); +EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, ParamValue); EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, ParamValue); EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, ParamValue); @@ -173,6 +175,7 @@ EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, ParamValue ParamString; typedef ParamValue ParamWstring; +typedef ParamValue ParamBytes; typedef ParamValue ParamVecBase2d; typedef ParamValue ParamVecBase2f;