YandexPrefs.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace Eiko.YaSDK.Data
  4. {
  5. public class YandexPrefsParams
  6. {
  7. public bool IsAutorised { get; set; } = false;
  8. public bool IsInit { get; set; } = false;
  9. public Dictionary<string, string> data;
  10. public Dictionary<string, int> score;
  11. public YandexPrefsParams()
  12. {
  13. data = new Dictionary<string, string>();
  14. score = new Dictionary<string, int>();
  15. }
  16. public void Fill(GetDataCallback data)
  17. {
  18. foreach (var item in data.data)
  19. {
  20. this.data.Add(item.key, item.value);
  21. }
  22. foreach (var item in data.score)
  23. {
  24. this.score.Add(item.key, item.value);
  25. }
  26. }
  27. }
  28. public static class YandexPrefs
  29. {
  30. private static YandexPrefsParams param;
  31. private static IPrefsProvider prefs = new NoInitializePrefsProvider();
  32. public static InitAsyncOperation Init()
  33. {
  34. param = new YandexPrefsParams();
  35. var operation = new InitAsyncOperation(param);
  36. YandexSDK.instance.InitData();
  37. return operation;
  38. }
  39. public static void SetInt(string key, int value)
  40. {
  41. Debug.Log(key + " " + value);
  42. prefs.SetInt(key, value);
  43. if (param.IsAutorised)
  44. {
  45. param.score[key] = value;
  46. YandexSDK.instance.SetPlayerScore(key,value);
  47. }
  48. else
  49. {
  50. PlayerPrefs.SetInt(key,value);
  51. }
  52. }
  53. public static void SetString(string key, string value)
  54. {
  55. Debug.Log(key + " " + value);
  56. prefs.SetString(key, value);
  57. if (param.IsAutorised)
  58. {
  59. param.data[key]=value;
  60. YandexSDK.instance.SetPlayerData(key,value);
  61. }
  62. else
  63. {
  64. PlayerPrefs.SetString(key, value);
  65. }
  66. }
  67. public static int GetInt(string key)
  68. {
  69. //Debug.Log(key + " " + value);
  70. return prefs.GetInt(key);
  71. if (param.IsAutorised)
  72. {
  73. if (param.score.TryGetValue(key, out var value))
  74. return value;
  75. else
  76. return 0;
  77. }
  78. else
  79. {
  80. return PlayerPrefs.GetInt(key, 0);
  81. }
  82. }
  83. public static string GetString(string key)
  84. {
  85. return prefs.GetString(key);
  86. if (param.IsAutorised)
  87. {
  88. if (param.data.TryGetValue(key, out var value))
  89. return value;
  90. else
  91. return "";
  92. }
  93. else
  94. {
  95. return PlayerPrefs.GetString(key, "");
  96. }
  97. }
  98. public class InitAsyncOperation : CustomYieldInstruction
  99. {
  100. public InitAsyncOperation(YandexPrefsParams param)
  101. {
  102. YandexSDK.instance.noAutorized += Instance_noAutorized;
  103. YandexSDK.instance.onDataRecived += Instance_onDataRecived; ;
  104. this.param = param;
  105. }
  106. private void Instance_onDataRecived(GetDataCallback obj)
  107. {
  108. param.Fill(obj);
  109. Callback(true);
  110. }
  111. private void Instance_noAutorized()
  112. {
  113. Callback(false);
  114. }
  115. private YandexPrefsParams param;
  116. public bool IsSuccess;
  117. public override bool keepWaiting => _keepWaiting;
  118. private bool _keepWaiting = true;
  119. private void Callback(bool success)
  120. {
  121. param.IsInit = true;
  122. _keepWaiting = false;
  123. param.IsAutorised = success;
  124. IsSuccess = success;
  125. if (success)
  126. prefs = new YandexPrefsProvider(param);
  127. else
  128. prefs = new PlayerPrefsProvider();
  129. }
  130. }
  131. }
  132. public interface IPrefsProvider {
  133. public void SetInt(string key, int value);
  134. public void SetString(string key, string value);
  135. public int GetInt(string key);
  136. public string GetString(string key);
  137. }
  138. public class NoInitializePrefsProvider : IPrefsProvider
  139. {
  140. private const string ErrorMessage = "YandexPrefs no init!";
  141. public int GetInt(string key)
  142. {
  143. Debug.LogError(ErrorMessage);
  144. return 0;
  145. }
  146. public string GetString(string key)
  147. {
  148. Debug.LogError(ErrorMessage);
  149. return "";
  150. }
  151. public void SetInt(string key, int value)
  152. {
  153. Debug.LogError(ErrorMessage);
  154. }
  155. public void SetString(string key, string value)
  156. {
  157. Debug.LogError(ErrorMessage);
  158. }
  159. }
  160. public class YandexPrefsProvider : IPrefsProvider
  161. {
  162. private YandexPrefsParams param;
  163. public YandexPrefsProvider(YandexPrefsParams param)
  164. {
  165. this.param = param;
  166. }
  167. public void SetInt(string key, int value)
  168. {
  169. param.score[key] = value;
  170. YandexSDK.instance.SetPlayerScore(key, value);
  171. }
  172. public void SetString(string key, string value)
  173. {
  174. param.data[key] = value;
  175. YandexSDK.instance.SetPlayerData(key, value);
  176. }
  177. public int GetInt(string key)
  178. {
  179. if (param.score.TryGetValue(key, out var value))
  180. return value;
  181. else
  182. return 0;
  183. }
  184. public string GetString(string key)
  185. {
  186. if (param.data.TryGetValue(key, out var value))
  187. return value;
  188. else
  189. return "";
  190. }
  191. }
  192. public class PlayerPrefsProvider: IPrefsProvider
  193. {
  194. public void SetInt(string key, int value)
  195. {
  196. PlayerPrefs.SetInt(key, value);
  197. }
  198. public void SetString(string key, string value)
  199. {
  200. PlayerPrefs.SetString(key, value);
  201. }
  202. public int GetInt(string key)
  203. {
  204. return PlayerPrefs.GetInt(key, 0);
  205. }
  206. public string GetString(string key)
  207. {
  208. return PlayerPrefs.GetString(key, "");
  209. }
  210. }
  211. }