Skip to content

Instantly share code, notes, and snippets.

@gotmachine
gotmachine / SerializablePartModule.cs
Created March 18, 2024 21:47
Custom serialization for partmodules
[AttributeUsage(AttributeTargets.Field)]
public class PartModuleSerializeAttribute : Attribute { }
public class SerializablePartModule : PartModule, ISerializationCallbackReceiver
{
private static readonly Dictionary<Type, SerializablePartModuleInfo> accessCache = new Dictionary<Type, SerializablePartModuleInfo>();
private static readonly BinaryFormatter bf = new BinaryFormatter();
[SerializeField] private byte[] serializedData;
[SerializeField] private int[] offsets;
@gotmachine
gotmachine / VectrosityCameraHandler.cs
Last active April 25, 2023 09:22
Enable Vectrosity in the KSP flight scene
using System.Collections;
using UnityEngine;
using Vectrosity;
using Camera = UnityEngine.Camera;
namespace VectrosityCameraHandler
{
[KSPAddon(KSPAddon.Startup.AllGameScenes, false)]
[DefaultExecutionOrder(9000)]
public class VectrosityCameraHandler : MonoBehaviour
@gotmachine
gotmachine / KSPPAWRefresh.cs
Created March 2, 2023 13:12
KSP PAW refresh
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
namespace Utils
{
public static class PAWUtils
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
@gotmachine
gotmachine / ShaderReplacer.cs
Created June 10, 2022 11:48
Harmony - Replace a method call in KSP and all loaded plugins
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using HarmonyLib;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Rocks;
using MonoMod.Utils;
@gotmachine
gotmachine / UnityObjectNullExtensions.cs
Created May 20, 2022 11:53
Extension methods for easier, faster more clear null and destroyed state checking for UnityEngine.Object
using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
#pragma warning disable IDE0041 // Use 'is null' check
namespace UnityObjectNullExtensions
{
public static class UnityObjectNullExtensionsSafe
@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
@gotmachine
gotmachine / ksp_debugging.md
Last active February 15, 2024 19:47
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 / 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 / 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 / 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;
}