Documentation for the Unity C# Library
Loading...
Searching...
No Matches
ApexUtils.cs
Go to the documentation of this file.
1
2using Newtonsoft.Json.Linq;
4using System;
5using System.Linq;
6using System.Net.NetworkInformation;
7using System.Net.Sockets;
8using TinCan;
9using TinCan.Json;
10using UnityEngine;
11
13{
14 public static class ApexUtils
15 {
16 public static string INVALID_IP = "0.0.0.0";
17 public static string INVALID_IPV6 = "::/0";
18 public static string HOME_IP = "127.0.0.1";
19 public static string HOME_IPV6 = "::1/128";
20
21 public static string GetMacAddress()
22 {
23 string macAddress = "";
24 try
25 {
26 PhysicalAddress physicalMacAddress = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(nic => nic.OperationalStatus == OperationalStatus.Up && nic.NetworkInterfaceType != NetworkInterfaceType.Loopback)?.GetPhysicalAddress();
27 macAddress = string.Join(":", Array.ConvertAll(physicalMacAddress.GetAddressBytes(), addressBytes => addressBytes.ToString("X2")));
28 }
29 catch(Exception exception)
30 {
31 Debug.LogError("[ApexUtils] " + exception.Message);
32 }
33 return macAddress;
34 }
35
36 public static string GetLocalIP()
37 {
38 string currentIp = GetIP(ADDRESSFAM.IPv4);
39 if(currentIp == INVALID_IP)
40 {
41 currentIp = GetIP(ADDRESSFAM.IPv6);
42 }
43
44 return currentIp;
45 }
46
47 private static string GetIP(ADDRESSFAM Addfam)
48 {
49 //Return null if ADDRESSFAM is Ipv6 but Os does not support it
50 if (Addfam == ADDRESSFAM.IPv6 && !Socket.OSSupportsIPv6)
51 {
52 return null;
53 }
54
55 string output = INVALID_IP;
56
57 foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
58 {
59#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
60 NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211;
61 NetworkInterfaceType _type2 = NetworkInterfaceType.Ethernet;
62
63 if ((item.NetworkInterfaceType == _type1 || item.NetworkInterfaceType == _type2) && item.OperationalStatus == OperationalStatus.Up)
64#endif
65 {
66 foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
67 {
68 //IPv4
69 if (Addfam == ADDRESSFAM.IPv4)
70 {
71 if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
72 {
73 if (ip.Address.ToString() != HOME_IP)
74 output = ip.Address.ToString();
75 }
76 }
77
78 //IPv6
79 else if (Addfam == ADDRESSFAM.IPv6)
80 {
81 if (ip.Address.AddressFamily == AddressFamily.InterNetworkV6)
82 {
83 if (ip.Address.ToString() != HOME_IPV6)
84 output = ip.Address.ToString();
85 }
86 }
87 }
88 }
89 }
90 return output;
91 }
92 public enum ADDRESSFAM
93 {
94 IPv4, IPv6
95 }
96
97 public static JObject ConvertStringToJObject(string json)
98 {
99 StringOfJSON stringOfJson = new StringOfJSON(json);
100 return stringOfJson.toJObject();
101 }
102
103 public static string GetSDKVersion()
104 {
105 return "1.01.03";
106 }
107 }
108}
109
110public static class StringExtensions
111{
112 public static bool Contains(this string source, string value, StringComparison comp)
113 {
114 return source?.IndexOf(value, comp) >= 0;
115 }
116}
static string GetSDKVersion()
Definition ApexUtils.cs:103
static string INVALID_IPV6
Definition ApexUtils.cs:17
static string GetMacAddress()
Definition ApexUtils.cs:21
static JObject ConvertStringToJObject(string json)
Definition ApexUtils.cs:97
static string GetIP(ADDRESSFAM Addfam)
Definition ApexUtils.cs:47
static string GetLocalIP()
Definition ApexUtils.cs:36
static bool Contains(this string source, string value, StringComparison comp)
Definition ApexUtils.cs:112