Skip to content

Commit

Permalink
Preventing the shitty spinlocks from Unity to bully the CPU while gar…
Browse files Browse the repository at this point in the history
…bage collecting (due the destroyance of the unneeded resources).

Works around #21 .
  • Loading branch information
Lisias committed Jun 27, 2021
1 parent 7049ab2 commit bf7f88b
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions Source/Refunding/PartModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bf7f88b

Please sign in to comment.