Add PackedByteArray conversion to PackedVector2Array, PackedVector3Array, PackedVector4Array and PackedColorArray

This commit is contained in:
OsakiTsukiko
2023-04-15 12:33:55 +03:00
committed by Rémi Verschelde
parent 03bd8ba9c2
commit 805ad87340
2 changed files with 88 additions and 0 deletions

View File

@@ -1012,6 +1012,62 @@ struct _VariantCall {
return dest;
}
static PackedVector2Array func_PackedByteArray_decode_vector2_array(PackedByteArray *p_instance) {
uint64_t size = p_instance->size();
PackedVector2Array dest;
if (size == 0) {
return dest;
}
ERR_FAIL_COND_V_MSG(size % sizeof(Vector2), dest, "PackedByteArray size must be a multiple of " + itos(sizeof(Vector2)) + " (size of Vector2) to convert to PackedVector2Array.");
const uint8_t *r = p_instance->ptr();
dest.resize(size / sizeof(Vector2));
ERR_FAIL_COND_V(dest.is_empty(), dest); // Avoid UB in case resize failed.
memcpy(dest.ptrw(), r, dest.size() * sizeof(Vector2));
return dest;
}
static PackedVector3Array func_PackedByteArray_decode_vector3_array(PackedByteArray *p_instance) {
uint64_t size = p_instance->size();
PackedVector3Array dest;
if (size == 0) {
return dest;
}
ERR_FAIL_COND_V_MSG(size % sizeof(Vector3), dest, "PackedByteArray size must be a multiple of " + itos(sizeof(Vector3)) + " (size of Vector3) to convert to PackedVector3Array.");
const uint8_t *r = p_instance->ptr();
dest.resize(size / sizeof(Vector3));
ERR_FAIL_COND_V(dest.is_empty(), dest); // Avoid UB in case resize failed.
memcpy(dest.ptrw(), r, dest.size() * sizeof(Vector3));
return dest;
}
static PackedVector4Array func_PackedByteArray_decode_vector4_array(PackedByteArray *p_instance) {
uint64_t size = p_instance->size();
PackedVector4Array dest;
if (size == 0) {
return dest;
}
ERR_FAIL_COND_V_MSG(size % sizeof(Vector4), dest, "PackedByteArray size must be a multiple of " + itos(sizeof(Vector4)) + " (size of Vector4) to convert to PackedVector4Array.");
const uint8_t *r = p_instance->ptr();
dest.resize(size / sizeof(Vector4));
ERR_FAIL_COND_V(dest.is_empty(), dest); // Avoid UB in case resize failed.
memcpy(dest.ptrw(), r, dest.size() * sizeof(Vector4));
return dest;
}
static PackedColorArray func_PackedByteArray_decode_color_array(PackedByteArray *p_instance) {
uint64_t size = p_instance->size();
PackedColorArray dest;
if (size == 0) {
return dest;
}
ERR_FAIL_COND_V_MSG(size % sizeof(Color), dest, "PackedByteArray size must be a multiple of " + itos(sizeof(Color)) + " (size of Color variant) to convert to PackedColorArray.");
const uint8_t *r = p_instance->ptr();
dest.resize(size / sizeof(Color));
ERR_FAIL_COND_V(dest.is_empty(), dest); // Avoid UB in case resize failed.
memcpy(dest.ptrw(), r, dest.size() * sizeof(Color));
return dest;
}
static void func_PackedByteArray_encode_u8(PackedByteArray *p_instance, int64_t p_offset, int64_t p_value) {
uint64_t size = p_instance->size();
ERR_FAIL_COND(p_offset < 0 || p_offset > int64_t(size) - 1);
@@ -2560,6 +2616,10 @@ static void _register_variant_builtin_methods_array() {
bind_function(PackedByteArray, to_int64_array, _VariantCall::func_PackedByteArray_decode_s64_array, sarray(), varray());
bind_function(PackedByteArray, to_float32_array, _VariantCall::func_PackedByteArray_decode_float_array, sarray(), varray());
bind_function(PackedByteArray, to_float64_array, _VariantCall::func_PackedByteArray_decode_double_array, sarray(), varray());
bind_function(PackedByteArray, to_vector2_array, _VariantCall::func_PackedByteArray_decode_vector2_array, sarray(), varray());
bind_function(PackedByteArray, to_vector3_array, _VariantCall::func_PackedByteArray_decode_vector3_array, sarray(), varray());
bind_function(PackedByteArray, to_vector4_array, _VariantCall::func_PackedByteArray_decode_vector4_array, sarray(), varray());
bind_function(PackedByteArray, to_color_array, _VariantCall::func_PackedByteArray_decode_color_array, sarray(), varray());
bind_functionnc(PackedByteArray, bswap16, _VariantCall::func_PackedByteArray_bswap16, sarray("offset", "count"), varray(0, -1));
bind_functionnc(PackedByteArray, bswap32, _VariantCall::func_PackedByteArray_bswap32, sarray("offset", "count"), varray(0, -1));