Skip to content

Commit

Permalink
Fix engine configs going missing when copying a part in editor (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
siimav authored Sep 8, 2023
1 parent 8e64322 commit 75434f0
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions TestFlightCore/TestFlightCore/TestFlightCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public class TestFlightCore : PartModuleExtended, ITestFlightCore

public List<ConfigNode> configs = new List<ConfigNode>(8);
public ConfigNode currentConfig;
public string configNodeData;

bool initialized = false;
float transferData;
Expand Down Expand Up @@ -216,14 +215,13 @@ public override void OnLoad(ConfigNode node)
{
configs.Clear();

foreach (ConfigNode subNode in cNodes) {
foreach (ConfigNode subNode in cNodes)
{
var newNode = new ConfigNode("CONFIG");
subNode.CopyTo(newNode);
configs.Add(newNode);
}
}

configNodeData = node.ToString();
}

internal void Log(string message)
Expand Down Expand Up @@ -833,13 +831,13 @@ public override void OnDestroy()
public override void OnAwake()
{
initialized = false;

if (!string.IsNullOrEmpty(configNodeData))

TestFlightCore pm = GetCoreFromPrefab();
if (pm != null)
{
var node = ConfigNode.Parse(configNodeData);
OnLoad(node);
configs = pm.configs;
}

// poll failure modules for any existing failures
foreach (ITestFlightFailure failure in TestFlightUtil.GetFailureModules(this.part, Alias))
{
Expand All @@ -851,6 +849,22 @@ public override void OnAwake()
}
}

private TestFlightCore GetCoreFromPrefab()
{
Part prefab = part?.partInfo?.partPrefab;
if (prefab != null)
{
int index = part.Modules.IndexOf(this);
if (index < 0)
index = part.Modules.Count;

var pm = prefab.Modules.Count > index ? prefab.Modules[index] as TestFlightCore : null;
return pm ?? prefab.FindModuleImplementing<TestFlightCore>();
}

return null;
}

public void OnCrewChange(GameEvents.HostedFromToAction<ProtoCrewMember, Part> e) => _OnCrewChange();
private void _OnCrewChange()
{
Expand Down

0 comments on commit 75434f0

Please sign in to comment.