SirJu/ActionGroupManager — pull request #3 by toadicus (2014-03-28 18:17:29+00:00)
Title: LightweightUINew::SetupRoot revision to avoid uncaught NullReferenceException during crashes

During a vessel crash sequence, the OnVesselChange and OnPartDie events are fired very often. In the AGM version 1.3.2.0, this results in a mass of execution in the relatively-expensive SetupRoot method. Furthermore, during such a sequence, root parts are not guaranteed to exist, and modules or parts may exist in partially-loaded states.

These two factors result in very heavy load from AGM during a crash sequence and many, many uncaught NullReferenceExceptions thrown from SetupRoot. This interrupts the OnPartDie and OnVesselChange event sequences, which prevents vessels from properly unloading and has rather extensive impacts on the game, from extreme performance degradation to a loss of game functionality (e.g. ordinary GUI elements such as "Revert to Launch" or "Return to Space Center" do not function). It also results in malformed vessels in the save file, which causes further Exceptional conditions in Squad code in subsequent visits to the same location.

This revision adds guards throughout the SetupRoot method to avoid Exceptional conditions, and wraps most of the method in a try/catch block to prevent spoiling the OnPartDie and OnVesselChange event sequences in case any Exceptions do arise.

This revision also adds a reinitialization timer to SetupRoot, preventing it from running more than once every half-second. This significantly reduces the additional load from AGM during a crash sequence, but should have no impact at all during normal play.