| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using DCL.Interface; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | public class EmotesCatalogBridge : MonoBehaviour |
| | 10 | | { |
| | 11 | | [Serializable] |
| | 12 | | private class AddEmotesResponse |
| | 13 | | { |
| | 14 | | public EmoteItem[] emotes; |
| | 15 | | public string context; |
| | 16 | | } |
| | 17 | |
|
| | 18 | | public event Action<WearableItem[]> OnEmotesReceived; |
| | 19 | | public event EmoteRejectedDelegate OnEmoteRejected; |
| | 20 | | public event Action<WearableItem[], string> OnOwnedEmotesReceived; |
| | 21 | |
|
| 42 | 22 | | private readonly HashSet<string> emotesToRequestThisFrame = new HashSet<string>(); |
| | 23 | | private bool isAlreadyDestroyed; |
| | 24 | |
|
| | 25 | | public static EmotesCatalogBridge GetOrCreate() |
| | 26 | | { |
| 40 | 27 | | var brigeGO = SceneReferences.i?.bridgeGameObject; |
| 40 | 28 | | if (SceneReferences.i?.bridgeGameObject == null) |
| 40 | 29 | | return new GameObject("Bridge").AddComponent<EmotesCatalogBridge>(); |
| | 30 | |
|
| 0 | 31 | | return brigeGO.GetOrCreateComponent<EmotesCatalogBridge>(); |
| | 32 | | } |
| | 33 | |
|
| 0 | 34 | | public void RequestEmote(string emoteId) { emotesToRequestThisFrame.Add(emoteId); } |
| | 35 | |
|
| | 36 | | // Alex: If at some point base emotes are not embedded in the client but sent by kernel |
| | 37 | | // this call wouldn't be listened by any Promise and wont be processed. |
| | 38 | | // This issue can be solved easily by adding a different call AddBaseEmotes and a different event |
| | 39 | | public void AddEmotesToCatalog(string payload) |
| | 40 | | { |
| 0 | 41 | | AddEmotesResponse request = null; |
| | 42 | |
|
| | 43 | | try |
| | 44 | | { |
| 0 | 45 | | request = JsonUtility.FromJson<AddEmotesResponse>(payload); |
| 0 | 46 | | } |
| 0 | 47 | | catch (Exception e) |
| | 48 | | { |
| 0 | 49 | | Debug.LogError($"Fail to parse emote json {e}"); |
| 0 | 50 | | } |
| | 51 | |
|
| 0 | 52 | | if (request == null) |
| 0 | 53 | | return; |
| | 54 | |
|
| 0 | 55 | | var context = request.context; |
| 0 | 56 | | if (string.IsNullOrEmpty(context)) |
| | 57 | | { |
| 0 | 58 | | Debug.LogError("EmotesCatalogBridge error: empty context is not supposed to be in the wearables request"); |
| | 59 | | } |
| | 60 | | else |
| | 61 | | { |
| 0 | 62 | | if (request.context.StartsWith("emotes:")) |
| | 63 | | { |
| 0 | 64 | | ResolveMissingEmotesRejection(request.context, request.emotes); |
| 0 | 65 | | OnEmotesReceived?.Invoke(request.emotes); |
| | 66 | | } |
| | 67 | | else |
| | 68 | | { |
| 0 | 69 | | OnOwnedEmotesReceived?.Invoke(request.emotes, request.context); |
| | 70 | | } |
| | 71 | | } |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | private void ResolveMissingEmotesRejection(string requestContext, EmoteItem[] emotes) |
| | 75 | | { |
| 0 | 76 | | var emoteIdsFromContext = ContextStringToEmoteIds(requestContext); |
| 0 | 77 | | var loadedEmoteIds = new HashSet<string>(); |
| 0 | 78 | | foreach (var emote in emotes) |
| | 79 | | { |
| 0 | 80 | | loadedEmoteIds.Add(emote.id); |
| | 81 | | } |
| | 82 | |
|
| 0 | 83 | | foreach(var emoteId in emoteIdsFromContext) |
| 0 | 84 | | if (!loadedEmoteIds.Contains(emoteId)) |
| | 85 | | { |
| 0 | 86 | | OnEmoteRejected?.Invoke(emoteId, "Emote from context not found in response: "); |
| | 87 | | } |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | public void RequestOwnedEmotes(string userId) |
| | 91 | | { |
| 0 | 92 | | WebInterface.RequestEmotes( |
| | 93 | | ownedByUser: userId, |
| | 94 | | emoteIds: null, |
| | 95 | | collectionIds: null, |
| | 96 | | context: $"{userId}" |
| | 97 | | ); |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | private static string EmoteIdsToContextString(IEnumerable<string> emoteIds) |
| | 101 | | { |
| 0 | 102 | | var commaJoinedEmoteIds = string.Join(",", emoteIds); |
| 0 | 103 | | return "emotes:" + commaJoinedEmoteIds; |
| | 104 | | } |
| | 105 | |
|
| | 106 | | private static string[] ContextStringToEmoteIds(string context) |
| | 107 | | { |
| 0 | 108 | | context = context.Replace("emotes:", ""); |
| 0 | 109 | | return context.Split(','); |
| | 110 | | } |
| | 111 | |
|
| | 112 | | private void Update() |
| | 113 | | { |
| 3903 | 114 | | if (emotesToRequestThisFrame.Count == 0) |
| 3903 | 115 | | return; |
| | 116 | |
|
| 0 | 117 | | var commaJoinedEmoteIds = string.Join(",", emotesToRequestThisFrame); |
| | 118 | |
|
| 0 | 119 | | WebInterface.RequestEmotes( |
| | 120 | | ownedByUser: null, |
| | 121 | | emoteIds: emotesToRequestThisFrame.ToArray(), |
| | 122 | | collectionIds: null, |
| | 123 | | context: EmoteIdsToContextString(emotesToRequestThisFrame) |
| | 124 | | ); |
| | 125 | |
|
| | 126 | |
|
| 0 | 127 | | emotesToRequestThisFrame.Clear(); |
| 0 | 128 | | } |
| | 129 | |
|
| | 130 | | public void Dispose() |
| | 131 | | { |
| 0 | 132 | | if (isAlreadyDestroyed) |
| 0 | 133 | | return; |
| 0 | 134 | | Destroy(gameObject); |
| 0 | 135 | | } |
| | 136 | |
|
| 82 | 137 | | private void OnDestroy() { isAlreadyDestroyed = true; } |
| | 138 | | } |