|
1 | 1 | using System; |
2 | | -using System.Reflection; |
3 | 2 |
|
4 | 3 | namespace B9PartSwitch.PartSwitch.PartModifiers |
5 | 4 | { |
@@ -53,13 +52,18 @@ private void applyNode(ConfigNode sourceNode) { |
53 | 52 | bool setsType = sourceNode.TryGetValue("type", ref type); |
54 | 53 |
|
55 | 54 | if (setsVolume) { |
56 | | - Type ModuleFuelTanks = module.GetType(); |
57 | | - FieldInfo volumeField = ModuleFuelTanks.GetField("volume"); |
58 | | - double curVolume = (double)volumeField.GetValue(module); |
59 | | - if (volume != curVolume) { |
60 | | - MethodInfo changeVolume = ModuleFuelTanks.GetMethod("ChangeVolume"); |
61 | | - changeVolume.Invoke(module, new object[]{ volume }); |
62 | | - } |
| 55 | + // Update the tank volume by sending a volume-change event in the format that Procedural Parts uses. |
| 56 | + // Procedural Parts reports the outside volume of the tank in cubic meters (or equivalently, |
| 57 | + // kiloliters). ModuleFuelTanks scales the reported volume by the `tankVolumeConversion` and |
| 58 | + // `utilization` fields to compute the available internal volume in liters. |
| 59 | + // Since the `volume` configuration field is meant to set the available volume in liters directly, |
| 60 | + // we need to read the scaling values and apply the inverse scaling to the value that we send. |
| 61 | + float scaleFactor = module.Fields.GetValue<float>("tankVolumeConversion"); |
| 62 | + float utilization = module.Fields.GetValue<float>("utilization"); |
| 63 | + var evtDetails = new BaseEventDetails(BaseEventDetails.Sender.USER); |
| 64 | + evtDetails.Set<string>("volName", "Tankage"); |
| 65 | + evtDetails.Set<double>("newTotalVolume", volume * 100 / utilization / scaleFactor); |
| 66 | + module.part.SendEvent("OnPartVolumeChanged", evtDetails, 0); |
63 | 67 | } |
64 | 68 | if (setsType) { |
65 | 69 | module.Fields.SetValue("type", type); |
|
0 commit comments