예제 #1
0
        private static void DoAttributeInstall(PlayFabApiSettings settings, IPlayFabInstanceApi instanceApi)
        {
            if (!_needsAttribution || settings.DisableAdvertising)
            {
                return; // Don't send this value to PlayFab if it's not required
            }
            var attribRequest = new ClientModels.AttributeInstallRequest();

            switch (settings.AdvertisingIdType)
            {
            case PlayFabSettings.AD_TYPE_ANDROID_ID: attribRequest.Adid = settings.AdvertisingIdValue; break;

            case PlayFabSettings.AD_TYPE_IDFA: attribRequest.Idfa = settings.AdvertisingIdValue; break;
            }
            var clientInstanceApi = instanceApi as PlayFabClientInstanceAPI;

            if (clientInstanceApi != null)
            {
                clientInstanceApi.AttributeInstall(attribRequest, OnAttributeInstall, null, settings);
            }
            else
            {
                PlayFabClientAPI.AttributeInstall(attribRequest, OnAttributeInstall, null, settings);
            }
        }
        private static void SendDeviceInfoToPlayFab(PlayFabApiSettings settings, IPlayFabInstanceApi instanceApi)
        {
            if (settings.DisableDeviceInfo || !_gatherDeviceInfo)
            {
                return;
            }

            var serializer = PluginManager.GetPlugin <ISerializerPlugin>(PluginContract.PlayFab_Serializer);
            var request    = new ClientModels.DeviceInfoRequest
            {
                Info = serializer.DeserializeObject <Dictionary <string, object> >(serializer.SerializeObject(new PlayFabDataGatherer()))
            };
            var clientInstanceApi = instanceApi as PlayFabClientInstanceAPI;

            if (clientInstanceApi != null)
            {
                clientInstanceApi.ReportDeviceInfo(request, null, OnGatherFail, settings);
            }
#if !DISABLE_PLAYFAB_STATIC_API
            else
            {
                PlayFabClientAPI.ReportDeviceInfo(request, null, OnGatherFail, settings);
            }
#endif
        }
예제 #3
0
 protected internal static void MakeApiCallWithFullUri <TResult>(string fullUri,
                                                                 PlayFabRequestCommon request, AuthType authType, Action <TResult> resultCallback,
                                                                 Action <PlayFabError> errorCallback, object customData = null, Dictionary <string, string> extraHeaders = null,
                                                                 PlayFabAuthenticationContext authenticationContext     = null, PlayFabApiSettings apiSettings = null,
                                                                 IPlayFabInstanceApi instanceApi = null)
     where TResult : PlayFabResultCommon
 {
     apiSettings = apiSettings ?? PlayFabSettings.staticSettings;
     // This will not be called if environment file does not exist or does not contain property the debugging URI
     _MakeApiCall(null, fullUri, request, authType, resultCallback, errorCallback, customData, extraHeaders, false,
                  authenticationContext, apiSettings, instanceApi);
 }
예제 #4
0
        protected internal static void MakeApiCall <TResult>(string apiEndpoint,
                                                             PlayFabRequestCommon request, AuthType authType, Action <TResult> resultCallback,
                                                             Action <PlayFabError> errorCallback, object customData = null, Dictionary <string, string> extraHeaders = null,
                                                             PlayFabAuthenticationContext authenticationContext     = null, PlayFabApiSettings apiSettings = null,
                                                             IPlayFabInstanceApi instanceApi = null)
            where TResult : PlayFabResultCommon
        {
            apiSettings = apiSettings ?? PlayFabSettings.staticSettings;
            var fullUrl = apiSettings.GetFullUrl(apiEndpoint, apiSettings.RequestGetParams);

            _MakeApiCall(apiEndpoint, fullUrl, request, authType, resultCallback, errorCallback, customData, extraHeaders,
                         false, authenticationContext, apiSettings, instanceApi);
        }
        /// <summary>
        /// When a PlayFab login occurs, check the result information, and
        ///   relay it to _OnPlayFabLogin where the information is used
        /// </summary>
        /// <param name="result"></param>
        public static void OnPlayFabLogin(PlayFabResultCommon result, PlayFabApiSettings settings,
                                          IPlayFabInstanceApi instanceApi)
        {
            var loginResult    = result as ClientModels.LoginResult;
            var registerResult = result as ClientModels.RegisterPlayFabUserResult;

            if (loginResult == null && registerResult == null)
            {
                return;
            }

            // Gather things common to the result types
            ClientModels.UserSettings settingsForUser = null;
            string playFabId  = null;
            string entityId   = null;
            string entityType = null;

            if (loginResult != null)
            {
                settingsForUser = loginResult.SettingsForUser;
                playFabId       = loginResult.PlayFabId;
                if (loginResult.EntityToken != null)
                {
                    entityId   = loginResult.EntityToken.Entity.Id;
                    entityType = loginResult.EntityToken.Entity.Type;
                }
            }
            else if (registerResult != null)
            {
                settingsForUser = registerResult.SettingsForUser;
                playFabId       = registerResult.PlayFabId;
                if (registerResult.EntityToken != null)
                {
                    entityId   = registerResult.EntityToken.Entity.Id;
                    entityType = registerResult.EntityToken.Entity.Type;
                }
            }

            _OnPlayFabLogin(settingsForUser, playFabId, entityId, entityType, settings, instanceApi);
        }
