| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL; |
| | 5 | | using DCL.Interface; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | [CreateAssetMenu(fileName = "UserProfile", menuName = "UserProfile")] |
| | 9 | | public class UserProfile : ScriptableObject //TODO Move to base variable |
| | 10 | | { |
| 1 | 11 | | static DateTime epochStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); |
| | 12 | | public event Action<UserProfile> OnUpdate; |
| | 13 | | public event Action<Texture2D> OnFaceSnapshotReadyEvent; |
| | 14 | | public event Action<string, long> OnAvatarExpressionSet; |
| | 15 | |
|
| 0 | 16 | | public string userId => model.userId; |
| 0 | 17 | | public string ethAddress => model.ethAddress; |
| 0 | 18 | | public string userName => model.name; |
| 0 | 19 | | public string description => model.description; |
| 0 | 20 | | public string email => model.email; |
| 0 | 21 | | public UserProfileModel.ParcelsWithAccess[] parcelsWithAccess => model.parcelsWithAccess; |
| 98 | 22 | | public List<string> blocked => model.blocked != null ? model.blocked : new List<string>(); |
| 3 | 23 | | public List<string> muted => model.muted ?? new List<string>(); |
| 0 | 24 | | public bool hasConnectedWeb3 => model.hasConnectedWeb3; |
| 0 | 25 | | public bool hasClaimedName => model.hasClaimedName; |
| 0 | 26 | | public AvatarModel avatar => model.avatar; |
| 0 | 27 | | public int tutorialStep => model.tutorialStep; |
| 118 | 28 | | internal Dictionary<string, int> inventory = new Dictionary<string, int>(); |
| | 29 | |
|
| 0 | 30 | | public Texture2D faceSnapshot { get; private set; } |
| | 31 | | private AssetPromise_Texture thumbnailPromise; |
| | 32 | |
|
| 118 | 33 | | internal UserProfileModel model = new UserProfileModel() //Empty initialization to avoid nullchecks |
| | 34 | | { |
| | 35 | | avatar = new AvatarModel() |
| | 36 | | }; |
| | 37 | |
|
| | 38 | | public void UpdateData(UserProfileModel newModel, bool downloadAssets = true) |
| | 39 | | { |
| 170 | 40 | | faceSnapshot = null; |
| | 41 | |
|
| 170 | 42 | | if (newModel == null) |
| | 43 | | { |
| 0 | 44 | | model = null; |
| 0 | 45 | | return; |
| | 46 | | } |
| | 47 | |
|
| 170 | 48 | | model.userId = newModel.userId; |
| 170 | 49 | | model.ethAddress = newModel.ethAddress; |
| 170 | 50 | | model.parcelsWithAccess = newModel.parcelsWithAccess; |
| 170 | 51 | | model.tutorialStep = newModel.tutorialStep; |
| 170 | 52 | | model.hasClaimedName = newModel.hasClaimedName; |
| 170 | 53 | | model.name = newModel.name; |
| 170 | 54 | | model.email = newModel.email; |
| 170 | 55 | | model.description = newModel.description; |
| 170 | 56 | | model.avatar.CopyFrom(newModel.avatar); |
| 170 | 57 | | model.snapshots = newModel.snapshots; |
| 170 | 58 | | model.hasConnectedWeb3 = newModel.hasConnectedWeb3; |
| 170 | 59 | | model.inventory = newModel.inventory; |
| 170 | 60 | | model.blocked = newModel.blocked; |
| 170 | 61 | | model.muted = newModel.muted; |
| | 62 | |
|
| 170 | 63 | | if (model.inventory != null) |
| | 64 | | { |
| 12 | 65 | | SetInventory(model.inventory); |
| | 66 | | } |
| | 67 | |
|
| 170 | 68 | | if (downloadAssets && model.snapshots != null) |
| | 69 | | { |
| | 70 | | //NOTE(Brian): Get before forget to prevent referenceCount == 0 and asset unload |
| 84 | 71 | | var newThumbnailPromise = ThumbnailsManager.GetThumbnail(model.snapshots.face256, OnFaceSnapshotReady); |
| 84 | 72 | | ThumbnailsManager.ForgetThumbnail(thumbnailPromise); |
| 84 | 73 | | thumbnailPromise = newThumbnailPromise; |
| 84 | 74 | | } |
| | 75 | | else |
| | 76 | | { |
| 86 | 77 | | ThumbnailsManager.ForgetThumbnail(thumbnailPromise); |
| 86 | 78 | | thumbnailPromise = null; |
| | 79 | | } |
| | 80 | |
|
| 170 | 81 | | OnUpdate?.Invoke(this); |
| 54 | 82 | | } |
| | 83 | |
|
| | 84 | | public int GetItemAmount(string itemId) |
| | 85 | | { |
| 3736 | 86 | | if (inventory == null || !inventory.ContainsKey(itemId)) |
| 3702 | 87 | | return 0; |
| | 88 | |
|
| 34 | 89 | | return inventory[itemId]; |
| | 90 | | } |
| | 91 | |
|
| | 92 | | private void OnFaceSnapshotReady(Asset_Texture texture) |
| | 93 | | { |
| 1 | 94 | | if (faceSnapshot != null) |
| 0 | 95 | | Destroy(faceSnapshot); |
| | 96 | |
|
| 1 | 97 | | if (texture != null) |
| 0 | 98 | | faceSnapshot = texture.texture; |
| | 99 | |
|
| 1 | 100 | | OnUpdate?.Invoke(this); |
| 1 | 101 | | OnFaceSnapshotReadyEvent?.Invoke(faceSnapshot); |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | public void OverrideAvatar(AvatarModel newModel, Texture2D newFaceSnapshot) |
| | 105 | | { |
| 1 | 106 | | if (model?.snapshots != null) |
| | 107 | | { |
| 1 | 108 | | if (thumbnailPromise != null) |
| | 109 | | { |
| 0 | 110 | | ThumbnailsManager.ForgetThumbnail(thumbnailPromise); |
| 0 | 111 | | thumbnailPromise = null; |
| | 112 | | } |
| | 113 | |
|
| 1 | 114 | | OnFaceSnapshotReady(null); |
| | 115 | | } |
| | 116 | |
|
| 1 | 117 | | model.avatar.CopyFrom(newModel); |
| 1 | 118 | | this.faceSnapshot = newFaceSnapshot; |
| 1 | 119 | | OnUpdate?.Invoke(this); |
| 1 | 120 | | OnFaceSnapshotReadyEvent?.Invoke(faceSnapshot); |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | public void SetAvatarExpression(string id) |
| | 124 | | { |
| 2 | 125 | | var timestamp = (long) (DateTime.UtcNow - epochStart).TotalMilliseconds; |
| 2 | 126 | | avatar.expressionTriggerId = id; |
| 2 | 127 | | avatar.expressionTriggerTimestamp = timestamp; |
| 2 | 128 | | WebInterface.SendExpression(id, timestamp); |
| 2 | 129 | | OnUpdate?.Invoke(this); |
| 2 | 130 | | OnAvatarExpressionSet?.Invoke(id, timestamp); |
| 2 | 131 | | } |
| | 132 | |
|
| | 133 | | public void SetInventory(string[] inventoryIds) |
| | 134 | | { |
| 32 | 135 | | inventory.Clear(); |
| 257 | 136 | | inventory = inventoryIds.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count()); |
| 32 | 137 | | } |
| | 138 | |
|
| 0 | 139 | | public string[] GetInventoryItemsIds() { return inventory.Keys.ToArray(); } |
| | 140 | |
|
| | 141 | | internal static UserProfile ownUserProfile; |
| | 142 | |
|
| | 143 | | public static UserProfile GetOwnUserProfile() |
| | 144 | | { |
| 1364 | 145 | | if (ownUserProfile == null) |
| | 146 | | { |
| 1 | 147 | | ownUserProfile = Resources.Load<UserProfile>("ScriptableObjects/OwnUserProfile"); |
| | 148 | | } |
| | 149 | |
|
| 1364 | 150 | | return ownUserProfile; |
| | 151 | | } |
| | 152 | |
|
| 0 | 153 | | public UserProfileModel CloneModel() => model.Clone(); |
| | 154 | |
|
| | 155 | | #if UNITY_EDITOR |
| | 156 | | private void OnEnable() |
| | 157 | | { |
| 118 | 158 | | Application.quitting -= CleanUp; |
| 118 | 159 | | Application.quitting += CleanUp; |
| 118 | 160 | | } |
| | 161 | |
|
| | 162 | | private void CleanUp() |
| | 163 | | { |
| 0 | 164 | | Application.quitting -= CleanUp; |
| 0 | 165 | | if (UnityEditor.AssetDatabase.Contains(this)) |
| 0 | 166 | | Resources.UnloadAsset(this); |
| 0 | 167 | | } |
| | 168 | | #endif |
| | 169 | | } |