| | 1 | | using System; |
| | 2 | | using System.Linq; |
| | 3 | | using DCL.Configuration; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace DCL.Builder |
| | 7 | | { |
| | 8 | | public class Scene |
| | 9 | | { |
| | 10 | | public enum Source { BUILDER, BUILDER_IN_WORLD, SDK } |
| | 11 | |
|
| 0 | 12 | | public string title => metadata.display.title; |
| 0 | 13 | | public string description => metadata.display.description; |
| 0 | 14 | | public string author => metadata.contact.name; |
| 0 | 15 | | public string navmapThumbnail => thumbnail; |
| 0 | 16 | | public Vector2Int @base => baseCoord; |
| 0 | 17 | | public Vector2Int[] parcels => parcelsCoord; |
| 0 | 18 | | public string id => entityId; |
| 0 | 19 | | public Source source => deploymentSource; |
| 0 | 20 | | public LandWithAccess land => sceneLand; |
| 0 | 21 | | public string[] requiredPermissions => metadata.requiredPermissions; |
| 1 | 22 | | public string projectId => metadata.source?.projectId; |
| 2 | 23 | | public bool isEmpty => metadata.source?.isEmpty ?? false; |
| 0 | 24 | | public long deployTimestamp => timestamp; |
| | 25 | |
|
| | 26 | | private CatalystSceneEntityMetadata metadata; |
| | 27 | | internal Source deploymentSource; |
| | 28 | | private Vector2Int baseCoord; |
| | 29 | | internal Vector2Int[] parcelsCoord; |
| | 30 | | private string thumbnail; |
| | 31 | | private string entityId; |
| | 32 | |
|
| | 33 | | internal LandWithAccess sceneLand; |
| | 34 | | internal long timestamp; |
| | 35 | | internal CatalystEntityContent[] contents; |
| | 36 | |
|
| 0 | 37 | | public Scene() { } |
| | 38 | |
|
| 5 | 39 | | public Scene(CatalystSceneEntityPayload pointerData, string contentUrl) |
| | 40 | | { |
| | 41 | |
|
| | 42 | | const string builderSourceName = "builder"; |
| | 43 | | const string builderInWorldSourceName = "builder-in-world"; |
| | 44 | |
|
| 5 | 45 | | metadata = pointerData.metadata; |
| 5 | 46 | | entityId = pointerData.id; |
| | 47 | |
|
| 5 | 48 | | deploymentSource = Source.SDK; |
| | 49 | |
|
| 5 | 50 | | if (pointerData.content != null && pointerData.content.Any(content => content.file == BIWSettings.BUILDER_SC |
| | 51 | | { |
| 0 | 52 | | deploymentSource = Source.BUILDER_IN_WORLD; |
| 0 | 53 | | } |
| 5 | 54 | | else if (metadata.source != null && metadata.source.origin == builderInWorldSourceName) |
| | 55 | | { |
| 0 | 56 | | deploymentSource = Source.BUILDER_IN_WORLD; |
| 0 | 57 | | } |
| 5 | 58 | | else if (metadata.source != null && metadata.source.origin == builderSourceName) |
| | 59 | | { |
| 0 | 60 | | deploymentSource = Source.BUILDER; |
| | 61 | | } |
| | 62 | |
|
| 5 | 63 | | if (pointerData.content != null) |
| 0 | 64 | | contents = pointerData.content; |
| | 65 | |
|
| 5 | 66 | | baseCoord = StringToVector2Int(metadata.scene.@base); |
| 5 | 67 | | parcelsCoord = metadata.scene.parcels.Select(StringToVector2Int).ToArray(); |
| 5 | 68 | | thumbnail = GetNavmapThumbnailUrl(pointerData, contentUrl); |
| | 69 | |
|
| 5 | 70 | | timestamp = pointerData.timestamp; |
| 5 | 71 | | } |
| | 72 | |
|
| | 73 | | public bool HasContent(string name) |
| | 74 | | { |
| 0 | 75 | | foreach (CatalystEntityContent content in contents) |
| | 76 | | { |
| 0 | 77 | | if (content.file == name) |
| 0 | 78 | | return true; |
| | 79 | | } |
| 0 | 80 | | return false; |
| | 81 | | } |
| | 82 | |
|
| | 83 | | public bool TryGetHashForContent(string name, out string hash) |
| | 84 | | { |
| 0 | 85 | | hash = null; |
| 0 | 86 | | foreach (CatalystEntityContent content in contents) |
| | 87 | | { |
| 0 | 88 | | if (content.file == name) |
| | 89 | | { |
| 0 | 90 | | hash = content.hash; |
| 0 | 91 | | return true; |
| | 92 | | } |
| | 93 | | } |
| 0 | 94 | | return false; |
| | 95 | | } |
| | 96 | |
|
| 0 | 97 | | public void SetScene(LandWithAccess land) { sceneLand = land; } |
| | 98 | |
|
| | 99 | | static Vector2Int StringToVector2Int(string coords) |
| | 100 | | { |
| 10 | 101 | | string[] coordSplit = coords.Split(','); |
| 10 | 102 | | if (coordSplit.Length == 2 && int.TryParse(coordSplit[0], out int x) && int.TryParse(coordSplit[1], out int |
| | 103 | | { |
| 10 | 104 | | return new Vector2Int(x, y); |
| | 105 | | } |
| | 106 | |
|
| 0 | 107 | | return Vector2Int.zero; |
| | 108 | | } |
| | 109 | |
|
| | 110 | | static string GetNavmapThumbnailUrl(CatalystSceneEntityPayload pointerData, string contentUrl) |
| | 111 | | { |
| | 112 | | const string contentDownloadUrlFormat = "{0}/contents/{1}"; |
| | 113 | | const string builderUrlFormat = "https://builder-api.decentraland.org/v1/projects/{0}/media/preview.png"; |
| | 114 | |
|
| 5 | 115 | | string thumbnail = pointerData.metadata.display.navmapThumbnail; |
| | 116 | |
|
| 5 | 117 | | bool isThumbnailPresent = !string.IsNullOrEmpty(thumbnail); |
| 5 | 118 | | bool isThumbnailFileDeployed = isThumbnailPresent && !thumbnail.StartsWith("http"); |
| | 119 | |
|
| 5 | 120 | | if (isThumbnailPresent && !isThumbnailFileDeployed) |
| | 121 | | { |
| 0 | 122 | | return thumbnail; |
| | 123 | | } |
| | 124 | |
|
| 5 | 125 | | if (isThumbnailFileDeployed && pointerData.content != null) |
| | 126 | | { |
| 0 | 127 | | string thumbnailHash = pointerData.content.FirstOrDefault(content => content.file == thumbnail)?.hash; |
| 0 | 128 | | if (!string.IsNullOrEmpty(thumbnailHash)) |
| | 129 | | { |
| 0 | 130 | | return string.Format(contentDownloadUrlFormat, contentUrl, thumbnailHash); |
| | 131 | | } |
| | 132 | | } |
| | 133 | |
|
| 5 | 134 | | if (pointerData.metadata.source != null && !string.IsNullOrEmpty(pointerData.metadata.source.projectId)) |
| | 135 | | { |
| 0 | 136 | | return string.Format(builderUrlFormat, pointerData.metadata.source.projectId); |
| | 137 | | } |
| | 138 | |
|
| 5 | 139 | | return thumbnail; |
| | 140 | | } |
| | 141 | | } |
| | 142 | | } |