| 124 | } |
| 125 | |
| 126 | SceneObject* SceneObjectsFactory::Spawn(Context& context, const ISerializable::DeserializeStream& stream) |
| 127 | { |
| 128 | // Get object id |
| 129 | Guid id = JsonTools::GetGuid(stream, "ID"); |
| 130 | ISerializeModifier* modifier = context.GetModifier(); |
| 131 | modifier->IdsMapping.TryGet(id, id); |
| 132 | if (!id.IsValid()) |
| 133 | { |
| 134 | LOG(Warning, "Invalid object id."); |
| 135 | return nullptr; |
| 136 | } |
| 137 | SceneObject* obj = nullptr; |
| 138 | |
| 139 | // Check for prefab instance |
| 140 | Guid prefabObjectId; |
| 141 | if (JsonTools::GetGuidIfValid(prefabObjectId, stream, "PrefabObjectID")) |
| 142 | { |
| 143 | // Get prefab asset id |
| 144 | const Guid prefabId = JsonTools::GetGuid(stream, "PrefabID"); |
| 145 | if (!prefabId.IsValid()) |
| 146 | { |
| 147 | LOG(Warning, "Invalid prefab id."); |
| 148 | return nullptr; |
| 149 | } |
| 150 | |
| 151 | // Load prefab |
| 152 | auto prefab = Content::LoadAsync<Prefab>(prefabId); |
| 153 | if (prefab == nullptr) |
| 154 | { |
| 155 | LOG(Warning, "Missing prefab {0}.", prefabId); |
| 156 | return nullptr; |
| 157 | } |
| 158 | if (prefab->WaitForLoaded()) |
| 159 | { |
| 160 | LOG(Warning, "Failed to load prefab {0}.", prefab->ToString()); |
| 161 | return nullptr; |
| 162 | } |
| 163 | |
| 164 | // Get prefab object data from the prefab |
| 165 | const ISerializable::DeserializeStream* prefabData; |
| 166 | if (!prefab->ObjectsDataCache.TryGet(prefabObjectId, prefabData)) |
| 167 | { |
| 168 | LOG(Warning, "Missing object {1} data in prefab {0}.", prefab->ToString(), prefabObjectId); |
| 169 | return nullptr; |
| 170 | } |
| 171 | |
| 172 | // Map prefab object ID to the deserialized instance ID |
| 173 | modifier->IdsMapping[prefabObjectId] = id; |
| 174 | |
| 175 | // Create prefab instance (recursive prefab loading to support nested prefabs) |
| 176 | obj = Spawn(context, *prefabData); |
| 177 | } |
| 178 | else |
| 179 | { |
| 180 | const auto typeNameMember = stream.FindMember("TypeName"); |
| 181 | if (typeNameMember != stream.MemberEnd()) |
| 182 | { |
| 183 | if (!typeNameMember->value.IsString()) |
no test coverage detected