예제 #1
0
        private void LoadObjects(
            AssetLoadContext loadContext,
            HeightMap heightMap,
            MapObject[] mapObjects,
            NamedCameras namedCameras,
            out WaypointCollection waypointCollection,
            out RoadCollection roads,
            out Bridge[] bridges,
            out CameraCollection cameras)
        {
            var waypoints = new List <Waypoint>();

            var bridgesList = new List <Bridge>();

            var roadTopology = new RoadTopology();

            for (var i = 0; i < mapObjects.Length; i++)
            {
                var mapObject = mapObjects[i];

                switch (mapObject.RoadType & RoadType.PrimaryType)
                {
                case RoadType.None:
                    switch (mapObject.TypeName)
                    {
                    case Waypoint.ObjectTypeName:
                        waypoints.Add(new Waypoint(mapObject));
                        break;

                    default:
                        GameObject.FromMapObject(mapObject, loadContext.AssetStore, GameObjects, heightMap, overwriteAngle: null, teamFactory: TeamFactory);
                        break;
                    }
                    break;

                case RoadType.BridgeStart:
                case RoadType.BridgeEnd:
                    // Multiple invalid bridges can be found in e.g GLA01.
                    if ((i + 1) >= mapObjects.Length || !mapObjects[i + 1].RoadType.HasFlag(RoadType.BridgeEnd))
                    {
                        Logger.Warn($"Invalid bridge: {mapObject.ToString()}, skipping...");
                        continue;
                    }

                    var bridgeEnd = mapObjects[++i];

                    bridgesList.Add(AddDisposable(new Bridge(
                                                      loadContext,
                                                      heightMap,
                                                      mapObject,
                                                      mapObject.Position,
                                                      bridgeEnd.Position,
                                                      GameObjects)));

                    break;

                case RoadType.Start:
                case RoadType.End:
                    var roadEnd = mapObjects[++i];

                    // Some maps have roads with invalid start- or endpoints.
                    // We'll skip processing them altogether.
                    if (mapObject.TypeName == "" || roadEnd.TypeName == "")
                    {
                        Logger.Warn($"Road {mapObject} has invalid start- or endpoint, skipping...");
                        continue;
                    }

                    if (!mapObject.RoadType.HasFlag(RoadType.Start) || !roadEnd.RoadType.HasFlag(RoadType.End))
                    {
                        throw new InvalidDataException();
                    }

                    // Note that we're searching with the type of either end.
                    // This is because of weirdly corrupted roads with unmatched ends in USA04, which work fine in WB and SAGE.
                    var roadTemplate =
                        loadContext.AssetStore.RoadTemplates.GetByName(mapObject.TypeName)
                        ?? loadContext.AssetStore.RoadTemplates.GetByName(roadEnd.TypeName);

                    if (roadTemplate == null)
                    {
                        throw new InvalidDataException($"Missing road template: {mapObject.TypeName}");
                    }

                    roadTopology.AddSegment(roadTemplate, mapObject, roadEnd);
                    break;
                }

                loadContext.GraphicsDevice.WaitForIdle();
            }

            GameObjects.InsertCreated();
            cameras            = new CameraCollection(namedCameras?.Cameras);
            roads              = AddDisposable(new RoadCollection(roadTopology, loadContext, heightMap, Terrain.RadiusCursorDecalsResourceSet));
            waypointCollection = new WaypointCollection(waypoints, MapFile.WaypointsList.WaypointPaths);
            bridges            = bridgesList.ToArray();
        }