Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions B9PartSwitch/PartSwitch/ModuleModifierInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ public IEnumerable<IPartModifier> CreatePartModifiers(Part part, PartModule pare
yield return new ModuleOutputResourceResetter(module);
yield return new ModuleDataHandlerBasic(module, originalNode, dataNode, moduleDataChangedEventDetails);
}
else if (module.moduleName == "ModuleFuelTanks") {
yield return new ModuleFuelTanksHandler(module, originalNode, dataNode, moduleDataChangedEventDetails);
}
else
yield return new ModuleDataHandlerBasic(module, originalNode, dataNode, moduleDataChangedEventDetails);
}
Expand Down
44 changes: 41 additions & 3 deletions B9PartSwitch/PartSwitch/PartModifiers/ModuleDataHandlerBasic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class ModuleDataHandlerBasic : PartModifierBase
protected readonly ConfigNode originalNode;
protected readonly ConfigNode dataNode;

private readonly BaseEventDetails moduleDataChangedEventDetails;
protected readonly BaseEventDetails moduleDataChangedEventDetails;

public ModuleDataHandlerBasic(PartModule module, ConfigNode originalNode, ConfigNode dataNode, BaseEventDetails moduleDataChangedEventDetails)
{
Expand All @@ -34,16 +34,54 @@ public ModuleDataHandlerBasic(PartModule module, ConfigNode originalNode, Config
public override void OnWillBeCopiedActiveSubtype() => Deactivate();
public override void OnWasCopiedActiveSubtype() => Activate();

private void Activate()
protected virtual void Activate()
{
module.Load(dataNode);
module.Events.Send("ModuleDataChanged", moduleDataChangedEventDetails);
}

private void Deactivate()
protected virtual void Deactivate()
{
module.Load(originalNode);
module.Events.Send("ModuleDataChanged", moduleDataChangedEventDetails);
}
}

public class ModuleFuelTanksHandler : ModuleDataHandlerBasic
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in its own file

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also can we get rid of the inheritance? It doesn't really use any of the base behavior other than the list of events it responds to which is better just copied.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Err to clarify, it should inherit from PartModifierBase rather than ModuleDataHandlerBasic

{
public ModuleFuelTanksHandler(
PartModule module, ConfigNode originalNode, ConfigNode dataNode, BaseEventDetails moduleDataChangedEventDetails
) : base(module, originalNode, dataNode, moduleDataChangedEventDetails)
{ }

protected override void Activate() => applyNode(dataNode);
protected override void Deactivate() => applyNode(originalNode);

private void applyNode(ConfigNode sourceNode) {
double volume = 0;
bool setsVolume = sourceNode.TryGetValue("volume", ref volume);
string type = null;
bool setsType = sourceNode.TryGetValue("type", ref type);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make setsVolume and setsType (and probably the values too) in the constructor rather than computing them here? The original node will always have these fields so they'll end up getting hit even if the intention is not to modify one of them.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I see we're throwing away everything on the node other than volume and type, do we really want that?


if (setsVolume) {
// Update the tank volume by sending a volume-change event in the format that Procedural Parts uses.
// Procedural Parts reports the outside volume of the tank in cubic meters (or equivalently,
// kiloliters). ModuleFuelTanks scales the reported volume by the `tankVolumeConversion` and
// `utilization` fields to compute the available internal volume in liters.
// Since the `volume` configuration field is meant to set the available volume in liters directly,
// we need to read the scaling values and apply the inverse scaling to the value that we send.
float scaleFactor = module.Fields.GetValue<float>("tankVolumeConversion");
float utilization = module.Fields.GetValue<float>("utilization");
var evtDetails = new BaseEventDetails(BaseEventDetails.Sender.USER);
evtDetails.Set<string>("volName", "Tankage");
evtDetails.Set<double>("newTotalVolume", volume * 100 / utilization / scaleFactor);
module.part.SendEvent("OnPartVolumeChanged", evtDetails, 0);
}
if (setsType) {
module.Fields.SetValue("type", type);
}
module.Events.Send("ModuleDataChanged", moduleDataChangedEventDetails);
}
}
}