Example #1
0
    def entity_property(
            self, type_name, offset, networked,
            true_value=None, false_value=None):
        """Entity property."""
        native_type = Type.is_native(type_name)

        def fget(ptr):
            """Return the property value."""
            # Is the property a native type?
            if native_type:

                # Retrieve the value
                value = getattr(ptr, 'get_' + type_name)(offset)

            # Is the property not a native type?
            else:

                # Retrieve the value
                value = self.convert(type_name, ptr + offset)

            # Does the property not have True/False values?
            if true_value is None:

                # Return the value
                return value

            # Return whether the value equals the True value
            return value == true_value

        def fset(ptr, value):
            """Set the property value and notify if networked."""
            # Does the property have True/False values?
            if true_value is not None:

                # Get the proper value to set
                value = true_value if value else false_value

            # Is the property a native type?
            if native_type:

                # Set the property
                getattr(ptr, 'set_' + type_name)(value, offset)

            # Is the property not a native type?
            else:

                # Get the class to set the property as
                cls = self.get_class(type_name)

                # Set the property
                get_object_pointer(value).copy(
                    ptr + offset, cls._size)

            # Is the property networked?
            if networked:

                # Notify the change of state
                edict_from_pointer(ptr).state_changed()

        return property(fget, fset)
Example #2
0
    def instance_attribute(self, type_name, offset, doc=None):
        """Create a wrapper for an instance attribute.

        Examples:
            Vector vecVal;
            bool bVal;
        """
        native_type = Type.is_native(type_name)

        def fget(ptr):
            """Return the instance attribute value."""
            # Handle custom type
            if not native_type:
                return self.convert(type_name, ptr + offset)

            # Handle native type
            return getattr(ptr, 'get_' + type_name)(offset)

        def fset(ptr, value):
            """Set the instance attribute value."""
            # Handle custom type
            if not native_type:
                cls = self.get_class(type_name)
                if cls is None:
                    raise NameError('Unknown class "{0}".'.format(type_name))

                get_object_pointer(value).copy(ptr + offset, cls._size)

            # Handle native type
            else:
                getattr(ptr, 'set_' + type_name)(value, offset)

        return property(fget, fset, None, doc)
    def _get_property(self, prop_name, prop_type):
        """Return the value of the given property name.

        :param str prop_name:
            The name of the property.
        :param SendPropType prop_type:
            The type of the property.
        """
        # Is the given property not valid?
        if prop_name not in self.template.properties:

            # Raise an exception...
            raise NameError(
                '"{}" is not a valid property for temp entity "{}".'.format(
                    prop_name, self.name))

        # Get the property data...
        prop, offset, type_name = self.template.properties[prop_name]

        # Are the prop types matching?
        if prop.type != prop_type:

            # Raise an exception...
            raise TypeError('"{}" is not of type "{}".'.format(
                prop_name, prop_type))

        # Is the property an array?
        if prop_type == SendPropType.ARRAY:

            # Return an array instance...
            return Array(manager, False, type_name, get_object_pointer(
                self) + offset, prop.length)

        # Is the given type not supported?
        if prop_type not in _supported_property_types:

            # Raise an exception...
            raise TypeError('"{}" is not supported.'.format(prop_type))

        # Is the type native?
        if Type.is_native(type_name):

            # Return the value...
            return getattr(
                get_object_pointer(self), 'get_' + type_name)(offset)

        # Otherwise
        else:

            # Make the object and return it...
            return make_object(
                manager.get_class(type_name),
                get_object_pointer(self) + offset)

        # Raise an exception...
        raise ValueError('Unable to get the value of "{}".'.format(prop_name))
