SaveData.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Text;
  5. using LitJson;
  6. using UnityEngine;
  7. public class SaveData : BaseBehaviour
  8. {
  9. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  10. public static event EventHandler<SaveLoadedEventArgs> OnGameLoaded;
  11. private static string SaveDataPath
  12. {
  13. get
  14. {
  15. return Application.persistentDataPath + "/SaveData/";
  16. }
  17. }
  18. private static string SaveDataFilePath
  19. {
  20. get
  21. {
  22. return SaveData.SaveDataPath + "save_data.bin";
  23. }
  24. }
  25. private void OnEnable()
  26. {
  27. }
  28. private void OnDisable()
  29. {
  30. }
  31. private void Update()
  32. {
  33. }
  34. public static bool IsBusy
  35. {
  36. get
  37. {
  38. return false;
  39. }
  40. }
  41. public static bool Save(GameData data)
  42. {
  43. byte[] buffer = SaveData.GetBuffer(data);
  44. bool result;
  45. try
  46. {
  47. if (!Directory.Exists(SaveData.SaveDataPath))
  48. {
  49. Directory.CreateDirectory(SaveData.SaveDataPath);
  50. }
  51. FileStream fileStream = File.OpenWrite(SaveData.SaveDataFilePath);
  52. BinaryWriter binaryWriter = new BinaryWriter(fileStream, Encoding.UTF8);
  53. binaryWriter.Write(buffer);
  54. binaryWriter.Close();
  55. fileStream.Close();
  56. result = true;
  57. }
  58. catch (FileNotFoundException)
  59. {
  60. Log.Warning("SaveData file is not exist");
  61. result = false;
  62. }
  63. return result;
  64. }
  65. public static bool IsAutoSaveDataExists()
  66. {
  67. return File.Exists(SaveData.SaveDataFilePath);
  68. }
  69. public static bool Load()
  70. {
  71. bool result;
  72. try
  73. {
  74. FileStream fileStream = File.OpenRead(SaveData.SaveDataFilePath);
  75. byte[] array = new byte[fileStream.Length];
  76. fileStream.Read(array, 0, array.Length);
  77. if (SaveData.OnGameLoaded != null)
  78. {
  79. SaveData.OnGameLoaded(null, new SaveLoadedEventArgs(SaveData.GetObject(array)));
  80. }
  81. fileStream.Close();
  82. result = true;
  83. }
  84. catch (FileNotFoundException)
  85. {
  86. Log.Warning("SaveData file is not exist");
  87. result = false;
  88. }
  89. return result;
  90. }
  91. public static void Delete()
  92. {
  93. if (File.Exists(SaveData.SaveDataFilePath))
  94. {
  95. File.Delete(SaveData.SaveDataFilePath);
  96. }
  97. }
  98. private static byte[] GetBuffer(GameData obj)
  99. {
  100. MemoryStream memoryStream = new MemoryStream();
  101. BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
  102. binaryWriter.Write(JsonMapper.ToJson(obj));
  103. binaryWriter.Close();
  104. return memoryStream.GetBuffer();
  105. }
  106. private static GameData GetObject(byte[] buffer)
  107. {
  108. MemoryStream input = new MemoryStream(buffer);
  109. BinaryReader binaryReader = new BinaryReader(input);
  110. GameData result = JsonMapper.ToObject<GameData>(binaryReader.ReadString());
  111. binaryReader.Close();
  112. return result;
  113. }
  114. }