BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / Platform / Samples / EntitlementCheck / EntitlementCheck.cs

EntitlementCheck.cs

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