Example #4
0
    def _get_property(self, prop_name, prop_type):
        """Return the value of the given property name.

        :param str prop_name:
            The name of the property.
        :param SendPropType prop_type:
            The type of the property.
        """
        # Is the given property not valid?
        if prop_name not in self.template.properties:

            # Raise an exception...
            raise NameError(
                '"{}" is not a valid property for temp entity "{}".'.format(
                    prop_name, self.name))

        # Get the property data...
        prop, offset, type_name = self.template.properties[prop_name]

        # Are the prop types matching?
        if prop.type != prop_type:

            # Raise an exception...
            raise TypeError('"{}" is not of type "{}".'.format(
                prop_name, prop_type))

        # Is the property an array?
        if prop_type == SendPropType.ARRAY:

            # Return an array instance...
            return Array(manager, False, type_name,
                         get_object_pointer(self) + offset, prop.length)

        # Is the given type not supported?
        if prop_type not in _supported_property_types:

            # Raise an exception...
            raise TypeError('"{}" is not supported.'.format(prop_type))

        # Is the type native?
        if Type.is_native(type_name):

            # Return the value...
            return getattr(get_object_pointer(self),
                           'get_' + type_name)(offset)

        # Otherwise
        else:

            # Make the object and return it...
            return make_object(manager.get_class(type_name),
                               get_object_pointer(self) + offset)

        # Raise an exception...
        raise ValueError('Unable to get the value of "{}".'.format(prop_name))
Example #5
0
    def pointer_attribute(self, type_name, offset, doc=None):
        """Create a wrapper for a pointer attribute.

        Examples:
            Vector* pVec;
            bool* pBool;
        """
        native_type = Type.is_native(type_name)

        def fget(ptr):
            """Get the pointer attribute value."""
            # Get the base address of the pointer. We are now on
            # "instance level"
            ptr = ptr.get_pointer(offset)

            # Handle custom type
            if not native_type:
                return self.convert(type_name, ptr)

            # Handle native type
            return getattr(ptr, 'get_' + type_name)()

        def fset(ptr, value):
            """Set the pointer attribute value."""
            # Handle custom type
            if not native_type:
                # Set the pointer
                ptr.set_pointer(value, offset)

                # Make sure the value will not deallocate as long as it is
                # part of this object
                ptr._pointer_values[offset] = value

            # Handle native type
            else:
                # Go down to "instance level"
                instance_ptr = ptr.get_pointer(offset)

                # Is there no space allocated?
                if not instance_ptr:
                    # Allocate space for the value
                    instance_ptr = alloc(TYPE_SIZES[type_name.upper()])

                    # Add the pointer to the set, so there will be a reference
                    # until the instance gets deleted
                    ptr._allocated_pointers.add(instance_ptr)

                    # Set the pointer
                    ptr.set_pointer(instance_ptr, offset)

                # Set the value
                getattr(instance_ptr, 'set_' + type_name)(value)

        return property(fget, fset, None, doc)
Example #6
0
    def pointer_attribute(self, type_name, offset, doc=None):
        """Create a wrapper for a pointer attribute.

        Examples:
            Vector* pVec;
            bool* pBool;
        """
        native_type = Type.is_native(type_name)

        def fget(ptr):
            """Get the pointer attribute value."""
            # Get the base address of the pointer. We are now on
            # "instance level"
            ptr = ptr.get_pointer(offset)

            # Handle custom type
            if not native_type:
                return self.convert(type_name, ptr)

            # Handle native type
            return getattr(ptr, 'get_' + type_name)()

        def fset(ptr, value):
            """Set the pointer attribute value."""
            # Handle custom type
            if not native_type:
                ptr.set_pointer(value)

                # Make sure the value will not deallocate as long as it is
                # part of this object
                ptr._pointer_values[offset] = value

            # Handle native type
            else:
                # Go down to "instance level"
                instance_ptr = ptr.get_pointer(offset)

                # Is there no space allocated?
                if not instance_ptr:
                    # Allocate space for the value
                    instance_ptr = alloc(TYPE_SIZES[type_name.upper()])

                    # Add the pointer to the set, so there will be a reference
                    # until the instance gets deleted
                    ptr._allocated_pointers.add(instance_ptr)

                    # Set the pointer
                    ptr.set_pointer(instance_ptr, offset)

                # Set the value
                getattr(instance_ptr, 'set_' + type_name)(value)

        return property(fget, fset, None, doc)
