Esempio n. 1
0
        private MLResult GetAllPurchaseHistoryInternal(Action <MLPurchaseHistoryQuery> callback)
        {
            ulong handle = MagicLeapNativeBindings.InvalidHandle;

            MLResult.Code historyResult = MLPurchaseNativeBindings.MLPurchaseHistoryQueryCreate(ref handle);
            MLResult      result        = MLResult.Create(historyResult);

            if (!result.IsOk || !MagicLeapNativeBindings.MLHandleIsValid(handle))
            {
                MLPluginLog.ErrorFormat("MLPurchase.GetAllPurchaseHistoryInternal failed to create purchase history query. Reason: {0}", result);
                return(result);
            }

            historyResult = MLPurchaseNativeBindings.MLPurchaseHistoryQueryGetPage(handle, MaxPurchaseHistoryItems);
            result        = MLResult.Create(historyResult);
            if (!result.IsOk)
            {
                MLPluginLog.ErrorFormat("MLPurchase.GetAllPurchaseHistoryInternal failed to submit purchase history query. Reason: {0}", result);
                DestroyPurchaseHistoryQuery(handle);
            }
            else
            {
                _pendingPurchaseHistoryQueries.Add(new PurchaseHistoryQueryInfo(handle, callback));
            }
            return(result);
        }
Esempio n. 2
0
        private MLResult MakePurchaseInternal(string token, Action <MLPurchaseRequest> callback)
        {
            ulong handle = MagicLeapNativeBindings.InvalidHandle;

            MLResult.Code purchaseResult = MLPurchaseNativeBindings.MLPurchaseCreate(ref handle);
            MLResult      result         = MLResult.Create(purchaseResult);

            if (!result.IsOk || !MagicLeapNativeBindings.MLHandleIsValid(handle))
            {
                MLPluginLog.ErrorFormat("MLPurchase.MakePurchaseInternal failed to create purchase request. Reason: {0}", result);
                return(result);
            }

            MLPluginLog.Debug("Purchasing: " + token);
            purchaseResult = MLPurchaseNativeBindings.MLPurchaseSubmit(handle, token);
            result         = MLResult.Create(purchaseResult);
            if (!result.IsOk)
            {
                MLPluginLog.ErrorFormat("MLPurchase.MakePurchaseInternal failed to submit purchase request. Reason: {0}", result);
                DestroyPurchaseRequest(handle);
            }
            else
            {
                _pendingPurchaseRequests.Add(new PurchaseRequestInfo(handle, callback));
            }
            return(result);
        }
Esempio n. 3
0
        private void UpdatePurchaseRequest()
        {
            List <PurchaseRequestInfo> completedRequests = new List <PurchaseRequestInfo>();

            for (int i = 0; i < _pendingPurchaseRequests.Count; ++i)
            {
                PurchaseRequestInfo info   = _pendingPurchaseRequests[i];
                MLCloudStatus       status = MLCloudStatus.NotDone;
                info.Details.Result = MLPurchaseNativeBindings.MLPurchaseGetResult(info.Handle, ref info.Details.PurchaseConfirmation, ref status);

                if (info.Details.Result == MLResult.Code.Ok)
                {
                    if (status == MLCloudStatus.Done)
                    {
                        completedRequests.Add(info);
                    }
                }
                else
                {
                    completedRequests.Add(info);
                }
            }

            PublishPurchaseRequests(completedRequests);
        }
Esempio n. 4
0
        private void UpdateItemDetails()
        {
            List <ItemDetailQueryInfo> completedQueries = new List <ItemDetailQueryInfo>();

            for (int i = 0; i < _pendingItemDetailQueries.Count; ++i)
            {
                ItemDetailQueryInfo          info    = _pendingItemDetailQueries[i];
                MLPurchaseItemDetailsResults results = MLPurchaseItemDetailsResults.Create();
                MLCloudStatus status = MLCloudStatus.NotDone;
                info.Details.Result = MLPurchaseNativeBindings.MLPurchaseItemDetailsGetResult(info.Handle, ref results, ref status);

                if (info.Details.Result == MLResult.Code.Ok)
                {
                    if (status == MLCloudStatus.Done)
                    {
                        info.Details.ItemResults = new MLPurchaseItemDetailsResult[results.count];
                        for (int j = 0; j < results.count; j++)
                        {
                            IntPtr offsetPtr = new IntPtr(results.itemDetails.ToInt64() + (Marshal.SizeOf(typeof(MLPurchaseItemDetailsResult)) * j));
                            info.Details.ItemResults[j] = (MLPurchaseItemDetailsResult)Marshal.PtrToStructure(offsetPtr, typeof(MLPurchaseItemDetailsResult));
                        }
                        completedQueries.Add(info);
                    }
                }
                else
                {
                    completedQueries.Add(info);
                }
            }

            PublishItemDetails(completedQueries);
        }
Esempio n. 5
0
        private MLResult InitiateItemDetailsQuery(ulong handle, string[] itemIds)
        {
            // Allocate memory for query
            int    size       = Marshal.SizeOf(typeof(IntPtr));
            IntPtr itemIdsPtr = Marshal.AllocHGlobal(size * itemIds.Length);

            for (int i = 0; i < itemIds.Length; i++)
            {
                IntPtr idPtr = Marshal.StringToHGlobalAuto(itemIds[i]);
                Marshal.WriteIntPtr(new IntPtr(itemIdsPtr.ToInt64() + (Marshal.SizeOf(typeof(IntPtr)) * i)), idPtr);
            }

            // Build the query
            MLPurchaseItemDetailsQuery query = new MLPurchaseItemDetailsQuery()
            {
                ids     = itemIdsPtr,
                idCount = Convert.ToUInt32(itemIds.Length)
            };

            // Perform the query
            MLResult.Code detailsResult = MLPurchaseNativeBindings.MLPurchaseItemDetailsGet(handle, ref query);
            MLResult      result        = MLResult.Create(detailsResult);

            // Free up memory before checking results of query
            for (int i = 0; i < itemIds.Length; i++)
            {
                IntPtr idPtr = Marshal.ReadIntPtr(new IntPtr(itemIdsPtr.ToInt64() + (Marshal.SizeOf(typeof(IntPtr)) * i)));
                Marshal.FreeHGlobal(idPtr);
            }
            Marshal.FreeHGlobal(itemIdsPtr);

            return(result);
        }
Esempio n. 6
0
        private MLResult GetItemDetailsInternal(string[] itemIds, Action <MLPurchaseItemDetail> callback)
        {
            // Create handle
            ulong handle = MagicLeapNativeBindings.InvalidHandle;

            MLResult.Code detailsResult = MLPurchaseNativeBindings.MLPurchaseItemDetailsCreate(ref handle);
            MLResult      result        = MLResult.Create(detailsResult);

            if (!result.IsOk || !MagicLeapNativeBindings.MLHandleIsValid(handle))
            {
                MLPluginLog.ErrorFormat("MLPurchase.GetItemDetailsInternal failed, unable to create item details query. Reason: {0}", result);
                return(result);
            }

            result = InitiateItemDetailsQuery(handle, itemIds);

            if (!result.IsOk)
            {
                MLPluginLog.ErrorFormat("MLPurchase.GetItemDetailsInternal failed to submit item details request. Reason: {0}", result);
                DestroyItemDetail(handle);
            }
            else
            {
                _pendingItemDetailQueries.Add(new ItemDetailQueryInfo(handle, callback));
            }
            return(result);
        }
Esempio n. 7
0
        private void UpdatePurchaseHistoryQuery()
        {
            List <PurchaseHistoryQueryInfo> completedQueries = new List <PurchaseHistoryQueryInfo>();

            for (int i = 0; i < _pendingPurchaseHistoryQueries.Count; ++i)
            {
                PurchaseHistoryQueryInfo info          = _pendingPurchaseHistoryQueries[i];
                MLPurchaseHistoryResult  historyResult = MLPurchaseHistoryResult.Create();
                info.Details.Result = MLPurchaseNativeBindings.MLPurchaseHistoryQueryGetPageResult(info.Handle, ref historyResult);

                if (info.Details.Result == MLResult.Code.Ok)
                {
                    if (historyResult.status == MLCloudStatus.Done)
                    {
                        uint numPurchaseConfirmationsToAdd = Math.Min(historyResult.count, info.NumItemsLeft);
                        for (int j = 0; j < numPurchaseConfirmationsToAdd; j++)
                        {
                            IntPtr offsetPtr = new IntPtr(historyResult.confirmations.ToInt64() + (Marshal.SizeOf(typeof(MLPurchaseConfirmation)) * j));
                            info.Details.PurchaseConfirmations.Add((MLPurchaseConfirmation)Marshal.PtrToStructure(offsetPtr, typeof(MLPurchaseConfirmation)));
                        }
                        info.NumItemsLeft -= numPurchaseConfirmationsToAdd;

                        MLPluginLog.DebugFormat("purchase history query: hasNextPage {0}, fetchAll {1}, NumItemsLeft {2}",
                                                historyResult.hasNextPage ? "true" : "false",
                                                info.FetchAll ? "true" : "false",
                                                info.NumItemsLeft.ToString()); // TESTING

                        if (historyResult.hasNextPage && (info.FetchAll || info.NumItemsLeft > 0))
                        {
                            info.Details.Result = MLPurchaseNativeBindings.MLPurchaseHistoryQueryGetPage(info.Handle, Math.Min(info.NumItemsLeft, MaxPurchaseHistoryItems));
                            if (info.Details.Result != MLResult.Code.Ok)
                            {
                                MLPluginLog.ErrorFormat("MLPurchase.UpdatePurchaseHistoryQuery failed to query for succeeding purchase history pages. Reason: {0}", GetResultString(info.Details.Result));
                                completedQueries.Add(info);
                            }
                        }
                        else
                        {
                            completedQueries.Add(info);
                        }
                    }
                }
                else
                {
                    MLPluginLog.Debug("purchase history query get page result, result: " + info.Details.Result.ToString());
                    completedQueries.Add(info);
                }
            }

            PublishPurchaseHistories(completedQueries);
        }
Esempio n. 8
0
 private bool DestroyPurchaseHistoryQuery(ulong handle)
 {
     if (MagicLeapNativeBindings.MLHandleIsValid(handle))
     {
         MLResult.Code result = MLPurchaseNativeBindings.MLPurchaseHistoryQueryDestroy(handle);
         if (result == MLResult.Code.Ok)
         {
             return(true);
         }
         MLPluginLog.ErrorFormat("MLPurchase.DestroyPurchaseHistoryQuery failed to free purchase history query handle. Reason: {0}", GetResultString(result));
         return(false);
     }
     MLPluginLog.Error("MLPurchase.DestroyPurchaseHistoryQuery failed, handle is invalid.");
     return(false);
 }
Esempio n. 9
0
 /// <summary>
 /// Properly dispose the Item Detail Query handle
 /// </summary>
 /// <param name="handle">Item Detail Query handle to be destroyed</param>
 /// <returns>Retrun true on success, otherwise false</returns>
 private bool DestroyItemDetail(ulong handle)
 {
     if (MagicLeapNativeBindings.MLHandleIsValid(handle))
     {
         MLResult.Code result = MLPurchaseNativeBindings.MLPurchaseItemDetailsDestroy(handle);
         if (result == MLResult.Code.Ok)
         {
             return(true);
         }
         MLPluginLog.ErrorFormat("MLPurchase.DestroyItemDetail failed, unable to free item details handle. Reason: {0}", GetResultString(result));
         return(false);
     }
     MLPluginLog.ErrorFormat("MLPurchase.DestroyItemDetail failed, handle is invalid.");
     return(false);
 }
Esempio n. 10
0
 /// <summary>
 /// Gets a readable version of the result code as an ASCII string.
 /// </summary>
 /// <param name="resultCode">The MLResult that should be converted.</param>
 /// <returns>ASCII string containing a readable version of the result code.</returns>
 public static string GetResultString(MLResult.Code resultCode)
 {
     return(Marshal.PtrToStringAnsi(MLPurchaseNativeBindings.MLPurchaseGetResultString(resultCode)));
 }