Skip to content

Commit

Permalink
various improvements to serialize library based on read/write needs f…
Browse files Browse the repository at this point in the history
…rom another project
  • Loading branch information
gafferongames committed Nov 14, 2024
1 parent 91c82b9 commit 6ed4268
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 85 deletions.
58 changes: 0 additions & 58 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,61 +25,3 @@ project "test"

project "example"
files { "example.cpp", "serialize.h" }

newaction
{
trigger = "clean",

description = "Clean all build files and output",

execute = function ()

files_to_delete =
{
"Makefile",
"*.make",
"*.txt",
"*.zip",
"*.tar.gz",
"*.db",
"*.opendb",
"*.vcproj",
"*.vcxproj",
"*.vcxproj.user",
"*.vcxproj.filters",
"*.sln",
"*.xcodeproj",
"*.xcworkspace"
}

directories_to_delete =
{
"obj",
"ipch",
"bin",
".vs",
"Debug",
"Release",
"release",
"cov-int",
"docs",
"xml"
}

for i,v in ipairs( directories_to_delete ) do
os.rmdir( v )
end

if not os.istarget "windows" then
os.execute "find . -name .DS_Store -delete"
for i,v in ipairs( files_to_delete ) do
os.execute( "rm -f " .. v )
end
else
for i,v in ipairs( files_to_delete ) do
os.execute( "del /F /Q " .. v )
end
end

end
}
143 changes: 116 additions & 27 deletions serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -1868,8 +1868,27 @@ namespace serialize

#define read_float serialize_float
#define read_double serialize_double
#define read_bytes serialize_bytes
#define read_string serialize_string

#define read_bytes( stream, data, bytes ) \
do \
{ \
uint8_t * data_ptr = (uint8_t*) data; \
if ( !stream.SerializeBytes( data_ptr, bytes ) ) \
{ \
return false; \
} \
} while (0)

#define read_string( stream, string, buffer_size ) \
do \
{ \
char * string_ptr = (char*) string; \
if ( !serialize_string_internal( stream, string_ptr, buffer_size ) ) \
{ \
return false; \
} \
} while (0)

#define read_align serialize_align
#define read_check serialize_check
#define read_object serialize_object
Expand Down Expand Up @@ -2170,7 +2189,7 @@ struct TestObject
data.int_relative = 5;

for ( int i = 0; i < (int) sizeof( data.bytes ); ++i )
data.bytes[i] = rand() % 255;
data.bytes[i] = (uint8_t) ( i + 5 ) * 13;

serialize_copy_string( data.string, "hello world!", sizeof(data.string) - 1 );
}
Expand Down Expand Up @@ -2259,39 +2278,108 @@ inline void test_serialize()

bool ReadFunction( serialize::ReadStream & readStream )
{
(void) readStream;
// IMPORTANT: You wouldn't normally write a read function like this, but I'm just checking each value as it's read in
// Note that the only thing the read function has to have is to return bool: true on success, false on failing to read.
// This is important because protects you from maliciously crafted packets.

{
uint32_t value;
read_bits( readStream, value, 4 );
serialize_check( value == 13 );
}

{
bool value;
read_bool( readStream, value );
serialize_check( value == true );
}

{
uint8_t value;
read_uint8( readStream, value );
serialize_check( value == 255 );
}

{
uint16_t value;
read_uint16( readStream, value );
serialize_check( value == 65535 );
}

{
uint32_t value;
read_uint32( readStream, value );
serialize_check( value == 0xFFFFFFFF );
}

{
uint64_t value;
read_uint64( readStream, value );
serialize_check( value == 0xFFFFFFFFFFFFFFFFULL ); // i am very full
}

{
int value;
read_int( readStream, value, 10, 90 );
serialize_check( value == 55 );
}

{
float value;
read_float( readStream, value );
serialize_check( value == 100.0f );
}

/*
write_bits( writeStream, 13, 4 );
write_bool( writeStream, true );
write_uint8( writeStream, 255 );
write_uint16( writeStream, 65535 );
write_uint32( writeStream, 0xFFFFFFFF );
write_uint32( writeStream, 0xFFFFFFFFFFFFFFFFULL );
write_float( writeStream, 100.0f );
write_double( writeStream, 1000000000.0f );
{
double value;
read_double( readStream, value );
serialize_check( value == 1000000000.0 );
}

char data[5] = { 1, 2, 3, 4, 5 };
write_bytes( writeStream, data, 5 );
{
char value[5];
read_bytes( readStream, value, 5 );
serialize_check( value[0] == 1 );
serialize_check( value[1] == 2 );
serialize_check( value[2] == 3 );
serialize_check( value[3] == 4 );
serialize_check( value[4] == 5 );
}

const char * string = "hello";
write_string( writeStream, string, 10 );
{
char string[10];
read_string( readStream, string, 10 );
serialize_check( string[0] == 'h' );
serialize_check( string[1] == 'e' );
serialize_check( string[2] == 'l' );
serialize_check( string[3] == 'l' );
serialize_check( string[4] == 'o' );
serialize_check( string[5] == '\0' );
}

write_align( writeStream );
read_align( readStream );

TestContext context;
context.min = -10;
context.max = +10;

writeStream.SetContext( &context );
readStream.SetContext( &context );
{
TestObject expectedObject;
expectedObject.Init();

TestObject readObject;

TestObject object;
object.Init();
read_object( readStream, readObject );

write_object( writeStream, object );
serialize_check( readObject == expectedObject );
}

write_int_relative( writeStream, 100, 105 );
*/
{
int value;
read_int_relative( readStream, 100, value );
serialize_check( value == 105 );
}

return true;
}
Expand All @@ -2314,7 +2402,8 @@ inline void test_read_write()
write_uint8( writeStream, 255 );
write_uint16( writeStream, 65535 );
write_uint32( writeStream, 0xFFFFFFFF );
write_uint32( writeStream, 0xFFFFFFFFFFFFFFFFULL );
write_uint64( writeStream, 0xFFFFFFFFFFFFFFFFULL );
write_int( writeStream, 55, 10, 90 );
write_float( writeStream, 100.0f );
write_double( writeStream, 1000000000.0f );

Expand Down Expand Up @@ -2349,7 +2438,7 @@ inline void test_read_write()
// read from the buffer
{
serialize::ReadStream readStream;
readStream.Initialize( buffer, BufferSize );
readStream.Initialize( buffer, bytesWritten );
serialize_check( ReadFunction( readStream ) );
}
}
Expand All @@ -2364,7 +2453,7 @@ inline void test_read_write()

inline void serialize_test()
{
// while ( 1 )
while ( 1 )
{
SERIALIZE_RUN_TEST( test_endian );
SERIALIZE_RUN_TEST( test_bitpacker );
Expand Down

0 comments on commit 6ed4268

Please sign in to comment.