RemoteTechnologiesGroup / RemoteTech

Community developed continuation of Kerbal Space Program's RemoteTech mod.
http://remotetechnologiesgroup.github.io/RemoteTech
GNU General Public License v2.0
233 stars 102 forks source link

RTUtil.GetBool causes the values and sub-nodes in ProtoPartModuleSnapshot.moduleValues to duplicate #805

Closed allista closed 4 years ago

allista commented 4 years ago

A user of the Configurable Containers noted that after upgrading to KSP-1.8 parts with ModuleTankManager started to duplicate all their tanks after each switch to the vessel. They also established that this only occurs when RT is installed.

I started debugging and found out that, indeed, almost the whole PartModule config gets duplicated (values and subnodes) before its OnSave method is called; by accessing its ProtoModuleSnapshot in RTUtil.GetBool (and probably elsewhere).

I suppose the reason is stated in KSP-1.8 modders notes:

The ProtoModuleSnapshot constructor does not create the confignode string at all times any more. As the configNode should only be used once the part is packed or the vessel is onrails or when making a backup of an existing protomodulesnapshot this was generating massive garbage and CPU hit for no purpose. If your code is accessing the snapshots when the partmodule is active then the suggestion is ensuring to only check that for unloaded vessels)

Below is the stack trace showing how it happens:

  at AT_Utils.PartModuleExtensions.Log (PartModule pm, System.String msg, System.Object[] args) [0x00000] in <752f80ca7e6b46489a0c114955ce5ceb>:0 
  at AT_Utils.ModuleTankManager.OnSave (ConfigNode node) [0x00000] in <51ff50592246467b89b34540739a91e0>:0 
  at PartModule.Save (ConfigNode node) [0x00000] in <2c9e31d65a604d1980fb0cb89728fc1e>:0 
  at ProtoPartModuleSnapshot.EnsureModuleValuesIsInitialized () [0x00000] in <2c9e31d65a604d1980fb0cb89728fc1e>:0 
  at ProtoPartModuleSnapshot.Save (ConfigNode node) [0x00000] in <2c9e31d65a604d1980fb0cb89728fc1e>:0 
  at RemoteTech.RTUtil.GetBool (ProtoPartModuleSnapshot ppms, System.String value) [0x00000] in <b4f31cbb1b164f468a3f11347362c977>:0 
  at RemoteTech.RTUtil.IsAntenna (ProtoPartModuleSnapshot ppms) [0x00000] in <b4f31cbb1b164f468a3f11347362c977>:0 
  at RemoteTech.AntennaManager+<>c.<RegisterProtos>b__18_0 (ProtoPartModuleSnapshot ppms) [0x00000] in <b4f31cbb1b164f468a3f11347362c977>:0 
  at System.Linq.Enumerable+WhereListIterator`1[TSource].MoveNext () [0x00000] in <fbb5ed17eb6e46c680000f8910ebb50c>:0 
  at RemoteTech.AntennaManager.RegisterProtos (Vessel v) [0x00000] in <b4f31cbb1b164f468a3f11347362c977>:0 
  at RemoteTech.RTCore.Start () [0x00000] in <b4f31cbb1b164f468a3f11347362c977>:0 
  at RemoteTech.RTCoreFlight.Start () [0x00000] in <b4f31cbb1b164f468a3f11347362c977>:0 

(Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)

[ConfigurableContainers: 19:37:02.742] Orbital Space Dock[69C0D76A]:Multipurpose Resource Tank XL[CE768AC7]:ModuleTankManager[A1DF148E]: OnSave config: MODULE
{
    name = ModuleTankManager
    isEnabled = True
    ModuleSaveFromPrefab = False
    Volume = 117
    DoCostPatch = False
    CostPatch = 0
    DoMassPatch = False
    MassPatch = 0
    stagingEnabled = True
    AddRemoveEnabled = True
    TypeChangeEnabled = True
    ExcludeTankTypes = 
    IncludeTankTypes = 
    MANAGED = True
============= here starts the duplication of values =============
    name = ModuleTankManager
    isEnabled = True
    ModuleSaveFromPrefab = False
    Volume = 117
    DoCostPatch = False
    CostPatch = 0
    DoMassPatch = False
    MassPatch = 0
    ThisModule = ModuleTankManager
    stagingEnabled = True
    EVENTS
    {
    }
    ACTIONS
    {
    }
    UPGRADESAPPLIED
    {
    }
============= here starts the duplication of sub-nodes =============
    EVENTS
    {
    }
    ACTIONS
    {
    }
}