| 1 | #ifndef ENGINE_SHARED_UUID_MANAGER_H |
| 2 | #define ENGINE_SHARED_UUID_MANAGER_H |
| 3 | |
| 4 | #include <vector> |
| 5 | |
| 6 | enum |
| 7 | { |
| 8 | UUID_MAXSTRSIZE = 37, // 12345678-0123-5678-0123-567890123456 |
| 9 | |
| 10 | UUID_INVALID = -2, |
| 11 | UUID_UNKNOWN = -1, |
| 12 | |
| 13 | OFFSET_UUID = 1 << 16, |
| 14 | }; |
| 15 | |
| 16 | struct CUuid |
| 17 | { |
| 18 | unsigned char m_aData[16]; |
| 19 | |
| 20 | bool operator==(const CUuid &Other) const; |
| 21 | bool operator!=(const CUuid &Other) const; |
| 22 | bool operator<(const CUuid &Other) const; |
| 23 | }; |
| 24 | |
| 25 | extern const CUuid UUID_ZEROED; |
| 26 | |
| 27 | CUuid RandomUuid(); |
| 28 | CUuid CalculateUuid(const char *pName); |
| 29 | // The buffer length should be at least UUID_MAXSTRSIZE. |
| 30 | void FormatUuid(CUuid Uuid, char *pBuffer, unsigned BufferLength); |
| 31 | // Returns nonzero on failure. |
| 32 | int ParseUuid(CUuid *pUuid, const char *pBuffer); |
| 33 | |
| 34 | struct CName |
| 35 | { |
| 36 | CUuid m_Uuid; |
| 37 | const char *m_pName; |
| 38 | }; |
| 39 | |
| 40 | struct CNameIndexed |
| 41 | { |
| 42 | CUuid m_Uuid; |
| 43 | int m_Id; |
| 44 | |
| 45 | bool operator<(const CNameIndexed &Other) const { return m_Uuid < Other.m_Uuid; } |
| 46 | bool operator==(const CNameIndexed &Other) const { return m_Uuid == Other.m_Uuid; } |
| 47 | }; |
| 48 | |
| 49 | class CPacker; |
| 50 | class CUnpacker; |
| 51 | |
| 52 | class CUuidManager |
| 53 | { |
| 54 | std::vector<CName> m_vNames; |
| 55 | std::vector<CNameIndexed> m_vNamesSorted; |
| 56 | |
| 57 | public: |
| 58 | void RegisterName(int Id, const char *pName); |
| 59 | CUuid GetUuid(int Id) const; |
| 60 | const char *GetName(int Id) const; |
| 61 | int LookupUuid(CUuid Uuid) const; |
| 62 | int NumUuids() const; |
| 63 | |
| 64 | int UnpackUuid(CUnpacker *pUnpacker) const; |
| 65 | int UnpackUuid(CUnpacker *pUnpacker, CUuid *pOut) const; |
| 66 | void PackUuid(int Id, CPacker *pPacker) const; |
| 67 | |
| 68 | void DebugDump() const; |
| 69 | }; |
| 70 | |
| 71 | extern CUuidManager g_UuidManager; |
| 72 | |
| 73 | #endif // ENGINE_SHARED_UUID_MANAGER_H |
| 74 | |