예제 #6
0
        private static void GetAdvertIdFromUnity(PlayFabApiSettings settings, IPlayFabInstanceApi instanceApi)
        {
#if UNITY_5_3_OR_NEWER && (UNITY_ANDROID || UNITY_IOS) && (!UNITY_EDITOR || TESTING)
            Application.RequestAdvertisingIdentifierAsync(
                (advertisingId, trackingEnabled, error) =>
            {
                settings.DisableAdvertising = !trackingEnabled;
                if (!trackingEnabled)
                {
                    return;
                }
#if UNITY_ANDROID
                settings.AdvertisingIdType = PlayFabSettings.AD_TYPE_ANDROID_ID;
#elif UNITY_IOS
                settings.AdvertisingIdType = PlayFabSettings.AD_TYPE_IDFA;
#endif
                settings.AdvertisingIdValue = advertisingId;
                DoAttributeInstall(settings, instanceApi);
            }
                );
#endif
        }
예제 #7
0
        /// <summary>
        /// Separated from OnPlayFabLogin, to explicitly lose the refs to loginResult and registerResult, because
        ///   only one will be defined, but both usually have all the information we REALLY need here.
        /// But the result signatures are different and clunky, so do the separation above, and processing here
        /// </summary>
        private static void _OnPlayFabLogin(ClientModels.UserSettings settingsForUser, string playFabId, string entityId, string entityType, PlayFabApiSettings settings, IPlayFabInstanceApi instanceApi)
        {
            _needsAttribution = _gatherDeviceInfo = _gatherScreenTime = false;
            if (settingsForUser != null)
            {
                _needsAttribution = settingsForUser.NeedsAttribution;
                _gatherDeviceInfo = settingsForUser.GatherDeviceInfo;
                _gatherScreenTime = settingsForUser.GatherFocusInfo;
            }

            // Device attribution (adid or idfa)
            if (settings.AdvertisingIdType != null && settings.AdvertisingIdValue != null)
            {
                DoAttributeInstall(settings, instanceApi);
            }
            else
            {
                GetAdvertIdFromUnity(settings, instanceApi);
            }

            // Device information gathering
            SendDeviceInfoToPlayFab(settings, instanceApi);

#if !DISABLE_PLAYFABENTITY_API
            if (!string.IsNullOrEmpty(entityId) && !string.IsNullOrEmpty(entityType) && _gatherScreenTime)
            {
                PlayFabHttp.InitializeScreenTimeTracker(entityId, entityType, playFabId);
            }
            else
            {
                settings.DisableFocusTimeCollection = true;
            }
#endif
        }
