|
1 | 1 | using HarmonyLib; |
2 | 2 | using KSP.Map; |
3 | | -using System.Text.RegularExpressions; |
| 3 | +using KSP.Sim.impl; |
4 | 4 |
|
5 | 5 | namespace CommunityFixes.Fix.TrackingStationDebrisNameFix |
6 | 6 | { |
7 | 7 | [Fix("Fix debris name in the tracking station")] |
8 | 8 | internal class TrackingStationDebrisNameFix : BaseFix |
9 | 9 | { |
| 10 | + private static SpaceWarp.API.Logging.ILogger _logger; |
10 | 11 | public override void OnInitialized() |
11 | 12 | { |
| 13 | + _logger = Logger; |
12 | 14 | HarmonyInstance.PatchAll(typeof(TrackingStationDebrisNameFix)); |
13 | 15 | } |
14 | 16 |
|
| 17 | + /** |
| 18 | + * Postfix the display of debris in the Tracking Station. Instead of displaying 'Debris: <GUID>', we display its formal name. |
| 19 | + ***/ |
15 | 20 | [HarmonyPatch(typeof(MapUI), nameof(MapUI.HandleDebrisObjectEntryConfigurations))] |
16 | 21 | [HarmonyPostfix] |
17 | 22 | public static void HandleDebrisObjectEntryConfigurationsPostfix( |
18 | 23 | MapItem item, |
19 | 24 | MapUISelectableEntry obj |
20 | 25 | ) |
21 | 26 | { |
22 | | - string debrisName = ((object)item._itemName).ToString(); |
23 | | - var match = Regex.Match(debrisName, @"-(\d+)$"); |
24 | | - var newName = match.Success |
25 | | - ? Regex.Replace(debrisName, @"-\d+$", "") |
26 | | - : debrisName; |
27 | | - obj.Name = string.Format("Debris of {0}", newName); |
| 27 | + obj.Name = ((object)item._itemName).ToString(); |
28 | 28 | } |
29 | 29 |
|
| 30 | + /** |
| 31 | + * Postfix the creation of a new vessel. If it's a debris, we give it an appropriate name. |
| 32 | + **/ |
| 33 | + [HarmonyPatch(typeof(SpaceSimulation), nameof(SpaceSimulation.CreateVesselSimObjectFromPart))] |
| 34 | + [HarmonyPostfix] |
| 35 | + public static void CreateVesselSimObjectFromPartPostfix( |
| 36 | + PartComponent rootPart, |
| 37 | + ref SimulationObjectModel __result |
| 38 | + ) |
| 39 | + { |
| 40 | + System.Diagnostics.Debug.Write("requin"); |
| 41 | + VesselComponent vessel = __result.FindComponent<VesselComponent>(); |
| 42 | + if (!vessel._hasCommandModule) |
| 43 | + { |
| 44 | + renameVessel(vessel, "Unknown Debris"); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * 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). |
| 50 | + **/ |
| 51 | + [HarmonyPatch(typeof(SpaceSimulation), nameof(SpaceSimulation.SplitCombinedVesselSimObject))] |
| 52 | + [HarmonyPostfix] |
| 53 | + public static void SplitCombinedVesselSimObjectPostfix( |
| 54 | + VesselComponent combinedVessel, // the vessel with the root part |
| 55 | + IGGuid detachingPartId, |
| 56 | + ref SimulationObjectModel __result |
| 57 | + ) |
| 58 | + { |
| 59 | + System.Diagnostics.Debug.Write("albatros"); |
| 60 | + VesselComponent vessel = __result.FindComponent<VesselComponent>(); // the new vessel splited from the vessel with the root part |
| 61 | + 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) |
| 62 | + renameDebrisVessel(vessel, originalVesselName); |
| 63 | + renameDebrisVessel(combinedVessel, originalVesselName); |
| 64 | + if (vessel._hasCommandModule) |
| 65 | + { |
| 66 | + renameVessel(vessel, originalVesselName); // if a command module happens to be in the splitted vessel, we give it the name of the original vessel |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Rename the vessel as 'Debris of xxx' if it's a debris. |
| 72 | + **/ |
| 73 | + private static void renameDebrisVessel(VesselComponent vessel, string originalVesselName) |
| 74 | + { |
| 75 | + if (vessel._hasCommandModule) return; |
| 76 | + renameVessel(vessel, "Debris of " + originalVesselName); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Rename the vessel with the specified name. |
| 81 | + ***/ |
| 82 | + private static void renameVessel(VesselComponent vessel, string newName) |
| 83 | + { |
| 84 | + System.Diagnostics.Debug.Write("Renaming " + vessel.SimulationObject.Name + " to " + newName); |
| 85 | + vessel.SimulationObject.Name = newName; |
| 86 | + } |
30 | 87 | } |
31 | 88 | } |
0 commit comments