Skip to content

Commit 5b9afee

Browse files
committed
Merge remote-tracking branch 'origin/dev' into dev
2 parents 9383072 + 97aee9e commit 5b9afee

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed

src/CommunityFixes/Fix/DecoupledCraftNameFix/DecoupledCraftNameFix.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ private void HandleSeparationEvent(VesselComponent vessel1, VesselComponent vess
3939
{
4040
Logger.LogDebug($"Separated: {vessel1?.Name}, {vessel2?.Name}");
4141

42-
if (vessel2 is not { Name: var newName } ||
42+
if ((vessel2 is not { Name: var newName } ||
4343
!newName.StartsWith("Default Name") ||
44-
string.IsNullOrEmpty(vessel1?.Name))
44+
string.IsNullOrEmpty(vessel1?.Name)) &&
45+
vessel1.Name!=vessel2.Name)
4546
{
4647
return;
4748
}
Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,88 @@
11
using HarmonyLib;
22
using KSP.Map;
3-
using System.Text.RegularExpressions;
3+
using KSP.Sim.impl;
44

55
namespace CommunityFixes.Fix.TrackingStationDebrisNameFix
66
{
77
[Fix("Fix debris name in the tracking station")]
88
internal class TrackingStationDebrisNameFix : BaseFix
99
{
10+
private static SpaceWarp.API.Logging.ILogger _logger;
1011
public override void OnInitialized()
1112
{
13+
_logger = Logger;
1214
HarmonyInstance.PatchAll(typeof(TrackingStationDebrisNameFix));
1315
}
1416

17+
/**
18+
* Postfix the display of debris in the Tracking Station. Instead of displaying 'Debris: <GUID>', we display its formal name.
19+
***/
1520
[HarmonyPatch(typeof(MapUI), nameof(MapUI.HandleDebrisObjectEntryConfigurations))]
1621
[HarmonyPostfix]
1722
public static void HandleDebrisObjectEntryConfigurationsPostfix(
1823
MapItem item,
1924
MapUISelectableEntry obj
2025
)
2126
{
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();
2828
}
2929

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+
}
3087
}
3188
}

0 commit comments

Comments
 (0)