Geen omschrijving
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TransactionLog.cs 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace UnityEngine.Purchasing
  5. {
  6. /// <summary>
  7. /// Records processed transactions on the file system
  8. /// for de duplication purposes.
  9. /// </summary>
  10. internal class TransactionLog
  11. {
  12. private readonly ILogger logger;
  13. private readonly string persistentDataPath;
  14. public TransactionLog(ILogger logger, string persistentDataPath)
  15. {
  16. this.logger = logger;
  17. if (!string.IsNullOrEmpty(persistentDataPath))
  18. {
  19. this.persistentDataPath = Path.Combine(Path.Combine(persistentDataPath, "Unity"), "UnityPurchasing");
  20. }
  21. }
  22. /// <summary>
  23. /// Removes all transactions from the log.
  24. /// </summary>
  25. public void Clear()
  26. {
  27. Directory.Delete(persistentDataPath, true);
  28. }
  29. public bool HasRecordOf(string transactionID)
  30. {
  31. if (string.IsNullOrEmpty(transactionID) || string.IsNullOrEmpty(persistentDataPath))
  32. {
  33. return false;
  34. }
  35. return Directory.Exists(GetRecordPath(transactionID));
  36. }
  37. public void Record(string transactionID)
  38. {
  39. // Consumables have additional de-duplication logic.
  40. if (!(string.IsNullOrEmpty(transactionID) || string.IsNullOrEmpty(persistentDataPath)))
  41. {
  42. var path = GetRecordPath(transactionID);
  43. try
  44. {
  45. Directory.CreateDirectory(path);
  46. }
  47. catch (Exception recordPathException)
  48. {
  49. // A wide variety of exceptions can occur, for all of which
  50. // nothing is the best course of action.
  51. logger.LogException(recordPathException);
  52. }
  53. }
  54. }
  55. private string GetRecordPath(string transactionID)
  56. {
  57. return Path.Combine(persistentDataPath, ComputeHash(transactionID));
  58. }
  59. /// <summary>
  60. /// Compute a 64 bit Knuth hash of a transaction ID.
  61. /// This should be more than sufficient for the few thousand maximum
  62. /// products expected in an App.
  63. /// </summary>
  64. internal static string ComputeHash(string transactionID)
  65. {
  66. var hash = 3074457345618258791ul;
  67. for (var i = 0; i < transactionID.Length; i++)
  68. {
  69. hash += transactionID[i];
  70. hash *= 3074457345618258799ul;
  71. }
  72. var builder = new StringBuilder(16);
  73. foreach (var b in BitConverter.GetBytes(hash))
  74. {
  75. builder.AppendFormat("{0:X2}", b);
  76. }
  77. return builder.ToString();
  78. }
  79. }
  80. }