PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Assets/GameAnalytics/Plugins/Framework/Scripts/GA_Archive.cs

https://bitbucket.org/AgentCodeMonkey/gameframework-unity-project
C# | 129 lines | 87 code | 22 blank | 20 comment | 14 complexity | 391608f4bdff081e75c26e716dc41588 MD5 | raw file
  1. /// <summary>
  2. /// This class handles archiving of data when internet connection is not available. The priority for archiving data follows the
  3. /// categories: user > business > quality > design
  4. /// </summary>
  5. using UnityEngine;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System;
  9. using System.IO;
  10. using LitJson;
  11. public class GA_Archive
  12. {
  13. public string FILE_NAME = "GA_archive";
  14. /// <summary>
  15. /// Archives json data so it can be sent at a later time, when an internet connection is available.
  16. /// </summary>
  17. /// <param name='json'>
  18. /// The json data as a string
  19. /// </param>
  20. /// <param name='serviceType'>
  21. /// The category type
  22. /// </param>
  23. public void ArchiveData(string json, GA_Submit.CategoryType serviceType)
  24. {
  25. #if !UNITY_WEBPLAYER
  26. StreamWriter fileWriter = null;
  27. string fileName = Application.persistentDataPath + "/" + FILE_NAME;
  28. if (File.Exists(fileName))
  29. {
  30. if (new FileInfo(fileName).Length + System.Text.ASCIIEncoding.Unicode.GetByteCount(json) <= GA.Settings.ArchiveMaxFileSize)
  31. {
  32. fileWriter = File.AppendText(fileName);
  33. }
  34. }
  35. else if (System.Text.ASCIIEncoding.Unicode.GetByteCount(json) <= GA.Settings.ArchiveMaxFileSize)
  36. {
  37. fileWriter = File.CreateText(fileName);
  38. }
  39. if (fileWriter != null)
  40. {
  41. fileWriter.WriteLine(serviceType + " " + json);
  42. fileWriter.Close();
  43. }
  44. #endif
  45. }
  46. /// <summary>
  47. /// Gets data which has previously been archived due to lack of internet connectivity.
  48. /// The file containing the archived data is then deleted.
  49. /// </summary>
  50. /// <returns>
  51. /// The archived data as a list of items with parameters and category
  52. /// </returns>
  53. public List<GA_Submit.Item> GetArchivedData()
  54. {
  55. #if UNITY_WEBPLAYER
  56. return null;
  57. #else
  58. List<GA_Submit.Item> items = new List<GA_Submit.Item>();
  59. StreamReader fileReader = null;
  60. string fileName = Application.persistentDataPath + "/" + FILE_NAME;
  61. if (File.Exists(fileName))
  62. {
  63. fileReader = File.OpenText(fileName);
  64. }
  65. if (fileReader != null)
  66. {
  67. string line = null;
  68. while ((line = fileReader.ReadLine()) != null)
  69. {
  70. string[] lineSplit = line.Split(' ');
  71. if (lineSplit.Length >= 2)
  72. {
  73. string categoryString = lineSplit[0];
  74. string json = line.Substring(lineSplit[0].Length + 1);
  75. bool saveData = false;
  76. GA_Submit.CategoryType category = GA_Submit.CategoryType.GA_User;
  77. foreach (KeyValuePair<GA_Submit.CategoryType, string> kvp in GA.API.Submit.Categories)
  78. {
  79. if (kvp.Key.ToString().Equals(categoryString))
  80. {
  81. category = kvp.Key;
  82. saveData = true;
  83. }
  84. }
  85. if (saveData)
  86. {
  87. List<Dictionary<string, object>> itemsParameters = JsonMapper.ToObject<List<Dictionary<string, object>>>(json);
  88. foreach (Dictionary<string, object> parameters in itemsParameters)
  89. {
  90. GA_Submit.Item item = new GA_Submit.Item
  91. {
  92. Type = category,
  93. Parameters = parameters,
  94. AddTime = Time.time
  95. };
  96. items.Add(item);
  97. }
  98. }
  99. }
  100. }
  101. fileReader.Close();
  102. File.Delete(fileName);
  103. }
  104. return items;
  105. #endif
  106. }
  107. }