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