diff --git a/Source/CommNetFixer.cs b/Source/CommNetFixer.cs index c13bd170..eb053b9c 100644 --- a/Source/CommNetFixer.cs +++ b/Source/CommNetFixer.cs @@ -5,10 +5,9 @@ namespace RealSolarSystem { [KSPAddon(KSPAddon.Startup.SpaceCentre, true)] - public class RSSCommNetSettings : MonoBehaviour { - void Start() + public void Start() { try { @@ -18,7 +17,7 @@ void Start() float occlusionMultiplierInAtm = 1.0f; float occlusionMultiplierInVac = 1.0f; - Debug.Log("[RealSolarSystem]: Checking for custom CommNet settings..."); + Debug.Log("[RealSolarSystem] Checking for custom CommNet settings..."); foreach (ConfigNode RSSSettings in GameDatabase.Instance.GetConfigNodes("REALSOLARSYSTEM")) { @@ -32,7 +31,7 @@ void Start() { // Set the default CommNet parameters for RealSolarSystem. - Debug.Log("[RealSolarSystem]: Updating the CommNet settings..."); + Debug.Log("[RealSolarSystem] Updating the CommNet settings..."); HighLogic.CurrentGame.Parameters.CustomParams().enableGroundStations = enableExtraGroundStations; HighLogic.CurrentGame.Parameters.CustomParams().occlusionMultiplierAtm = occlusionMultiplierInAtm; @@ -41,7 +40,7 @@ void Start() } catch (Exception exceptionStack) { - Debug.Log("[RealSolarSystem]: RSSCommNetSettings.Start() caught an exception: " + exceptionStack); + Debug.Log("[RealSolarSystem] RSSCommNetSettings.Start() caught an exception: " + exceptionStack); } finally { diff --git a/Source/GUI.cs b/Source/GUI.cs index 34aff635..b64b1007 100644 --- a/Source/GUI.cs +++ b/Source/GUI.cs @@ -5,9 +5,7 @@ namespace RealSolarSystem { // From Starwaster. - [KSPAddon(KSPAddon.Startup.Flight, false)] - public class RealSolarSystemEditor : MonoBehaviour { static Rect windowPosition = new Rect(64, 64, 320, 640); @@ -39,8 +37,6 @@ public void Apply() { Camera[] cameras = Camera.allCameras; - float ftmp; - try { bool notFound = true; @@ -49,7 +45,7 @@ public void Apply() { if (camName.Equals(cam.name)) { - if (float.TryParse(depth, out ftmp)) + if (float.TryParse(depth, out float ftmp)) cam.depth = ftmp; if (float.TryParse(farClipPlane, out ftmp)) @@ -68,12 +64,12 @@ public void Apply() if (notFound) { - Debug.Log("[RealSolarSystem]: Could not find camera " + camName + " when applying settings!"); + Debug.Log($"[RealSolarSystem] Could not find camera {camName} when applying settings!"); } } catch (Exception exceptionStack) { - Debug.Log("[RealSolarSystem]: Error applying to camera " + camName + ": exception " + exceptionStack.Message); + Debug.Log($"[RealSolarSystem] Error applying to camera {camName}: exception {exceptionStack.Message}"); } } } @@ -110,7 +106,7 @@ public void Update() } catch (Exception exceptionStack) { - Debug.Log("[RealSolarSystem]: Exception getting camera " + cam.name + "\n" + exceptionStack); + Debug.Log($"[RealSolarSystem] Exception getting camera {cam.name}\n{exceptionStack}"); } } } @@ -121,8 +117,7 @@ public void Update() } } - - void OnGUI() + public void OnGUI() { if (GUIOpen) { @@ -137,7 +132,7 @@ public void Start() windowStyle.stretchHeight = true; } - void ShowGUI(int windowID) + private void ShowGUI(int windowID) { GUILayout.BeginVertical(); diff --git a/Source/InstallationChecker.cs b/Source/InstallationChecker.cs index cf330a62..32d0e9e0 100644 --- a/Source/InstallationChecker.cs +++ b/Source/InstallationChecker.cs @@ -5,10 +5,9 @@ namespace RealSolarSystem { [KSPAddon(KSPAddon.Startup.Instantly, true)] - class RSSInstallationCheck : MonoBehaviour { - void Start () + public void Start () { try { @@ -18,7 +17,7 @@ void Start () if (!Directory.Exists (szTextureFolderPath)) { - Debug.Log ("[RealSolarSystem]: No texture pack detected!"); + Debug.Log ("[RealSolarSystem] No texture pack detected!"); PopupDialog.SpawnPopupDialog ( @@ -53,7 +52,7 @@ void Start () } catch (Exception exceptionStack) { - Debug.Log ("[RealSolarSystem]: RSSInstallationCheck.Start() caught an exception: " + exceptionStack); + Debug.Log("[RealSolarSystem] RSSInstallationCheck.Start() caught an exception: " + exceptionStack); } finally { diff --git a/Source/RSSRunwayFix.cs b/Source/RSSRunwayFix.cs index 326c0aa9..1052c137 100644 --- a/Source/RSSRunwayFix.cs +++ b/Source/RSSRunwayFix.cs @@ -4,276 +4,278 @@ namespace RealSolarSystem { - [KSPAddon(KSPAddon.Startup.Flight, false)] - - public class RSSRunwayFix : MonoBehaviour - { - private bool hold = false; - - public bool debug = false; - - public float holdThreshold = 2700; - public float holdThresholdSqr; - - public float originalThreshold = 0; - public float originalThresholdSqr = 0; - - private int layerMask = 1<<15; - - private int frameSkip = 0; - private bool rwy = false; - - private bool waiting = false; - private IEnumerator waitCoro = null; - private bool coroComplete = false; - - private Vector3 down; - - private string[] collidersToFix = - { - "Section4", "Section3", "Section2", "Section1", "End27", "End09", - }; - - public void Start() { - - printDebug("Start"); - - foreach (ConfigNode n in GameDatabase.Instance.GetConfigNodes("RSSRUNWAYFIX")) - { - if (bool.TryParse(n.GetValue("debug"), out bool bTemp)) - { - debug = bTemp; - } - if (float.TryParse(n.GetValue("debug"), out float fTemp)) - { - holdThreshold = fTemp; - } - } - - GameEvents.onVesselGoOffRails.Add (onVesselGoOffRails); - GameEvents.onVesselGoOnRails.Add (onVesselGoOnRails); - GameEvents.onVesselSwitching.Add (onVesselSwitching); - GameEvents.onVesselSituationChange.Add (onVesselSituationChange); - - //GameEvents.onFloatingOriginShift.Add(onFloatingOriginShift); - } - - public void OnDestroy() { - printDebug("OnDestroy"); - GameEvents.onVesselGoOffRails.Remove (onVesselGoOffRails); - GameEvents.onVesselGoOnRails.Remove (onVesselGoOnRails); - GameEvents.onVesselSwitching.Remove (onVesselSwitching); - GameEvents.onVesselSituationChange.Remove(onVesselSituationChange); - - //GameEvents.onFloatingOriginShift.Remove(onFloatingOriginShift); - } - - public void onFloatingOriginShift(Vector3d v0, Vector3d v1) - { - if (!hold) - { - return; - } - if (debug) printDebug($"RSSRWF: v0: {v0}, v1: {v1}, threshold: {FloatingOrigin.fetch.threshold}"); - } - - public void onVesselGoOnRails(Vessel v) - { - FloatingOrigin.fetch.threshold = originalThreshold; - FloatingOrigin.fetch.thresholdSqr = originalThresholdSqr; - hold = false; - } - - public void onVesselGoOffRails(Vessel v) { - - printDebug("started"); - - originalThreshold = FloatingOrigin.fetch.threshold; - originalThresholdSqr = FloatingOrigin.fetch.thresholdSqr; - - if (debug) printDebug($"original threshold={originalThreshold}"); - holdThresholdSqr = holdThreshold * holdThreshold; - - GameObject end09 = GameObject.Find(collidersToFix[0]); - if (end09 == null) - { - printDebug("no end09 found"); - hold = false; - return; - } - - //combine(end09.transform.parent.gameObject); - disableColliders(); - getDownwardVector(); - hold = true; - waiting = false; - } - - public void onVesselSwitching(Vessel from, Vessel to) { - - if (to == null || to.situation != Vessel.Situations.LANDED) { // FIXME: Do we need PRELAUNCH here? - return; - } - - getDownwardVector(); - waiting = false; - } - - private void getDownwardVector() - { - Vessel v = FlightGlobals.ActiveVessel; - down = (v.CoM - v.mainBody.transform.position).normalized * -1; - } - - public void onVesselSituationChange(GameEvents.HostedFromToAction data) - { - if (data.host != FlightGlobals.ActiveVessel) - { - return; - } - - hold = data.to == Vessel.Situations.LANDED; - printDebug($"vessel: {data.host.vesselName}, situation: {data.to}, hold: {hold}"); - - if (!hold && FloatingOrigin.fetch.threshold > originalThreshold && originalThreshold > 0) - { - printDebug($"coro: {waitCoro}, complete: {coroComplete}"); - if (waitCoro != null && !coroComplete) - { - printDebug("stopping coro"); - StopCoroutine(waitCoro); - } - - waitCoro = restoreThreshold(); - printDebug($"created new coro: {waitCoro}"); - - coroComplete = false; - StartCoroutine(waitCoro); - } - } - - private IEnumerator restoreThreshold() - { - if (debug) printDebug($"in coro; hold={hold}, waiting={waiting}, alt={FlightGlobals.ActiveVessel.radarAltitude}"); - while (!hold && !waiting && FlightGlobals.ActiveVessel.radarAltitude < 10) - { - if (debug) printDebug($"radar alt: {FlightGlobals.ActiveVessel.radarAltitude}, waiting 5 sec"); - waiting = true; - yield return new WaitForSeconds(5); - waiting = false; - printDebug("waiting is over"); - } - - // Check again as situation could have changed - if (!hold && FloatingOrigin.fetch.threshold > originalThreshold && originalThreshold > 0) { - if (debug) printDebug($"Restoring original thresholds ({FloatingOrigin.fetch.threshold} > {originalThreshold}), "+ - $"alt={FlightGlobals.ActiveVessel.radarAltitude}"); - FloatingOrigin.fetch.threshold = originalThreshold; - FloatingOrigin.fetch.thresholdSqr = originalThresholdSqr; - } - printDebug("coro finished"); - coroComplete = true; - } - - public void FixedUpdate() - { - frameSkip++; - if (frameSkip < 25) - { - return; - } - frameSkip = 0; - - if (!checkRunway()) - { - if (rwy) - { - if (debug) printDebug($"rwy=false; threshold={FloatingOrigin.fetch.threshold}, original threshold={originalThreshold}"); - rwy = false; - } - - return; - } - - FloatingOrigin.fetch.threshold = holdThreshold; - FloatingOrigin.fetch.thresholdSqr = holdThresholdSqr; - - if (!rwy) - { - if (debug) printDebug($"rwy=true; threshold={FloatingOrigin.fetch.threshold}, original threshold={originalThreshold}"); - rwy = true; - } - - FloatingOrigin.SetSafeToEngage(false); - } - - private bool checkRunway() - { - if (!hold) - { - return false; - } - - Vessel v = FlightGlobals.ActiveVessel; - if (v.situation != Vessel.Situations.LANDED && v.situation != Vessel.Situations.PRELAUNCH) - { - return false; - } - - getDownwardVector(); - - RaycastHit raycastHit; - bool hit = Physics.Raycast(v.transform.position, down, out raycastHit, 100, layerMask); - if (!hit) - { - return false; - } - - string colliderName = raycastHit.collider.gameObject.name; - //if (debug) printDebug($"hit collider: {colliderName}"); - if (colliderName != "runway_collider") - { - return false; - } - - return true; - } - - internal void printDebug(String message) { - - if (!debug) return; - - var trace = new System.Diagnostics.StackTrace(); - String caller = trace.GetFrame(1).GetMethod ().Name; - int line = trace.GetFrame (1).GetFileLineNumber (); - Debug.Log($"[RealSolarSystem] {caller}:{line}: {message}"); - } - - private void disableColliders() - { - foreach (string c in collidersToFix) - { - GameObject o = GameObject.Find(c); - if (o == null) - { - if (debug) printDebug($"Object {c} not found, skipping"); - continue; - } - if (!o.activeInHierarchy) - { - if (debug) printDebug($"{o.name} is not active, skipping"); - continue; - } - - MeshCollider cl = o.GetComponentInChildren(); - if (cl == null) - { - if (debug) printDebug($"No mesh collider in {c}"); - continue; - } - if (debug) printDebug($"disabling {cl.name}"); - cl.enabled = false; - } - } - } + [KSPAddon(KSPAddon.Startup.Flight, false)] + public class RSSRunwayFix : MonoBehaviour + { + private bool hold = false; + + public bool debug = false; + + public float holdThreshold = 2700; + public float holdThresholdSqr; + + public float originalThreshold = 0; + public float originalThresholdSqr = 0; + + private int layerMask = 1<<15; + + private int frameSkip = 0; + private bool rwy = false; + + private bool waiting = false; + private IEnumerator waitCoro = null; + private bool coroComplete = false; + + private Vector3 down; + + private string[] collidersToFix = + { + "Section4", "Section3", "Section2", "Section1", "End27", "End09", + }; + + public void Start() + { + PrintDebug("Start"); + + foreach (ConfigNode n in GameDatabase.Instance.GetConfigNodes("RSSRUNWAYFIX")) + { + if (bool.TryParse(n.GetValue("debug"), out bool bTemp)) + { + debug = bTemp; + } + if (float.TryParse(n.GetValue("debug"), out float fTemp)) + { + holdThreshold = fTemp; + } + } + + GameEvents.onVesselGoOffRails.Add (OnVesselGoOffRails); + GameEvents.onVesselGoOnRails.Add (OnVesselGoOnRails); + GameEvents.onVesselSwitching.Add (OnVesselSwitching); + GameEvents.onVesselSituationChange.Add (OnVesselSituationChange); + + //GameEvents.onFloatingOriginShift.Add(onFloatingOriginShift); + } + + public void OnDestroy() + { + PrintDebug("OnDestroy"); + GameEvents.onVesselGoOffRails.Remove (OnVesselGoOffRails); + GameEvents.onVesselGoOnRails.Remove (OnVesselGoOnRails); + GameEvents.onVesselSwitching.Remove (OnVesselSwitching); + GameEvents.onVesselSituationChange.Remove(OnVesselSituationChange); + + //GameEvents.onFloatingOriginShift.Remove(onFloatingOriginShift); + } + + public void OnFloatingOriginShift(Vector3d v0, Vector3d v1) + { + if (!hold) + { + return; + } + if (debug) PrintDebug($"RSSRWF: v0: {v0}, v1: {v1}, threshold: {FloatingOrigin.fetch.threshold}"); + } + + public void OnVesselGoOnRails(Vessel v) + { + FloatingOrigin.fetch.threshold = originalThreshold; + FloatingOrigin.fetch.thresholdSqr = originalThresholdSqr; + hold = false; + } + + public void OnVesselGoOffRails(Vessel v) + { + PrintDebug("started"); + + originalThreshold = FloatingOrigin.fetch.threshold; + originalThresholdSqr = FloatingOrigin.fetch.thresholdSqr; + + if (debug) PrintDebug($"original threshold={originalThreshold}"); + holdThresholdSqr = holdThreshold * holdThreshold; + + GameObject end09 = GameObject.Find(collidersToFix[0]); + if (end09 == null) + { + PrintDebug("no end09 found"); + hold = false; + return; + } + + //combine(end09.transform.parent.gameObject); + DisableColliders(); + GetDownwardVector(); + hold = true; + waiting = false; + } + + public void OnVesselSwitching(Vessel from, Vessel to) + { + if (to == null || to.situation != Vessel.Situations.LANDED) + { + // FIXME: Do we need PRELAUNCH here? + return; + } + + GetDownwardVector(); + waiting = false; + } + + private void GetDownwardVector() + { + Vessel v = FlightGlobals.ActiveVessel; + down = (v.CoM - v.mainBody.transform.position).normalized * -1; + } + + public void OnVesselSituationChange(GameEvents.HostedFromToAction data) + { + if (data.host != FlightGlobals.ActiveVessel) + { + return; + } + + hold = data.to == Vessel.Situations.LANDED; + PrintDebug($"vessel: {data.host.vesselName}, situation: {data.to}, hold: {hold}"); + + if (!hold && FloatingOrigin.fetch.threshold > originalThreshold && originalThreshold > 0) + { + PrintDebug($"coro: {waitCoro}, complete: {coroComplete}"); + if (waitCoro != null && !coroComplete) + { + PrintDebug("stopping coro"); + StopCoroutine(waitCoro); + } + + waitCoro = RestoreThreshold(); + PrintDebug($"created new coro: {waitCoro}"); + + coroComplete = false; + StartCoroutine(waitCoro); + } + } + + private IEnumerator RestoreThreshold() + { + if (debug) PrintDebug($"in coro; hold={hold}, waiting={waiting}, alt={FlightGlobals.ActiveVessel.radarAltitude}"); + while (!hold && !waiting && FlightGlobals.ActiveVessel.radarAltitude < 10) + { + if (debug) PrintDebug($"radar alt: {FlightGlobals.ActiveVessel.radarAltitude}, waiting 5 sec"); + waiting = true; + yield return new WaitForSeconds(5); + waiting = false; + PrintDebug("waiting is over"); + } + + // Check again as situation could have changed + if (!hold && FloatingOrigin.fetch.threshold > originalThreshold && originalThreshold > 0) { + if (debug) PrintDebug($"Restoring original thresholds ({FloatingOrigin.fetch.threshold} > {originalThreshold}), "+ + $"alt={FlightGlobals.ActiveVessel.radarAltitude}"); + FloatingOrigin.fetch.threshold = originalThreshold; + FloatingOrigin.fetch.thresholdSqr = originalThresholdSqr; + } + PrintDebug("coro finished"); + coroComplete = true; + } + + public void FixedUpdate() + { + frameSkip++; + if (frameSkip < 25) + { + return; + } + frameSkip = 0; + + if (!CheckRunway()) + { + if (rwy) + { + if (debug) PrintDebug($"rwy=false; threshold={FloatingOrigin.fetch.threshold}, original threshold={originalThreshold}"); + rwy = false; + } + + return; + } + + FloatingOrigin.fetch.threshold = holdThreshold; + FloatingOrigin.fetch.thresholdSqr = holdThresholdSqr; + + if (!rwy) + { + if (debug) PrintDebug($"rwy=true; threshold={FloatingOrigin.fetch.threshold}, original threshold={originalThreshold}"); + rwy = true; + } + + FloatingOrigin.SetSafeToEngage(false); + } + + private bool CheckRunway() + { + if (!hold) + { + return false; + } + + Vessel v = FlightGlobals.ActiveVessel; + if (v.situation != Vessel.Situations.LANDED && v.situation != Vessel.Situations.PRELAUNCH) + { + return false; + } + + GetDownwardVector(); + + bool hit = Physics.Raycast(v.transform.position, down, out RaycastHit raycastHit, 100, layerMask); + if (!hit) + { + return false; + } + + string colliderName = raycastHit.collider.gameObject.name; + //if (debug) printDebug($"hit collider: {colliderName}"); + if (colliderName != "runway_collider") + { + return false; + } + + return true; + } + + internal void PrintDebug(string message) + { + + if (!debug) return; + + var trace = new System.Diagnostics.StackTrace(); + string caller = trace.GetFrame(1).GetMethod().Name; + int line = trace.GetFrame(1).GetFileLineNumber(); + Debug.Log($"[RealSolarSystem] {caller}:{line}: {message}"); + } + + private void DisableColliders() + { + foreach (string c in collidersToFix) + { + GameObject o = GameObject.Find(c); + if (o == null) + { + if (debug) PrintDebug($"Object {c} not found, skipping"); + continue; + } + if (!o.activeInHierarchy) + { + if (debug) PrintDebug($"{o.name} is not active, skipping"); + continue; + } + + MeshCollider cl = o.GetComponentInChildren(); + if (cl == null) + { + if (debug) PrintDebug($"No mesh collider in {c}"); + continue; + } + if (debug) PrintDebug($"disabling {cl.name}"); + cl.enabled = false; + } + } + } } diff --git a/Source/TimeWarpFixer.cs b/Source/TimeWarpFixer.cs index 21dfce2f..28f4aed3 100644 --- a/Source/TimeWarpFixer.cs +++ b/Source/TimeWarpFixer.cs @@ -3,7 +3,6 @@ namespace RealSolarSystem { [KSPAddon(KSPAddon.Startup.EveryScene, false)] - public class TimeWarpFixer : MonoBehaviour { public double lastTime = 0; @@ -20,7 +19,6 @@ public void Start() public void Update() { - // Update the TimeWarp rates. if (!fixedTimeWarp && TimeWarp.fetch != null) @@ -31,16 +29,14 @@ public void Update() foreach (ConfigNode node in GameDatabase.Instance.GetConfigNodes("REALSOLARSYSTEM")) twNode = node.GetNode("timeWarpRates"); - Debug.Log("[RealSolarSystem]: Setting TimeWarp rates..."); - - float ftmp; + Debug.Log("[RealSolarSystem] Setting TimeWarp rates..."); if (twNode != null) { for (int i = 1; i < 8; i++) { if (twNode.HasValue("rate" + i)) - if (float.TryParse(twNode.GetValue("rate" + i), out ftmp)) + if (float.TryParse(twNode.GetValue("rate" + i), out float ftmp)) TimeWarp.fetch.warpRates[i] = ftmp; } } diff --git a/Source/Watchdogs.cs b/Source/Watchdogs.cs index 6993bf80..2fd3546b 100644 --- a/Source/Watchdogs.cs +++ b/Source/Watchdogs.cs @@ -6,7 +6,6 @@ namespace RealSolarSystem // from being reverted by other mods when our back is turned. [KSPAddon(KSPAddon.Startup.FlightAndKSC, false)] - public class RSSWatchDog : MonoBehaviour { ConfigNode RSSSettings = null; @@ -18,7 +17,6 @@ public class RSSWatchDog : MonoBehaviour public void Start() { - foreach (ConfigNode node in GameDatabase.Instance.GetConfigNodes("REALSOLARSYSTEM")) RSSSettings = node; @@ -93,14 +91,14 @@ public void Update() { cam.nearClipPlane = nearClip; - Debug.Log("[RealSolarSystem]: Watchdog: Setting camera " + cam.name + " near clip to " + nearClip + " so camera now has " + cam.nearClipPlane); + Debug.Log($"[RealSolarSystem] Watchdog: Setting camera {cam.name} near clip to {nearClip} so camera now has {cam.nearClipPlane}"); } if (farClip > 0) { cam.farClipPlane = farClip; - Debug.Log("[RealSolarSystem]: Watchdog: Setting camera " + cam.name + " far clip to " + farClip + " so camera now has " + cam.farClipPlane); + Debug.Log($"[RealSolarSystem] Watchdog: Setting camera {cam.name} far clip to {farClip} so camera now has {cam.farClipPlane}"); } } } @@ -108,7 +106,6 @@ public void Update() public void OnVesselSOIChanged(GameEvents.HostedFromToAction evt) { watchdogRun = false; - delayCounter = 0; } @@ -124,7 +121,7 @@ private void OnVesselSituationChanged(GameEvents.HostedFromToAction