Example #7
0
    def entity_property(self, type_name, offset, networked):
        """Entity property."""
        native_type = Type.is_native(type_name)

        def fget(ptr):
            """Return the property value."""
            # Is the property a native type?
            if native_type:

                value = getattr(ptr, 'get_' + type_name)(offset)

                # Does the value need cast to an integer?
                if type_name == 'char':
                    if value == '\x00':
                        return 0
                    return int(value)
                return value

            # Return the value
            return self.convert(type_name, ptr + offset)

        def fset(ptr, value):
            """Set the property value and notify if networked."""
            # Is the property a native type?
            if native_type:

                # Does the value need cast to a string?
                if type_name == 'char':
                    if not value:
                        value = '\x00'
                    value = str(value)

                # Set the property
                getattr(ptr, 'set_' + type_name)(value, offset)

            # Is the property not a native type?
            else:

                # Get the class to set the property as
                cls = self.get_class(type_name)

                # Set the property
                get_object_pointer(value).copy(
                    ptr + offset, cls._size)

            # Is the property networked?
            if networked:

                # Notify the change of state
                edict_from_pointer(ptr).state_changed()

        return property(fget, fset)
Example #8
0
    def entity_property(self, type_name, offset, networked):
        """Entity property."""
        native_type = Type.is_native(type_name)

        def fget(ptr):
            """Return the property value."""
            # Is the property a native type?
            if native_type:

                value = getattr(ptr, 'get_' + type_name)(offset)

                # Does the value need cast to an integer?
                if type_name == 'char':
                    if value == '\x00':
                        return 0
                    return int(value)
                return value

            # Return the value
            return self.convert(type_name, ptr + offset)

        def fset(ptr, value):
            """Set the property value and notify if networked."""
            # Is the property a native type?
            if native_type:

                # Does the value need cast to a string?
                if type_name == 'char':
                    if not value:
                        value = '\x00'
                    value = str(value)

                # Set the property
                getattr(ptr, 'set_' + type_name)(value, offset)

            # Is the property not a native type?
            else:

                # Get the class to set the property as
                cls = self.get_class(type_name)

                # Set the property
                get_object_pointer(value).copy(ptr + offset, cls._size)

            # Is the property networked?
            if networked:

                # Notify the change of state
                edict_from_pointer(ptr).state_changed()

        return property(fget, fset)
Example #9
0
    def _get_type_size(type_name):
        """Helper method returning the size of the given type.

        :param str type_name: The name of the type.
        """
        # Is the type native?
        if Type.is_native(type_name):

            # Return the size of the type...
            return TYPE_SIZES[type_name.upper()]

        # Otherwise...
        else:

            # Get the size of the type...
            return get_size(manager.get_class(type_name))

        # Raise an exception...
        raise ValueError('"{}" is not a supported type.'.format(type_name))
Example #10
0
    def _get_type_size(type_name):
        """Helper method returning the size of the given type.

        :param str type_name: The name of the type.
        """
        # Is the type native?
        if Type.is_native(type_name):

            # Return the size of the type...
            return TYPE_SIZES[type_name.upper()]

        # Otherwise...
        else:

            # Get the size of the type...
            return get_size(manager.get_class(type_name))

        # Raise an exception...
        raise ValueError('"{}" is not a supported type.'.format(type_name))
Example #11
0
    def instance_attribute(self, type_name, offset, doc=None):
        """Create a wrapper for an instance attribute.

        Examples:
            Vector vecVal;
            bool bVal;
        """
        native_type = Type.is_native(type_name)

        def fget(ptr):
            """Return the instance attribute value."""
            # Handle custom type
            if not native_type:
                return self.convert(type_name, ptr + offset)

            # Handle native type
            return getattr(ptr, 'get_' + type_name)(offset)

        def fset(ptr, value):
            """Set the instance attribute value."""
            # Handle custom type
            if not native_type:
                cls = self.get_class(type_name)
                if cls is None:
                    raise NameError('Unknown class "{0}".'.format(type_name))

                get_object_pointer(value).copy(
                    ptr + offset,
                    cls._size
                )

            # Handle native type
            else:
                getattr(ptr, 'set_' + type_name)(value, offset)

        return property(fget, fset, None, doc)
