udVersion.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using UnityEngine;
  4. namespace udSDK
  5. {
  6. [StructLayout(LayoutKind.Sequential)]
  7. public struct udVersion
  8. {
  9. public byte major;
  10. public byte minor;
  11. public byte patch;
  12. }
  13. public class UDVersion
  14. {
  15. // with no arguments, this class gets the current sdk version
  16. // alternatively, you can pass in version numbers to create a version object
  17. udVersion versionStruct;
  18. public int major, minor, patch;
  19. public UDVersion()
  20. {
  21. IntPtr pVersion = Marshal.AllocHGlobal(Marshal.SizeOf(versionStruct));
  22. udError error = udVersion_GetVersion(pVersion);
  23. if (error != udError.udE_Success)
  24. throw new Exception("Failed to get udVersion.");
  25. versionStruct = (udVersion)Marshal.PtrToStructure<udVersion>(pVersion);
  26. Marshal.FreeHGlobal(pVersion);
  27. this.major = (int)versionStruct.major;
  28. this.minor = (int)versionStruct.minor;
  29. this.patch = (int)versionStruct.patch;
  30. }
  31. public UDVersion(int majorVersion, int minorVersion, int patchVersion)
  32. {
  33. this.major = majorVersion;
  34. this.minor = minorVersion;
  35. this.patch = patchVersion;
  36. versionStruct = new udVersion
  37. {
  38. major = (byte)this.major,
  39. minor = (byte)this.minor,
  40. patch = (byte)this.patch,
  41. };
  42. }
  43. // Formats a udVersion into a sensible looking string
  44. public string Format()
  45. {
  46. return (string)(major+"."+minor+"."+patch);
  47. }
  48. // Checks for equality
  49. public bool Equals(UDVersion otherVersion)
  50. {
  51. if(major == otherVersion.major && minor == otherVersion.minor && patch == otherVersion.patch)
  52. return true;
  53. return false;
  54. }
  55. // Returns whether or not a udVersion is greater than another version
  56. // Considers "depth", so you can limit the check to major, major and minor, or major minor and patch
  57. // Default behaviour is to check against everything : major minor and patch
  58. public bool GreaterThan(UDVersion otherVersion, int depth=2)
  59. {
  60. if(major > otherVersion.major)
  61. return true;
  62. if(major < otherVersion.major || depth == 0)
  63. return false;
  64. if(minor > otherVersion.minor)
  65. return true;
  66. if(minor < otherVersion.minor || depth == 1)
  67. return false;
  68. if(patch > otherVersion.patch)
  69. return true;
  70. return false;
  71. }
  72. [DllImport(UDSDKLibrary.name)]
  73. private static extern udError udVersion_GetVersion(IntPtr pVersion);
  74. }
  75. }