From ca59161caa82223074882cf283d406fc42f88e5b Mon Sep 17 00:00:00 2001 From: siimav Date: Tue, 23 Jul 2024 00:57:41 +0300 Subject: [PATCH] Some refactoring and cleanup --- Source/CameraFixer.cs | 6 +++--- Source/GrassSeasoner.cs | 34 ++++++++++++++++------------------ Source/KSCLoader.cs | 6 +++--- Source/KSCSiteManager.cs | 4 ++-- Source/KSCSwitcher.cs | 14 +++++++------- 5 files changed, 31 insertions(+), 33 deletions(-) diff --git a/Source/CameraFixer.cs b/Source/CameraFixer.cs index 6556ed7..c5e2580 100644 --- a/Source/CameraFixer.cs +++ b/Source/CameraFixer.cs @@ -68,7 +68,7 @@ public void Start() if (ksc == null) { - Debug.Log("KSCSwitcher could not find KSC to fix the camera."); + Debug.Log("[KSCSwitcher] could not find KSC to fix the camera."); return; } foreach (var cam in Resources.FindObjectsOfTypeAll()) @@ -78,7 +78,7 @@ public void Start() CelestialBody kerbin = FlightGlobals.Bodies.Find(body => body.name == ksc.sphere.name); if (kerbin == null) { - Debug.Log("KSCSwitcher could not find find the CelestialBody specified as KSC's sphere."); + Debug.Log("[KSCSwitcher] could not find find the CelestialBody specified as KSC's sphere."); return; } double nomHeight = kerbin.pqsController.GetSurfaceHeight(ksc.repositionRadial.normalized) - kerbin.Radius; @@ -93,7 +93,7 @@ public void Start() cam.altitudeInitial = 0f - (float)ksc.repositionRadiusOffset; } cam.ResetCamera(); - Debug.Log("KSCSwitcher fixed the Space Center camera."); + Debug.Log("[KSCSwitcher] fixed the Space Center camera."); } } } diff --git a/Source/GrassSeasoner.cs b/Source/GrassSeasoner.cs index cb0d426..7b9bdbc 100644 --- a/Source/GrassSeasoner.cs +++ b/Source/GrassSeasoner.cs @@ -4,10 +4,10 @@ namespace regexKSP { - public class GrassSeasoner + public static class GrassSeasoner { public static Material[] KSCGrassMaterials { get; private set; } = null; - public static GrassSeasoner Instance; + public static Color GroundColor { get @@ -32,24 +32,22 @@ public static void SetGrassColor(Color newColor) public static bool TryGetKSCGrassColor(CelestialBody home, ConfigNode pqsCity, out Color col) { col = new Color(); - if (pqsCity.HasValue("changeGrassColor")) + if (pqsCity?.HasValue("changeGrassColor") ?? false && + bool.TryParse(pqsCity.GetValue("changeGrassColor"), out bool btmp) && btmp) { - if (bool.TryParse(pqsCity.GetValue("changeGrassColor"), out bool btmp) && btmp) + if (pqsCity.HasValue("grassColor")) { - if (pqsCity.HasValue("grassColor")) - { - if (pqsCity.TryGetValue("grassColor", ref col)) - { - Debug.Log($"KSCSwitcher found KSC grass color {col} from config"); - return true; - } - } - else if (double.TryParse(pqsCity.GetValue("latitude"), out double lat) && double.TryParse(pqsCity.GetValue("longitude"), out double lon)) + if (pqsCity.TryGetValue("grassColor", ref col)) { - if(TryParseGroundColor(home, lat, lon, out col, 2f)) - return true; + Debug.Log($"[KSCSwitcher] found KSC grass color {col} from config"); + return true; } } + else if (double.TryParse(pqsCity.GetValue("latitude"), out double lat) && double.TryParse(pqsCity.GetValue("longitude"), out double lon)) + { + if (TryParseGroundColor(home, lat, lon, out col, 2f)) + return true; + } } return false; } @@ -83,7 +81,7 @@ public static bool TryParseGroundColor(CelestialBody body, double lat, double lo y = Mathf.Clamp(y, 0, texture.Height); col = texture.GetPixelColor(x, y); - Debug.Log($"KSCSwitcher parsed {col} from color map at {x}, {y}"); + Debug.Log($"[KSCSwitcher] parsed {col} from color map at {x}, {y}"); col *= colorMult; col.a = 1; return true; @@ -117,7 +115,7 @@ public class EditorGrassFixer : MonoBehaviour { public void Start() { - Debug.Log($"KSCSwitcher editor grass fixer start"); + Debug.Log($"[KSCSwitcher] editor grass fixer start"); GameObject scenery = GameObject.Find("VABscenery") ?? GameObject.Find("SPHscenery"); Material material = scenery?.GetChild("ksc_terrain")?.GetComponent()?.sharedMaterial; @@ -129,7 +127,7 @@ public void Start() return; material.color = c * 1.5f; - Debug.Log($"KSCSwitcher editor grass fixer end"); + Debug.Log($"[KSCSwitcher] editor grass fixer end"); } } } diff --git a/Source/KSCLoader.cs b/Source/KSCLoader.cs index 7bb9862..6855f93 100644 --- a/Source/KSCLoader.cs +++ b/Source/KSCLoader.cs @@ -45,7 +45,7 @@ private void OnGameStateCreated(Game game) else { KSCSwitcher.SetSiteAndResetCamera(site); - Debug.Log("KSCSwitcher set the launch site to the last site, " + l.lastSite); + Debug.Log("[KSCSwitcher] set the launch site to the last site, " + l.lastSite); return; } } @@ -61,13 +61,13 @@ private void OnGameStateCreated(Game game) ConfigNode site = Sites.GetSiteByName(Sites.defaultSite); if (site == null) { - Debug.LogError("KSCSwitcher found a default site name but could not retrieve the site config: " + Sites.defaultSite); + Debug.LogError("[KSCSwitcher] found a default site name but could not retrieve the site config: " + Sites.defaultSite); return; } else { KSCSwitcher.SetSiteAndResetCamera(site); - Debug.Log("KSCSwitcher set the initial launch site to the default" + Sites.defaultSite); + Debug.Log("[KSCSwitcher] set the initial launch site to the default" + Sites.defaultSite); } } } diff --git a/Source/KSCSiteManager.cs b/Source/KSCSiteManager.cs index b6929df..fddc4e0 100644 --- a/Source/KSCSiteManager.cs +++ b/Source/KSCSiteManager.cs @@ -76,10 +76,10 @@ public KSCSiteManager() } else { - Debug.Log("KSCSwitcher No LaunchSites node found!"); + Debug.Log("[KSCSwitcher] No LaunchSites node found!"); } - Debug.Log("KSCSwitcher loaded " + Sites.Count + " launch sites."); + Debug.Log("[KSCSwitcher] loaded " + Sites.Count + " launch sites."); } public ConfigNode GetSiteByName(string name) diff --git a/Source/KSCSwitcher.cs b/Source/KSCSwitcher.cs index b295a03..f2a67d3 100644 --- a/Source/KSCSwitcher.cs +++ b/Source/KSCSwitcher.cs @@ -277,7 +277,7 @@ public static bool SetSiteAndResetCamera(ConfigNode KSC, bool force = false) { FloatingOrigin.SetOffset(PSystemSetup.Instance.SCTransform.position); LastFloatingOriginKSC = KSC.GetValue("name"); - Debug.Log("KSCSwitcher set floating point origin offset"); + Debug.Log("[KSCSwitcher] set floating point origin offset"); } return b; @@ -303,7 +303,7 @@ public static bool SetSite(ConfigNode KSC) { if (!ksc.name.Equals(pqsCity.GetValue("KEYname"))) { - Debug.Log("KSCSwitcher: Could not retrieve KSC to move, reporting failure and moving on."); + Debug.Log("[KSCSwitcher] Could not retrieve KSC to move, reporting failure and moving on."); return false; } } @@ -375,7 +375,7 @@ public static bool SetSite(ConfigNode KSC) } } - if(GrassSeasoner.TryGetKSCGrassColor(home, pqsCity, out Color col)) + if (GrassSeasoner.TryGetKSCGrassColor(home, pqsCity, out Color col)) { GrassSeasoner.SetGrassColor(col); } @@ -395,7 +395,7 @@ public static bool SetSite(ConfigNode KSC) } else { - Debug.LogError("KSCSwitcher: Could not retrieve KSC to move, reporting failure and moving on."); + Debug.LogError("[KSCSwitcher] Could not retrieve KSC to move, reporting failure and moving on."); return false; } @@ -476,7 +476,7 @@ private void SetSite(LaunchSite newSite) private void FocusOnSite(Vector2d loc) { - Debug.Log("Focusing on site"); + Debug.Log("[KSCSwitcher] Focusing on site"); PlanetariumCamera camera = PlanetariumCamera.fetch; CelestialBody Kerbin = KSCBody; Vector3d point = ScaledSpace.LocalToScaledSpace(Kerbin.GetWorldSurfacePosition(loc.x, loc.y, 0)); @@ -517,9 +517,9 @@ private void LoadTextures() eyeButtonHighlight = GameDatabase.Instance.GetTexture("KSCSwitcher/Plugins/Icons/eye-highlight", false); magButtonNormal = GameDatabase.Instance.GetTexture("KSCSwitcher/Plugins/Icons/magnifier-normal", false); } - catch (Exception e) + catch (Exception ex) { - Debug.Log("Could not load button textures for KSCSwitcher, reverting to old button style: " + e.StackTrace); + Debug.LogError("[KSCSwitcher] Could not load button textures for KSCSwitcher, reverting to old button style: " + ex); oldButton = true; }