Example #12
0
    def _set_property(self, prop_name, prop_type, value):
        """Set the given property to the given value.

        :param str prop_name: The name of the property.
        :param SendPropType prop_type: The type of the property.
        :param value object: To value to set to the given property.
        """
        # Is the given property not valid?
        if prop_name not in self.template.properties:

            # Raise an exception...
            raise NameError(
                '"{}" is not a valid property for temp entity "{}".'.format(
                    prop_name, self.name))

        # Get the property data...
        prop, offset, type_name = self.template.properties[prop_name]

        # Are the prop types matching?
        if prop.type != prop_type:

            # Raise an exception...
            raise TypeError('"{}" is not of type "{}".'.format(
                prop_name, prop_type))

        # Is the property an array?
        if prop_type == SendPropType.ARRAY:

            # Is the given value not an Array instance?
            if not isinstance(value, Array):

                # Raise an exception...
                raise TypeError('Given value is not an Array instance.')

            # Is the length not matching?
            if value._length != prop.length:

                # Raise an exception...
                raise ValueError('Given array is not of length "{}".'.format(
                    prop.length))

            # Copy the values...
            value.copy(get_object_pointer(self), self._get_type_size(
                type_name) * prop.length)

            # No need to go further...
            return

        # Otherwise, is the type native?
        elif Type.is_native(type_name):

            # Set the value of the property...
            getattr(get_object_pointer(self), 'set_{}'.format(type_name))(
                value, offset)

            # No need to go further...
            return

        # Otherwise...
        else:

            # Get the class...
            cls = manager.get_class(type_name)

            # Is the given value valid?
            if not isinstance(value, cls):

                # Raise an exception...
                raise TypeError('"{}" is not a valid "{}" value.'.format(
                    value, type_name))

            # Set the value of the property...
            get_object_pointer(value).copy(
                get_object_pointer(self) + offset, get_size(cls))

            # No need to go further...
            return

        # Raise an exception...
        raise NameError('Unable to set "{}" for the temp entity "{}".'.format(
            prop_name, self.name))
Example #13
0
    def _set_property(self, prop_name, prop_type, value):
        """Set the given property to the given value.

        :param str prop_name: The name of the property.
        :param SendPropType prop_type: The type of the property.
        :param value object: To value to set to the given property.
        """
        # Is the given property not valid?
        if prop_name not in self.template.properties:

            # Raise an exception...
            raise NameError(
                '"{}" is not a valid property for temp entity "{}".'.format(
                    prop_name, self.name))

        # Get the property data...
        prop, offset, type_name = self.template.properties[prop_name]

        # Are the prop types matching?
        if prop.type != prop_type:

            # Raise an exception...
            raise TypeError('"{}" is not of type "{}".'.format(
                prop_name, prop_type))

        # Is the property an array?
        if prop_type == SendPropType.ARRAY:

            # Is the given value not an Array instance?
            if not isinstance(value, Array):

                # Raise an exception...
                raise TypeError('Given value is not an Array instance.')

            # Is the length not matching?
            if value._length != prop.length:

                # Raise an exception...
                raise ValueError('Given array is not of length "{}".'.format(
                    prop.length))

            # Copy the values...
            value.copy(get_object_pointer(self),
                       self._get_type_size(type_name) * prop.length)

            # No need to go further...
            return

        # Otherwise, is the type native?
        elif Type.is_native(type_name):

            # Set the value of the property...
            getattr(get_object_pointer(self),
                    'set_{}'.format(type_name))(value, offset)

            # No need to go further...
            return

        # Otherwise...
        else:

            # Get the class...
            cls = manager.get_class(type_name)

            # Is the given value valid?
            if not isinstance(value, cls):

                # Raise an exception...
                raise TypeError('"{}" is not a valid "{}" value.'.format(
                    value, type_name))

            # Set the value of the property...
            get_object_pointer(value).copy(
                get_object_pointer(self) + offset, get_size(cls))

            # No need to go further...
            return

        # Raise an exception...
        raise NameError('Unable to set "{}" for the temp entity "{}".'.format(
            prop_name, self.name))