From bf7f88b59068528c378b9c728848d4b43400d360 Mon Sep 17 00:00:00 2001 From: Lisias Date: Sun, 27 Jun 2021 03:46:47 -0300 Subject: [PATCH] Preventing the shitty spinlocks from Unity to bully the CPU while garbage collecting (due the destroyance of the unneeded resources). Works around https://github.com/net-lisias-ksp/KSP-Recall/issues/21 . --- Source/Refunding/PartModule.cs | 40 ++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/Source/Refunding/PartModule.cs b/Source/Refunding/PartModule.cs index c277ffc..3f0f53d 100644 --- a/Source/Refunding/PartModule.cs +++ b/Source/Refunding/PartModule.cs @@ -88,10 +88,14 @@ public override void OnLoad(ConfigNode node) // Always clean up the Resource on loading, as we need to get rid of reminiscents // of the previous attempts. We don't want the Refunding resource on Edit Scene anyway. // - // Additionally, clean up Parts those Refunding was inactivated by the user. - this.RemoveResourceIfAvailable(); + this.ResetResource(); - if (!this.active) return; + if (!this.active) + { + // Additionally, clean up Parts those Refunding was inactivated by the user. + this.RemoveResourceIfAvailable(); + return; + } // The cost of the craft is billed on launch, **after** loading the craft. // @@ -287,15 +291,11 @@ private double CalculateModuleCost(PartModule pm) private void UpdateResource() { Log.dbg("UpdateResource {0}:{1:X}", this.part.partInfo.name, this.part.GetInstanceID()); - if (this.IsStackable()) // Giving up on handling Stackables for now. The Rails stunt didn't worked as expected... - { - this.RemoveResourceIfAvailable(); - Log.dbg("Part is Stackable. Removed Refunding support!"); + if (this.RemoveResourceWhenNeeded()) // Giving up on handling Stackables for now. The Rails stunt didn't worked as expected... return; - } // Rebuild the Refund Resource if it was destroyed by something like a Fuel Switch - PartResource pr = this.part.Resources.Get(RESOURCENAME) ?? this.RestoreResource(); + PartResource pr = this.RestoreResource(); Log.dbg("Before {0} {1} {2} {3}", pr.ToString(), pr.amount, pr.maxAmount, pr.info.unitCost); @@ -315,6 +315,16 @@ private void UpdateResource() Log.dbg("After {0} {1} {2} {3}", pr.ToString(), pr.amount, pr.maxAmount, pr.info.unitCost); } + private void ResetResource() + { + Log.dbg("Resetting {0} on part {1}", RESOURCENAME, this.partInstanceId); + if (null == this.pr) return; + + FieldInfo field = typeof(PartResource).GetField("maxAmount", BindingFlags.Instance | BindingFlags.Public); + field.SetValue(this.pr, 1d); + this.pr.amount = 0; + } + private void RemoveResourceIfAvailable() { Log.dbg("Removing {0} from part {1}", RESOURCENAME, this.PartInstanceId); @@ -325,10 +335,13 @@ private void RemoveResourceIfAvailable() } // Remove the Refunding Resource from parts with PartModuleCargo that can be stackable - internal void RemoveResourceWhenNeeded() + internal bool RemoveResourceWhenNeeded() { - if (this.IsStackable()) + Log.dbg("Part {0} is Stackable. Removed Refunding support!", this.partInstanceId); + bool r; + if (r = this.IsStackable()) this.RemoveResourceIfAvailable(); + return r; } private bool IsStackable() @@ -374,9 +387,8 @@ private PartResource RestoreResource() if (null == pr) this.part.Resources.Add(this.pr); - FieldInfo field = typeof(PartResource).GetField("maxAmount", BindingFlags.Instance | BindingFlags.Public); - field.SetValue(pr, 1d); - pr.amount = 0; + this.ResetResource(); + return pr; } #else