diff --git a/FerramAerospaceResearch/FARPartGeometry/GeometryPartModule.cs b/FerramAerospaceResearch/FARPartGeometry/GeometryPartModule.cs index cb8825a6..156d394f 100644 --- a/FerramAerospaceResearch/FARPartGeometry/GeometryPartModule.cs +++ b/FerramAerospaceResearch/FARPartGeometry/GeometryPartModule.cs @@ -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 ignoredTransforms, unignoredTransforms; + + public bool ignoreIfNoRenderer; + + public bool rebuildOnAnimation; + + public GeometryPartModuleConfig() + { + forceUseColliders = false; + forceUseMeshes = false; + ignoreForMainAxis = false; + ignoredTransforms = new List(); + unignoredTransforms = new List(); + ignoreIfNoRenderer = true; + rebuildOnAnimation = false; + } + } + public class GeometryPartModule : PartModule, IRescalable { private static int ignoreLayer0 = -1; @@ -85,17 +112,7 @@ public class GeometryPartModule : PartModule, IRescalable private int _sendUpdateTick; private int _meshesToUpdate = -1; - [SerializeField] private bool forceUseColliders; - - [SerializeField] private bool forceUseMeshes; - - [SerializeField] private bool ignoreForMainAxis; - - [SerializeField] private List ignoredTransforms, unignoredTransforms; - - [SerializeField] private bool ignoreIfNoRenderer = true; - - [SerializeField] private bool rebuildOnAnimation; + [SerializeField] private GeometryPartModuleConfig config = new(); public bool HasCrossSectionAdjusters { @@ -138,7 +155,7 @@ public bool Ready public bool IgnoreForMainAxis { - get { return ignoreForMainAxis; } + get { return config.ignoreForMainAxis; } } public void OnRescale(ScalingFactor factor) @@ -190,10 +207,6 @@ private void OnEditorAttach() public override void OnAwake() { base.OnAwake(); - if (ignoredTransforms == null) - ignoredTransforms = new List(); - if (unignoredTransforms == null) - unignoredTransforms = new List(); // Part has connected member that would work but it is not public... part.OnEditorAttach += OnEditorAttach; @@ -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) @@ -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 @@ -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(); if (mr == null) @@ -831,13 +844,13 @@ private List CreateMeshListFromTransforms(ref List meshTran part.Modules.Contains(); //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); @@ -874,7 +887,7 @@ private List CreateMeshListFromTransforms(ref List meshTran if (t.gameObject.activeInHierarchy == false) continue; - MeshData md = GetVisibleMeshData(t, ignoreIfNoRenderer, false); + MeshData md = GetVisibleMeshData(t, config.ignoreIfNoRenderer, false); if (md == null) continue; @@ -884,12 +897,12 @@ private List CreateMeshListFromTransforms(ref List 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; @@ -901,10 +914,10 @@ private List CreateMeshListFromTransforms(ref List 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; @@ -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(); + 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)