Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Viveport;
- public class ViveportTest : MonoBehaviour
- {
- public struct LicenseCheckResult
- {
- public bool isDone;
- public bool isSuccess;
- // for success
- public long issueTime;
- public long expirationTime;
- public int latestVersion;
- public bool updateRequired;
- // for fail
- public int errorCode;
- public string errorMessage;
- public override string ToString()
- {
- return "LicenseCheckResult" +
- "\n isDone: " + isDone +
- "\n isSuccess: " + isSuccess +
- "\n issueTime: " + issueTime +
- "\n expirationTime: " + expirationTime +
- "\n latestVersion: " + latestVersion +
- "\n updateRequired: " + updateRequired +
- "\n errorCode: " + errorCode +
- "\n errorMessage: " + errorMessage;
- }
- public void Reset()
- {
- isDone = default(bool);
- isSuccess = default(bool);
- issueTime = default(long);
- expirationTime = default(long);
- latestVersion = default(int);
- updateRequired = default(bool);
- errorCode = default(int);
- errorMessage = default(string);
- }
- }
- static string APP_ID = "------------------------------------";
- static string APP_KEY = "------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------";
- static string kUnknownUserId = "UnknownU-serI-dUnk-nown-UserIdUnknow"; // value returned from viveport_api.dll if user not signed in
- private readonly static object initLock = new object();
- public static bool IsInitialized { get { return isSdkInitDone && isUserStatsInitDone && licenseCheckResult.isDone; } }
- public static bool IsInitializationSuccess { get { return IsSkdInitializedSuccess && IsUserStatsInitializedSuccess && DoesUserHaveLicense; } }
- public static bool IsSkdInitializedSuccess { get { return isSdkInitDone && isSdkInitResult == 0; } }
- public static bool IsUserStatsInitializedSuccess { get { return isUserStatsInitDone && isUserStatsInitResult == 0; } }
- private static LicenseCheckResult licenseCheckResult;
- public static bool DoesUserHaveLicense
- {
- get
- {
- return licenseCheckResult.isSuccess;
- }
- }
- private static bool isSdkInitDone;
- private static int isSdkInitResult;
- private static bool isUserStatsInitDone;
- private static int isUserStatsInitResult;
- private Coroutine initCoroutine;
- public void Init()
- {
- // hack: Init hangs for a long time if the Vive software is not installed. So check if it's installed and fail early if not
- if (!IsViveportAppInstalled())
- {
- Debug.LogError("Viveport: Viveport software not installed");
- InitStatusHandler(-1);
- return;
- }
- ResetToInitialState();
- // Hack workaround: viveport sdk (android) (or us!) was executing UserStats.IsReady() callback not on main thread, and everything was a mess
- // We use locks and coroutine to avoid that
- if (initCoroutine != null)
- StopCoroutine(initCoroutine);
- initCoroutine = StartCoroutine(InitCoroutine());
- }
- public static void Shutdown()
- {
- if (isSdkInitResult != 0)
- {
- Api.Shutdown(ShutdownHandler);
- }
- }
- public static bool IsUserSignedIn()
- {
- return IsInitialized && GetUserId() != kUnknownUserId;
- }
- public static bool DoesSignedInUserHaveLicense()
- {
- return IsUserSignedIn();
- }
- public static string GetUserId()
- {
- if (!IsInitialized)
- return kUnknownUserId;
- return User.GetUserId();
- }
- public static string GetUserName()
- {
- if (!IsInitialized)
- return "<unknown user>";
- return User.GetUserName();
- }
- public static bool IsViveportAppInstalled()
- {
- return true;
- }
- private IEnumerator InitCoroutine()
- {
- // wait for SDK init
- Debug.Log("Initializing ViveportSDK...");
- Api.Init(InitStatusHandler, APP_ID);
- // not locked, just reading as atomic operation
- while (!isSdkInitDone)
- {
- //Debug.Log("Waiting on Init of Viveport SDK...");
- yield return null;
- }
- if (IsSkdInitializedSuccess)
- {
- Debug.Log("Viveport: InitStatusHandler is successful");
- }
- else
- {
- Debug.LogError("Viveport: InitStatusHandler error : " + isSdkInitResult + "\nInit error, close your app and make sure your app ID is correct or not.");
- yield break;
- }
- // wait for UserStats init
- Debug.Log("Initializing Viveport User Stats...");
- UserStats.IsReady(IsReadyHandler);
- // not locked, just reading as atomic operation
- while (!isUserStatsInitDone)
- {
- //Debug.Log("Waiting on Init of Viveport User Stats...");
- yield return null;
- }
- if (IsUserStatsInitializedSuccess)
- {
- Debug.Log("Viveport: IsReadyHandler is successful");
- }
- else
- {
- Debug.LogError("Viveport: IsReadyHandler error: " + isUserStatsInitResult + "\nIsReady error, close your app and make sure the viveport is launched normally.");
- yield break;
- }
- // wait for license check
- // if it fails on dev, still mark it as passed
- Debug.Log("Checking Viveport license for App ID: " + APP_ID);
- Api.GetLicense(new LicenseChecker(OnLicenseCheckSuccess, OnLicenseCheckFailure), APP_ID, APP_KEY);
- // not locked, just reading as atomic operation
- while (!licenseCheckResult.isDone)
- {
- //Debug.Log("Waiting on License Check...");
- yield return null;
- }
- yield return new WaitForSeconds(3);
- //This call to get name will take between 0.25 to 0.5 seconds and will block the thread.
- Debug.Log("My Username is " + User.GetUserName());
- if (licenseCheckResult.isSuccess)
- {
- Debug.Log("Viveport: License check: success\n" + licenseCheckResult.ToString());
- }
- else
- {
- Debug.LogError(
- "Viveport: License check: failed [" + licenseCheckResult.errorCode + "] " + licenseCheckResult.errorMessage +
- "\nPlease see: https://developer.viveport.com/documents/sdk/en/errorcodes.html");
- // TODO Kova: re-enable license fail
- //if (VRCApplicationSetup.Instance.ServerEnvironment == VRC.Core.ApiServerEnvironment.Dev)
- {
- // debug enable license for testing
- Debug.Log("Viveport: (enabling license anyway for dev build)");
- licenseCheckResult.isSuccess = true;
- }
- yield break;
- }
- Debug.Log("Viveport: Initialization flow done and succeeded.");
- }
- private static void InitStatusHandler(int nResult)
- {
- lock (initLock)
- {
- isSdkInitDone = true;
- isSdkInitResult = nResult;
- }
- }
- private static void IsReadyHandler(int nResult)
- {
- // this is callback is called on another thread, no logs or adding anything else please
- lock (initLock)
- {
- isUserStatsInitDone = true;
- isUserStatsInitResult = nResult;
- }
- }
- private static void OnLicenseCheckSuccess(long issueTime, long expirationTime, int latestVersion, bool updateRequired)
- {
- lock (initLock)
- {
- licenseCheckResult.isDone = true;
- licenseCheckResult.isSuccess = true;
- licenseCheckResult.issueTime = issueTime;
- licenseCheckResult.expirationTime = expirationTime;
- licenseCheckResult.latestVersion = latestVersion;
- licenseCheckResult.updateRequired = updateRequired;
- }
- }
- private static void OnLicenseCheckFailure(int errorCode, string errorMessage)
- {
- lock (initLock)
- {
- licenseCheckResult.isDone = true;
- licenseCheckResult.isSuccess = false;
- licenseCheckResult.errorCode = errorCode;
- licenseCheckResult.errorMessage = errorMessage;
- }
- }
- private static void ShutdownHandler(int nResult)
- {
- if (nResult == 0)
- {
- Debug.Log("Viveport: ShutdownHandler is successful");
- }
- else
- {
- Debug.Log("Viveport: ShutdownHandler error: " + nResult + ", shutting down anyway");
- }
- }
- private static void ResetToInitialState()
- {
- lock (initLock)
- {
- isSdkInitDone = false;
- isSdkInitResult = int.MinValue;
- isUserStatsInitDone = false;
- isUserStatsInitResult = int.MinValue;
- licenseCheckResult.Reset();
- }
- }
- class LicenseChecker : Api.LicenseChecker
- {
- public delegate void OnSuccessDelegate(long issueTime, long expirationTime, int latestVersion, bool updateRequired);
- OnSuccessDelegate onSuccess;
- public delegate void OnFailureDelegate(int errorCode, string errorMessage);
- OnFailureDelegate onFailure;
- public LicenseChecker(OnSuccessDelegate onSuccess, OnFailureDelegate onFailure)
- {
- this.onSuccess = onSuccess;
- this.onFailure = onFailure;
- }
- public override void OnSuccess(long issueTime, long expirationTime, int latestVersion, bool updateRequired)
- {
- if (onSuccess != null)
- onSuccess(issueTime, expirationTime, latestVersion, updateRequired);
- }
- public override void OnFailure(int errorCode, string errorMessage)
- {
- if (onFailure != null)
- onFailure(errorCode, errorMessage);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement