Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.4 KiB

  1. using System;
  2. using UnityEngine;
  3. using Oculus.Platform;
  4. using Oculus.Platform.Models;
  5. namespace Oculus.Platform.Samples.EntitlementCheck
  6. {
  7. public class EntitlementCheck : MonoBehaviour
  8. {
  9. // Implements a default behavior for entitlement check failures by simply exiting the app.
  10. // Set to false if the app wants to provide custom logic to handle entitlement check failures.
  11. // For example, the app can instead display a modal dialog to the user and exit gracefully.
  12. public bool exitAppOnFailure = true;
  13. // The app can optionally subscribe to these events to do custom entitlement check logic.
  14. public static event Action UserFailedEntitlementCheck;
  15. public static event Action UserPassedEntitlementCheck;
  16. void Start()
  17. {
  18. try
  19. {
  20. // Init the Oculust Platform SDK and send an entitlement check request.
  21. if (!Oculus.Platform.Core.IsInitialized())
  22. {
  23. Oculus.Platform.Core.Initialize();
  24. }
  25. Entitlements.IsUserEntitledToApplication().OnComplete(EntitlementCheckCallback);
  26. }
  27. catch
  28. {
  29. // Treat any potential initialization exceptions as an entitlement check failure.
  30. HandleEntitlementCheckResult(false);
  31. }
  32. }
  33. // Called when the Oculus Platform completes the async entitlement check request and a result is available.
  34. void EntitlementCheckCallback(Message msg)
  35. {
  36. // If the user passed the entitlement check, msg.IsError will be false.
  37. // If the user failed the entitlement check, msg.IsError will be true.
  38. HandleEntitlementCheckResult(msg.IsError == false);
  39. }
  40. void HandleEntitlementCheckResult(bool result)
  41. {
  42. if (result) // User passed entitlement check
  43. {
  44. Debug.Log("Oculus user entitlement check successful.");
  45. try
  46. {
  47. // Raise the user passed entitlement check event if the app subscribed a handler to it.
  48. if (UserPassedEntitlementCheck != null)
  49. {
  50. UserPassedEntitlementCheck();
  51. }
  52. }
  53. catch
  54. {
  55. // Suppressing any exceptions to avoid potential exceptions in the app-provided event handler.
  56. Debug.LogError("Suppressed exception in app-provided UserPassedEntitlementCheck() event handler.");
  57. }
  58. }
  59. else // User failed entitlement check
  60. {
  61. try
  62. {
  63. // Raise the user failed entitlement check event if the app subscribed a handler to it.
  64. if (UserFailedEntitlementCheck != null)
  65. {
  66. UserFailedEntitlementCheck();
  67. }
  68. }
  69. catch
  70. {
  71. // Suppressing any exceptions to avoid potential exceptions in the app-provided event handler.
  72. // Ensures the default entitlement check behavior will still execute, if enabled.
  73. Debug.LogError("Suppressed exception in app-provided UserFailedEntitlementCheck() event handler.");
  74. }
  75. if (exitAppOnFailure)
  76. {
  77. // Implements a default behavior for an entitlement check failure -- log the failure and exit the app.
  78. Debug.LogError("Oculus user entitlement check failed. Exiting now.");
  79. #if UNITY_EDITOR
  80. UnityEditor.EditorApplication.isPlaying = false;
  81. #else
  82. UnityEngine.Application.Quit();
  83. #endif
  84. }
  85. else
  86. {
  87. Debug.LogError("Oculus user entitlement check failed.");
  88. }
  89. }
  90. }
  91. }
  92. }