Browse Source

Proper UUID generation

Marko Pintera 10 years ago
parent
commit
ee9c441334

+ 5 - 0
BansheeCore/Include/BsPlatform.h

@@ -373,6 +373,11 @@ namespace BansheeEngine
 		 */
 		static bool getMACAddress(MACAddress& address);
 
+		/**
+		 * @brief	Creates a new universally unique identifier and returns it as a string.
+		 */
+		static String generateUUID();
+
 		/**
 		 * @brief	Queries the internal system performance counter you can use for very precise time
 		 * 			measurements. Value is in milliseconds.

+ 2 - 21
BansheeCore/Include/BsUUID.h

@@ -1,9 +1,6 @@
 #pragma once
 
 #include "BsCorePrerequisites.h"
-#include "BsModule.h"
-#include "BsSpinLock.h"
-#include <random>
 
 namespace BansheeEngine
 {
@@ -12,28 +9,12 @@ namespace BansheeEngine
 	 *
 	 * @note	Thread safe.
 	 */
-	class BS_CORE_EXPORT UUIDGenerator : public Module<UUIDGenerator>
+	class BS_CORE_EXPORT UUIDGenerator
 	{
-		/**
-		 * @brief	Type of UUID generation to use.
-		 */
-		enum UUIDVersion
-		{
-			UUIDV_TimeBased = 0x01
-		};
-
 	public:
-		UUIDGenerator();
-
 		/**
 		 * @brief	Generate a new random universally unique identifier.
 		 */
-		String generateRandom();
-
-	private:
-		std::mt19937 mRandomGenerator;
-		MACAddress mMACAddress;
-		SpinLock mSpinLock;
-		bool mHaveMacAddress;
+		static String generateRandom();
 	};
 }

+ 0 - 3
BansheeCore/Source/BsCoreApplication.cpp

@@ -36,7 +36,6 @@
 #include "BsQueryManager.h"
 #include "BsThreadPool.h"
 #include "BsTaskScheduler.h"
-#include "BsUUID.h"
 #include "BsRenderStats.h"
 #include "BsMessageHandler.h"
 #include "BsResourceListenerManager.h"
@@ -110,7 +109,6 @@ namespace BansheeEngine
 		ThreadPool::shutDown();
 		ProfilingManager::shutDown();
 		ProfilerCPU::shutDown();
-		UUIDGenerator::shutDown();
 		MessageHandler::shutDown();
 		ShaderManager::shutDown();
 
@@ -129,7 +127,6 @@ namespace BansheeEngine
 
 		ShaderManager::startUp(getShaderIncludeHandler());
 		MessageHandler::startUp();
-		UUIDGenerator::startUp();
 		ProfilerCPU::startUp();
 		ProfilingManager::startUp();
 		ThreadPool::startUp<TThreadPool<ThreadBansheePolicy>>((numWorkerThreads));

+ 1 - 65
BansheeCore/Source/BsUUID.cpp

@@ -6,72 +6,8 @@ using namespace std::chrono;
 
 namespace BansheeEngine
 {
-	UUIDGenerator::UUIDGenerator()
-		:mRandomGenerator((unsigned int)system_clock::now().time_since_epoch().count())
-	{
-		mHaveMacAddress = Platform::getMACAddress(mMACAddress);
-	}
-
-	void appendHex(String& str, UINT8 n)
-	{
-		static const char* digits = "0123456789abcdef";
-		str += digits[(n >> 4) & 0xF];
-		str += digits[n & 0xF];
-	}
-
-
-	void appendHex(String& str, UINT16 n)
-	{
-		appendHex(str, UINT8(n >> 8));
-		appendHex(str, UINT8(n & 0xFF));
-	}
-
-
-	void appendHex(String& str, UINT32 n)
-	{
-		appendHex(str, UINT16(n >> 16));
-		appendHex(str, UINT16(n & 0xFFFF));
-	}
-
-
 	String UUIDGenerator::generateRandom()
 	{
-		mSpinLock.lock();
-
-		auto timestamp = system_clock::now().time_since_epoch().count();
-
-		UINT32 timeLow = UINT32(timestamp & 0xFFFFFFFF);
-		UINT16 timeMid = UINT16((timestamp >> 32) & 0xFFFF);
-		UINT16 timeHiAndVersion = UINT16((timestamp >> 48) & 0x0FFF) + (UUIDV_TimeBased << 12);
-		UINT16 clockSeq = (UINT16(mRandomGenerator() >> 4) & 0x3FFF) | 0x8000;
-
-		String result;
-		result.reserve(36);
-
-		appendHex(result, timeLow);
-		result += '-';
-
-		appendHex(result, timeMid);
-		result += '-';
-
-		appendHex(result, timeHiAndVersion);
-		result += '-';
-
-		appendHex(result, clockSeq);
-		result += '-';
-
-		if (mHaveMacAddress)
-		{
-			for (int i = 0; i < sizeof(MACAddress); ++i)
-				appendHex(result, mMACAddress.value[i]);
-		}
-		else
-		{
-			for (int i = 0; i < sizeof(MACAddress); ++i)
-				appendHex(result, (UINT8)(mRandomGenerator() % 255));
-		}
-
-		mSpinLock.unlock();
-		return result;
+		return Platform::generateUUID();
 	}
 };

+ 14 - 0
BansheeCore/Source/Win32/BsWin32Platform.cpp

@@ -351,6 +351,20 @@ namespace BansheeEngine
 		return false;
 	}
 
+	String Platform::generateUUID()
+	{
+		UUID uuid;
+		UuidCreate(&uuid);
+
+		UINT8* uuidStr;
+		UuidToStringA(&uuid, &uuidStr);
+
+		String output((char*)uuidStr);
+		RpcStringFreeA(&uuidStr);
+
+		return output;
+	}
+
 	double Platform::queryPerformanceTimerMs()
 	{
 		LARGE_INTEGER counterValue;

+ 0 - 6
TODO.txt

@@ -6,12 +6,6 @@ Assembly refresh
 When serializing Camera I cannot save the reference to RenderTexture. Make it a Resource?
 Possibly set up automatic refresh in debug mode after initialization? As an ad-hoc unit test
 
-----------------------------------------------------------------------
-Project library
-
-My GUID generation is screwed up. If multiple GUIDs are generated in succession then the timestamp will remain
-the same and the only variable will be the 4byte random number, which can sometimes end up identical to the previous number.
-
 ----------------------------------------------------------------------
 C# Material/Shader: