Created
June 22, 2014 17:06
-
-
Save noisecrime/fb80151510db85d173de to your computer and use it in GitHub Desktop.
Unity - Validate Mac Address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NoiseCrime Gist | |
// 2014.06.22 | |
// Unity Version: 3.5.7+ | |
// Class to examine all Mac Addresses on Machine, selects the fastest to use as a validation ID. | |
// Can specify multiple valid Mac Addresses. | |
// Can Quit Application if valid Mac not found. | |
using UnityEngine; | |
using System.Collections; | |
using System.Net.NetworkInformation; | |
using System; | |
public class App_ValidateMacAddress : MonoBehaviour | |
{ | |
public string[] _validMacAddresses; | |
public bool _requireMacAddress = false; | |
public bool _logMacAddresses = false; | |
public bool _skipInEditor = true; | |
void Start () | |
{ | |
if(_skipInEditor && Application.isEditor) return; | |
// Do Not Log data if not in editor | |
_logMacAddresses = (_logMacAddresses || !Application.isEditor); | |
// Get the best/fastest Nic Mac Address | |
string bestMacAddress = GetFastestMacAddress(); | |
// Validation | |
if(_requireMacAddress) | |
{ | |
if(ValidateMacAddress(bestMacAddress)) | |
{ | |
Debug.Log("App_ValidateMacAddress: Validated: " + bestMacAddress); | |
} | |
else | |
{ | |
Debug.LogWarning("App_ValidateMacAddress: Validation failed! Now Quitting: " + bestMacAddress); | |
Application.Quit(); | |
} | |
} | |
} | |
void onEnable() {} // Used to force script execution order | |
/// <summary> | |
/// Validates the given mac address (found via searching NetworkInterface) against a list of approved Mac Addresses. | |
/// </summary> | |
/// <returns> | |
/// True if a valid Mac address. | |
/// </returns> | |
bool ValidateMacAddress(string bestMacAddress) | |
{ | |
foreach(string mac in _validMacAddresses) | |
{ | |
if(mac == bestMacAddress) return true; | |
} | |
return false; | |
} | |
/// <summary> | |
/// Gets the fastest mac address. | |
/// As there are many Mac Addresses on a PC we use the 'speed' as a selection criteria, along with a minimium address length. | |
/// However in cases where there are multiple addresses with the same speed it is unknown if the same Mac is selected each time. | |
/// nic.type can return Ethernet, Loopback, Tunnel, maybe others. | |
/// </summary> | |
/// <returns> | |
/// The fastest mac address. | |
/// </returns> | |
string GetFastestMacAddress() | |
{ | |
Debug.Log("App_ValidateMacAddress: GetFastestMacAddress"); | |
string results = ""; | |
string bestMacAddress = ""; | |
long bestMacMaxSpeed = -1; | |
int MIN_MAC_ADDR_LENGTH = 12; | |
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) | |
{ | |
string thisMacAddress = nic.GetPhysicalAddress().ToString(); | |
if (nic.Speed > bestMacMaxSpeed && !String.IsNullOrEmpty(thisMacAddress) && thisMacAddress.Length >= MIN_MAC_ADDR_LENGTH ) | |
{ | |
bestMacMaxSpeed = nic.Speed; | |
bestMacAddress = thisMacAddress; | |
} | |
if(_logMacAddresses) | |
{ | |
if(String.IsNullOrEmpty(thisMacAddress)) thisMacAddress = "Unknown"; | |
results += string.Format("MAC Address: {0, -24} Type: {1, -12} Speed: {2}\n", thisMacAddress, nic.NetworkInterfaceType, nic.Speed ); | |
} | |
} | |
if(_logMacAddresses) | |
{ | |
Debug.Log(results); | |
Debug.Log("BestMacAddress: " + bestMacAddress + " Speed: " + bestMacMaxSpeed); | |
} | |
return bestMacAddress; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment