Skip to content

Instantly share code, notes, and snippets.

@gotmachine
gotmachine / OpenFileDialog
Created October 28, 2020 07:20
P/Invoke OpenFileDialog (no dependency on System.Windows.Forms)
public class OpenFileDialog
{
[DllImport("comdlg32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private class OpenFileName
{
public int structSize = 0;
public IntPtr dlgOwner = IntPtr.Zero;
@gotmachine
gotmachine / PhysicsHold.cs
Last active December 5, 2020 04:11
PhysicsHold : KSP proof of concept mod for making landed vessels physicsless.
using KSP.UI.Screens.Flight;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using UnityEngine;
using Debug = UnityEngine.Debug;
/*
@gotmachine
gotmachine / ResourceBrokerPatch.cs
Created March 3, 2021 13:50
Kerbalism ResourceBroker patch
// Experimental patch that force all BaseConverter derivatives to use the Kerbalism resource simulation, intended for NFE/FFT/SystemHeat support
// This will get ride of the "incoherent producer" message on loaded vessels, but there are some limitations :
// - This bypass resource flow priorities and modes, excepted in the case of a NO_FLOW resource.
// Specifically, NFE reactors should act correctly as far as uranium is concerned.
// - This use the last sim step information to attempt to compensate the stock high timewarp speeds issues,
// but since this works outside of the main kerbalism recipes loop, this will likely still act in weird ways
// when a mix of stock and kerbalism producers/consumers are active for the same resource.
// - This require renaming Kerbalism's own ResourceBroker class to something like KsmResourceBroker so harmony doesn't get confused
using System;
@gotmachine
gotmachine / KSPRecoveryFix.cs
Created March 22, 2021 14:42
Fix for KSP 1.11+ not accounting for modules implementing IPartCostModifier when refunding parts on vessel recovery
using System;
using KSP.Localization;
using KSP.UI.Screens;
using KSP.UI.Screens.SpaceCenter.MissionSummaryDialog;
using KSP.UI.Util;
using UnityEngine;
namespace TestPlugin
{
[KSPAddon(KSPAddon.Startup.AllGameScenes, false)]
@gotmachine
gotmachine / KSPCollapsedPAWInventoriesHarmonyPatch.cs
Last active June 30, 2021 09:28
KSP Collapsed PAW Inventories HarmonyPatch
// Add a collapsed by default PAW group to all ModuleInventoryPart :
[HarmonyPatch(typeof(ModuleInventoryPart))]
[HarmonyPatch("OnStart")]
class ModuleInventoryPart_OnStart
{
static void Postfix(ModuleInventoryPart __instance)
{
__instance.Fields["InventorySlots"].group = new BasePAWGroup("Inventory", "Inventory", true);
}
@gotmachine
gotmachine / KSPInventories.cs
Created June 30, 2021 19:43
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;
}
@gotmachine
gotmachine / CustomPartActionControl.cs
Created November 18, 2021 23:59
Create a completely custom PAW control for KSP
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
namespace UINumericFloatEdit
{
[KSPAddon(KSPAddon.Startup.FlightAndEditor, false)]
public class CustomPartActionControlSpawner : MonoBehaviour
{
@gotmachine
gotmachine / KSPVesselOcclusionTester.cs
Created December 29, 2021 11:14
Compute each part visible surface area from an arbitrary direction, using a custom shader and a render texture, and analyzing the results through a bursted Job.
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using UnityEngine;
using UnityEngine.Profiling;
using UnityEngine.Rendering;
@gotmachine
gotmachine / ksp_debugging.md
Last active October 18, 2024 14:52
Debugging and profiling KSP plugins

This guide applies only to KSP 1.8 and latter. It covers modifying a KSP installation to allow :

  • In IDE debugging of KSP plugins by using breakpoints, inspecting runtime variables, doing step-by-step execution, etc. Both Visual Studio (including VS for Mac) and JetBrains Rider support debugging Unity games, and by extension KSP plugins.
  • Using the Unity editor profiling tools, which include great tools to measure and analyze CPU/GPU usage and memory allocations.

This guide is extensively tested for a Windows / Visual Studio scenario. However, it is theoretically possible to make all that work under MacOS or Linux, either with the Rider IDE or Visual Studio for Mac. This guide has limited details about those scenarios. I encourage you to leave a comment if you have additional information / experience.

Modifying KSP for profiling/debugging

Downloading the Unity editor

@gotmachine
gotmachine / CustomAssemblyLoader.cs
Last active May 14, 2022 09:51
Manual assembly loading for KSP
using System;
using System.IO;
using System.Reflection;
using System.Linq;
using UnityEngine;
namespace KSPCommunityFixes
{
[KSPAddon(KSPAddon.Startup.Instantly, true)]
public class MyAssemblyLoader : MonoBehaviour