Skip to content

Commit b8c8b9b

Browse files
committed
Set volume with event instead of reflection
1 parent ffd17ac commit b8c8b9b

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

B9PartSwitch/PartSwitch/PartModifiers/ModuleDataHandlerBasic.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Reflection;
32

43
namespace B9PartSwitch.PartSwitch.PartModifiers
54
{
@@ -53,13 +52,18 @@ private void applyNode(ConfigNode sourceNode) {
5352
bool setsType = sourceNode.TryGetValue("type", ref type);
5453

5554
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);
6367
}
6468
if (setsType) {
6569
module.Fields.SetValue("type", type);

0 commit comments

Comments
 (0)