public override void InitializeAsync() { var worker = new BackgroundWorker(); worker.DoWork += delegate(object s, DoWorkEventArgs workArgs) { lock (Locker) { // Because I've bound models to the view, this is going to get messy. var contentPath = ToolboxUpdater.GetApplicationContentPath(); if (IsConstructionNotReady) { var ingotRequirements = new Dictionary<string, BlueprintRequirement>(); var oreRequirements = new Dictionary<string, BlueprintRequirement>(); var timeTaken = new TimeSpan(); var cubeAssetDict = new Dictionary<string, CubeAssetModel>(); var componentAssetDict = new Dictionary<string, CubeAssetModel>(); var cubeAssets = new List<CubeAssetModel>(); var componentAssets = new List<CubeAssetModel>(); var ingotAssets = new List<OreAssetModel>(); var oreAssets = new List<OreAssetModel>(); foreach (var block in CubeGrid.CubeBlocks) { var blockName = block.SubtypeName; if (string.IsNullOrEmpty(blockName)) { blockName = block.TypeId.ToString(); } var cubeBlockDefinition = SpaceEngineersApi.GetCubeDefinition(block.TypeId, CubeGrid.GridSizeEnum, block.SubtypeName); float cubeMass = 0; TimeSpan blockTime = TimeSpan.Zero; string blockTexture = null; if (cubeBlockDefinition != null) { foreach (var component in cubeBlockDefinition.Components) { TimeSpan componentTime; SpaceEngineersApi.AccumulateCubeBlueprintRequirements(component.Subtype, component.Type, component.Count, ingotRequirements, out componentTime); timeTaken += componentTime; var cd = (MyObjectBuilder_ComponentDefinition)SpaceEngineersApi.GetDefinition(component.Type, component.Subtype); float componentMass = cd.Mass * component.Count; float componentVolume = cd.Volume.Value * component.Count; cubeMass += componentMass; var componentName = component.Subtype; if (componentAssetDict.ContainsKey(componentName)) { componentAssetDict[componentName].Count += component.Count; componentAssetDict[componentName].Mass += componentMass; componentAssetDict[componentName].Volume += componentVolume; componentAssetDict[componentName].Time += componentTime; } else { var componentTexture = SpaceEngineersCore.GetDataPathOrDefault(cd.Icon, Path.Combine(contentPath, cd.Icon)); var m = new CubeAssetModel() { Name = cd.DisplayName, Mass = componentMass, Volume = componentVolume, Count = component.Count, Time = componentTime, TextureFile = componentTexture }; componentAssets.Add(m); componentAssetDict.Add(componentName, m); } } blockTime = new TimeSpan((long)(TimeSpan.TicksPerSecond * cubeBlockDefinition.BuildTimeSeconds)); blockTexture = SpaceEngineersCore.GetDataPathOrDefault(cubeBlockDefinition.Icon, Path.Combine(contentPath, cubeBlockDefinition.Icon)); } timeTaken += blockTime; if (cubeAssetDict.ContainsKey(blockName)) { cubeAssetDict[blockName].Count++; cubeAssetDict[blockName].Mass += cubeMass; cubeAssetDict[blockName].Time += blockTime; } else { var m = new CubeAssetModel() { Name = cubeBlockDefinition == null ? blockName : cubeBlockDefinition.DisplayName, Mass = cubeMass, Count = 1, TextureFile = blockTexture, Time = blockTime }; cubeAssets.Add(m); cubeAssetDict.Add(blockName, m); } } foreach (var kvp in ingotRequirements) { TimeSpan ingotTime; SpaceEngineersApi.AccumulateCubeBlueprintRequirements(kvp.Value.SubtypeId, kvp.Value.Id.TypeId, kvp.Value.Amount, oreRequirements, out ingotTime); var cd = (MyObjectBuilder_PhysicalItemDefinition)SpaceEngineersApi.GetDefinition(kvp.Value.Id.TypeId, kvp.Value.SubtypeId); var componentTexture = SpaceEngineersCore.GetDataPathOrDefault(cd.Icon, Path.Combine(contentPath, cd.Icon)); var volume = cd.Volume.HasValue ? (double)kvp.Value.Amount * cd.Volume.Value : 0d; var ingotAsset = new OreAssetModel() { Name = cd.DisplayName, Amount = kvp.Value.Amount, Mass = (double)kvp.Value.Amount * cd.Mass, Volume = volume, Time = ingotTime, TextureFile = componentTexture }; ingotAssets.Add(ingotAsset); timeTaken += ingotTime; } foreach (var kvp in oreRequirements) { var cd = (MyObjectBuilder_PhysicalItemDefinition)SpaceEngineersApi.GetDefinition(kvp.Value.Id.TypeId, kvp.Value.SubtypeId); if (cd != null) { var componentTexture = SpaceEngineersCore.GetDataPathOrDefault(cd.Icon, Path.Combine(contentPath, cd.Icon)); var volume = cd.Volume.HasValue ? (double)kvp.Value.Amount * cd.Volume.Value : 0d; var oreAsset = new OreAssetModel() { Name = cd.DisplayName, Amount = kvp.Value.Amount, Mass = (double)kvp.Value.Amount * cd.Mass, Volume = volume, TextureFile = componentTexture }; oreAssets.Add(oreAsset); } } _dispatcher.Invoke(DispatcherPriority.Input, (Action)delegate { CubeAssets = cubeAssets; ComponentAssets = componentAssets; IngotAssets = ingotAssets; OreAssets = oreAssets; TimeToProduce = timeTaken; }); IsConstructionNotReady = false; } if (IsSubsSystemNotReady) { var cubeList = new List<CubeItemModel>(); foreach (var block in CubeGrid.CubeBlocks) { var cubeDefinition = SpaceEngineersApi.GetCubeDefinition(block.TypeId, CubeGrid.GridSizeEnum, block.SubtypeName); _dispatcher.Invoke(DispatcherPriority.Input, (Action)delegate { cubeList.Add(new CubeItemModel(block, cubeDefinition) { TextureFile = cubeDefinition == null ? null : SpaceEngineersCore.GetDataPathOrDefault(cubeDefinition.Icon, Path.Combine(contentPath, cubeDefinition.Icon)) }); }); } _dispatcher.Invoke(DispatcherPriority.Input, (Action)delegate { CubeList = new ObservableCollection<CubeItemModel>(cubeList); }); IsSubsSystemNotReady = false; } } }; worker.RunWorkerAsync(); }