medfall

A super great game engine
Log | Files | Refs

commit ffb9f33f73353ef08724d38ae3ec14ba6a147521
parent 250b68693726abee793195d1ed1d9b37dbd69a3f
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sun May 28 01:14:53 +0300

Use the new pattern matcher to parse manifests

Diffstat:
launcher/main.cc | 35++++++++++++++++++++---------------
make.lua | 2+-
sha2.h | 1+
3 files changed, 22 insertions(+), 16 deletions(-)
diff --git a/launcher/main.cc b/launcher/main.cc @@ -12,9 +12,12 @@ #include "http.h" #include "locked.h" #include "work_queue.h" +#include "ggformat.h" #include "str.h" #include "sha2.h" #include "strlcpy.h" +#include "strtonum.h" +#include "patterns.h" #include "platform_exec.h" #include "platform_io.h" #include "platform_network.h" @@ -103,28 +106,30 @@ static bool parse_version( Version * v, const char * str ) { static bool parse_manifest( std::map< std::string, ManifestEntry > * manifest_ptr, const char * data ) { std::map< std::string, ManifestEntry > & manifest = *manifest_ptr; - size_t len = strlen( data ); - size_t i = 0; - while( true ) { - char file_name[ 256 ]; - char sha256[ SHA256_DIGEST_STRING_LENGTH * 2 + 1 ]; - unsigned long long file_size_ull; - int bytes_read; - int fields = sscanf( data + i, "%127s %64[0-9a-f] %llu%n", file_name, sha256, &file_size_ull, &bytes_read ); - if( fields != 3 ) { + for( array< array< const char > > line : gmatch( data, "([^\n]+)" ) ) { + ASSERT( line.n == 1 ); + + Matches matches; + bool ok = match( &matches, line[ 0 ], "^(%S+) (" SHA256_PATTERN ") (%d+)$" ); + if( !ok ) return false; + + ASSERT( matches[ 1 ].n + 1 == SHA256_DIGEST_STRING_LENGTH ); + + const str< 256 > file_name( "{}", matches[ 0 ] ); + const str< 16 > file_size( "{}", matches[ 2 ] ); + + u64 size = u64( strtonum( file_size.c_str(), 1, S64_MAX, NULL ) ); + if( size == 0 ) { manifest.clear(); return false; } ManifestEntry entry; - strlcpy( entry.checksum.e, sha256, sizeof( entry.checksum.e ) ); - entry.file_size = checked_cast< u64 >( file_size_ull ); - - manifest[ file_name ] = entry; + strlcpy( entry.checksum.e, matches[ 1 ].ptr(), sizeof( entry.checksum.e ) ); + entry.file_size = size; - i += bytes_read; - if( i >= len - 1 ) break; + manifest[ file_name.c_str() ] = entry; } return true; diff --git a/make.lua b/make.lua @@ -8,7 +8,7 @@ bin( "medfall", { "main", "hm", "heightmap", "terrain_manager", "btt", "gpubtt", windows_bin_ldflags( "medfall", "opengl32.lib gdi32.lib Ws2_32.lib" ) linux_bin_ldflags( "medfall", "-lX11 -lXrandr -lXinerama -lXcursor" ) -bin( "launch", { "launcher/main", "http", "sha2", game_objs }, { "imgui", "monocypher", "whereami", game_libs } ) +bin( "launch", { "launcher/main", "http", "sha2", "patterns", game_objs }, { "imgui", "monocypher", "whereami", game_libs } ) windows_bin_ldflags( "launch", "opengl32.lib gdi32.lib Ws2_32.lib" ) linux_bin_ldflags( "launch", "-lX11 -lXrandr -lXinerama -lXcursor" ) diff --git a/sha2.h b/sha2.h @@ -43,6 +43,7 @@ #define SHA256_BLOCK_LENGTH 64 #define SHA256_DIGEST_LENGTH 32 #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1) +#define SHA256_PATTERN "%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x" #define SHA512_BLOCK_LENGTH 128 #define SHA512_DIGEST_LENGTH 64 #define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1)