static private FromCache ( string gamerUuid ) : IList |
||
gamerUuid | string | |
리턴 | IList |
internal static 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;
}
/// <summary> /// Requests the current receipts from the Store. If the Store is not available, the cached /// receipts from a previous call are returned. /// </summary> /// <returns>The list of Receipt objects.</returns> public async Task <IList <Receipt> > RequestReceiptsAsync() { // We need the gamer UUID for the encryption of the cached receipts, so if the dev // hasn't retrieved the gamer UUID yet, we'll grab it now. var task = Task <IList <Receipt> > .Factory.StartNew(() => { if (_gamerInfo == null) { _gamerInfo = RequestGamerInfoAsync().Result; } // No gamerInfo means no receipts if (_gamerInfo == null) { return(null); } var tcs = new TaskCompletionSource <IList <Receipt> >(); var listener = new ReceiptsListener(tcs, _publicKey, _gamerInfo.Uuid); RequestReceipts(listener); return(tcs.Task.TimeoutAfter(timeout).Result); }); try { return(await task); } catch (Exception e) { Log(e.GetType().Name + ": " + e.Message); } return(_gamerInfo != null?ReceiptsListener.FromCache(_gamerInfo.Uuid) : null); }