public async Task SetCarAsync(CarDescription car, string skinId = Kn5RenderableCar.DefaultSkin, CancellationToken cancellationToken = default(CancellationToken)) { ClearBeforeChangingCar(); GC.Collect(); GC.WaitForPendingFinalizers(); try { _loadingCar = car; if (_carWrapper == null) { _car = car; return; } if (car == null) { ClearExisting(); CarNode = null; _carBoundingBox = null; _car = null; Scene.UpdateBoundingBox(); return; } Kn5RenderableCar loaded = null; var previous = _previousCars.FirstOrDefault(x => x.Id == car.MainKn5File); if (previous != null) { _previousCars.Remove(previous); ClearExisting(); _carWrapper.AddRange(previous.Objects); _car = car; loaded = previous.Objects.OfType <Kn5RenderableCar>().First(); CopyValues(loaded, CarNode); CarNode = loaded; _carBoundingBox = null; if (_selectSkinLater) { CarNode.SelectSkin(DeviceContextHolder, _selectSkin); _selectSkinLater = false; } else { CarNode.SelectSkin(DeviceContextHolder, skinId); } Scene.UpdateBoundingBox(); return; } await car.LoadAsync(); if (cancellationToken.IsCancellationRequested) { return; } await Task.Run(() => { loaded = new Kn5RenderableCar(car, Matrix.Identity, _selectSkinLater ? _selectSkin : skinId, asyncTexturesLoading: AsyncTexturesLoading, asyncOverrideTexturesLoading: AsyncOverridesLoading, allowSkinnedObjects: AllowSkinnedObjects); _selectSkinLater = false; if (cancellationToken.IsCancellationRequested) { return; } CopyValues(loaded, CarNode); if (cancellationToken.IsCancellationRequested) { return; } loaded.Draw(DeviceContextHolder, null, SpecialRenderMode.InitializeOnly); }); if (cancellationToken.IsCancellationRequested || _loadingCar != car) { loaded?.Dispose(); return; } ClearExisting(); _carWrapper.Add(loaded); ExtendCar(loaded, _carWrapper); _car = car; _selectSkin = null; CarNode = loaded; _carBoundingBox = null; IsDirty = true; Scene.UpdateBoundingBox(); } catch (Exception e) { MessageBox.Show(e.ToString()); throw; } finally { if (ReferenceEquals(_loadingCar, car)) { _loadingCar = null; } } }
public async Task SetCarAsync(CarObject car, string skinId = Kn5RenderableCar.DefaultSkin, CancellationToken cancellationToken = default(CancellationToken)) { LicensePlateSelected = false; try { _loadingCar = car; if (_carWrapper == null) { _car = car; return; } if (car == null) { ClearExisting(); _car = null; CarNode = null; Scene.UpdateBoundingBox(); return; } Kn5RenderableCar loaded = null; FlatMirror reflection = null; var previous = _previousCars.FirstOrDefault(x => x.Id == car.Id); if (previous != null) { _previousCars.Remove(previous); ClearExisting(); _carWrapper.AddRange(previous.Objects); _car = car; loaded = previous.Objects.OfType <Kn5RenderableCar>().First(); loaded.LightsEnabled = CarNode?.LightsEnabled == true; CarNode = loaded; if (_selectSkinLater) { CarNode.SelectSkin(DeviceContextHolder, _selectSkin); _selectSkinLater = false; } else { CarNode.SelectSkin(DeviceContextHolder, skinId); } Scene.UpdateBoundingBox(); return; } await Task.Run(() => { var carKn5 = Kn5.FromFile(FileUtils.GetMainCarFilename(car.Location)); if (cancellationToken.IsCancellationRequested) { return; } loaded = new Kn5RenderableCar(carKn5, car.Location, Matrix.Identity, _selectSkinLater ? _selectSkin : skinId); _selectSkinLater = false; if (cancellationToken.IsCancellationRequested) { return; } loaded.LightsEnabled = CarNode?.LightsEnabled == true; if (cancellationToken.IsCancellationRequested) { return; } loaded.Draw(DeviceContextHolder, null, SpecialRenderMode.InitializeOnly); if (cancellationToken.IsCancellationRequested) { return; } reflection = new FlatMirror(loaded, new Plane(Vector3.Zero, Vector3.UnitY)); reflection.Draw(DeviceContextHolder, null, SpecialRenderMode.InitializeOnly); }); if (cancellationToken.IsCancellationRequested || _loadingCar != car) { loaded?.Dispose(); return; } ClearExisting(); _carWrapper.Add(loaded); _carWrapper.Add(reflection); _car = car; CarNode = loaded; _selectSkin = null; Scene.UpdateBoundingBox(); } catch (Exception e) { MessageBox.Show(e.ToString()); throw; } finally { if (ReferenceEquals(_loadingCar, car)) { _loadingCar = null; } } }