Skip to content

Commit

Permalink
Minimizes the (ab)use of the GC due a Unity terrible dicision making.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lisias committed Jun 27, 2021
1 parent e7af0dd commit 70399c3
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions Source/Refunding/PartModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class Refunding : PartModule, IPartCostModifier
private Part prefab { get => _prefab ?? (_prefab = this.part.partInfo.partPrefab); set => _prefab = value; }
internal double costFix = 0;
private int delayTicks = 0;
private PartResource pr; // Buffer to store the synthetical Part Resource (avoiding overloading the GC, see Issue #21 on GitHub.

#region KSP Life Cycle

Expand Down Expand Up @@ -363,13 +364,16 @@ private bool IsStackable()
private PartResource RestoreResource()
{
// THIS SHOULD NOT BE CALLED when this.active is false, or it may inject a Refunding resource when none is desired.
PartResource pr = this.part.Resources.Get(RESOURCENAME);
if (null == pr)
if (null == this.pr)
{
PartResourceDefinition prd = PartResourceLibrary.Instance.GetDefinition(RESOURCENAME);
pr = this.part.Resources.Add(prd.name, 0, 1, false, false, false, false, PartResource.FlowMode.None);
this.pr = this.part.Resources.Add(prd.name, 0, 1, false, false, false, false, PartResource.FlowMode.None);
}

PartResource pr = this.part.Resources.Get(RESOURCENAME);
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;
Expand Down

0 comments on commit 70399c3

Please sign in to comment.