-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix debris name
- Loading branch information
Showing
2 changed files
with
67 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 64 additions & 7 deletions
71
src/CommunityFixes/Fix/TrackingStationDebrisNameFix/TrackingStationDebrisNameFix.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |