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