Skip to content

Commit

Permalink
Use prefab config in flight scene as the node passed is from the craf…
Browse files Browse the repository at this point in the history
…t, should fix the issue with aero forces being different after reverting flight. Regular launch maintained correct field values from Unity serialization but fell back to defaults after reverting as the ConfigNode was empty.
  • Loading branch information
dkavolis committed Aug 30, 2022
1 parent 476e99b commit 2324be4
Showing 1 changed file with 59 additions and 34 deletions.
93 changes: 59 additions & 34 deletions FerramAerospaceResearch/FARPartGeometry/GeometryPartModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,33 @@ You should have received a copy of the GNU General Public License

namespace FerramAerospaceResearch.FARPartGeometry
{
[Serializable]
internal struct GeometryPartModuleConfig
{
public bool forceUseColliders;

public bool forceUseMeshes;

public bool ignoreForMainAxis;

public List<string> ignoredTransforms, unignoredTransforms;

public bool ignoreIfNoRenderer;

public bool rebuildOnAnimation;

public GeometryPartModuleConfig()
{
forceUseColliders = false;
forceUseMeshes = false;
ignoreForMainAxis = false;
ignoredTransforms = new List<string>();
unignoredTransforms = new List<string>();
ignoreIfNoRenderer = true;
rebuildOnAnimation = false;
}
}

public class GeometryPartModule : PartModule, IRescalable<GeometryPartModule>
{
private static int ignoreLayer0 = -1;
Expand Down Expand Up @@ -85,17 +112,7 @@ public class GeometryPartModule : PartModule, IRescalable<GeometryPartModule>
private int _sendUpdateTick;
private int _meshesToUpdate = -1;

[SerializeField] private bool forceUseColliders;

[SerializeField] private bool forceUseMeshes;

[SerializeField] private bool ignoreForMainAxis;

[SerializeField] private List<string> ignoredTransforms, unignoredTransforms;

[SerializeField] private bool ignoreIfNoRenderer = true;

[SerializeField] private bool rebuildOnAnimation;
[SerializeField] private GeometryPartModuleConfig config = new();

public bool HasCrossSectionAdjusters
{
Expand Down Expand Up @@ -138,7 +155,7 @@ public bool Ready

public bool IgnoreForMainAxis
{
get { return ignoreForMainAxis; }
get { return config.ignoreForMainAxis; }
}

public void OnRescale(ScalingFactor factor)
Expand Down Expand Up @@ -190,10 +207,6 @@ private void OnEditorAttach()
public override void OnAwake()
{
base.OnAwake();
if (ignoredTransforms == null)
ignoredTransforms = new List<string>();
if (unignoredTransforms == null)
unignoredTransforms = new List<string>();

// Part has connected member that would work but it is not public...
part.OnEditorAttach += OnEditorAttach;
Expand Down Expand Up @@ -358,10 +371,10 @@ internal void RebuildAllMeshData()

private bool IgnoredPredicate(Transform t)
{
if (unignoredTransforms.Contains(t.name))
if (config.unignoredTransforms.Contains(t.name))
return false;

if (ignoredTransforms.Contains(t.name))
if (config.ignoredTransforms.Contains(t.name))
return true;

if (t.gameObject.layer != ignoreLayer0)
Expand Down Expand Up @@ -647,7 +660,7 @@ private void CheckAnimations()

if (!updateShape)
return;
if (rebuildOnAnimation)
if (config.rebuildOnAnimation)
RebuildAllMeshData();
else
//event to update voxel, with rate limiter for computer's sanity and error reduction
Expand Down Expand Up @@ -776,7 +789,7 @@ private MeshData GetVisibleMeshData(Transform t, bool skipIfNoRenderer, bool onl

if (mf != null)
{
if (skipIfNoRenderer && !unignoredTransforms.Contains(t.name))
if (skipIfNoRenderer && !config.unignoredTransforms.Contains(t.name))
{
MeshRenderer mr = t.GetComponent<MeshRenderer>();
if (mr == null)
Expand Down Expand Up @@ -831,13 +844,13 @@ private List<MeshData> CreateMeshListFromTransforms(ref List<Transform> meshTran
part.Modules.Contains<ModuleResourceHarvester>();

//Voxelize colliders
if ((forceUseColliders ||
if ((config.forceUseColliders ||
isFairing ||
isDrill ||
rendererBounds.size.x * rendererBounds.size.z < colliderBounds.size.x * colliderBounds.size.z * 1.6f &&
rendererBounds.size.y < colliderBounds.size.y * 1.2f &&
(rendererBounds.center - colliderBounds.center).magnitude < 0.3f) &&
!forceUseMeshes)
!config.forceUseMeshes)
foreach (Transform t in meshTransforms)
{
MeshData md = GetColliderMeshData(t);
Expand Down Expand Up @@ -874,7 +887,7 @@ private List<MeshData> CreateMeshListFromTransforms(ref List<Transform> meshTran
if (t.gameObject.activeInHierarchy == false)
continue;

MeshData md = GetVisibleMeshData(t, ignoreIfNoRenderer, false);
MeshData md = GetVisibleMeshData(t, config.ignoreIfNoRenderer, false);
if (md == null)
continue;

Expand All @@ -884,12 +897,12 @@ private List<MeshData> CreateMeshListFromTransforms(ref List<Transform> meshTran
}

//Voxelize Everything
if ((cantUseColliders || forceUseMeshes || isFairing) && !isDrill)
if ((cantUseColliders || config.forceUseMeshes || isFairing) && !isDrill)
foreach (Transform t in meshTransforms)
{
if (jettisonTransforms.Contains(t.name))
continue;
MeshData md = GetVisibleMeshData(t, ignoreIfNoRenderer, false);
MeshData md = GetVisibleMeshData(t, config.ignoreIfNoRenderer, false);
if (md == null)
continue;

Expand All @@ -901,10 +914,10 @@ private List<MeshData> CreateMeshListFromTransforms(ref List<Transform> meshTran
else
{
//Voxelize Everything
if ((cantUseColliders || forceUseMeshes || isFairing) && !isDrill)
if ((cantUseColliders || config.forceUseMeshes || isFairing) && !isDrill)
foreach (Transform t in meshTransforms)
{
MeshData md = GetVisibleMeshData(t, ignoreIfNoRenderer, false);
MeshData md = GetVisibleMeshData(t, config.ignoreIfNoRenderer, false);
if (md == null)
continue;

Expand Down Expand Up @@ -1032,13 +1045,25 @@ public void Rescale(Vector3 relativeRescaleFactor)
public override void OnLoad(ConfigNode node)
{
base.OnLoad(node);
LoadBool(node, "forceUseColliders", ref forceUseColliders);
LoadBool(node, "forceUseMeshes", ref forceUseMeshes);
LoadBool(node, "ignoreForMainAxis", ref ignoreForMainAxis);
LoadBool(node, "ignoreIfNoRenderer", ref ignoreIfNoRenderer);
LoadBool(node, "rebuildOnAnimation", ref rebuildOnAnimation);
LoadList(node, "ignoreTransform", ref ignoredTransforms);
LoadList(node, "unignoreTransform", ref unignoredTransforms);

if (HighLogic.LoadedSceneIsFlight)
{
// Flight passes node from the craft config which is empty, use the loaded prefab instead
GeometryPartModule modulePrefab = part.partInfo.partPrefab.FindModuleImplementing<GeometryPartModule>();
if (modulePrefab is not null)
{
config = modulePrefab.config;
return;
}
}

LoadBool(node, "forceUseColliders", ref config.forceUseColliders);
LoadBool(node, "forceUseMeshes", ref config.forceUseMeshes);
LoadBool(node, "ignoreForMainAxis", ref config.ignoreForMainAxis);
LoadBool(node, "ignoreIfNoRenderer", ref config.ignoreIfNoRenderer);
LoadBool(node, "rebuildOnAnimation", ref config.rebuildOnAnimation);
LoadList(node, "ignoreTransform", ref config.ignoredTransforms);
LoadList(node, "unignoreTransform", ref config.unignoredTransforms);
}

private void LoadBool(ConfigNode node, string nodeName, ref bool value)
Expand Down

0 comments on commit 2324be4

Please sign in to comment.