static void WaitForAccountInfoToLoad(EntitlementCallback callback) { if (string.IsNullOrEmpty(CloudProjectSettings.accessToken)) { var hasToken = false; var framesWaited = 0; EditorApplication.update += (() => { if (hasToken) { return; } if (!string.IsNullOrEmpty(CloudProjectSettings.accessToken)) { hasToken = true; GetEntitlementViaWebAsync(callback); } else { framesWaited++; if (framesWaited > k_EntitlementFramesToWait) { // no access token, assume no account and thus not-entitled: EntitlementCurrent = MarsSku.NotEntitled; hasToken = true; } } }); return; } GetEntitlementViaWebAsync(callback); }
static void GetEntitlementViaWebAsync(EntitlementCallback callback) { var userEmail = CloudProjectSettings.userName.ToLower(); if (userEmail.EndsWith("@unity3d.com") || userEmail.EndsWith("@unity.com")) { EntitlementCurrent = MarsSku.Base; SavedEntitlement = EntitlementCurrent; callback?.Invoke(true, EntitlementCurrent); return; } var url = string.Format(k_EntitlementEndpoint, CloudProjectSettings.userId); var request = UnityWebRequest.Get(url); var bufferHandler = new DownloadHandlerBuffer(); request.SetRequestHeader("Content-Type", "application/json"); request.SetRequestHeader("Cache-Control", "no-cache"); request.SetRequestHeader("Authorization", $"Bearer {CloudProjectSettings.accessToken}"); request.downloadHandler = bufferHandler; try { request.SendWebRequest().completed += ((x) => { var licenseData = bufferHandler.text; if (licenseData.Contains("Expired Access Token") || licenseData.Contains("Invalid Access Token") || licenseData.Contains("132.107") || licenseData.Contains("132.108") || licenseData.Contains("120.003")) { Debug.LogWarning($"{MARSSession.TokenExpiredMessage}:\n{licenseData}"); EntitlementTokenExpired = true; EntitlementCurrent = MarsSku.NotEntitled; } else { EntitlementTokenExpired = false; EntitlementCurrent = SearchStringForMarsSku(licenseData); SavedEntitlement = EntitlementCurrent; } s_EntitlementReceived = true; callback?.Invoke(true, s_EntitlementCurrent); }); } catch (Exception ex) { Debug.LogException(ex); s_EntitlementReceived = true; callback?.Invoke(false, EntitlementCurrent); } }
/// <summary> /// Get the mars entitlement level, either from Hub cache or asynchronously /// </summary> /// <param name="callback">a callback which returns whether entitlement query was successful, as well as the /// MarsSKUs the account is entitled to if it was</param> /// <returns></returns> public static void GetMarsEntitlementAsync(EntitlementCallback callback) { if (s_EntitlementReceived) { callback?.Invoke(true, s_EntitlementCurrent); return; } if (s_EntitlementRequested) { EntitlementChanged += (() => { if (callback != null) { var original = callback; callback = null; original?.Invoke(true, s_EntitlementCurrent); } }); return; } s_EntitlementRequested = true; var didRequestSucceed = true; if (Application.isBatchMode) { EntitlementCurrent = MarsSku.Free; } else if (BuildPipeline.isBuildingPlayer) { EntitlementCurrent = MarsSku.Free; } else if (Environment.GetCommandLineArgs().Contains("-runTests")) { EntitlementCurrent = MarsSku.Free; } else if (s_EntitlementAlwaysFail) { EntitlementCurrent = MarsSku.NotEntitled; } else if (s_EntitlementCheckEnabled) { if (s_UseUnityHub) { EntitlementCurrent = DetermineMarsEntitlementThroughHub(); SavedEntitlement = EntitlementCurrent; } else if (SavedEntitlement != MarsSku.NotEntitled) { EntitlementCurrent = SavedEntitlement; } else { WaitForAccountInfoToLoad(callback); return; } } else { EntitlementCurrent = MarsSku.Base; } s_EntitlementReceived = true; if (s_EntitlementCurrent == MarsSku.NotEntitled) { Debug.LogWarning(MARSSession.NotEntitledMessageWithUrl); } callback?.Invoke(didRequestSucceed, s_EntitlementCurrent); }