Prefs.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Concurrent;
  4. using System.Threading;
  5. using System.Linq;
  6. using System.IO;
  7. using UnityEngine;
  8. using TapSDK.Core;
  9. using TapSDK.Core.Internal.Log;
  10. namespace TapSDK.Core.Standalone.Internal {
  11. public class Prefs {
  12. internal static readonly string OLD_PERSISTENT_FILE_NAME = "tapdb_storage_v2";
  13. private string persistentFilePath;
  14. private readonly ConcurrentDictionary<string, object> data;
  15. private readonly Thread persistThread;
  16. private readonly AutoResetEvent persistEvent;
  17. public Prefs() {
  18. string newCacheFileName = OLD_PERSISTENT_FILE_NAME;
  19. if( TapTapSDK.taptapSdkOptions != null && !string.IsNullOrEmpty(TapTapSDK.taptapSdkOptions.clientId)) {
  20. newCacheFileName = OLD_PERSISTENT_FILE_NAME + "_" + TapTapSDK.taptapSdkOptions.clientId;
  21. }
  22. persistentFilePath = Path.Combine(Application.persistentDataPath, newCacheFileName);
  23. // 兼容旧版缓存文件
  24. if( !File.Exists(persistentFilePath)) {
  25. string oldPath = Path.Combine(Application.persistentDataPath, OLD_PERSISTENT_FILE_NAME);
  26. if (File.Exists(oldPath)){
  27. File.Move(oldPath, persistentFilePath);
  28. }
  29. }
  30. if (File.Exists(persistentFilePath)) {
  31. try {
  32. string json = File.ReadAllText(persistentFilePath);
  33. Dictionary<string, object> jsonData = Json.Deserialize(json) as Dictionary<string, object>;
  34. data = new ConcurrentDictionary<string, object>(jsonData);
  35. } catch (Exception e) {
  36. TapLog.Error(e.Message);
  37. File.Delete(persistentFilePath);
  38. }
  39. }
  40. if (data == null) {
  41. data = new ConcurrentDictionary<string, object>();
  42. }
  43. persistEvent = new AutoResetEvent(false);
  44. persistThread = new Thread(PersistProc) {
  45. IsBackground = true
  46. };
  47. persistThread.Start();
  48. }
  49. public T Get<T>(string key) {
  50. if (data.TryGetValue(key, out object val)) {
  51. return (T)val;
  52. }
  53. return default;
  54. }
  55. public void Set<T>(string key, T value) {
  56. data[key] = value;
  57. persistEvent.Set();
  58. }
  59. public bool TryRemove<T>(string key, out T val) {
  60. if (data.TryRemove(key, out object v)) {
  61. val = (T)v;
  62. persistEvent.Set();
  63. return true;
  64. }
  65. val = default;
  66. return false;
  67. }
  68. public void AddOrUpdate(string key, object addValue, Func<string, object, object> updateValueFactory) {
  69. data.AddOrUpdate(key, addValue, updateValueFactory);
  70. persistEvent.Set();
  71. }
  72. private void PersistProc() {
  73. while (true) {
  74. persistEvent.WaitOne();
  75. try {
  76. Dictionary<string, object> dict = data.ToArray()
  77. .ToDictionary(kv => kv.Key, kv => kv.Value);
  78. string json = Json.Serialize(dict);
  79. File.WriteAllText(persistentFilePath, json);
  80. } catch (Exception e) {
  81. TapLog.Error(e.Message);
  82. }
  83. }
  84. }
  85. }
  86. }