janwas
c0ed950657
this snowballed into a massive search+destroy of the hodgepodge of mostly equivalent types we had in use (int, uint, unsigned, unsigned int, i32, u32, ulong, uintN). it is more efficient to use 64-bit types in 64-bit mode, so the preferred default is size_t (for anything remotely resembling a size or index). tile coordinates are ssize_t to allow more efficient conversion to/from floating point. flags are int because we almost never need more than 15 distinct bits, bit test/set is not slower and int is fastest to type. finally, some data that is pretty much directly passed to OpenGL is now typed accordingly. after several hours, the code now requires fewer casts and less guesswork. other changes: - unit and player IDs now have an "invalid id" constant in the respective class to avoid casting and -1 - fix some endian/64-bit bugs in the map (un)packing. added a convenience function to write/read a size_t. - ia32: change CPUID interface to allow passing in ecx (required for cache topology detection, which I need at work). remove some unneeded functions from asm, replace with intrinsics where possible. This was SVN commit r5942.
142 lines
2.9 KiB
C++
142 lines
2.9 KiB
C++
// Functions for (de)serialization of jsvals
|
|
|
|
// WIP, not yet functional
|
|
|
|
#include "network/Serialization.h"
|
|
#include "JSConversions.h"
|
|
#include "ps/CStr.h"
|
|
|
|
class jsval_ser : public ISerializable
|
|
{
|
|
enum
|
|
{
|
|
TAG_BOOLEAN_FALSE,
|
|
TAG_BOOLEAN_TRUE,
|
|
TAG_INT,
|
|
TAG_DOUBLE,
|
|
TAG_STRING,
|
|
TAG_NOT_SERIALIZABLE = -1
|
|
} m_tag;
|
|
jsval m_data;
|
|
public:
|
|
jsval_ser() : m_tag( TAG_NOT_SERIALIZABLE )
|
|
{
|
|
}
|
|
jsval_ser( jsval data ) : m_data( data )
|
|
{
|
|
if( m_data == JSVAL_FALSE )
|
|
m_tag = TAG_BOOLEAN_FALSE;
|
|
if( m_data == JSVAL_TRUE )
|
|
m_tag = TAG_BOOLEAN_TRUE;
|
|
if( JSVAL_IS_INT( m_data ) )
|
|
m_tag = TAG_INT;
|
|
if( JSVAL_IS_DOUBLE( m_data ) )
|
|
m_tag = TAG_DOUBLE;
|
|
if( JSVAL_IS_STRING( m_data ) )
|
|
m_tag = TAG_STRING;
|
|
m_tag = TAG_NOT_SERIALIZABLE;
|
|
}
|
|
operator jsval() const
|
|
{
|
|
return( m_data );
|
|
}
|
|
operator CStr() const
|
|
{
|
|
return( ToPrimitive<CStrW>( m_data ) );
|
|
}
|
|
size_t GetSerializedLength() const
|
|
{
|
|
switch( m_tag )
|
|
{
|
|
case TAG_BOOLEAN_FALSE:
|
|
case TAG_BOOLEAN_TRUE:
|
|
return( 1 );
|
|
case TAG_INT:
|
|
return( 5 );
|
|
case TAG_DOUBLE:
|
|
return( 9 );
|
|
case TAG_STRING:
|
|
return( 1 + (ToPrimitive<CStrW>(m_data)).GetSerializedLength() );
|
|
default:
|
|
debug_warn("An attempt was made to serialize a jsval other than a number, boolean or string." );
|
|
return( 1 );
|
|
}
|
|
}
|
|
u8* Serialize( u8* buffer ) const
|
|
{
|
|
Serialize_int_1( buffer, m_tag );
|
|
switch( m_tag )
|
|
{
|
|
case TAG_BOOLEAN_FALSE:
|
|
case TAG_BOOLEAN_TRUE:
|
|
break;
|
|
case TAG_INT:
|
|
{
|
|
u32 ival = JSVAL_TO_INT( m_data );
|
|
Serialize_int_4( buffer, ival );
|
|
}
|
|
break;
|
|
case TAG_DOUBLE:
|
|
{
|
|
union {
|
|
u64 ival;
|
|
double dval;
|
|
} val;
|
|
cassert(sizeof(u64) == sizeof(double));
|
|
val.dval = *JSVAL_TO_DOUBLE( m_data );
|
|
Serialize_int_8( buffer, val.ival );
|
|
}
|
|
break;
|
|
case TAG_STRING:
|
|
buffer = ( ToPrimitive<CStrW>( m_data ) ).Serialize( buffer );
|
|
break;
|
|
default:
|
|
debug_warn("An attempt was made to serialize a jsval other than a number, boolean or string." );
|
|
break;
|
|
}
|
|
return( buffer );
|
|
}
|
|
const u8* Deserialize( const u8* buffer, const u8* end )
|
|
{
|
|
Deserialize_int_1( buffer, (u8&)m_tag );
|
|
switch( m_tag )
|
|
{
|
|
case TAG_BOOLEAN_FALSE:
|
|
m_data = JSVAL_FALSE;
|
|
break;
|
|
case TAG_BOOLEAN_TRUE:
|
|
m_data = JSVAL_TRUE;
|
|
break;
|
|
case TAG_INT:
|
|
{
|
|
u32 ival;
|
|
Deserialize_int_4( buffer, ival );
|
|
m_data = INT_TO_JSVAL( ival );
|
|
}
|
|
break;
|
|
case TAG_DOUBLE:
|
|
{
|
|
union {
|
|
u64 ival;
|
|
double dval;
|
|
} val;
|
|
cassert(sizeof(u64) == sizeof(double));
|
|
Deserialize_int_8( buffer, val.ival );
|
|
JS_NewDoubleValue( g_ScriptingHost.GetContext(), val.dval, &m_data );
|
|
}
|
|
break;
|
|
case TAG_STRING:
|
|
{
|
|
CStrW ival;
|
|
buffer = ival.Deserialize( buffer, end );
|
|
m_data = ToJSVal<CStrW>( ival );
|
|
}
|
|
break;
|
|
default:
|
|
debug_warn("An attempt was made to deserialize a jsval other than a number, boolean or string." );
|
|
break;
|
|
}
|
|
return( buffer );
|
|
}
|
|
};
|