From b9993f722a136df25fb698ecc707d666cd4f13aa Mon Sep 17 00:00:00 2001 From: pasalvetti Date: Sat, 28 Sep 2024 10:30:33 +0200 Subject: [PATCH 1/2] Update fix debris name --- .../DecoupledCraftNameFix.cs | 5 +- .../TrackingStationDebrisNameFix.cs | 73 +++++++++++++++++-- 2 files changed, 70 insertions(+), 8 deletions(-) diff --git a/src/CommunityFixes/Fix/DecoupledCraftNameFix/DecoupledCraftNameFix.cs b/src/CommunityFixes/Fix/DecoupledCraftNameFix/DecoupledCraftNameFix.cs index 23af1d9..a135832 100644 --- a/src/CommunityFixes/Fix/DecoupledCraftNameFix/DecoupledCraftNameFix.cs +++ b/src/CommunityFixes/Fix/DecoupledCraftNameFix/DecoupledCraftNameFix.cs @@ -39,9 +39,10 @@ private void HandleSeparationEvent(VesselComponent vessel1, VesselComponent vess { Logger.LogDebug($"Separated: {vessel1?.Name}, {vessel2?.Name}"); - if (vessel2 is not { Name: var newName } || + if ((vessel2 is not { Name: var newName } || !newName.StartsWith("Default Name") || - string.IsNullOrEmpty(vessel1?.Name)) + string.IsNullOrEmpty(vessel1?.Name)) && + vessel1.Name!=vessel2.Name) { return; } diff --git a/src/CommunityFixes/Fix/TrackingStationDebrisNameFix/TrackingStationDebrisNameFix.cs b/src/CommunityFixes/Fix/TrackingStationDebrisNameFix/TrackingStationDebrisNameFix.cs index b5eb2ea..e926123 100644 --- a/src/CommunityFixes/Fix/TrackingStationDebrisNameFix/TrackingStationDebrisNameFix.cs +++ b/src/CommunityFixes/Fix/TrackingStationDebrisNameFix/TrackingStationDebrisNameFix.cs @@ -1,17 +1,26 @@ using HarmonyLib; using KSP.Map; using System.Text.RegularExpressions; +using Newtonsoft.Json; +using KSP.Sim.impl; +using Castle.Components.DictionaryAdapter.Xml; +using static KSP.Api.UIDataPropertyStrings.View; namespace CommunityFixes.Fix.TrackingStationDebrisNameFix { [Fix("Fix debris name in the tracking station")] internal class TrackingStationDebrisNameFix : BaseFix { + private static SpaceWarp.API.Logging.ILogger _logger; public override void OnInitialized() { + _logger = Logger; HarmonyInstance.PatchAll(typeof(TrackingStationDebrisNameFix)); } + /** + * Postfix the display of debris in the Tracking Station. Instead of displaying 'Debris: ', we display its formal name. + ***/ [HarmonyPatch(typeof(MapUI), nameof(MapUI.HandleDebrisObjectEntryConfigurations))] [HarmonyPostfix] public static void HandleDebrisObjectEntryConfigurationsPostfix( @@ -19,13 +28,65 @@ public static void HandleDebrisObjectEntryConfigurationsPostfix( MapUISelectableEntry obj ) { - string debrisName = ((object)item._itemName).ToString(); - var match = Regex.Match(debrisName, @"-(\d+)$"); - var newName = match.Success - ? Regex.Replace(debrisName, @"-\d+$", "") - : debrisName; - obj.Name = string.Format("Debris of {0}", newName); + obj.Name = ((object)item._itemName).ToString(); } + /** + * Postfix the creation of a new vessel. If it's a debris, we give it an appropriate name. + **/ + [HarmonyPatch(typeof(SpaceSimulation), nameof(SpaceSimulation.CreateVesselSimObjectFromPart))] + [HarmonyPostfix] + public static void CreateVesselSimObjectFromPartPostfix( + PartComponent rootPart, + ref SimulationObjectModel __result + ) + { + System.Diagnostics.Debug.Write("requin"); + VesselComponent vessel = __result.FindComponent(); + if (!vessel._hasCommandModule) + { + renameVessel(vessel, "Unknown Debris"); + } + } + + /** + * Postfix the decoupling of a vessel into two subvessels, renaming the debris (if such vessel exists) and keeping the original name for the subvessel with a command module (in case the original root part ends up being a debris). + **/ + [HarmonyPatch(typeof(SpaceSimulation), nameof(SpaceSimulation.SplitCombinedVesselSimObject))] + [HarmonyPostfix] + public static void SplitCombinedVesselSimObjectPostfix( + VesselComponent combinedVessel, // the vessel with the root part + IGGuid detachingPartId, + ref SimulationObjectModel __result + ) + { + System.Diagnostics.Debug.Write("albatros"); + VesselComponent vessel = __result.FindComponent(); // the new vessel splited from the vessel with the root part + String originalVesselName = combinedVessel.Name.Replace("Debris of ", ""); // recreating the original vessel name by removing 'Debris of' (in case more than one linear decouplings happened at the same time) + renameDebrisVessel(vessel, originalVesselName); + renameDebrisVessel(combinedVessel, originalVesselName); + if (vessel._hasCommandModule) + { + renameVessel(vessel, originalVesselName); // if a command module happens to be in the splitted vessel, we give it the name of the original vessel + } + } + + /** + * Rename the vessel as 'Debris of xxx' if it's a debris. + **/ + private static void renameDebrisVessel(VesselComponent vessel, string originalVesselName) + { + if (vessel._hasCommandModule) return; + renameVessel(vessel, "Debris of " + originalVesselName); + } + + /** + * Rename the vessel with the specified name. + ***/ + private static void renameVessel(VesselComponent vessel, string newName) + { + System.Diagnostics.Debug.Write("Renaming " + vessel.SimulationObject.Name + " to " + newName); + vessel.SimulationObject.Name = newName; + } } } From 86aaf536e701e234026af439f209a27d651123c4 Mon Sep 17 00:00:00 2001 From: pasalvetti Date: Sat, 28 Sep 2024 10:44:45 +0200 Subject: [PATCH 2/2] Removed unused imports --- .../TrackingStationDebrisNameFix.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/CommunityFixes/Fix/TrackingStationDebrisNameFix/TrackingStationDebrisNameFix.cs b/src/CommunityFixes/Fix/TrackingStationDebrisNameFix/TrackingStationDebrisNameFix.cs index e926123..0ada8f8 100644 --- a/src/CommunityFixes/Fix/TrackingStationDebrisNameFix/TrackingStationDebrisNameFix.cs +++ b/src/CommunityFixes/Fix/TrackingStationDebrisNameFix/TrackingStationDebrisNameFix.cs @@ -1,10 +1,6 @@ using HarmonyLib; using KSP.Map; -using System.Text.RegularExpressions; -using Newtonsoft.Json; using KSP.Sim.impl; -using Castle.Components.DictionaryAdapter.Xml; -using static KSP.Api.UIDataPropertyStrings.View; namespace CommunityFixes.Fix.TrackingStationDebrisNameFix {