Utilities and nice-to-haves

This commit is contained in:
HikikoMarmy
2025-07-01 13:51:38 +01:00
parent 31ce20de50
commit 5ae467de40
4 changed files with 134 additions and 0 deletions

View File

@@ -0,0 +1,95 @@
#pragma once
#include <vector>
#include <cstdint>
#include <stdexcept>
#include <cstring>
class ByteBufferReader {
public:
ByteBufferReader( const uint8_t *data, size_t size )
: buffer( data ), bufferSize( size ), offset( 0 )
{
}
explicit ByteBufferReader( const std::vector<uint8_t> &vec )
: ByteBufferReader( vec.data(), vec.size() )
{
}
template<typename T>
T read()
{
if( offset + sizeof( T ) > bufferSize )
throw std::out_of_range( "BufferReader: read out of bounds" );
T value;
std::memcpy( &value, buffer + offset, sizeof( T ) );
offset += sizeof( T );
return value;
}
void readBytes( void *dest, size_t size )
{
if( offset + size > bufferSize )
throw std::out_of_range( "BufferReader: readBytes out of bounds" );
std::memcpy( dest, buffer + offset, size );
offset += size;
}
std::string readString( size_t length )
{
if( offset + length > bufferSize )
throw std::out_of_range( "BufferReader: readString out of bounds" );
std::string str( reinterpret_cast< const char * >( buffer + offset ), length );
offset += length;
return str;
}
template<typename T, size_t N>
std::array<T, N> readArray()
{
std::array<T, N> arr;
for( size_t i = 0; i < N; ++i )
arr[ i ] = read<T>();
return arr;
}
void skip( size_t count )
{
if( offset + count > bufferSize )
throw std::out_of_range( "BufferReader: skip out of bounds" );
offset += count;
}
template<typename T>
T peek() const
{
if( offset + sizeof( T ) > bufferSize )
throw std::out_of_range( "BufferReader: peek out of bounds" );
T value;
std::memcpy( &value, buffer + offset, sizeof( T ) );
return value;
}
bool eof() const
{
return offset >= bufferSize;
}
size_t tell() const
{
return offset;
}
size_t remaining() const
{
return bufferSize - offset;
}
private:
const uint8_t *buffer;
size_t bufferSize;
size_t offset;
};

22
Common/ForwardDecl.h Normal file
View File

@@ -0,0 +1,22 @@
#pragma once
#include <memory>
class RealmUser;
using sptr_user = std::shared_ptr< RealmUser >;
using wptr_user = std::weak_ptr< RealmUser >;
class GameSession;
using sptr_game_session = std::shared_ptr< GameSession >;
class RealmSocket;
using sptr_socket = std::shared_ptr< RealmSocket >;
class ByteBuffer;
using sptr_byte_stream = std::shared_ptr< ByteBuffer >;
class ChatRoomSession;
using sptr_chat_room_session = std::shared_ptr< ChatRoomSession >;
class RealmCharacter;
using sptr_realm_character = std::shared_ptr< RealmCharacter >;

View File

@@ -1,6 +1,9 @@
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
#include <string> #include <string>
#include <cstdint>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h> #include <windows.h>
#include "Utility.h" #include "Utility.h"
@@ -48,6 +51,17 @@ uint32_t Util::ByteSwap( uint32_t val )
( ( val >> 24 ) & 0x000000FF ); ( ( val >> 24 ) & 0x000000FF );
} }
std::string Util::IPFromAddr( const sockaddr_in &addr )
{
char buffer[ INET_ADDRSTRLEN ] = {};
const char *result = inet_ntop( AF_INET, &addr.sin_addr, buffer, sizeof( buffer ) );
if( result )
return std::string( buffer );
else
return {};
}
std::string Util::WideToUTF8( const std::wstring &wstr ) std::string Util::WideToUTF8( const std::wstring &wstr )
{ {
if( wstr.empty() ) return {}; if( wstr.empty() ) return {};

View File

@@ -2,6 +2,7 @@
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include <WinSock2.h>
namespace Util namespace Util
{ {
@@ -17,6 +18,8 @@ namespace Util
return ( value >= min && value <= max ); return ( value >= min && value <= max );
} }
std::string IPFromAddr( const sockaddr_in &addr );
std::string WideToUTF8( const std::wstring &wstr ); std::string WideToUTF8( const std::wstring &wstr );
std::wstring UTF8ToWide( const std::string &str ); std::wstring UTF8ToWide( const std::string &str );
} }