| | 1 | | using DCL; |
| | 2 | | using DCL.Configuration; |
| | 3 | | using Newtonsoft.Json; |
| | 4 | | using Newtonsoft.Json.Linq; |
| | 5 | | using System.Collections; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | public class AssetCatalogBridge : MonoBehaviour |
| | 10 | | { |
| | 11 | | public static bool VERBOSE = false; |
| | 12 | |
|
| | 13 | | public static System.Action<SceneObject> OnItemAdded; |
| | 14 | | public static System.Action<SceneObject> OnSceneCatalogItemAdded; |
| | 15 | | public static System.Action<SceneAssetPack> OnSceneAssetPackAdded; |
| | 16 | |
|
| 16 | 17 | | public static AssetCatalogBridge i { get; internal set; } |
| | 18 | |
|
| | 19 | | private SceneAssetPackDictionary sceneAssetPackCatalogValue; |
| | 20 | |
|
| | 21 | | public SceneAssetPackDictionary sceneAssetPackCatalog |
| | 22 | | { |
| | 23 | | get |
| | 24 | | { |
| 109 | 25 | | if (sceneAssetPackCatalogValue == null) |
| | 26 | | { |
| 36 | 27 | | sceneAssetPackCatalogValue = Resources.Load<SceneAssetPackDictionary>("SceneAssetPackCatalog"); |
| | 28 | | } |
| | 29 | |
|
| 109 | 30 | | return sceneAssetPackCatalogValue; |
| | 31 | | } |
| | 32 | | } |
| | 33 | |
|
| | 34 | | private SceneObjectDictionary sceneObjectCatalogValue; |
| | 35 | |
|
| | 36 | | public SceneObjectDictionary sceneObjectCatalog |
| | 37 | | { |
| | 38 | | get |
| | 39 | | { |
| 126 | 40 | | if (sceneObjectCatalogValue == null) |
| | 41 | | { |
| 36 | 42 | | sceneObjectCatalogValue = Resources.Load<SceneObjectDictionary>("SceneObjectCatalog"); |
| | 43 | | } |
| | 44 | |
|
| 126 | 45 | | return sceneObjectCatalogValue; |
| | 46 | | } |
| | 47 | | } |
| | 48 | |
|
| 96 | 49 | | public void Awake() { i = this; } |
| | 50 | |
|
| | 51 | | public ContentProvider GetContentProviderForAssetIdInSceneObjectCatalog(string assetId) |
| | 52 | | { |
| 17 | 53 | | if (sceneObjectCatalogValue.TryGetValue(assetId, out SceneObject sceneObject)) |
| 17 | 54 | | return CreateContentProviderForSceneObject(sceneObject); |
| | 55 | |
|
| 0 | 56 | | return null; |
| | 57 | | } |
| | 58 | |
|
| | 59 | | ContentProvider CreateContentProviderForSceneObject(SceneObject sceneObject) |
| | 60 | | { |
| 17 | 61 | | ContentProvider contentProvider = new ContentProvider(); |
| 17 | 62 | | contentProvider.baseUrl = BIWUrlUtils.GetUrlSceneObjectContent(); |
| 96 | 63 | | foreach (KeyValuePair<string, string> content in sceneObject.contents) |
| | 64 | | { |
| 31 | 65 | | ContentServerUtils.MappingPair mappingPair = new ContentServerUtils.MappingPair(); |
| 31 | 66 | | mappingPair.file = content.Key; |
| 31 | 67 | | mappingPair.hash = content.Value; |
| 31 | 68 | | contentProvider.contents.Add(mappingPair); |
| | 69 | | } |
| | 70 | |
|
| 17 | 71 | | contentProvider.BakeHashes(); |
| 17 | 72 | | return contentProvider; |
| | 73 | | } |
| | 74 | |
|
| | 75 | | public SceneObject GetSceneObjectById(string assetId) |
| | 76 | | { |
| 0 | 77 | | if (sceneObjectCatalogValue.TryGetValue(assetId, out SceneObject sceneObject)) |
| 0 | 78 | | return sceneObject; |
| 0 | 79 | | return null; |
| | 80 | | } |
| | 81 | |
|
| | 82 | | public void AddFullSceneObjectCatalog(string payload) |
| | 83 | | { |
| 32 | 84 | | JObject jObject = (JObject)JsonConvert.DeserializeObject(payload); |
| 32 | 85 | | if (jObject["ok"].ToObject<bool>()) |
| | 86 | | { |
| 32 | 87 | | JArray array = JArray.Parse(jObject["data"].ToString()); |
| | 88 | |
|
| 128 | 89 | | foreach (JObject item in array) |
| | 90 | | { |
| 32 | 91 | | i.AddSceneAssetPackToCatalog(item); |
| | 92 | | } |
| | 93 | | } |
| 32 | 94 | | } |
| | 95 | |
|
| | 96 | | public void ClearCatalog() |
| | 97 | | { |
| 44 | 98 | | sceneObjectCatalog.Clear(); |
| 44 | 99 | | sceneAssetPackCatalog.Clear(); |
| 44 | 100 | | } |
| | 101 | |
|
| | 102 | | public void AddSceneObjectToCatalog(SceneObject sceneObject) |
| | 103 | | { |
| 39 | 104 | | if (sceneObjectCatalog.ContainsKey(sceneObject.id)) |
| 0 | 105 | | return; |
| | 106 | |
|
| 39 | 107 | | sceneObjectCatalog.Add(sceneObject.id, sceneObject); |
| 39 | 108 | | OnItemAdded?.Invoke(sceneObject); |
| 39 | 109 | | } |
| | 110 | |
|
| | 111 | | public void RemoveSceneObjectToSceneCatalog(string sceneObjectId) |
| | 112 | | { |
| 0 | 113 | | if (!sceneObjectCatalog.ContainsKey(sceneObjectId)) |
| 0 | 114 | | return; |
| | 115 | |
|
| 0 | 116 | | sceneObjectCatalog.Remove(sceneObjectId); |
| 0 | 117 | | } |
| | 118 | |
|
| | 119 | | public void AddScenesObjectToSceneCatalog(string payload) |
| | 120 | | { |
| 0 | 121 | | AddScenesObjectToSceneCatalog(JsonConvert.DeserializeObject<SceneObject[]>(payload)); |
| 0 | 122 | | } |
| | 123 | |
|
| | 124 | | public void AddScenesObjectToSceneCatalog(SceneObject[] sceneObjects) |
| | 125 | | { |
| 4 | 126 | | foreach (var sceneObject in sceneObjects) |
| | 127 | | { |
| 1 | 128 | | AddSceneObjectToSceneCatalog(sceneObject); |
| | 129 | | } |
| 1 | 130 | | } |
| | 131 | |
|
| | 132 | | public void AddSceneObjectToSceneCatalog(SceneObject sceneObject) |
| | 133 | | { |
| 1 | 134 | | if (sceneObjectCatalog.ContainsKey(sceneObject.id)) |
| 0 | 135 | | return; |
| | 136 | |
|
| 1 | 137 | | sceneObjectCatalog.Add(sceneObject.id, sceneObject); |
| 1 | 138 | | OnSceneCatalogItemAdded?.Invoke(sceneObject); |
| 1 | 139 | | } |
| | 140 | |
|
| | 141 | | public void AddSceneAssetPackToCatalog(SceneAssetPack sceneAssetPack) |
| | 142 | | { |
| 33 | 143 | | if (sceneAssetPackCatalog.ContainsKey(sceneAssetPack.id)) |
| 1 | 144 | | return; |
| | 145 | |
|
| 142 | 146 | | foreach (SceneObject sceneObject in sceneAssetPack.assets) |
| | 147 | | { |
| 39 | 148 | | AddSceneObjectToCatalog(sceneObject); |
| | 149 | | } |
| | 150 | |
|
| 32 | 151 | | sceneAssetPackCatalog.Add(sceneAssetPack.id, sceneAssetPack); |
| 32 | 152 | | OnSceneAssetPackAdded?.Invoke(sceneAssetPack); |
| 32 | 153 | | } |
| | 154 | |
|
| | 155 | | public SceneAssetPack AddSceneAssetPackToCatalog(JObject payload) |
| | 156 | | { |
| 32 | 157 | | if (VERBOSE) |
| 0 | 158 | | Debug.Log("add sceneAssetPack: " + payload); |
| | 159 | |
|
| 32 | 160 | | SceneAssetPack sceneAssetPack = JsonConvert.DeserializeObject<SceneAssetPack>(payload.ToString()); |
| 32 | 161 | | AddSceneAssetPackToCatalog(sceneAssetPack); |
| 32 | 162 | | return sceneAssetPack; |
| | 163 | | } |
| | 164 | | } |