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