| | 1 | | using System; |
| | 2 | | using System.Runtime.InteropServices; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace DCL.Helpers |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// Provider class to use LocalStorage instead of IndexedDB because it was not reliably saving settings. |
| | 9 | | /// For all the gets, we have to check that the PlayerPref key does exist. We may lose information that was |
| | 10 | | /// succesfully stored in IndexedDB. That check should only go once per user per key. |
| | 11 | | /// </summary> |
| | 12 | | public class PlayerPrefsProviderLocalStorage : IPlayerPrefsProvider |
| | 13 | | { |
| | 14 | | //Nothing to save when using local storage |
| 0 | 15 | | public void Save() { } |
| | 16 | |
|
| | 17 | | public bool HasKey(string key) => |
| 0 | 18 | | HasKeyInLocalStorage(key) == 1; |
| | 19 | |
|
| | 20 | | public int GetInt(string key) => |
| 0 | 21 | | GetInt(key, 0); |
| | 22 | |
|
| | 23 | | public int GetInt(string key, int defaultValue) |
| | 24 | | { |
| 0 | 25 | | var valueGotFromLocalStorage = int.Parse(GetLocalStorageValue(key, defaultValue.ToString())); |
| | 26 | |
|
| 0 | 27 | | if (valueGotFromLocalStorage.Equals(defaultValue) && PlayerPrefs.HasKey(key)) |
| | 28 | | { |
| | 29 | | // We get the saved value in PlayerPrefs and move it to LocalStorage |
| 0 | 30 | | valueGotFromLocalStorage = PlayerPrefs.GetInt(key); |
| 0 | 31 | | SetInt(key, valueGotFromLocalStorage); |
| 0 | 32 | | PlayerPrefs.DeleteKey(key); |
| | 33 | | } |
| | 34 | |
|
| 0 | 35 | | return valueGotFromLocalStorage; |
| | 36 | | } |
| | 37 | |
|
| | 38 | | public void SetInt(string key, int value) |
| | 39 | | { |
| 0 | 40 | | SaveStringToLocalStorage(key, value.ToString()); |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | public bool GetBool(string key, bool defaultValue) => |
| 0 | 44 | | GetInt(key, defaultValue ? 1 : 0) == 1; |
| | 45 | |
|
| | 46 | | public void SetBool(string key, bool value) => |
| 0 | 47 | | SetInt(key, value ? 1 : 0); |
| | 48 | |
|
| | 49 | | public string GetString(string key, string defaultValue) |
| | 50 | | { |
| 0 | 51 | | string valueGotFromLocalStorage = GetLocalStorageValue(key, string.IsNullOrEmpty(defaultValue) ? "" : defaul |
| | 52 | |
|
| 0 | 53 | | if (!string.IsNullOrEmpty(valueGotFromLocalStorage) && valueGotFromLocalStorage.Equals(defaultValue) && Play |
| | 54 | | { |
| | 55 | | // We get the saved value in PlayerPrefs and move it to LocalStorage |
| 0 | 56 | | valueGotFromLocalStorage = PlayerPrefs.GetString(key); |
| 0 | 57 | | SetString(key, valueGotFromLocalStorage); |
| 0 | 58 | | PlayerPrefs.DeleteKey(key); |
| | 59 | | } |
| | 60 | |
|
| 0 | 61 | | return valueGotFromLocalStorage; |
| | 62 | | } |
| | 63 | |
|
| | 64 | | public void SetString(string key, string value) |
| | 65 | | { |
| 0 | 66 | | SaveStringToLocalStorage(key, value); |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | public float GetFloat(string key, float defaultValue) |
| | 70 | | { |
| 0 | 71 | | var valueGotFromLocalStorage = float.Parse(GetLocalStorageValue(key, defaultValue.ToString())); |
| | 72 | |
|
| 0 | 73 | | if (valueGotFromLocalStorage.Equals(defaultValue) && PlayerPrefs.HasKey(key)) |
| | 74 | | { |
| | 75 | | // We get the saved value in PlayerPrefs and move it to LocalStorage |
| 0 | 76 | | valueGotFromLocalStorage = PlayerPrefs.GetFloat(key); |
| 0 | 77 | | SetFloat(key, valueGotFromLocalStorage); |
| 0 | 78 | | PlayerPrefs.DeleteKey(key); |
| | 79 | | } |
| | 80 | |
|
| 0 | 81 | | return valueGotFromLocalStorage; |
| | 82 | | } |
| | 83 | |
|
| | 84 | | public void SetFloat(string key, float value) |
| | 85 | | { |
| 0 | 86 | | SaveStringToLocalStorage(key, value.ToString()); |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | private string GetLocalStorageValue(string key, string defaultValue) |
| | 90 | | { |
| 0 | 91 | | if (HasKey(key)) |
| 0 | 92 | | return LoadStringFromLocalStorage(key); |
| | 93 | |
|
| 0 | 94 | | return defaultValue; |
| | 95 | | } |
| | 96 | |
|
| | 97 | | #if UNITY_WEBGL |
| | 98 | | [DllImport("__Internal")] |
| | 99 | | private static extern void SaveStringToLocalStorage(string key, string value); |
| | 100 | |
|
| | 101 | | [DllImport("__Internal")] |
| | 102 | | private static extern string LoadStringFromLocalStorage(string key); |
| | 103 | |
|
| | 104 | | [DllImport("__Internal")] |
| | 105 | | private static extern int HasKeyInLocalStorage(string key); |
| | 106 | | #else |
| 0 | 107 | | private static void SaveStringToLocalStorage(string key, string value) { } |
| | 108 | |
|
| | 109 | | private static string LoadStringFromLocalStorage(string key) => |
| 0 | 110 | | ""; |
| | 111 | |
|
| | 112 | | private static int HasKeyInLocalStorage(string key) => |
| 0 | 113 | | 0; |
| | 114 | | #endif |
| | 115 | | } |
| | 116 | | } |