Skip to content

Instantly share code, notes, and snippets.

@gotmachine
Created June 30, 2021 19:43
Show Gist options
  • Save gotmachine/1e4c5d9fbb272e363c6fd43794706e54 to your computer and use it in GitHub Desktop.
Save gotmachine/1e4c5d9fbb272e363c6fd43794706e54 to your computer and use it in GitHub Desktop.
KSP : messing around with inventories
[HarmonyPatch(typeof(UIPartActionInventorySlot))]
[HarmonyPatch("OnPointerClick")]
class UIPartActionInventorySlot_OnPointerClick
{
static bool Prefix(UIPartActionInventorySlot __instance, PointerEventData eventData, ModuleInventoryPart ___moduleInventoryPart, InventoryPartListTooltipController ___tooltipController)
{
if (!(___moduleInventoryPart is ModuleActiveInventoryPart activeInventory))
{
return true;
}
if (HighLogic.LoadedSceneIsFlight && eventData.button == PointerEventData.InputButton.Right && !UIPartActionController.Instance.InventoryAndCargoPartExist())
{
if (__instance.slotIndex > -1 && activeInventory.storedParts.ContainsKey(__instance.slotIndex))
{
StoredPart storedPart = activeInventory.storedParts[__instance.slotIndex];
if (activeInventory.activeParts.TryGetValue(storedPart, out Part activePart))
{
UIPartActionController.Instance.SpawnPartActionWindow(activePart);
UIPartActionControllerInventory.Instance.DestroyTooltip(); // doesn't work...
return false;
}
}
}
return true;
}
}
public class ModuleActiveCargoPart : ModuleCargoPart
{
public Part parentPart;
public override void OnFixedUpdate()
{
base.OnFixedUpdate();
if (parentPart != null && HighLogic.LoadedSceneIsFlight)
{
part.transform.rotation = parentPart.transform.rotation;
part.transform.position = parentPart.transform.position;
part.attPos0 = parentPart.transform.localPosition;
part.attRotation0 = parentPart.transform.localRotation;
}
}
public override void OnLoad(ConfigNode node)
{
base.OnLoad(node);
stackableQuantity = 1;
}
}
public class ModuleActiveInventoryPart : ModuleInventoryPart
{
public Dictionary<StoredPart, Part> activeParts = new Dictionary<StoredPart, Part>();
public override void OnStart(StartState state)
{
base.OnStart(state);
if (!HighLogic.LoadedSceneIsFlight)
{
return;
}
foreach (StoredPart storedPart in storedParts.Values)
{
if (!storedPart.snapshot.partPrefab.HasModuleImplementing<ModuleActiveCargoPart>())
{
continue;
}
Part newPart = storedPart.snapshot.CreatePart();
newPart.partInfo = storedPart.snapshot.partInfo;
newPart.physicalSignificance = Part.PhysicalSignificance.NONE;
newPart.transform.rotation = part.transform.rotation;
newPart.transform.position = part.transform.position;
newPart.attPos0 = part.transform.localPosition;
newPart.attRotation0 = part.transform.localRotation;
for (int i = 0; i < newPart.transform.childCount; i++)
{
Destroy(newPart.transform.GetChild(i).gameObject);
}
newPart.vessel = vessel;
if (newPart.flightID == 0)
{
newPart.flightID = ShipConstruction.GetUniqueFlightID(HighLogic.CurrentGame.flightState);
}
newPart.enabled = true; // Call Start/OnStart
newPart.InitializeModules();
ModuleActiveCargoPart activeCargoModule = newPart.FindModuleImplementing<ModuleActiveCargoPart>();
activeCargoModule.parentPart = part;
activeParts.Add(storedPart, newPart);
}
}
public override void OnSave(ConfigNode node)
{
// ignore kerbal inventories
if (vessel != null)
{
foreach (KeyValuePair<StoredPart, Part> activePart in activeParts)
{
activePart.Key.snapshot = new ProtoPartSnapshot(activePart.Value, vessel.protoVessel);
}
}
base.OnSave(node);
}
public new void OnDestroy()
{
base.OnDestroy();
foreach (Part activePart in activeParts.Values)
{
Destroy(activePart);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment