Entry point for the OUYA API. Applications should use the singleton instance.
Esempio n. 1
0
        // Load the cached gamerUuid from file
        static internal string FromCache()
        {
            OuyaFacade.Log("Returning cached gamerUuid");
            string gamerUuid = null;

            try
            {
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (store.FileExists(gamerUuidFileName))
                    {
                        using (var reader = new StreamReader(store.OpenFile(gamerUuidFileName, FileMode.Open)))
                        {
                            gamerUuid = reader.ReadToEnd();
                            try
                            {
                                Guid.Parse(gamerUuid);
                            }
                            catch (Exception e2)
                            {
                                OuyaFacade.Log("Incorrectly formatted gamerUuid: " + e2.Message);
                                gamerUuid = null;
                            }
                        }
                    }
                }
            }
            catch (Exception e1)
            {
                OuyaFacade.Log("Error decaching gamerUuid: " + e1.Message);
            }
            return(gamerUuid);
        }
        // Encrypt the receipts and save them to file.
        static void ToCache(IList <Receipt> receipts, string gamerUuid)
        {
            OuyaFacade.Log("Caching receipts");
            var json  = new JSONObject();
            var array = new JSONArray();

            foreach (var receipt in receipts)
            {
                var r = new JSONObject();
                r.Put("identifier", receipt.Identifier);
                // PriceInCents is now deprecated. Use LocalPrice and CurrencyCode instead.
                // Retain field for compatibility.
                r.Put("priceInCents", 0);
                r.Put("purchaseDate", receipt.PurchaseDate.ToGMTString());
                r.Put("generatedDate", receipt.GeneratedDate.ToGMTString());
                r.Put("gamerUuid", receipt.Gamer);
                r.Put("uuid", receipt.Uuid);
                r.Put("localPrice", receipt.LocalPrice);
                r.Put("currencyCode", receipt.Currency);
                array.Put(r);
            }
            json.Accumulate("receipts", array);
            var text = json.ToString();
            var encryptedReceipts = CryptoHelper.Encrypt(text, gamerUuid);

            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var writer = new StreamWriter(store.OpenFile(receiptsFileName, FileMode.OpenOrCreate)))
                {
                    writer.Write(encryptedReceipts);
                }
            }
        }
Esempio n. 3
0
        // Encrypt the receipts and save them to file.
        void ToCache(IList <Receipt> receipts)
        {
            OuyaFacade.Log("Caching receipts");
            var json  = new JSONObject();
            var array = new JSONArray();

            foreach (var receipt in receipts)
            {
                var r = new JSONObject();
                r.Put("identifier", receipt.Identifier);
                r.Put("priceInCents", receipt.PriceInCents);
                r.Put("purchaseDate", receipt.PurchaseDate.ToGMTString());
                r.Put("generatedDate", receipt.GeneratedDate.ToGMTString());
                r.Put("gamerUuid", receipt.Gamer);
                r.Put("uuid", receipt.Uuid);
                array.Put(r);
            }
            json.Accumulate("receipts", array);
            var text = json.ToString();
            var encryptedReceipts = CryptoHelper.Encrypt(text, _gamerUuid);

            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var writer = new StreamWriter(store.OpenFile(receiptsFileName, FileMode.OpenOrCreate)))
                {
                    writer.Write(encryptedReceipts);
                }
            }
        }
        // Load the cached receipts from file and return the decrypted result.
        static internal IList <Receipt> FromCache(string gamerUuid)
        {
            OuyaFacade.Log("Returning cached receipts");
            IList <Receipt> receipts          = null;
            string          encryptedReceipts = string.Empty;

            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (store.FileExists(receiptsFileName))
                {
                    using (var reader = new StreamReader(store.OpenFile(receiptsFileName, FileMode.Open)))
                    {
                        encryptedReceipts = reader.ReadToEnd();
                    }
                }
            }
            if (!string.IsNullOrEmpty(encryptedReceipts))
            {
                var decryptedReceipts = CryptoHelper.Decrypt(encryptedReceipts, gamerUuid);
                var json = new JSONObject(decryptedReceipts);
                var list = json.OptJSONArray("receipts");
                if (list != null)
                {
                    receipts = new List <Receipt>(list.Length());
                    for (int i = 0; i < list.Length(); ++i)
                    {
                        var node          = list.GetJSONObject(i);
                        var identifier    = node.GetString("identifier");
                        var priceInCents  = node.GetInt("priceInCents");
                        var purchaseDate  = new Java.Util.Date(node.GetString("purchaseDate"));
                        var generatedDate = new Java.Util.Date(node.GetString("generatedDate"));
                        var gamer         = node.GetString("gamerUuid");
                        var uuid          = node.GetString("uuid");
                        // Cater for reading old receipts written with pre-1.0.8
                        double localPrice   = priceInCents / 100.0;
                        string currencyCode = "USD";
                        try
                        {
                            localPrice   = node.GetDouble("localPrice");
                            currencyCode = node.GetString("currencyCode");
                        }
                        catch (JSONException)
                        {
                            OuyaFacade.Log("Older receipt found. Assuming USD price.");
                        }
                        var receipt = new Receipt(identifier, priceInCents, purchaseDate, generatedDate, gamer, uuid, localPrice, currencyCode);
                        receipts.Add(receipt);
                    }
                }
            }
            // Return an empty list if nothing was found
            if (receipts == null)
            {
                receipts = new List <Receipt>();
            }
            return(receipts);
        }
Esempio n. 5
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            facade = OuyaFacade.Instance;
            facade.Init(Activity, "insert your developer key here");

            base.Initialize();
            
            // Start the IAP test as an async task so it runs in the background.
            DoIapTest();
        }
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            Game1.Activity = this;
            var g = new Game1();
            SetContentView(g.Window);
            g.Run();

            PurchaseFacade = OuyaFacade.Instance;
            PurchaseFacade.Init(this, DEVELOPER_ID);
        }
        static IList <Receipt> ReceiptsFromResponse(string receiptsResponse, IPublicKey publicKey)
        {
            IList <Receipt> receipts = null;

            using (var helper = new OuyaEncryptionHelper())
            {
                using (var response = new JSONObject(receiptsResponse))
                {
                    OuyaFacade.Log("Decrypting receipts response");
                    receipts = helper.DecryptReceiptResponse(response, publicKey);
                }
            }
            return(receipts);
        }
Esempio n. 8
0
        // Load the cached gamerInfo from file
        static internal GamerInfo FromCache()
        {
            OuyaFacade.Log("Returning cached gamerInfo");
            GamerInfo gamerInfo = null;

            try
            {
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (store.FileExists(gamerInfoFileName))
                    {
                        using (var reader = new BinaryReader(store.OpenFile(gamerInfoFileName, FileMode.Open)))
                        {
                            var version = reader.ReadInt32();
                            switch (version)
                            {
                            case 1:
                            {
                                var uuid = reader.ReadString();
                                try
                                {
                                    Guid.Parse(uuid);
                                    var userName = reader.ReadString();
                                    gamerInfo = new GamerInfo(uuid, userName);
                                }
                                catch (Exception e2)
                                {
                                    OuyaFacade.Log("Incorrectly formatted gamerUuid: " + e2.Message);
                                }
                            }
                            break;

                            default:
                                throw new InvalidDataException("Unknown cache version number " + version);
                            }
                        }
                    }
                }
            }
            catch (Exception e1)
            {
                OuyaFacade.Log("Error decaching gamerInfo: " + e1.Message);
            }
            return(gamerInfo);
        }
        public void OnFailure(int errorCode, string errorMessage, Bundle optionalData)
        {
            // If we have a cached result, return that
            IList <Receipt> receipts = null;

            try
            {
                // Parse receipts into a list
                receipts = FromCache(_gamerUuid);
            }
            catch (Exception e)
            {
                OuyaFacade.Log("Error decaching receipts: " + e.Message);
                _tcs.SetException(new OuyaRequestException(errorCode, errorMessage, optionalData));
            }

            _tcs.SetResult(receipts);
        }
Esempio n. 10
0
        public void OnFailure(int errorCode, string errorMessage, Bundle optionalData)
        {
            // If we have a cached result, return that
            GamerInfo gamerInfo = null;

            try
            {
                // Parse gamerUuid from file
                gamerInfo = FromCache();
            }
            catch (Exception e)
            {
                OuyaFacade.Log("Error decaching gamerInfo: " + e.Message);
                _tcs.SetException(new OuyaRequestException(errorCode, errorMessage, optionalData));
            }

            _tcs.SetResult(gamerInfo);
        }
Esempio n. 11
0
 // Save gamerUuid to file.
 static void ToCache(string gamerUuid)
 {
     OuyaFacade.Log("Caching gamerUuid");
     try
     {
         using (var store = IsolatedStorageFile.GetUserStoreForApplication())
         {
             using (var writer = new StreamWriter(store.OpenFile(gamerUuidFileName, FileMode.OpenOrCreate)))
             {
                 writer.Write(gamerUuid);
             }
         }
     }
     catch (Exception e)
     {
         OuyaFacade.Log("Error caching gamerUuid: " + e.Message);
     }
 }
Esempio n. 12
0
 // Save gamerInfo to file.
 static void ToCache(GamerInfo gamerInfo)
 {
     OuyaFacade.Log("Caching gamerInfo uuid:" + gamerInfo.Uuid + " name:" + gamerInfo.Username);
     try
     {
         using (var store = IsolatedStorageFile.GetUserStoreForApplication())
         {
             using (var writer = new BinaryWriter(store.OpenFile(gamerInfoFileName, FileMode.OpenOrCreate)))
             {
                 writer.Write(gamerInfoVersion);
                 writer.Write(gamerInfo.Uuid);
                 writer.Write(gamerInfo.Username);
             }
         }
     }
     catch (Exception e)
     {
         OuyaFacade.Log("Error caching gamerInfo: " + e.Message);
     }
 }
Esempio n. 13
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            Game1.Activity = this;
            var g = new Game1();
            SetContentView(g.Window);
            g.Run();

            byte[] applicationKey = null;
            using (var stream = Resources.OpenRawResource(Resource.Raw.key))
            {
                using (var ms = new MemoryStream())
                {
                    stream.CopyTo(ms);
                    applicationKey = ms.ToArray();
                }
            }

            PurchaseFacade = OuyaFacade.Instance;
            PurchaseFacade.Init(this, DEVELOPER_ID, applicationKey);
        }
Esempio n. 14
0
        // Load the cached receipts from file and return the decrypted result.
        IList <Receipt> FromCache()
        {
            OuyaFacade.Log("Returning cached receipts");
            IList <Receipt> receipts          = null;
            string          encryptedReceipts = string.Empty;

            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (store.FileExists(receiptsFileName))
                {
                    using (var reader = new StreamReader(store.OpenFile(receiptsFileName, FileMode.Open)))
                    {
                        encryptedReceipts = reader.ReadToEnd();
                    }
                }
            }
            if (!string.IsNullOrEmpty(encryptedReceipts))
            {
                var decryptedReceipts = CryptoHelper.Decrypt(encryptedReceipts, _gamerUuid);
                var json = new JSONObject(decryptedReceipts);
                var list = json.OptJSONArray("receipts");
                if (list != null)
                {
                    receipts = new List <Receipt>(list.Length());
                    for (int i = 0; i < list.Length(); ++i)
                    {
                        var node          = list.GetJSONObject(i);
                        var identifier    = node.GetString("identifier");
                        var priceInCents  = node.GetInt("priceInCents");
                        var purchaseDate  = new Java.Util.Date(node.GetString("purchaseDate"));
                        var generatedDate = new Java.Util.Date(node.GetString("generatedDate"));
                        var gamerUuid     = node.GetString("gamerUuid");
                        var uuid          = node.GetString("uuid");
                        var receipt       = new Receipt(identifier, priceInCents, purchaseDate, generatedDate, gamerUuid, uuid);
                        receipts.Add(receipt);
                    }
                }
            }
            return(receipts);
        }