| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Text.RegularExpressions; |
| | 5 | | using DCL.Builder.Manifest; |
| | 6 | | using DCL.Components; |
| | 7 | | using DCL.Configuration; |
| | 8 | | using DCL.Controllers; |
| | 9 | | using DCL.Models; |
| | 10 | | using Newtonsoft.Json; |
| | 11 | | using Newtonsoft.Json.Linq; |
| | 12 | | using UnityEngine; |
| | 13 | |
|
| | 14 | | namespace DCL.Builder |
| | 15 | | { |
| | 16 | | public static class ManifestTranslator |
| | 17 | | { |
| 0 | 18 | | private static readonly Dictionary<string, int> idToHumanReadableDictionary = new Dictionary<string, int>() |
| | 19 | | { |
| | 20 | | { "Transform", (int) CLASS_ID_COMPONENT.TRANSFORM }, |
| | 21 | |
|
| | 22 | | { "GLTFShape", (int) CLASS_ID.GLTF_SHAPE }, |
| | 23 | |
|
| | 24 | | { "NFTShape", (int) CLASS_ID.NFT_SHAPE }, |
| | 25 | |
|
| | 26 | | { "Name", (int)CLASS_ID.NAME }, |
| | 27 | |
|
| | 28 | | { "LockedOnEdit", (int)CLASS_ID.LOCKED_ON_EDIT }, |
| | 29 | |
|
| | 30 | | { "VisibleOnEdit", (int)CLASS_ID.VISIBLE_ON_EDIT }, |
| | 31 | |
|
| | 32 | | { "Script", (int) CLASS_ID_COMPONENT.SMART_ITEM } |
| | 33 | | }; |
| | 34 | |
|
| | 35 | | public static WebBuilderScene StatelessToWebBuilderScene(StatelessManifest manifest, Vector2Int parcelSize) |
| | 36 | | { |
| 0 | 37 | | WebBuilderScene builderScene = new WebBuilderScene(); |
| 0 | 38 | | builderScene.id = Guid.NewGuid().ToString(); |
| | 39 | |
|
| 0 | 40 | | BuilderGround ground = new BuilderGround(); |
| 0 | 41 | | List<string> namesList = new List<string>(); |
| | 42 | | //We iterate all the entities to create its counterpart in the builder manifest |
| 0 | 43 | | foreach (Entity entity in manifest.entities) |
| | 44 | | { |
| 0 | 45 | | BuilderEntity builderEntity = new BuilderEntity(); |
| 0 | 46 | | builderEntity.id = entity.id; |
| 0 | 47 | | string entityName = builderEntity.id; |
| | 48 | |
|
| | 49 | | // Iterate the entity components to transform them to the builder format |
| 0 | 50 | | foreach (Component entityComponent in entity.components) |
| | 51 | | { |
| 0 | 52 | | BuilderComponent builderComponent = new BuilderComponent(); |
| 0 | 53 | | builderComponent.id = Guid.NewGuid().ToString(); |
| 0 | 54 | | builderComponent.type = entityComponent.type; |
| 0 | 55 | | builderComponent.data = entityComponent.value; |
| 0 | 56 | | builderEntity.components.Add(builderComponent.id); |
| | 57 | |
|
| 0 | 58 | | if (entityComponent.type == "NFTShape") |
| | 59 | | { |
| 0 | 60 | | NFTShape.Model model = JsonConvert.DeserializeObject<NFTShape.Model>(entityComponent.value.ToStr |
| 0 | 61 | | builderComponent.data = JsonConvert.SerializeObject(GetWebBuilderRepresentationOfNFT(model)); |
| | 62 | |
|
| | 63 | | //This is the name format that is used by builder, we will have a different name in unity due to |
| 0 | 64 | | entityName = "nft"; |
| | 65 | | } |
| | 66 | |
|
| 0 | 67 | | if (entityComponent.type == "GLTFShape") |
| | 68 | | { |
| 0 | 69 | | var gltfModel = JsonConvert.DeserializeObject<GLTFShape.Model>(builderComponent.data.ToString()) |
| | 70 | |
|
| | 71 | | //We get the associated asset to the GLFTShape and add it to the scene |
| 0 | 72 | | var asset = AssetCatalogBridge.i.sceneObjectCatalog.Get(gltfModel.assetId); |
| 0 | 73 | | if (!builderScene.assets.ContainsKey(asset.id)) |
| 0 | 74 | | builderScene.assets.Add(asset.id, asset); |
| | 75 | |
|
| | 76 | | //If the asset is a floor, we handle this situation for builder |
| 0 | 77 | | if (asset.category == BIWSettings.FLOOR_CATEGORY) |
| | 78 | | { |
| 0 | 79 | | ground.assetId = asset.id; |
| 0 | 80 | | ground.componentId = builderComponent.id; |
| 0 | 81 | | builderEntity.disableGizmos = true; |
| | 82 | | } |
| | 83 | |
|
| 0 | 84 | | entityName = asset.name; |
| | 85 | | } |
| | 86 | |
|
| 0 | 87 | | if (!builderScene.components.ContainsKey(builderComponent.id)) |
| 0 | 88 | | builderScene.components.Add(builderComponent.id, builderComponent); |
| | 89 | | } |
| | 90 | |
|
| | 91 | | // We need to give to each entity a unique name so we search for a unique name there |
| | 92 | | // Also, since the name of the entity will be used in the code, we need to ensure that the it doesn't ha |
| 0 | 93 | | builderEntity.name = GetCleanUniqueName(namesList, entityName); |
| | 94 | |
|
| 0 | 95 | | if (!builderScene.entities.ContainsKey(builderEntity.id)) |
| 0 | 96 | | builderScene.entities.Add(builderEntity.id, builderEntity); |
| | 97 | | } |
| | 98 | |
|
| | 99 | | //We add the limits to the scene, the current metrics are calculated in the builder |
| 0 | 100 | | builderScene.limits = BIWUtils.GetSceneMetricsLimits(parcelSize.x + parcelSize.y); |
| 0 | 101 | | builderScene.ground = ground; |
| | 102 | |
|
| 0 | 103 | | return builderScene; |
| | 104 | | } |
| | 105 | |
|
| | 106 | | public static StatelessManifest WebBuilderSceneToStatelessManifest(WebBuilderScene scene) |
| | 107 | | { |
| 0 | 108 | | StatelessManifest manifest = new StatelessManifest(); |
| 0 | 109 | | manifest.schemaVersion = 1; |
| | 110 | |
|
| 0 | 111 | | foreach (var entity in scene.entities.Values) |
| | 112 | | { |
| 0 | 113 | | Entity statlesEntity = new Entity(); |
| 0 | 114 | | statlesEntity.id = entity.id; |
| | 115 | |
|
| 0 | 116 | | foreach (string componentId in entity.components) |
| | 117 | | { |
| 0 | 118 | | foreach (BuilderComponent component in scene.components.Values) |
| | 119 | | { |
| 0 | 120 | | if(component.id != componentId) |
| | 121 | | continue; |
| | 122 | |
|
| 0 | 123 | | Component statelesComponent = new Component(); |
| 0 | 124 | | statelesComponent.type = component.type; |
| | 125 | |
|
| 0 | 126 | | if (statelesComponent.type == "NFTShape") |
| | 127 | | { |
| | 128 | | string url; |
| | 129 | | try |
| | 130 | | { |
| | 131 | | // Builder use a different way to load the NFT so we convert it to our system |
| 0 | 132 | | url = ((NFTShapeBuilderRepresentantion) component.data).url; |
| 0 | 133 | | } |
| 0 | 134 | | catch (Exception e) |
| | 135 | | { |
| | 136 | | // Builder handles the components differently if they come from another site, if we can' |
| 0 | 137 | | JObject jObject = JObject.Parse(component.data.ToString()); |
| 0 | 138 | | url = jObject["url"].ToString(); |
| 0 | 139 | | } |
| 0 | 140 | | string assedId = url.Replace(BIWSettings.NFT_ETHEREUM_PROTOCOL, ""); |
| 0 | 141 | | int index = assedId.IndexOf("/", StringComparison.Ordinal); |
| 0 | 142 | | string partToremove = assedId.Substring(index); |
| 0 | 143 | | assedId = assedId.Replace(partToremove, ""); |
| | 144 | |
|
| | 145 | | // We need to use this kind of representation because the color from unity is not serializab |
| 0 | 146 | | NFTShapeStatelessRepresentantion nftModel = new NFTShapeStatelessRepresentantion(); |
| 0 | 147 | | nftModel.color = new NFTShapeStatelessRepresentantion.ColorRepresentantion(0.6404918f, 0.611 |
| 0 | 148 | | nftModel.src = url; |
| 0 | 149 | | nftModel.assetId = assedId; |
| | 150 | |
|
| 0 | 151 | | statelesComponent.value = nftModel; |
| 0 | 152 | | } |
| | 153 | | else |
| | 154 | | { |
| 0 | 155 | | statelesComponent.value = component.data; |
| | 156 | | } |
| | 157 | |
|
| 0 | 158 | | statlesEntity.components.Add(statelesComponent); |
| | 159 | | } |
| | 160 | | } |
| | 161 | |
|
| 0 | 162 | | manifest.entities.Add(statlesEntity); |
| | 163 | | } |
| | 164 | |
|
| 0 | 165 | | return manifest; |
| | 166 | | } |
| | 167 | |
|
| | 168 | | public static StatelessManifest ParcelSceneToStatelessManifest(IParcelScene scene) |
| | 169 | | { |
| 0 | 170 | | StatelessManifest manifest = new StatelessManifest(); |
| 0 | 171 | | manifest.schemaVersion = 1; |
| | 172 | |
|
| 0 | 173 | | foreach (var entity in scene.entities.Values) |
| | 174 | | { |
| 0 | 175 | | Entity statlesEntity = new Entity(); |
| 0 | 176 | | statlesEntity.id = entity.entityId.ToString(); |
| | 177 | |
|
| 0 | 178 | | foreach (KeyValuePair<CLASS_ID_COMPONENT, IEntityComponent> entityComponent in scene.componentsManagerLe |
| | 179 | | { |
| 0 | 180 | | Component statelesComponent = new Component(); |
| 0 | 181 | | statelesComponent.type = idToHumanReadableDictionary.FirstOrDefault( x => x.Value == (int)entityComp |
| | 182 | |
|
| | 183 | | // Transform component is handle a bit different due to quaternion serializations |
| 0 | 184 | | if (entityComponent.Key == CLASS_ID_COMPONENT.TRANSFORM) |
| | 185 | | { |
| 0 | 186 | | ProtocolV2.TransformComponent entityTransformComponentModel = new ProtocolV2.TransformComponent( |
| 0 | 187 | | entityTransformComponentModel.position = WorldStateUtils.ConvertUnityToScenePosition(entity.game |
| 0 | 188 | | entityTransformComponentModel.rotation = new ProtocolV2.QuaternionRepresentation(entity.gameObje |
| 0 | 189 | | entityTransformComponentModel.scale = entity.gameObject.transform.lossyScale; |
| | 190 | |
|
| 0 | 191 | | statelesComponent.value = entityTransformComponentModel; |
| 0 | 192 | | } |
| | 193 | | else |
| | 194 | | { |
| 0 | 195 | | statelesComponent.value = entityComponent.Value.GetModel(); |
| | 196 | | } |
| | 197 | |
|
| 0 | 198 | | statlesEntity.components.Add(statelesComponent); |
| | 199 | | } |
| | 200 | |
|
| 0 | 201 | | foreach (KeyValuePair<Type, ISharedComponent> entitySharedComponent in scene.componentsManagerLegacy.Get |
| | 202 | | { |
| 0 | 203 | | Component statelesComponent = new Component(); |
| 0 | 204 | | statelesComponent.type = idToHumanReadableDictionary.FirstOrDefault( x => x.Value == (int)entityShar |
| 0 | 205 | | statelesComponent.value = entitySharedComponent.Value.GetModel(); |
| 0 | 206 | | statlesEntity.components.Add(statelesComponent); |
| | 207 | | } |
| | 208 | |
|
| 0 | 209 | | manifest.entities.Add(statlesEntity); |
| | 210 | | } |
| | 211 | |
|
| 0 | 212 | | return manifest; |
| | 213 | | } |
| | 214 | |
|
| | 215 | | public static WebBuilderScene ParcelSceneToWebBuilderScene(ParcelScene scene) |
| | 216 | | { |
| 0 | 217 | | WebBuilderScene builderScene = new WebBuilderScene(); |
| 0 | 218 | | builderScene.id = Guid.NewGuid().ToString(); |
| | 219 | |
|
| 0 | 220 | | BuilderGround ground = new BuilderGround(); |
| 0 | 221 | | List<string> namesList = new List<string>(); |
| | 222 | |
|
| | 223 | | //We iterate all the entities to create its counterpart in the builder manifest |
| 0 | 224 | | foreach (IDCLEntity entity in scene.entities.Values) |
| | 225 | | { |
| 0 | 226 | | BuilderEntity builderEntity = new BuilderEntity(); |
| 0 | 227 | | builderEntity.id = entity.entityId.ToString(); |
| 0 | 228 | | string componentType = ""; |
| 0 | 229 | | string entityName = ""; |
| | 230 | |
|
| | 231 | | // Iterate the entity components to transform them to the builder format |
| 0 | 232 | | foreach (KeyValuePair<CLASS_ID_COMPONENT, IEntityComponent> entityComponent in scene.componentsManagerLe |
| | 233 | | { |
| 0 | 234 | | BuilderComponent builderComponent = new BuilderComponent(); |
| 0 | 235 | | switch (entityComponent.Key) |
| | 236 | | { |
| | 237 | | case CLASS_ID_COMPONENT.TRANSFORM: |
| 0 | 238 | | componentType = "Transform"; |
| | 239 | |
|
| | 240 | | // We can't serialize the quaternions from Unity since newton serializes have recursive prob |
| 0 | 241 | | ProtocolV2.TransformComponent entityTransformComponentModel = new ProtocolV2.TransformCompon |
| 0 | 242 | | entityTransformComponentModel.position = WorldStateUtils.ConvertUnityToScenePosition(entity. |
| 0 | 243 | | entityTransformComponentModel.rotation = new ProtocolV2.QuaternionRepresentation(entity.game |
| 0 | 244 | | entityTransformComponentModel.scale = entity.gameObject.transform.lossyScale; |
| | 245 | |
|
| 0 | 246 | | builderComponent.data = entityTransformComponentModel; |
| | 247 | |
|
| 0 | 248 | | break; |
| | 249 | | case CLASS_ID_COMPONENT.SMART_ITEM: |
| 0 | 250 | | componentType = "Script"; |
| | 251 | | break; |
| | 252 | | } |
| | 253 | |
|
| | 254 | | // We generate a new uuid for the component since there is no uuid for components in the stateful sc |
| 0 | 255 | | builderComponent.id = Guid.NewGuid().ToString(); |
| 0 | 256 | | builderComponent.type = componentType; |
| | 257 | |
|
| | 258 | | // Since the transform model data is different from the others, we set it in the switch instead of h |
| 0 | 259 | | if (builderComponent.type != "Transform") |
| 0 | 260 | | builderComponent.data = entityComponent.Value.GetModel(); |
| | 261 | |
|
| 0 | 262 | | builderEntity.components.Add(builderComponent.id); |
| | 263 | |
|
| 0 | 264 | | if (!builderScene.components.ContainsKey(builderComponent.id)) |
| 0 | 265 | | builderScene.components.Add(builderComponent.id, builderComponent); |
| | 266 | | } |
| | 267 | |
|
| | 268 | | // Iterate the entity shared components to transform them to the builder format |
| 0 | 269 | | foreach (KeyValuePair<System.Type, ISharedComponent> sharedEntityComponent in scene.componentsManagerLeg |
| | 270 | | { |
| 0 | 271 | | BuilderComponent builderComponent = new BuilderComponent(); |
| | 272 | | // We generate a new uuid for the component since there is no uuid for components in the stateful sc |
| 0 | 273 | | builderComponent.id = Guid.NewGuid().ToString(); |
| 0 | 274 | | builderComponent.data = sharedEntityComponent.Value.GetModel(); |
| | 275 | |
|
| 0 | 276 | | if (sharedEntityComponent.Value is GLTFShape) |
| | 277 | | { |
| 0 | 278 | | componentType = "GLTFShape"; |
| | 279 | |
|
| 0 | 280 | | var gltfModel = (GLTFShape.Model) builderComponent.data; |
| | 281 | |
|
| | 282 | | //We get the associated asset to the GLFTShape and add it to the scene |
| 0 | 283 | | var asset = AssetCatalogBridge.i.sceneObjectCatalog.Get(gltfModel.assetId); |
| 0 | 284 | | if (!builderScene.assets.ContainsKey(asset.id)) |
| 0 | 285 | | builderScene.assets.Add(asset.id, asset); |
| | 286 | |
|
| | 287 | | // This is a special case. The builder needs the ground separated from the rest of the component |
| | 288 | | // Since all the grounds have the same asset, we assign it and disable the gizmos in the builder |
| 0 | 289 | | if (asset.category == BIWSettings.FLOOR_CATEGORY) |
| | 290 | | { |
| 0 | 291 | | ground.assetId = asset.id; |
| 0 | 292 | | ground.componentId = builderComponent.id; |
| 0 | 293 | | builderEntity.disableGizmos = true; |
| | 294 | | } |
| | 295 | |
|
| 0 | 296 | | entityName = asset.name; |
| 0 | 297 | | } |
| 0 | 298 | | else if (sharedEntityComponent.Value is NFTShape) |
| | 299 | | { |
| 0 | 300 | | componentType = "NFTShape"; |
| | 301 | |
|
| | 302 | | // This is a special case where we are assigning the builder url field for NFTs because builder |
| 0 | 303 | | NFTShape.Model model = (NFTShape.Model) builderComponent.data; |
| 0 | 304 | | builderComponent.data = GetWebBuilderRepresentationOfNFT(model); |
| | 305 | |
|
| | 306 | | //This is the name format that is used by builder, we will have a different name in unity due to |
| 0 | 307 | | entityName = "nft"; |
| 0 | 308 | | } |
| 0 | 309 | | else if (sharedEntityComponent.Key == typeof(DCLName)) |
| | 310 | | { |
| 0 | 311 | | componentType = "Name"; |
| 0 | 312 | | entityName = ((DCLName.Model) sharedEntityComponent.Value.GetModel()).value; |
| 0 | 313 | | } |
| 0 | 314 | | else if (sharedEntityComponent.Key == typeof(DCLLockedOnEdit)) |
| | 315 | | { |
| 0 | 316 | | componentType = "LockedOnEdit"; |
| | 317 | | } |
| | 318 | |
|
| 0 | 319 | | builderComponent.type = componentType; |
| | 320 | |
|
| 0 | 321 | | builderEntity.components.Add(builderComponent.id); |
| 0 | 322 | | if (!builderScene.components.ContainsKey(builderComponent.id)) |
| 0 | 323 | | builderScene.components.Add(builderComponent.id, builderComponent); |
| | 324 | | } |
| | 325 | |
|
| | 326 | | // We need to give to each entity a unique name so we search for a unique name there |
| | 327 | | // Also, since the name of the entity will be used in the code, we need to ensure that the it doesn't ha |
| 0 | 328 | | builderEntity.name = GetCleanUniqueName(namesList, entityName); |
| | 329 | |
|
| 0 | 330 | | if (!builderScene.entities.ContainsKey(builderEntity.id)) |
| 0 | 331 | | builderScene.entities.Add(builderEntity.id, builderEntity); |
| | 332 | | } |
| | 333 | |
|
| | 334 | | //We add the limits to the scene, the current metrics are calculated in the builder |
| 0 | 335 | | builderScene.limits = BIWUtils.GetSceneMetricsLimits(scene.parcels.Count); |
| 0 | 336 | | builderScene.metrics = new SceneMetricsModel(); |
| 0 | 337 | | builderScene.ground = ground; |
| | 338 | |
|
| 0 | 339 | | return builderScene; |
| | 340 | | } |
| | 341 | |
|
| | 342 | | private static NFTShapeBuilderRepresentantion GetWebBuilderRepresentationOfNFT(NFTShape.Model model) |
| | 343 | | { |
| 0 | 344 | | NFTShapeBuilderRepresentantion representantion = new NFTShapeBuilderRepresentantion(); |
| 0 | 345 | | representantion.url = model.src; |
| 0 | 346 | | return representantion; |
| | 347 | | } |
| | 348 | |
|
| | 349 | | private static string GetCleanUniqueName(List<string> namesList, string currentName) |
| | 350 | | { |
| | 351 | | //We clean the name to don't include special characters |
| 0 | 352 | | Regex r = new Regex("(?:[^a-z]|(?<=['\"])s)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | Rege |
| | 353 | |
|
| 0 | 354 | | string newName = r.Replace(currentName, String.Empty); |
| 0 | 355 | | newName = newName.ToLower(); |
| | 356 | |
|
| | 357 | | //We get a unique name |
| 0 | 358 | | bool uniqueName = false; |
| 0 | 359 | | int cont = 2; |
| 0 | 360 | | while (!uniqueName) |
| | 361 | | { |
| 0 | 362 | | if (!namesList.Contains(newName)) |
| 0 | 363 | | uniqueName = true; |
| | 364 | | else |
| 0 | 365 | | newName = newName + cont; |
| | 366 | |
|
| 0 | 367 | | cont++; |
| | 368 | | } |
| | 369 | |
|
| 0 | 370 | | namesList.Add(newName); |
| 0 | 371 | | return newName; |
| | 372 | | } |
| | 373 | |
|
| | 374 | | public static IParcelScene ManifestToParcelSceneWithOnlyData(Manifest.Manifest manifest) |
| | 375 | | { |
| 0 | 376 | | GameObject parcelGameObject = new GameObject("Builder Scene SceneId: " + manifest.scene.id); |
| 0 | 377 | | ParcelScene scene = parcelGameObject.AddComponent<ParcelScene>(); |
| | 378 | |
|
| | 379 | | //We remove the old assets to they don't collide with the new ones |
| 0 | 380 | | BIWUtils.RemoveAssetsFromCurrentScene(); |
| | 381 | |
|
| | 382 | | //The data is built so we set the data of the scene |
| 0 | 383 | | scene.SetData(CreateSceneDataFromScene(manifest)); |
| | 384 | |
|
| 0 | 385 | | return scene; |
| | 386 | | } |
| | 387 | |
|
| | 388 | | private static LoadParcelScenesMessage.UnityParcelScene CreateSceneDataFromScene(Manifest.Manifest manifest) |
| | 389 | | { |
| | 390 | | //We add the assets from the scene to the catalog |
| 0 | 391 | | var assets = manifest.scene.assets.Values.ToArray(); |
| 0 | 392 | | AssetCatalogBridge.i.AddScenesObjectToSceneCatalog(assets); |
| | 393 | |
|
| | 394 | | //We create and assign the data of the scene |
| 0 | 395 | | LoadParcelScenesMessage.UnityParcelScene parcelData = new LoadParcelScenesMessage.UnityParcelScene(); |
| 0 | 396 | | parcelData.id = manifest.scene.id; |
| | 397 | |
|
| | 398 | | //We set the current scene in the 0,0 |
| 0 | 399 | | int x = 0; |
| 0 | 400 | | int y = 0; |
| | 401 | |
|
| 0 | 402 | | parcelData.basePosition = new Vector2Int(x, y); |
| | 403 | |
|
| | 404 | | //We set the parcels as the first one is in the base position, the first one will be in the bottom-left corn |
| 0 | 405 | | parcelData.parcels = new Vector2Int[manifest.project.rows * manifest.project.cols]; |
| | 406 | |
|
| | 407 | | //We assign the parcels position |
| 0 | 408 | | for (int index = 0; index < parcelData.parcels.Length; index++) |
| | 409 | | { |
| 0 | 410 | | parcelData.parcels[index] = new Vector2Int(x, y); |
| 0 | 411 | | x++; |
| 0 | 412 | | if (x == manifest.project.rows) |
| | 413 | | { |
| 0 | 414 | | y++; |
| 0 | 415 | | x = 0; |
| | 416 | | } |
| | 417 | | } |
| | 418 | |
|
| | 419 | | //We prepare the mappings to the scenes |
| 0 | 420 | | Dictionary<string, string> contentDictionary = new Dictionary<string, string>(); |
| | 421 | |
|
| 0 | 422 | | foreach (var sceneObject in assets) |
| | 423 | | { |
| 0 | 424 | | foreach (var content in sceneObject.contents) |
| | 425 | | { |
| 0 | 426 | | if (!contentDictionary.ContainsKey(content.Key)) |
| 0 | 427 | | contentDictionary.Add(content.Key, content.Value); |
| | 428 | | } |
| | 429 | | } |
| | 430 | |
|
| | 431 | | //We add the mappings to the scene |
| 0 | 432 | | BIWUtils.AddSceneMappings(contentDictionary, BIWUrlUtils.GetUrlSceneObjectContent(), parcelData); |
| | 433 | |
|
| 0 | 434 | | return parcelData; |
| | 435 | | } |
| | 436 | |
|
| | 437 | | public static IParcelScene ManifestToParcelScene(Manifest.Manifest manifest) |
| | 438 | | { |
| 0 | 439 | | GameObject parcelGameObject = new GameObject("Builder Scene SceneId: " + manifest.scene.id); |
| 0 | 440 | | ParcelScene scene = parcelGameObject.AddComponent<ParcelScene>(); |
| | 441 | |
|
| | 442 | | //We remove the old assets to they don't collide with the new ones |
| 0 | 443 | | BIWUtils.RemoveAssetsFromCurrentScene(); |
| | 444 | |
|
| | 445 | | //The data is built so we set the data of the scene |
| 0 | 446 | | scene.SetData(CreateSceneDataFromScene(manifest)); |
| | 447 | |
|
| | 448 | | // We iterate all the entities to create the entity in the scene |
| 0 | 449 | | foreach (BuilderEntity builderEntity in manifest.scene.entities.Values) |
| | 450 | | { |
| 0 | 451 | | var entity = scene.CreateEntity(builderEntity.id.GetHashCode()); |
| | 452 | |
|
| 0 | 453 | | bool nameComponentFound = false; |
| | 454 | | // We iterate all the id of components in the entity, to add the component |
| 0 | 455 | | foreach (string idComponent in builderEntity.components) |
| | 456 | | { |
| | 457 | | //This shouldn't happen, the component should be always in the scene, but just in case |
| 0 | 458 | | if (!manifest.scene.components.ContainsKey(idComponent)) |
| | 459 | | continue; |
| | 460 | |
|
| | 461 | | // We get the component from the scene and create it in the entity |
| 0 | 462 | | BuilderComponent component = manifest.scene.components[idComponent]; |
| | 463 | |
|
| 0 | 464 | | switch (component.type) |
| | 465 | | { |
| | 466 | | case "Transform": |
| 0 | 467 | | DCLTransform.Model model = JsonConvert.DeserializeObject<DCLTransform.Model>(component.data. |
| 0 | 468 | | EntityComponentsUtils.AddTransformComponent(scene, entity, model); |
| 0 | 469 | | break; |
| | 470 | |
|
| | 471 | | case "GLTFShape": |
| 0 | 472 | | LoadableShape.Model gltfModel = JsonConvert.DeserializeObject<LoadableShape.Model>(component |
| 0 | 473 | | EntityComponentsUtils.AddGLTFComponent(scene, entity, gltfModel, component.id); |
| 0 | 474 | | break; |
| | 475 | |
|
| | 476 | | case "NFTShape": |
| | 477 | | //Builder use a different way to load the NFT so we convert it to our system |
| 0 | 478 | | string url = JsonConvert.DeserializeObject<string>(component.data.ToString()); |
| 0 | 479 | | string assedId = url.Replace(BIWSettings.NFT_ETHEREUM_PROTOCOL, ""); |
| 0 | 480 | | int index = assedId.IndexOf("/", StringComparison.Ordinal); |
| 0 | 481 | | string partToremove = assedId.Substring(index); |
| 0 | 482 | | assedId = assedId.Replace(partToremove, ""); |
| | 483 | |
|
| 0 | 484 | | NFTShape.Model nftModel = new NFTShape.Model(); |
| 0 | 485 | | nftModel.color = new Color(0.6404918f, 0.611472f, 0.8584906f); |
| 0 | 486 | | nftModel.src = url; |
| 0 | 487 | | nftModel.assetId = assedId; |
| | 488 | |
|
| 0 | 489 | | EntityComponentsUtils.AddNFTShapeComponent(scene, entity, nftModel, component.id); |
| 0 | 490 | | break; |
| | 491 | |
|
| | 492 | | case "Name": |
| 0 | 493 | | nameComponentFound = true; |
| 0 | 494 | | DCLName.Model nameModel = JsonConvert.DeserializeObject<DCLName.Model>(component.data.ToStri |
| 0 | 495 | | nameModel.builderValue = builderEntity.name; |
| 0 | 496 | | EntityComponentsUtils.AddNameComponent(scene , entity, nameModel, Guid.NewGuid().ToString()) |
| 0 | 497 | | break; |
| | 498 | |
|
| | 499 | | case "LockedOnEdit": |
| 0 | 500 | | DCLLockedOnEdit.Model lockedModel = JsonConvert.DeserializeObject<DCLLockedOnEdit.Model>(com |
| 0 | 501 | | EntityComponentsUtils.AddLockedOnEditComponent(scene , entity, lockedModel, Guid.NewGuid().T |
| | 502 | | break; |
| | 503 | | } |
| | 504 | | } |
| | 505 | |
|
| | 506 | | // We need to mantain the builder name of the entity, so we create the equivalent part in biw. We do thi |
| 0 | 507 | | if (!nameComponentFound) |
| | 508 | | { |
| 0 | 509 | | DCLName.Model nameModel = new DCLName.Model(); |
| 0 | 510 | | nameModel.value = builderEntity.name; |
| 0 | 511 | | nameModel.builderValue = builderEntity.name; |
| 0 | 512 | | EntityComponentsUtils.AddNameComponent(scene , entity, nameModel, Guid.NewGuid().ToString()); |
| | 513 | | } |
| | 514 | | } |
| | 515 | |
|
| | 516 | | //We already have made all the necessary steps to configure the parcel, so we init the scene |
| 0 | 517 | | scene.sceneLifecycleHandler.SetInitMessagesDone(); |
| | 518 | |
|
| 0 | 519 | | return scene; |
| | 520 | | } |
| | 521 | |
|
| | 522 | | } |
| | 523 | | } |