add getter for console get types

templated function for getting data from a field
This commit is contained in:
marauder2k7 2025-04-09 15:14:46 +01:00
parent 47f68d9371
commit 7c3fbfc9d8
2 changed files with 85 additions and 88 deletions

View file

@ -163,6 +163,41 @@ namespace PropertyInfo
bool default_print(String & result, SimObjectType * const & data); bool default_print(String & result, SimObjectType * const & data);
template<typename T, size_t count>
const char* FormatProperty(const void* dataPtr)
{
static const U32 bufSize = 256;
char* buffer = Con::getReturnBuffer(bufSize);
FormatProperty<T,count>(dataPtr, buffer, bufSize);
return buffer;
}
template<typename T, size_t count>
char* FormatProperty(const void* dataPtr, char* buffer, U32 bufSize)
{
const T* values = reinterpret_cast<const T*>(dataPtr);
char* ptr = buffer;
for (size_t i = 0; i < count; ++i)
{
S32 written = 0;
if constexpr (std::is_same_v<T, int> || std::is_same_v<T, S32>)
written = dSprintf(ptr, bufSize - (ptr - buffer), "%d", values[i]);
else if constexpr (std::is_same_v<T, float> || std::is_same_v<T, F32>)
written = dSprintf(ptr, bufSize - (ptr - buffer), "%g", values[i]);
else
AssertFatal(sizeof(T) == 0, "Unsupported type in FormatProperty");
ptr += written;
if (i < count - 1)
*ptr++ = ' ';
}
*ptr = '\0';
return ptr; // return end of written string for chaining
}
template<typename T, size_t count> template<typename T, size_t count>
inline bool ParseProperty(char* str, T(&out)[count]) inline bool ParseProperty(char* str, T(&out)[count])
{ {

View file

@ -170,11 +170,8 @@ ImplementConsoleTypeCasters( TypePoint2I, Point2I )
ConsoleGetType( TypePoint2I ) ConsoleGetType( TypePoint2I )
{ {
Point2I *pt = (Point2I *) dptr; const char* buff = PropertyInfo::FormatProperty<S32, 2>(dptr);
static const U32 bufSize = 256; return buff;
char* returnBuffer = Con::getReturnBuffer(bufSize);
dSprintf(returnBuffer, bufSize, "%d %d", pt->x, pt->y);
return returnBuffer;
} }
ConsoleSetType( TypePoint2I ) ConsoleSetType( TypePoint2I )
@ -204,11 +201,8 @@ ImplementConsoleTypeCasters( TypePoint2F, Point2F )
ConsoleGetType( TypePoint2F ) ConsoleGetType( TypePoint2F )
{ {
Point2F *pt = (Point2F *) dptr; const char* buff = PropertyInfo::FormatProperty<F32, 2>(dptr);
static const U32 bufSize = 256; return buff;
char* returnBuffer = Con::getReturnBuffer(bufSize);
dSprintf(returnBuffer, bufSize, "%g %g", pt->x, pt->y);
return returnBuffer;
} }
ConsoleSetType( TypePoint2F ) ConsoleSetType( TypePoint2F )
@ -238,11 +232,8 @@ ImplementConsoleTypeCasters(TypePoint3I, Point3I)
ConsoleGetType( TypePoint3I ) ConsoleGetType( TypePoint3I )
{ {
Point3I *pt = (Point3I *) dptr; const char* buff = PropertyInfo::FormatProperty<S32, 3>(dptr);
static const U32 bufSize = 256; return buff;
char* returnBuffer = Con::getReturnBuffer(bufSize);
dSprintf(returnBuffer, bufSize, "%d %d %d", pt->x, pt->y, pt->z);
return returnBuffer;
} }
ConsoleSetType( TypePoint3I ) ConsoleSetType( TypePoint3I )
@ -272,11 +263,8 @@ ImplementConsoleTypeCasters(TypePoint3F, Point3F)
ConsoleGetType( TypePoint3F ) ConsoleGetType( TypePoint3F )
{ {
Point3F *pt = (Point3F *) dptr; const char* buff = PropertyInfo::FormatProperty<F32, 3>(dptr);
static const U32 bufSize = 256; return buff;
char* returnBuffer = Con::getReturnBuffer(bufSize);
dSprintf(returnBuffer, bufSize, "%g %g %g", pt->x, pt->y, pt->z);
return returnBuffer;
} }
ConsoleSetType( TypePoint3F ) ConsoleSetType( TypePoint3F )
@ -306,11 +294,8 @@ ImplementConsoleTypeCasters( TypePoint4F, Point4F )
ConsoleGetType( TypePoint4F ) ConsoleGetType( TypePoint4F )
{ {
Point4F *pt = (Point4F *) dptr; const char* buff = PropertyInfo::FormatProperty<F32, 4>(dptr);
static const U32 bufSize = 256; return buff;
char* returnBuffer = Con::getReturnBuffer(bufSize);
dSprintf(returnBuffer, bufSize, "%g %g %g %g", pt->x, pt->y, pt->z, pt->w);
return returnBuffer;
} }
ConsoleSetType( TypePoint4F ) ConsoleSetType( TypePoint4F )
@ -340,12 +325,8 @@ ImplementConsoleTypeCasters( TypeRectI, RectI )
ConsoleGetType( TypeRectI ) ConsoleGetType( TypeRectI )
{ {
RectI *rect = (RectI *) dptr; const char* buff = PropertyInfo::FormatProperty<S32, 4>(dptr);
static const U32 bufSize = 256; return buff;
char* returnBuffer = Con::getReturnBuffer(bufSize);
dSprintf(returnBuffer, bufSize, "%d %d %d %d", rect->point.x, rect->point.y,
rect->extent.x, rect->extent.y);
return returnBuffer;
} }
ConsoleSetType( TypeRectI ) ConsoleSetType( TypeRectI )
@ -375,12 +356,8 @@ ImplementConsoleTypeCasters( TypeRectF, RectF )
ConsoleGetType( TypeRectF ) ConsoleGetType( TypeRectF )
{ {
RectF *rect = (RectF *) dptr; const char* buff = PropertyInfo::FormatProperty<F32, 4>(dptr);
static const U32 bufSize = 256; return buff;
char* returnBuffer = Con::getReturnBuffer(bufSize);
dSprintf(returnBuffer, bufSize, "%g %g %g %g", rect->point.x, rect->point.y,
rect->extent.x, rect->extent.y);
return returnBuffer;
} }
ConsoleSetType( TypeRectF ) ConsoleSetType( TypeRectF )
@ -413,17 +390,18 @@ ImplementConsoleTypeCasters( TypeMatrixF, MatrixF )
ConsoleGetType( TypeMatrixF ) ConsoleGetType( TypeMatrixF )
{ {
MatrixF* mat = ( MatrixF* ) dptr;
Point3F col0, col1, col2;
mat->getColumn(0, &col0);
mat->getColumn(1, &col1);
mat->getColumn(2, &col2);
static const U32 bufSize = 256; static const U32 bufSize = 256;
char* returnBuffer = Con::getReturnBuffer(bufSize); char* buffer = Con::getReturnBuffer(bufSize);
dSprintf(returnBuffer,bufSize,"%g %g %g %g %g %g %g %g %g",
col0.x, col0.y, col0.z, col1.x, col1.y, col1.z, col2.x, col2.y, col2.z); F32* mat = (F32*)dptr;
return returnBuffer; buffer = PropertyInfo::FormatProperty<F32, 3>(mat + 0, buffer, bufSize);
*buffer++ = ' ';
buffer = PropertyInfo::FormatProperty<F32, 3>(mat + 4, buffer, bufSize);
*buffer++ = ' ';
buffer = PropertyInfo::FormatProperty<F32, 3>(mat + 8, buffer, bufSize);
*buffer = '\0'; // null-terminate just in case
return buffer;
} }
ConsoleSetType( TypeMatrixF ) ConsoleSetType( TypeMatrixF )
@ -455,14 +433,9 @@ ConsoleMappedType(MatrixPosition, TypeMatrixPosition, Point3F, MatrixF, "")
ConsoleGetType( TypeMatrixPosition ) ConsoleGetType( TypeMatrixPosition )
{ {
F32 *col = (F32 *) dptr + 3; F32* mat = (F32*)dptr;
static const U32 bufSize = 256; const char* buff = PropertyInfo::FormatProperty<F32, 4>(mat + 8);
char* returnBuffer = Con::getReturnBuffer(bufSize); return buff;
if(col[12] == 1.f)
dSprintf(returnBuffer, bufSize, "%g %g %g", col[0], col[4], col[8]);
else
dSprintf(returnBuffer, bufSize, "%g %g %g %g", col[0], col[4], col[8], col[12]);
return returnBuffer;
} }
ConsoleSetType( TypeMatrixPosition ) ConsoleSetType( TypeMatrixPosition )
@ -495,12 +468,11 @@ ConsoleMappedType(MatrixRotation, TypeMatrixRotation, AngAxisF, MatrixF, "")
ConsoleGetType( TypeMatrixRotation ) ConsoleGetType( TypeMatrixRotation )
{ {
AngAxisF aa(*(MatrixF *) dptr); AngAxisF aa(*(MatrixF*)dptr);
aa.axis.normalize(); aa.axis.normalize();
static const U32 bufSize = 256; aa.angle = mRadToDeg(aa.angle);
char* returnBuffer = Con::getReturnBuffer(bufSize); const char* buff = PropertyInfo::FormatProperty<F32, 4>(&aa);
dSprintf(returnBuffer,bufSize,"%g %g %g %g",aa.axis.x,aa.axis.y,aa.axis.z,mRadToDeg(aa.angle)); return buff;
return returnBuffer;
} }
ConsoleSetType( TypeMatrixRotation ) ConsoleSetType( TypeMatrixRotation )
@ -539,10 +511,9 @@ ImplementConsoleTypeCasters( TypeAngAxisF, AngAxisF )
ConsoleGetType( TypeAngAxisF ) ConsoleGetType( TypeAngAxisF )
{ {
AngAxisF* aa = ( AngAxisF* ) dptr; AngAxisF* aa = ( AngAxisF* ) dptr;
static const U32 bufSize = 256; aa->angle = mRadToDeg(aa->angle);
char* returnBuffer = Con::getReturnBuffer(bufSize); const char* buff = PropertyInfo::FormatProperty<F32, 4>(aa);
dSprintf(returnBuffer,bufSize,"%g %g %g %g",aa->axis.x,aa->axis.y,aa->axis.z,mRadToDeg(aa->angle)); return buff;
return returnBuffer;
} }
ConsoleSetType( TypeAngAxisF ) ConsoleSetType( TypeAngAxisF )
@ -577,13 +548,8 @@ ImplementConsoleTypeCasters( TypeTransformF, TransformF )
ConsoleGetType( TypeTransformF ) ConsoleGetType( TypeTransformF )
{ {
TransformF* aa = ( TransformF* ) dptr; const char* buff = PropertyInfo::FormatProperty<F32, 7>(dptr);
static const U32 bufSize = 256; return buff;
char* returnBuffer = Con::getReturnBuffer(bufSize);
dSprintf( returnBuffer, bufSize, "%g %g %g %g %g %g %g",
aa->mPosition.x, aa->mPosition.y, aa->mPosition.z,
aa->mOrientation.axis.x, aa->mOrientation.axis.y, aa->mOrientation.axis.z, aa->mOrientation.angle );
return returnBuffer;
} }
ConsoleSetType( TypeTransformF ) ConsoleSetType( TypeTransformF )
@ -623,15 +589,8 @@ ImplementConsoleTypeCasters( TypeBox3F, Box3F )
ConsoleGetType( TypeBox3F ) ConsoleGetType( TypeBox3F )
{ {
const Box3F* pBox = (const Box3F*)dptr; const char* buff = PropertyInfo::FormatProperty<F32, 6>(dptr);
return buff;
static const U32 bufSize = 256;
char* returnBuffer = Con::getReturnBuffer(bufSize);
dSprintf(returnBuffer, bufSize, "%g %g %g %g %g %g",
pBox->minExtents.x, pBox->minExtents.y, pBox->minExtents.z,
pBox->maxExtents.x, pBox->maxExtents.y, pBox->maxExtents.z);
return returnBuffer;
} }
ConsoleSetType( TypeBox3F ) ConsoleSetType( TypeBox3F )
@ -668,14 +627,16 @@ ImplementConsoleTypeCasters( TypeEaseF, EaseF )
ConsoleGetType( TypeEaseF ) ConsoleGetType( TypeEaseF )
{ {
const EaseF* pEase = (const EaseF*)dptr;
static const U32 bufSize = 256; static const U32 bufSize = 256;
char* returnBuffer = Con::getReturnBuffer(bufSize); char* buffer = Con::getReturnBuffer(bufSize);
dSprintf(returnBuffer, bufSize, "%d %d %g %g",
pEase->mDir, pEase->mType, pEase->mParam[0], pEase->mParam[1]);
return returnBuffer; EaseF* pEase = (EaseF*)dptr;
buffer = PropertyInfo::FormatProperty<S32, 2>(pEase + 0, buffer, bufSize);
*buffer++ = ' ';
buffer = PropertyInfo::FormatProperty<F32, 2>(pEase + 2, buffer, bufSize);
*buffer = '\0'; // null-terminate just in case
return buffer;
} }
ConsoleSetType( TypeEaseF ) ConsoleSetType( TypeEaseF )
@ -717,13 +678,14 @@ ConsoleGetType(TypeRotationF)
if (pt->mRotationType == RotationF::Euler) if (pt->mRotationType == RotationF::Euler)
{ {
EulerF out = pt->asEulerF(RotationF::Degrees); EulerF out = pt->asEulerF(RotationF::Degrees);
dSprintf(returnBuffer, bufSize, "%g %g %g", out.x, out.y, out.z); returnBuffer = PropertyInfo::FormatProperty<F32, 3>(out, returnBuffer, bufSize);
} }
else if (pt->mRotationType == RotationF::AxisAngle) else if (pt->mRotationType == RotationF::AxisAngle)
{ {
AngAxisF out = pt->asAxisAngle(RotationF::Degrees); AngAxisF out = pt->asAxisAngle(RotationF::Degrees);
dSprintf(returnBuffer, bufSize, "%g %g %g %g", out.axis.x, out.axis.y, out.axis.z, out.angle); returnBuffer = PropertyInfo::FormatProperty<F32, 4>(&out, returnBuffer, bufSize);
} }
*returnBuffer = '\0'; // null-terminate just in case
return returnBuffer; return returnBuffer;
} }