Skip to content

Commit b9993f7

Browse files
committed
Update fix debris name
1 parent 3fe9aac commit b9993f7

File tree

2 files changed

+70
-8
lines changed

2 files changed

+70
-8
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: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,92 @@
11
using HarmonyLib;
22
using KSP.Map;
33
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;
48

59
namespace CommunityFixes.Fix.TrackingStationDebrisNameFix
610
{
711
[Fix("Fix debris name in the tracking station")]
812
internal class TrackingStationDebrisNameFix : BaseFix
913
{
14+
private static SpaceWarp.API.Logging.ILogger _logger;
1015
public override void OnInitialized()
1116
{
17+
_logger = Logger;
1218
HarmonyInstance.PatchAll(typeof(TrackingStationDebrisNameFix));
1319
}
1420

21+
/**
22+
* Postfix the display of debris in the Tracking Station. Instead of displaying 'Debris: <GUID>', we display its formal name.
23+
***/
1524
[HarmonyPatch(typeof(MapUI), nameof(MapUI.HandleDebrisObjectEntryConfigurations))]
1625
[HarmonyPostfix]
1726
public static void HandleDebrisObjectEntryConfigurationsPostfix(
1827
MapItem item,
1928
MapUISelectableEntry obj
2029
)
2130
{
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();
2832
}
2933

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+
}
3091
}
3192
}

0 commit comments

Comments
 (0)