| | 1 | | using DCL.Helpers; |
| | 2 | | using Newtonsoft.Json; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace DCL.EquippedEmotes |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Plugin feature that initialize the Equipped Emotes feature. |
| | 10 | | /// </summary> |
| | 11 | | public class EquippedEmotesInitializerPlugin : IPlugin |
| | 12 | | { |
| | 13 | | internal const string PLAYER_PREFS_EQUIPPED_EMOTES_KEY = "EquippedNFTEmotes"; |
| | 14 | |
|
| 0 | 15 | | internal DataStore_EmotesCustomization emotesCustomizationDataStore => DataStore.i.emotesCustomization; |
| 0 | 16 | | internal DataStore_FeatureFlag featureFlagsDataStore => DataStore.i.featureFlags; |
| | 17 | |
|
| 0 | 18 | | public EquippedEmotesInitializerPlugin() |
| | 19 | | { |
| 0 | 20 | | LoadDefaultEquippedEmotes(); |
| | 21 | |
|
| 0 | 22 | | LoadEquippedEmotesFromLocalStorage(); |
| | 23 | |
|
| 0 | 24 | | emotesCustomizationDataStore.equippedEmotes.OnSet += OnEquippedEmotesSet; |
| 0 | 25 | | emotesCustomizationDataStore.equippedEmotes.OnAdded += OnEquippedEmoteAddedOrRemoved; |
| 0 | 26 | | emotesCustomizationDataStore.equippedEmotes.OnRemoved += OnEquippedEmoteAddedOrRemoved; |
| 0 | 27 | | } |
| | 28 | |
|
| | 29 | | internal List<string> GetDefaultEmotes() |
| | 30 | | { |
| 0 | 31 | | return new List<string> |
| | 32 | | { |
| | 33 | | "handsair", |
| | 34 | | "wave", |
| | 35 | | "fistpump", |
| | 36 | | "dance", |
| | 37 | | "raiseHand", |
| | 38 | | "clap", |
| | 39 | | "money", |
| | 40 | | "kiss", |
| | 41 | | "headexplode", |
| | 42 | | "shrug" |
| | 43 | | }; |
| | 44 | | } |
| | 45 | |
|
| 0 | 46 | | internal void LoadDefaultEquippedEmotes() { SetEquippedEmotes(GetDefaultEmotes()); } |
| | 47 | |
|
| | 48 | | internal void LoadEquippedEmotesFromLocalStorage() |
| | 49 | | { |
| | 50 | | List<string> storedEquippedEmotes; |
| | 51 | |
|
| | 52 | | try |
| | 53 | | { |
| 0 | 54 | | storedEquippedEmotes = JsonConvert.DeserializeObject<List<string>>(PlayerPrefsUtils.GetString(PLAYER_PRE |
| 0 | 55 | | } |
| 0 | 56 | | catch |
| | 57 | | { |
| 0 | 58 | | storedEquippedEmotes = null; |
| 0 | 59 | | } |
| | 60 | |
|
| 0 | 61 | | if (storedEquippedEmotes == null) |
| 0 | 62 | | storedEquippedEmotes = GetDefaultEmotes(); |
| | 63 | |
|
| 0 | 64 | | SetEquippedEmotes(storedEquippedEmotes); |
| 0 | 65 | | } |
| | 66 | |
|
| 0 | 67 | | internal void OnEquippedEmotesSet(IEnumerable<EquippedEmoteData> equippedEmotes) { SaveEquippedEmotesInLocalStor |
| | 68 | |
|
| 0 | 69 | | internal void OnEquippedEmoteAddedOrRemoved(EquippedEmoteData equippedEmote) { SaveEquippedEmotesInLocalStorage( |
| | 70 | |
|
| | 71 | | internal void SaveEquippedEmotesInLocalStorage() |
| | 72 | | { |
| 0 | 73 | | List<string> emotesIdsToStore = new List<string>(); |
| 0 | 74 | | foreach (EquippedEmoteData equippedEmoteData in emotesCustomizationDataStore.equippedEmotes.Get()) |
| | 75 | | { |
| 0 | 76 | | emotesIdsToStore.Add(equippedEmoteData != null ? equippedEmoteData.id : null); |
| | 77 | | } |
| | 78 | |
|
| | 79 | | // TODO: We should avoid static calls and create injectable interfaces |
| 0 | 80 | | PlayerPrefsUtils.SetString(PLAYER_PREFS_EQUIPPED_EMOTES_KEY, JsonConvert.SerializeObject(emotesIdsToStore)); |
| 0 | 81 | | PlayerPrefsUtils.Save(); |
| 0 | 82 | | } |
| | 83 | |
|
| | 84 | | internal void SetEquippedEmotes(List<string> storedEquippedEmotes) |
| | 85 | | { |
| 0 | 86 | | List<EquippedEmoteData> storedEquippedEmotesData = new List<EquippedEmoteData>(); |
| 0 | 87 | | foreach (string emoteId in storedEquippedEmotes) |
| | 88 | | { |
| 0 | 89 | | storedEquippedEmotesData.Add( |
| | 90 | | string.IsNullOrEmpty(emoteId) ? null : new EquippedEmoteData { id = emoteId, cachedThumbnail = null |
| | 91 | | } |
| 0 | 92 | | emotesCustomizationDataStore.equippedEmotes.Set(storedEquippedEmotesData); |
| 0 | 93 | | } |
| | 94 | |
|
| | 95 | | public void Dispose() |
| | 96 | | { |
| 0 | 97 | | emotesCustomizationDataStore.equippedEmotes.OnSet -= OnEquippedEmotesSet; |
| 0 | 98 | | emotesCustomizationDataStore.equippedEmotes.OnAdded -= OnEquippedEmoteAddedOrRemoved; |
| 0 | 99 | | emotesCustomizationDataStore.equippedEmotes.OnRemoved -= OnEquippedEmoteAddedOrRemoved; |
| 0 | 100 | | } |
| | 101 | | } |
| | 102 | | } |