Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance optimization: support eliminating some extraneous saving by KSP #80

Merged
merged 1 commit into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions GameData/KSPCommunityFixes/Settings.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@ KSP_COMMUNITY_FIXES
packedInterval = 0.5 // interval when the active vessel is timewarping
}

// This tweak eliminates KSP's stock behavior of saving every time
// you exit a UI-only space center building (AC, MC, etc)
// and every time you delete a vessel in the Tracking Station
FewerSaves = false

// ##########################
// Modding
// ##########################
Expand Down
1 change: 1 addition & 0 deletions KSPCommunityFixes/KSPCommunityFixes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
<Compile Include="Modding\DockingPortLockedEvents.cs" />
<Compile Include="Modding\OnSymmetryFieldChanged.cs" />
<Compile Include="Modding\ReflectionTypeLoadExceptionHandler.cs" />
<Compile Include="Performance\FewerSaves.cs" />
<Compile Include="Performance\PQSUpdateNoMemoryAlloc.cs" />
<Compile Include="Performance\ProgressTrackingSpeedBoost.cs" />
<Compile Include="QoL\AutostrutActions.cs" />
Expand Down
108 changes: 108 additions & 0 deletions KSPCommunityFixes/Performance/FewerSaves.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using HarmonyLib;
using System.Collections.Generic;
using UnityEngine;
using KSP.UI.Screens;
using KSP.UI;
using System.Reflection.Emit;

namespace KSPCommunityFixes
{
public class FewerSaves : BasePatch
{
protected override Version VersionMin => new Version(1, 8, 0);

protected override void ApplyPatches(List<PatchInfo> patches)
{
patches.Add(new PatchInfo(
PatchMethodType.Prefix,
AccessTools.Method(typeof(ACSceneSpawner), nameof(ACSceneSpawner.onACDespawn)),
this));

patches.Add(new PatchInfo(
PatchMethodType.Prefix,
AccessTools.Method(typeof(AdministrationSceneSpawner), nameof(AdministrationSceneSpawner.onAdminDespawn)),
this));

patches.Add(new PatchInfo(
PatchMethodType.Prefix,
AccessTools.Method(typeof(MCSceneSpawner), nameof(MCSceneSpawner.OnMCDespawn)),
this));

patches.Add(new PatchInfo(
PatchMethodType.Prefix,
AccessTools.Method(typeof(RDSceneSpawner), nameof(RDSceneSpawner.onRDDespawn)),
this));

patches.Add(new PatchInfo(
PatchMethodType.Transpiler,
AccessTools.Method(typeof(SpaceTracking), "OnVesselDeleteConfirm"),
this));
}

static bool ACSceneSpawner_onACDespawn_Prefix(ACSceneSpawner __instance)
{
UIMasterController.Instance.RemoveCanvas(__instance.ACScreenPrefab);
MusicLogic.fetch.UnpauseWithCrossfade();
return false;
}

static bool AdministrationSceneSpawner_onAdminDespawn_Prefix(AdministrationSceneSpawner __instance)
{
UIMasterController.Instance.RemoveCanvas(__instance.AdministrationScreenPrefab);
MusicLogic.fetch.UnpauseWithCrossfade();
return false;
}

static bool MCSceneSpawner_OnMCDespawn_Prefix(MCSceneSpawner __instance)
{
UIMasterController.Instance.RemoveCanvas(__instance.missionControlPrefab);
MusicLogic.fetch.UnpauseWithCrossfade();
return false;
}

static bool RDSceneSpawner_onRDDespawn_Prefix(RDSceneSpawner __instance)
{
UIMasterController.Instance.RemoveCanvas(__instance.RDScreenPrefab);
RenderSettings.defaultReflectionMode = __instance.oldReflectionMode;
RenderSettings.customReflection = __instance.oldReflection;
MusicLogic.fetch.UnpauseWithCrossfade();
return false;
}

static IEnumerable<CodeInstruction> SpaceTracking_OnVesselDeleteConfirm_Transpiler(IEnumerable<CodeInstruction> instructions)
{
int startIndex = -1;
int endIndex = -1;

var codes = new List<CodeInstruction>(instructions);

for (int i = 0; i < codes.Count; i++)
{
if (codes[i].opcode == OpCodes.Ldstr &&
codes[i].operand as string == "persistent")
{
startIndex = i;

for (int j = startIndex; j < codes.Count; j++)
{
if (codes[j].opcode == OpCodes.Ldarg_0)
{
endIndex = j;
break;
}
}
break;
}
}

if (startIndex > -1 && endIndex > -1)
{
// Cuts out the section about GamePersistence.SaveGame()
codes.RemoveRange(startIndex, endIndex - startIndex);
}

return codes;
}
}
}