udContext.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using UnityEngine;
  4. namespace udSDK
  5. {
  6. [StructLayout(LayoutKind.Sequential)]
  7. public struct udSessionInfo
  8. {
  9. public uint isOffline; //!< Is not 0 if this is an offline session (dongle or other offline license)
  10. public uint isDomain; //!< If this is not 0 then this is a domain session (0 is non-domain session)
  11. public uint isPremium; //!< If this session will have access to premium features
  12. public double expiresTimestamp; //!< The timestamp in UTC when the session will automatically end
  13. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1024)]
  14. public char[] displayName; //!< The null terminated display name of the user
  15. };
  16. /* Unity serializable version of udSessionInfo
  17. */
  18. public struct UDSessionInfo
  19. {
  20. public bool isOffline; //!< Is not 0 if this is an offline session (dongle or other offline license)
  21. public bool isDomain; //!< If this is not 0 then this is a domain session (0 is non-domain session)
  22. public bool isPremium; //!< If this session will have access to premium features
  23. public float expiresTimestamp; //!< The timestamp in UTC when the session will automatically end
  24. public string displayName; //!< The null terminated display name of the user
  25. };
  26. public class udContext
  27. {
  28. public IntPtr pContext = IntPtr.Zero;
  29. public udSessionInfo pInfo = new udSessionInfo();
  30. public udContext()
  31. {
  32. if (Application.platform == RuntimePlatform.Android)
  33. {
  34. AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  35. AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
  36. AndroidJavaClass jcsdk = new AndroidJavaClass("com.euclideon.udSDK");
  37. jcsdk.CallStatic("setupJNI", jo);
  38. }
  39. }
  40. ~udContext()
  41. {
  42. if (pContext != IntPtr.Zero) {
  43. //this does not need to be called currently:
  44. //Disconnect();
  45. }
  46. }
  47. public void IgnoreCertificateVerification(bool ignore)
  48. {
  49. if (ignore)
  50. Debug.LogWarning("WARNING: Certificate verification disabled");
  51. udConfig_IgnoreCertificateVerification(ignore);
  52. }
  53. public void Connect(string pURL, string pApplicationName, string pUsername, string pPassword)
  54. {
  55. udError error = udContext.udContext_TryResume(ref pContext, pURL, pApplicationName, pUsername, true);
  56. if (error != udError.udE_Success)
  57. error = udContext_Connect(ref pContext, pURL, pApplicationName, pUsername, pPassword);
  58. if (error == udError.udE_ServerError)
  59. throw new Exception("Could not connect to server.");
  60. else if (error == udError.udE_AuthError)
  61. throw new Exception("Username or Password incorrect.");
  62. else if (error == udError.udE_OutOfSync)
  63. throw new Exception("Your clock doesn't match the remote server clock.");
  64. else if (error == udError.udE_DecryptionKeyRequired || error == udError.udE_DecryptionKeyMismatch )
  65. throw new Exception("A decryption key is required, or not matching");
  66. else if (error == udError.udE_SignatureMismatch )
  67. throw new Exception("Server not accepting digital signature.");
  68. else if (error != udError.udE_Success)
  69. throw new Exception("Unknown error occurred: " + error.ToString() + ", please try again later.");
  70. }
  71. public void Try_Resume(string pURL, string pApplicationName, string pUsername, bool tryDongle)
  72. {
  73. udError error = udContext_TryResume(ref pContext, pURL, pApplicationName, pUsername, tryDongle);
  74. if (error != udError.udE_Success)
  75. {
  76. throw new Exception("Unable to keep session alive: " + error.ToString());
  77. }
  78. }
  79. public void Disconnect(bool endSession = false)
  80. {
  81. udError error = udContext_Disconnect(ref pContext, endSession);
  82. if (error != udSDK.udError.udE_Success)
  83. throw new Exception("udContext.Disconnect failed.");
  84. }
  85. public UDSessionInfo GetSessionInfo()
  86. {
  87. udError error = udContext_GetSessionInfo(pContext, ref pInfo);
  88. if (error != udSDK.udError.udE_Success)
  89. throw new Exception("udContext.Disconnect failed.");
  90. udSessionInfo sessionInfo = pInfo;
  91. UDSessionInfo formattedInfo = new UDSessionInfo();
  92. System.DateTime epochStart = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
  93. ulong cur_time = (ulong)(System.DateTime.UtcNow - epochStart).TotalSeconds;
  94. formattedInfo.expiresTimestamp = (float)(sessionInfo.expiresTimestamp - cur_time);
  95. formattedInfo.displayName = new string(sessionInfo.displayName);
  96. formattedInfo.displayName = formattedInfo.displayName.Trim('\0');
  97. formattedInfo.isDomain = sessionInfo.isDomain != 0 ? true : false;
  98. formattedInfo.isOffline = sessionInfo.isOffline != 0 ? true : false;
  99. formattedInfo.isPremium = sessionInfo.isPremium != 0 ? true : false;
  100. return formattedInfo;
  101. }
  102. [DllImport(UDSDKLibrary.name)]
  103. private static extern udError udContext_TryResume(ref IntPtr ppContext, string pURL, string pApplicationName, string pUsername, bool tryDongle);
  104. [DllImport(UDSDKLibrary.name)]
  105. private static extern udError udContext_Connect(ref IntPtr ppContext, string pURL, string pApplicationName, string pUsername, string pPassword);
  106. [DllImport(UDSDKLibrary.name)]
  107. private static extern udError udContext_Disconnect(ref IntPtr ppContext, bool endSession);
  108. [DllImport(UDSDKLibrary.name)]
  109. private static extern udError udContext_GetSessionInfo(IntPtr pContext, ref udSessionInfo pInfo);
  110. [DllImport(UDSDKLibrary.name)]
  111. private static extern udError udConfig_IgnoreCertificateVerification(bool ignore); // this ought to be in udConfig - but we don't implement any other part of that
  112. }
  113. }