Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-bures committed Sep 28, 2024
2 parents 9383072 + 97aee9e commit 5b9afee
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,88 @@
using HarmonyLib;
using KSP.Map;
using System.Text.RegularExpressions;
using KSP.Sim.impl;

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: <GUID>', we display its formal name.
***/
[HarmonyPatch(typeof(MapUI), nameof(MapUI.HandleDebrisObjectEntryConfigurations))]
[HarmonyPostfix]
public static void HandleDebrisObjectEntryConfigurationsPostfix(
MapItem item,
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<VesselComponent>();
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<VesselComponent>(); // 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;
}
}
}

0 comments on commit 5b9afee

Please sign in to comment.