Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix engine configs going missing when copying a part in editor #280

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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