My room-mate was kvetching at me about my use of "ulong" in my code to represent a GUID within the World of Warcraft game client. I pointed out to her that I also had a structure that worked like a union too, in case I needed to access the low-word or high-word for anything, but she just claimed it was an ugly hack (she can get really crabby around the holidays), especially when it came to assigning and reading the actual GUID because of the indirection operator that was needed.
To appease her I decided to modify my structure to handle implicit assignment and conversion between the "ulong"-type, cleaning up the code, and giving explicit meaning to the use of "ulong" anywhere in the code base.
Here's my solution, it can be dropped in to your project, and instead of using "ulong" to represent your 64-bit GUIDs, you can just use this structure.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace WarcraftVoodoo
{
[StructLayout(LayoutKind.Explicit)]
public struct GameGUID
{
[FieldOffset(0)]
public UInt64 GUID;
[FieldOffset(0)]
public UInt32 LowWord;
[FieldOffset(4)]
public UInt32 HighWord;
public static implicit operator GameGUID(ulong rhs)
{
GameGUID g = new GameGUID();
g.GUID = rhs;
return g;
}
public static implicit operator ulong(GameGUID rhs)
{
return rhs.GUID;
}
}
}
To use the above in your code:
Code:
GameGUID targetGUID = 64646464; // made up GUID to illustrate point
m_gameClient.Injection.SelectUnit(targetGUID); // conversion to ulong for the function call is implicit
Let me know if I overlooked anything.