Skip to content

Instantly share code, notes, and snippets.

@corycorvus
Created July 21, 2020 21:30
Show Gist options
  • Save corycorvus/9a981f397c4ce86082f7f8834e2e936e to your computer and use it in GitHub Desktop.
Save corycorvus/9a981f397c4ce86082f7f8834e2e936e to your computer and use it in GitHub Desktop.
using System.Collections;
using UnityEngine;
using Viveport;
// Viveport DRM Documentation:
// https://developer.vive.com/resources/documentation/viveport-sdk/apis/drm-api/
// NOTE: To use Unity functions such as Application.Quit within callback functions requires running on the main thread.
// Please attach 'MainThreadDispatcher.cs' to an object in your scene
// https://developer.vive.com/resources/documentation/viveport-sdk/integration-with-viveport-sdk/for-unity-developers/using-the-unity-main-thread-dispatcher-script/
[RequireComponent(typeof(MainThreadDispatcher))]
public class ViveportDRM : MonoBehaviour
{
// Get a VIVEPORT ID and VIVEPORT Key from the VIVEPORT Developer Console:
// https://developer.viveport.com/console/titles
// https://developer.vive.com/resources/documentation/viveport-sdk/overview/about-viveport-id-and-viveport-key/
static string VIVEPORT_ID = "VIVEPORT ID of the content"; // replace with developer VIVEPORT ID
static string VIVEPORT_KEY = "VIVEPORT Key of the content"; // replace with developer VIVEPORT Key
private const int SUCCESS = 0;
private static bool bInitComplete = false;
void Start()
{
Api.Init(InitStatusHandler, VIVEPORT_ID); // initialize VIVEPORT platform
Invoke("CheckInitStatus", 10); // check that VIVEPORT Init succeeded
}
void OnDestroy()
{
Api.Shutdown(ShutdownHandler);
}
private static void InitStatusHandler(int nResult) // The callback of Api.init()
{
if (nResult == SUCCESS)
{
Debug.Log("VIVEPORT init pass");
Api.GetLicense(new MyLicenseChecker(), VIVEPORT_ID, VIVEPORT_KEY); // the response of Api.Init() is success, continue using Api.GetLicense() API
bInitComplete = true;
}
else
{
Debug.Log("VIVEPORT init fail");
Application.Quit();
return; // the response of Api.Init() is fail
}
}
private static void ShutdownHandler(int nResult) // The callback of Api.Shutdown()
{
if (nResult == SUCCESS)
{
Application.Quit(); // the response of Api.Shutdown() is success, close the content
}
else
{
return; // the response of Api.Shutdown() is fail
}
}
private void CheckInitStatus()
{
if (!bInitComplete)
{
Debug.LogWarning("Viveport init check fail"); // init requires VIVEPORT app installed and online connection
Application.Quit();
}
else
Debug.Log("Viveport init check pass");
}
class MyLicenseChecker : Api.LicenseChecker
{
public override void OnSuccess(long issueTime, long expirationTime, int latestVersion, bool updateRequired)
{
// the response of Api.GetLicense() is DRM success, user is allowed to use the content and continue with content flow
Debug.Log("Viveport DRM pass");
Debug.Log("issueTime: " + issueTime);
Debug.Log("expirationTime: " + expirationTime);
MainThreadDispatcher.Instance().Enqueue(SuccessAction());
}
public override void OnFailure(int errorCode, string errorMessage)
{
// the response of Api.GetLicense() is DRM fail, user is not allowed to use the content
MainThreadDispatcher.Instance().Enqueue(FailAction());
}
// Use these methods to call Unity functions from the API callbacks on the main thread
IEnumerator SuccessAction()
{
// todo: continue loading game or load next scene
Debug.LogWarning("Viveport DRM Success");
yield return null;
}
IEnumerator FailAction()
{
Debug.LogWarning("Viveport DRM Fail: Quitting");
Application.Quit();
yield return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment