forked from ValveSoftware/source-sdk-2013
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniqueid.cpp
177 lines (152 loc) · 3.9 KB
/
uniqueid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
// Unique ID generation
//=============================================================================//
#include "tier0/platform.h"
#ifdef IS_WINDOWS_PC
#include <windows.h> // UUIDCreate
#else
#include "checksum_crc.h"
#endif
#include "tier1/uniqueid.h"
#include "tier1/utlbuffer.h"
//-----------------------------------------------------------------------------
// Creates a new unique id
//-----------------------------------------------------------------------------
void CreateUniqueId( UniqueId_t *pDest )
{
#ifdef IS_WINDOWS_PC
Assert( sizeof( UUID ) == sizeof( *pDest ) );
UuidCreate( (UUID *)pDest );
#else
// X360/linux TBD: Need a real UUID Implementation
Q_memset( pDest, 0, sizeof( UniqueId_t ) );
#endif
}
//-----------------------------------------------------------------------------
// Creates a new unique id from a string representation of one
//-----------------------------------------------------------------------------
bool UniqueIdFromString( UniqueId_t *pDest, const char *pBuf, int nMaxLen )
{
if ( nMaxLen == 0 )
{
nMaxLen = Q_strlen( pBuf );
}
char *pTemp = (char*)stackalloc( nMaxLen + 1 );
V_strncpy( pTemp, pBuf, nMaxLen + 1 );
--nMaxLen;
while( (nMaxLen >= 0) && isspace( pTemp[nMaxLen] ) )
{
--nMaxLen;
}
pTemp[ nMaxLen + 1 ] = 0;
while( *pTemp && isspace( *pTemp ) )
{
++pTemp;
}
#ifdef IS_WINDOWS_PC
Assert( sizeof( UUID ) == sizeof( *pDest ) );
if ( RPC_S_OK != UuidFromString( (unsigned char *)pTemp, (UUID *)pDest ) )
{
InvalidateUniqueId( pDest );
return false;
}
#else
// X360TBD: Need a real UUID Implementation
// For now, use crc to generate a unique ID from the UUID string.
Q_memset( pDest, 0, sizeof( UniqueId_t ) );
if ( nMaxLen > 0 )
{
CRC32_t crc;
CRC32_Init( &crc );
CRC32_ProcessBuffer( &crc, pBuf, nMaxLen );
CRC32_Final( &crc );
Q_memcpy( pDest, &crc, sizeof( CRC32_t ) );
}
#endif
return true;
}
//-----------------------------------------------------------------------------
// Sets an object ID to be an invalid state
//-----------------------------------------------------------------------------
void InvalidateUniqueId( UniqueId_t *pDest )
{
Assert( pDest );
memset( pDest, 0, sizeof( UniqueId_t ) );
}
bool IsUniqueIdValid( const UniqueId_t &id )
{
UniqueId_t invalidId;
memset( &invalidId, 0, sizeof( UniqueId_t ) );
return !IsUniqueIdEqual( invalidId, id );
}
bool IsUniqueIdEqual( const UniqueId_t &id1, const UniqueId_t &id2 )
{
return memcmp( &id1, &id2, sizeof( UniqueId_t ) ) == 0;
}
void UniqueIdToString( const UniqueId_t &id, char *pBuf, int nMaxLen )
{
pBuf[ 0 ] = 0;
// X360TBD: Need a real UUID Implementation
#ifdef IS_WINDOWS_PC
UUID *self = ( UUID * )&id;
unsigned char *outstring = NULL;
UuidToString( self, &outstring );
if ( outstring && *outstring )
{
Q_strncpy( pBuf, (const char *)outstring, nMaxLen );
RpcStringFree( &outstring );
}
#endif
}
void CopyUniqueId( const UniqueId_t &src, UniqueId_t *pDest )
{
memcpy( pDest, &src, sizeof( UniqueId_t ) );
}
bool Serialize( CUtlBuffer &buf, const UniqueId_t &src )
{
// X360TBD: Need a real UUID Implementation
#ifdef IS_WINDOWS_PC
if ( buf.IsText() )
{
UUID *pId = ( UUID * )&src;
unsigned char *outstring = NULL;
UuidToString( pId, &outstring );
if ( outstring && *outstring )
{
buf.PutString( (const char *)outstring );
RpcStringFree( &outstring );
}
else
{
buf.PutChar( '\0' );
}
}
else
{
buf.Put( &src, sizeof(UniqueId_t) );
}
return buf.IsValid();
#else
return false;
#endif
}
bool Unserialize( CUtlBuffer &buf, UniqueId_t &dest )
{
if ( buf.IsText() )
{
int nTextLen = buf.PeekStringLength();
char *pBuf = (char*)stackalloc( nTextLen );
buf.GetString( pBuf, nTextLen );
UniqueIdFromString( &dest, pBuf, nTextLen );
}
else
{
buf.Get( &dest, sizeof(UniqueId_t) );
}
return buf.IsValid();
}