From d7d9aece4e43bb6515e06fb06bb710018e12263b Mon Sep 17 00:00:00 2001 From: Lisias T Date: Fri, 7 Jun 2019 21:24:25 -0300 Subject: [PATCH] Implementing (fatal) Sanity Check for issue #34 https://github.com/net-lisias-ksp/TweakScale/issues/34 --- Source/Scale/GlobalSuppressions.cs | 8 ++++++ Source/Scale/PrefabDryCostWriter.cs | 40 ++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 Source/Scale/GlobalSuppressions.cs diff --git a/Source/Scale/GlobalSuppressions.cs b/Source/Scale/GlobalSuppressions.cs new file mode 100644 index 00000000..8e8ec74f --- /dev/null +++ b/Source/Scale/GlobalSuppressions.cs @@ -0,0 +1,8 @@ + +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage ("Potential Code Quality Issues", "RECS0082:Parameter has the same name as a member and hides it", Justification = "", Scope = "member", Target = "~M:TweakScale.GUI.AlertBox.Init(System.String,System.String,System.Action)")] + diff --git a/Source/Scale/PrefabDryCostWriter.cs b/Source/Scale/PrefabDryCostWriter.cs index 6a295b6a..aeb4c10d 100644 --- a/Source/Scale/PrefabDryCostWriter.cs +++ b/Source/Scale/PrefabDryCostWriter.cs @@ -49,6 +49,7 @@ private IEnumerator WriteDryCost() } int sanity_failures = 0; + int showstoppers_failures = 0; foreach (AvailablePart p in PartLoader.LoadedPartsList) { for (int i = WAIT_ROUNDS; i >= 0 && null == p.partPrefab && null == p.partPrefab.Modules && p.partPrefab.Modules.Count < 1; --i) @@ -106,8 +107,9 @@ private IEnumerator WriteDryCost() } #endif { - string r = this.checkForSanity(prefab); - if (null != r) + string r = null; + // We check for fixable problems first, in the hope to prevent by luck a ShowStopper below. + if (null != (r = this.checkForSanity(prefab))) { // There are some known situations where TweakScale is capsizing. If such situations are detected, we just // refuse to scale it. Sorry. Debug.LogWarningFormat("[TweakScale] Removing TweakScale support for {0}.", p.name); @@ -116,6 +118,16 @@ private IEnumerator WriteDryCost() ++sanity_failures; continue; } + + if (null != (r = this.checkForShowStoppers(prefab))) + { // This are situations that we should not allow the KSP to run to prevent serious corruption. + // This is **FAR** from a good measure, but it's the only viable. + Debug.LogWarningFormat("[TweakScale] **FATAL** Found a showstopper problem on {0}.", p.name); + prefab.Modules.Remove(prefab.Modules["TweakScale"]); + Debug.LogErrorFormat("[TweakScale] **FATAL** Part {0} has a fatal problem due {1}.", p.name, r); + ++showstoppers_failures; + continue; + } } try @@ -140,7 +152,11 @@ private IEnumerator WriteDryCost() } Debug.Log("TweakScale::WriteDryCost: Concluded"); PrefabDryCostWriter.isConcluded = true; - if (sanity_failures > 0) + if (showstoppers_failures > 0) + { + //todo MessageBox! + } + else if (sanity_failures > 0) { //todo MessageBox! } @@ -184,5 +200,23 @@ private string checkForSanity(Part p) return null; } + + private string checkForShowStoppers(Part p) + { + { + ConfigNode part = GameDatabase.Instance.GetConfigNode(p.partInfo.partUrl); + foreach (ConfigNode basket in part.GetNodes("MODULE")) + { + if ("TweakScale" != basket.GetValue("name")) continue; + foreach (ConfigNode.Value property in basket.values) + { + if (1 != basket.GetValues(property.name).Length) + return "having duplicated properties - see issue #34 - https://github.com/net-lisias-ksp/TweakScale/issues/34"; + } + } + } + + return null; + } } }