예제 #8
0
        /// <summary>
        /// Internal method for Make API Calls
        /// </summary>
        private static void _MakeApiCall <TResult>(string apiEndpoint, string fullUrl,
                                                   PlayFabRequestCommon request, AuthType authType, Action <TResult> resultCallback,
                                                   Action <PlayFabError> errorCallback, object customData, Dictionary <string, string> extraHeaders, bool allowQueueing, PlayFabAuthenticationContext authenticationContext, PlayFabApiSettings apiSettings, IPlayFabInstanceApi instanceApi)
            where TResult : PlayFabResultCommon
        {
            InitializeHttp();
            SendEvent(apiEndpoint, request, null, ApiProcessingEventType.Pre);

            var serializer   = PluginManager.GetPlugin <ISerializerPlugin>(PluginContract.PlayFab_Serializer);
            var reqContainer = new CallRequestContainer
            {
                ApiEndpoint    = apiEndpoint,
                FullUrl        = fullUrl,
                settings       = apiSettings,
                context        = authenticationContext,
                CustomData     = customData,
                Payload        = Encoding.UTF8.GetBytes(serializer.SerializeObject(request)),
                ApiRequest     = request,
                ErrorCallback  = errorCallback,
                RequestHeaders = extraHeaders ?? new Dictionary <string, string>(), // Use any headers provided by the customer
                instanceApi    = instanceApi
            };

            // Append any additional headers
            foreach (var pair in GlobalHeaderInjection)
            {
                if (!reqContainer.RequestHeaders.ContainsKey(pair.Key))
                {
                    reqContainer.RequestHeaders[pair.Key] = pair.Value;
                }
            }

#if PLAYFAB_REQUEST_TIMING
            reqContainer.Timing.StartTimeUtc = DateTime.UtcNow;
            reqContainer.Timing.ApiEndpoint  = apiEndpoint;
#endif

            // Add PlayFab Headers
            var transport = PluginManager.GetPlugin <ITransportPlugin>(PluginContract.PlayFab_Transport);
            reqContainer.RequestHeaders["X-ReportErrorAsSuccess"] = "true";                        // Makes processing PlayFab errors a little easier
            reqContainer.RequestHeaders["X-PlayFabSDK"]           = PlayFabSettings.VersionString; // Tell PlayFab which SDK this is
            switch (authType)
            {
#if ENABLE_PLAYFABSERVER_API || ENABLE_PLAYFABADMIN_API || UNITY_EDITOR
            case AuthType.DevSecretKey:
                if (apiSettings.DeveloperSecretKey == null)
                {
                    throw new PlayFabException(PlayFabExceptionCode.DeveloperKeyNotSet, "DeveloperSecretKey is not found in Request, Server Instance or PlayFabSettings");
                }
                reqContainer.RequestHeaders["X-SecretKey"] = apiSettings.DeveloperSecretKey; break;
#endif
#if !DISABLE_PLAYFABCLIENT_API
            case AuthType.LoginSession:
                if (authenticationContext != null)
                {
                    reqContainer.RequestHeaders["X-Authorization"] = authenticationContext.ClientSessionTicket;
                }
                break;
#endif
#if !DISABLE_PLAYFABENTITY_API
            case AuthType.EntityToken:
                if (authenticationContext != null)
                {
                    reqContainer.RequestHeaders["X-EntityToken"] = authenticationContext.EntityToken;
                }
                break;
#endif
            }

            // These closures preserve the TResult generic information in a way that's safe for all the devices
            reqContainer.DeserializeResultJson = () =>
            {
                reqContainer.ApiResult = serializer.DeserializeObject <TResult>(reqContainer.JsonResponse);
            };
            reqContainer.InvokeSuccessCallback = () =>
            {
                if (resultCallback != null)
                {
                    resultCallback((TResult)reqContainer.ApiResult);
                }
            };

            if (allowQueueing && _apiCallQueue != null)
            {
                for (var i = _apiCallQueue.Count - 1; i >= 0; i--)
                {
                    if (_apiCallQueue[i].ApiEndpoint == apiEndpoint)
                    {
                        _apiCallQueue.RemoveAt(i);
                    }
                }
                _apiCallQueue.Add(reqContainer);
            }
            else
            {
                transport.MakeApiCall(reqContainer);
            }
        }
예제 #9
0
        protected internal static Task <TResult> MakeApiCallAsync <TResult>(string apiEndpoint,
                                                                            PlayFabRequestCommon request, AuthType authType, object customData = null, Dictionary <string, string> extraHeaders = null, PlayFabAuthenticationContext authenticationContext = null, PlayFabApiSettings apiSettings = null, IPlayFabInstanceApi instanceApi = null)
            where TResult : PlayFabResultCommon
        {
            TaskCompletionSource <TResult> tcs = new TaskCompletionSource <TResult>();

            apiSettings = apiSettings ?? PlayFabSettings.staticSettings;
            var fullUrl = apiSettings.GetFullUrl(apiEndpoint, apiSettings.RequestGetParams);

            _MakeApiCall(apiEndpoint, fullUrl, request, authType, (TResult a) => tcs.SetResult(a), error =>
            {
                Debug.LogError(error.GenerateErrorReport());
                tcs.SetException(error);
            }, customData, extraHeaders, false, authenticationContext, apiSettings, instanceApi);
            return(tcs.Task);
        }
        private static async void SendDeviceInfoToPlayFab(PlayFabApiSettings settings, IPlayFabInstanceApi instanceApi)
        {
            if (settings.DisableDeviceInfo || !_gatherDeviceInfo)
            {
                return;
            }

            var serializer = PluginManager.GetPlugin <ISerializerPlugin>(PluginContract.PlayFab_Serializer);
            var request    = new ClientModels.DeviceInfoRequest
            {
                Info = serializer.DeserializeObject <Dictionary <string, object> >(serializer.SerializeObject(new PlayFabDataGatherer()))
            };
            var clientInstanceApi = instanceApi as PlayFabClientInstanceAPI;

            if (clientInstanceApi != null)
            {
                clientInstanceApi.ReportDeviceInfo(request, null, e => Debug.Log("OnGatherFail: " + e.GenerateErrorReport()), settings);
            }
            else
            {
                try
                {
                    await ClientAPI.ReportDeviceInfo(request.Info, customData : settings);
                }
                catch (PlayFabError e)
                {
                    Debug.Log("OnGatherFail: " + e.GenerateErrorReport());
                }
            }
        }