Skip to content

Commit

Permalink
Handling race conditions when Making History is present.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lisias committed Oct 25, 2018
1 parent ad93d20 commit dc5f69b
Showing 1 changed file with 43 additions and 20 deletions.
63 changes: 43 additions & 20 deletions Source/Scale/PrefabDryCostWriter.cs
@@ -1,51 +1,73 @@
using TweakScale.Annotations;
using UnityEngine;
using System.Linq;
using System.Linq;
using System;
using System.Collections;

using UnityEngine;
using TweakScale.Annotations;

namespace TweakScale
{
[KSPAddon(KSPAddon.Startup.MainMenu, true)]
internal class PrefabDryCostWriter : SingletonBehavior<PrefabDryCostWriter>
{
private static readonly int WAIT_ROUNDS = 120; // @60fps, would render 2 secs.

internal static bool isConcluded;
private IEnumerator coroutine;

[UsedImplicitly]
private void Start()
{
WriteDryCost();
coroutine = WriteDryCost();
StartCoroutine(coroutine);
}

private void WriteDryCost()
private IEnumerator WriteDryCost()
{
PrefabDryCostWriter.isConcluded = false;
Debug.Log("TweakScale::WriteDryCost: Started");
for (int i = WAIT_ROUNDS; i >= 0 && null == PartLoader.LoadedPartsList && PartLoader.LoadedPartsList.Count < 1; --i)
{
yield return null;
if (0 == i) Debug.LogError("TweakScale::Timeout waiting for PartLoader.LoadedPartsList!!");
}

foreach (AvailablePart p in PartLoader.LoadedPartsList)
{
Part prefab = p.partPrefab;
if (prefab == null)
for (int i = WAIT_ROUNDS; i >= 0 && null == p.partPrefab && null == p.partPrefab.Modules && p.partPrefab.Modules.Count < 1; --i)
{
Tools.LogWf("partPrefab is null: " + p.name);
continue;
yield return null;
if (0 == i) Debug.LogErrorFormat("TweakScale::Timeout waiting for {0}.prefab.Modules!!", p.name);
}

Part prefab = p.partPrefab;

// Historically, we had problems here.
// However, that co-routine stunt appears to have solved it.
// But we will keep this as a ghinea-pig in the case the problem happens again.
try
{
if (!prefab.Modules.Contains("TweakScale"))
continue;
}
catch (Exception e)
{
Debug.LogErrorFormat("[TweakScale] Exception on {0}.prefab.Modules.Contains: {1}", p.name, e);
Debug.LogWarningFormat("{0}", prefab.Modules);
continue; // TODO: Cook a way to try again!
}

try
{
if (prefab.Modules == null)
{
Tools.LogWf("partPrefab.Modules is null: " + p.name);
continue;
}
if (!prefab.Modules.Contains("TweakScale"))
continue;

TweakScale m = prefab.Modules["TweakScale"] as TweakScale;
m.DryCost = (float)(p.cost - prefab.Resources.Cast<PartResource>().Aggregate(0.0, (a, b) => a + b.maxAmount * b.info.unitCost));
if (prefab.Modules.Contains("FSfuelSwitch"))
m.ignoreResourcesForCost = true;
m.ignoreResourcesForCost |= prefab.Modules.Contains("FSfuelSwitch");

if (m.DryCost < 0)
{
Debug.LogErrorFormat("TweakScale::PrefabDryCostWriter: negative dryCost: part={0}, DryCost={1}", p.name, m.DryCost);
m.DryCost = 0;
}

#if DEBUG
Debug.LogFormat("Part {0} has drycost {1} with ignoreResourcesForCost {2}", p.name, m.DryCost, m.ignoreResourcesForCost);
#endif
Expand All @@ -56,6 +78,7 @@ private void WriteDryCost()
}
}
Debug.Log("TweakScale::WriteDryCost: Concluded");
PrefabDryCostWriter.isConcluded = true;
}
}
}

0 comments on commit dc5f69b

Please sign in to comment.