Skip to content

Instantly share code, notes, and snippets.

@gotmachine
gotmachine / FastFloatCurve.cs
Last active September 16, 2024 13:42
C# cubic hermite spline implementation similar to an Unity AnimationCurve, but faster
/// <summary>
/// A collection of keys mapping times to values, interpolated between keys by a cubic Hermite spline. <para/>
/// The implementation produce identical results as a UnityEngine.AnimationCurve, but calling Evaluate() is at least twice faster. <para/>
/// However, it doesn't support keys in/out weights, and behavior is always identical to WrapMode.ClampForever.
/// </summary>
public class FastFloatCurve : IEnumerable<FastFloatCurve.Key>, IConfigNode
{
/// <summary>
/// Define how a key tangent will be adjusted.
/// </summary>
@gotmachine
gotmachine / KSPDynamicPAWItem.cs
Created May 19, 2024 20:50
KSP Dynamic PAW Items
public class DynamicPAWModule : PartModule
{
private DynamicPAWToggle mainToggle;
private DynamicPAWToggle auxToggle;
private BasePAWGroup group;
public override void OnStart(StartState state)
{
group = new BasePAWGroup(nameof(DynamicPAWModule), "My Dynamic Module", false);
mainToggle = new DynamicPAWToggle(this, UI_Scene.All, group);
@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 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 / 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;