diff --git a/Telemachus/src/DataLinkFormatters.cs b/Telemachus/src/DataLinkFormatters.cs index 7f8508c..2c256bd 100644 --- a/Telemachus/src/DataLinkFormatters.cs +++ b/Telemachus/src/DataLinkFormatters.cs @@ -163,7 +163,7 @@ public class ActiveResourceListJSONFormatter : ResourceListJSONFormatter { public override object prepareForSerialization(object input) { - return SumResources((List)input, + return SumResources((List)input, x => x.amount); } } @@ -172,7 +172,7 @@ public class ActiveResourceTotalListJSONFormatter : ResourceListJSONFormatter { public override object prepareForSerialization(object input) { - return SumResources((List)input, + return SumResources((List)input, x => x.maxAmount); } } diff --git a/Telemachus/src/DataLinkHandlers.cs b/Telemachus/src/DataLinkHandlers.cs index 697d8ed..6c65030 100644 --- a/Telemachus/src/DataLinkHandlers.cs +++ b/Telemachus/src/DataLinkHandlers.cs @@ -1968,6 +1968,19 @@ public ResourceDataLinkHandler(VesselChangeDetector vesselChangeDetector, Format dataSources => { return getsResourceValues(dataSources); }, "r.resourceMax", "Max Resource Information [string resource type]", formatters.MaxResourceList, APIEntry.UnitType.UNITLESS)); + registerAPI(new APIEntry( + dataSources => { + List names = new List(); + PartResourceDefinitionList resourceDefinitionList = PartResourceLibrary.Instance.resourceDefinitions; + foreach (PartResourceDefinition resourceDefinition in resourceDefinitionList) + { + names.Add(resourceDefinition.name); + } + + return names; + }, + "r.resourceNameList", "List of resource names", + formatters.StringArray, APIEntry.UnitType.UNITLESS)); } #endregion @@ -1980,7 +1993,7 @@ protected List getsResourceValues(DataSources datasources) return resourceCache.get(datasources); } - protected List getsActiveResourceValues(DataSources datasources) + protected List getsActiveResourceValues(DataSources datasources) { activeResourceCache.vessel = datasources.vessel; return activeResourceCache.get(datasources); @@ -2026,7 +2039,7 @@ public Vessel vessel public List get(DataSources dataSources) { - string ID = dataSources.args[0]; + string ID = dataSources.args[0].ToLowerInvariant(); List avail = null, ret = null; lock (cacheLock) @@ -2051,7 +2064,19 @@ public List get(DataSources dataSources) #endregion } - public class ActiveResourceCache : ModuleCache + public class SimplifiedResource + { + public double amount { get; set; } + public double maxAmount { get; set; } + + public SimplifiedResource(double amount, double maxAmount) + { + this.amount = amount; + this.maxAmount = maxAmount; + } + } + + public class ActiveResourceCache : ModuleCache { #region ModuleCache @@ -2076,23 +2101,34 @@ protected override void refresh(Vessel vessel) try { partModules.Clear(); - - foreach (Part part in vessel.parts) + HashSet activeParts = new HashSet(); + foreach(Part part in vessel.GetActiveParts()) { - if (part.Resources.Count > 0) + if (part.inverseStage == vessel.currentStage) { - foreach (Vessel.ActiveResource resource in vessel.GetActiveResources()) - { - List list = null; - if (list == null) - { - list = new List(); - partModules[resource.info.name] = list; - } + activeParts.Add(part); + activeParts.UnionWith(part.crossfeedPartSet.GetParts()); + } + } - list.Add(resource); - } + PartSet activePartSet = new PartSet(activeParts); + PartResourceDefinitionList resourceDefinitionList = PartResourceLibrary.Instance.resourceDefinitions; + + foreach(PartResourceDefinition resourceDefinition in resourceDefinitionList) + { + String key = resourceDefinition.name.ToString().ToLowerInvariant(); + double amount = 0; + double maxAmount = 0; + bool pulling = true; + + activePartSet.GetConnectedResourceTotals(resourceDefinition.id, out amount, out maxAmount, pulling); + + if(!partModules.ContainsKey(key)){ + partModules[key] = new List(); } + + partModules[key].Add(new SimplifiedResource(amount, maxAmount)); + PluginLogger.debug("SIZE OF " + key + " " + partModules[key].Count + " " + amount ); } } catch (Exception e) @@ -2137,12 +2173,13 @@ protected override void refresh(Vessel vessel) foreach (PartResource partResource in part.Resources) { + String key = partResource.resourceName.ToLowerInvariant(); List list = null; - partModules.TryGetValue(partResource.resourceName, out list); + partModules.TryGetValue(key, out list); if (list == null) { list = new List(); - partModules[partResource.resourceName] = list; + partModules[key] = list; }