Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.30 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Viveport;
  5.  
  6. public class ViveportTest : MonoBehaviour
  7. {
  8.     public struct LicenseCheckResult
  9.     {
  10.         public bool isDone;
  11.         public bool isSuccess;
  12.  
  13.         // for success
  14.         public long issueTime;
  15.         public long expirationTime;
  16.         public int latestVersion;
  17.         public bool updateRequired;
  18.  
  19.         // for fail
  20.         public int errorCode;
  21.         public string errorMessage;
  22.  
  23.         public override string ToString()
  24.         {
  25.             return "LicenseCheckResult" +
  26.                  "\n    isDone: " + isDone +
  27.                  "\n    isSuccess: " + isSuccess +
  28.                  "\n    issueTime: " + issueTime +
  29.                  "\n    expirationTime: " + expirationTime +
  30.                  "\n    latestVersion: " + latestVersion +
  31.                  "\n    updateRequired: " + updateRequired +
  32.                  "\n    errorCode: " + errorCode +
  33.                  "\n    errorMessage: " + errorMessage;
  34.         }
  35.  
  36.         public void Reset()
  37.         {
  38.             isDone = default(bool);
  39.             isSuccess = default(bool);
  40.             issueTime = default(long);
  41.             expirationTime = default(long);
  42.             latestVersion = default(int);
  43.             updateRequired = default(bool);
  44.             errorCode = default(int);
  45.             errorMessage = default(string);
  46.         }
  47.     }
  48.  
  49.  
  50.     static string APP_ID = "------------------------------------";
  51.     static string APP_KEY = "------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------";
  52.     static string kUnknownUserId = "UnknownU-serI-dUnk-nown-UserIdUnknow";  // value returned from viveport_api.dll if user not signed in
  53.     private readonly static object initLock = new object();
  54.  
  55.     public static bool IsInitialized { get { return isSdkInitDone && isUserStatsInitDone && licenseCheckResult.isDone; } }
  56.     public static bool IsInitializationSuccess { get { return IsSkdInitializedSuccess && IsUserStatsInitializedSuccess && DoesUserHaveLicense; } }
  57.     public static bool IsSkdInitializedSuccess { get { return isSdkInitDone && isSdkInitResult == 0; } }
  58.     public static bool IsUserStatsInitializedSuccess { get { return isUserStatsInitDone && isUserStatsInitResult == 0; } }
  59.     private static LicenseCheckResult licenseCheckResult;
  60.  
  61.     public static bool DoesUserHaveLicense
  62.     {
  63.         get
  64.         {
  65.             return licenseCheckResult.isSuccess;
  66.         }
  67.     }
  68.     private static bool isSdkInitDone;
  69.     private static int isSdkInitResult;
  70.     private static bool isUserStatsInitDone;
  71.     private static int isUserStatsInitResult;
  72.  
  73.     private Coroutine initCoroutine;
  74.  
  75.  
  76.     public void Init()
  77.     {
  78.         // 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
  79.         if (!IsViveportAppInstalled())
  80.         {
  81.             Debug.LogError("Viveport: Viveport software not installed");
  82.             InitStatusHandler(-1);
  83.             return;
  84.         }
  85.  
  86.         ResetToInitialState();
  87.  
  88.         // Hack workaround: viveport sdk (android) (or us!) was executing UserStats.IsReady() callback not on main thread, and everything was a mess
  89.         // We use locks and coroutine to avoid that
  90.         if (initCoroutine != null)
  91.             StopCoroutine(initCoroutine);
  92.         initCoroutine = StartCoroutine(InitCoroutine());
  93.     }
  94.  
  95.     public static void Shutdown()
  96.     {
  97.         if (isSdkInitResult != 0)
  98.         {
  99.             Api.Shutdown(ShutdownHandler);
  100.         }
  101.     }
  102.  
  103.     public static bool IsUserSignedIn()
  104.     {
  105.         return IsInitialized && GetUserId() != kUnknownUserId;
  106.     }
  107.  
  108.     public static bool DoesSignedInUserHaveLicense()
  109.     {
  110.         return IsUserSignedIn();
  111.     }
  112.  
  113.     public static string GetUserId()
  114.     {
  115.         if (!IsInitialized)
  116.             return kUnknownUserId;
  117.         return User.GetUserId();
  118.     }
  119.  
  120.     public static string GetUserName()
  121.     {
  122.         if (!IsInitialized)
  123.             return "<unknown user>";
  124.         return User.GetUserName();
  125.     }
  126.  
  127.  
  128.     public static bool IsViveportAppInstalled()
  129.     {
  130.         return true;
  131.     }
  132.  
  133.     private IEnumerator InitCoroutine()
  134.     {
  135.         // wait for SDK init
  136.         Debug.Log("Initializing ViveportSDK...");
  137.         Api.Init(InitStatusHandler, APP_ID);
  138.         // not locked, just reading as atomic operation
  139.         while (!isSdkInitDone)
  140.         {
  141.             //Debug.Log("Waiting on Init of Viveport SDK...");
  142.             yield return null;
  143.         }
  144.         if (IsSkdInitializedSuccess)
  145.         {
  146.             Debug.Log("Viveport: InitStatusHandler is successful");
  147.         }
  148.         else
  149.         {
  150.             Debug.LogError("Viveport: InitStatusHandler error : " + isSdkInitResult + "\nInit error, close your app and make sure your app ID is correct or not.");
  151.             yield break;
  152.         }
  153.  
  154.         // wait for UserStats init
  155.         Debug.Log("Initializing Viveport User Stats...");
  156.         UserStats.IsReady(IsReadyHandler);
  157.         // not locked, just reading as atomic operation
  158.         while (!isUserStatsInitDone)
  159.         {
  160.             //Debug.Log("Waiting on Init of Viveport User Stats...");
  161.             yield return null;
  162.         }
  163.         if (IsUserStatsInitializedSuccess)
  164.         {
  165.             Debug.Log("Viveport: IsReadyHandler is successful");
  166.         }
  167.         else
  168.         {
  169.             Debug.LogError("Viveport: IsReadyHandler error: " + isUserStatsInitResult + "\nIsReady error, close your app and make sure the viveport is launched normally.");
  170.             yield break;
  171.         }
  172.  
  173.         // wait for license check
  174.         // if it fails on dev, still mark it as passed
  175.         Debug.Log("Checking Viveport license for App ID: " + APP_ID);
  176.         Api.GetLicense(new LicenseChecker(OnLicenseCheckSuccess, OnLicenseCheckFailure), APP_ID, APP_KEY);
  177.         // not locked, just reading as atomic operation
  178.         while (!licenseCheckResult.isDone)
  179.         {
  180.             //Debug.Log("Waiting on License Check...");
  181.             yield return null;
  182.         }
  183.  
  184.         yield return new WaitForSeconds(3);
  185.         //This call to get name will take between 0.25 to 0.5 seconds and will block the thread.
  186.         Debug.Log("My Username is " + User.GetUserName());
  187.  
  188.         if (licenseCheckResult.isSuccess)
  189.         {
  190.             Debug.Log("Viveport: License check: success\n" + licenseCheckResult.ToString());
  191.         }
  192.         else
  193.         {
  194.             Debug.LogError(
  195.                 "Viveport: License check: failed [" + licenseCheckResult.errorCode + "] " + licenseCheckResult.errorMessage +
  196.                 "\nPlease see: https://developer.viveport.com/documents/sdk/en/errorcodes.html");
  197.  
  198.             // TODO Kova: re-enable license fail
  199.             //if (VRCApplicationSetup.Instance.ServerEnvironment == VRC.Core.ApiServerEnvironment.Dev)
  200.             {
  201.                 // debug enable license for testing
  202.                 Debug.Log("Viveport: (enabling license anyway for dev build)");
  203.                 licenseCheckResult.isSuccess = true;
  204.             }
  205.  
  206.             yield break;
  207.         }
  208.  
  209.         Debug.Log("Viveport: Initialization flow done and succeeded.");
  210.     }
  211.  
  212.     private static void InitStatusHandler(int nResult)
  213.     {
  214.         lock (initLock)
  215.         {
  216.             isSdkInitDone = true;
  217.             isSdkInitResult = nResult;
  218.         }
  219.     }
  220.  
  221.     private static void IsReadyHandler(int nResult)
  222.     {
  223.         // this is callback is called on another thread, no logs or adding anything else please
  224.         lock (initLock)
  225.         {
  226.             isUserStatsInitDone = true;
  227.             isUserStatsInitResult = nResult;
  228.         }
  229.     }
  230.  
  231.     private static void OnLicenseCheckSuccess(long issueTime, long expirationTime, int latestVersion, bool updateRequired)
  232.     {
  233.         lock (initLock)
  234.         {
  235.             licenseCheckResult.isDone = true;
  236.             licenseCheckResult.isSuccess = true;
  237.             licenseCheckResult.issueTime = issueTime;
  238.             licenseCheckResult.expirationTime = expirationTime;
  239.             licenseCheckResult.latestVersion = latestVersion;
  240.             licenseCheckResult.updateRequired = updateRequired;
  241.         }
  242.     }
  243.  
  244.     private static void OnLicenseCheckFailure(int errorCode, string errorMessage)
  245.     {
  246.         lock (initLock)
  247.         {
  248.             licenseCheckResult.isDone = true;
  249.             licenseCheckResult.isSuccess = false;
  250.             licenseCheckResult.errorCode = errorCode;
  251.             licenseCheckResult.errorMessage = errorMessage;
  252.         }
  253.     }
  254.  
  255.     private static void ShutdownHandler(int nResult)
  256.     {
  257.         if (nResult == 0)
  258.         {
  259.             Debug.Log("Viveport: ShutdownHandler is successful");
  260.         }
  261.         else
  262.         {
  263.             Debug.Log("Viveport: ShutdownHandler error: " + nResult + ", shutting down anyway");
  264.         }
  265.     }
  266.  
  267.     private static void ResetToInitialState()
  268.     {
  269.         lock (initLock)
  270.         {
  271.             isSdkInitDone = false;
  272.             isSdkInitResult = int.MinValue;
  273.             isUserStatsInitDone = false;
  274.             isUserStatsInitResult = int.MinValue;
  275.  
  276.             licenseCheckResult.Reset();
  277.         }
  278.     }
  279.  
  280.     class LicenseChecker : Api.LicenseChecker
  281.     {
  282.         public delegate void OnSuccessDelegate(long issueTime, long expirationTime, int latestVersion, bool updateRequired);
  283.         OnSuccessDelegate onSuccess;
  284.  
  285.         public delegate void OnFailureDelegate(int errorCode, string errorMessage);
  286.         OnFailureDelegate onFailure;
  287.  
  288.         public LicenseChecker(OnSuccessDelegate onSuccess, OnFailureDelegate onFailure)
  289.         {
  290.             this.onSuccess = onSuccess;
  291.             this.onFailure = onFailure;
  292.         }
  293.  
  294.         public override void OnSuccess(long issueTime, long expirationTime, int latestVersion, bool updateRequired)
  295.         {
  296.             if (onSuccess != null)
  297.                 onSuccess(issueTime, expirationTime, latestVersion, updateRequired);
  298.         }
  299.  
  300.         public override void OnFailure(int errorCode, string errorMessage)
  301.         {
  302.             if (onFailure != null)
  303.                 onFailure(errorCode, errorMessage);
  304.         }
  305.     }
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement