Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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
48 changes: 47 additions & 1 deletion KSPCommunityFixes/BugFixes/DoubleCurvePreserveTangents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ class DoubleCurvePreserveTangents : BasePatch

protected override void ApplyPatches()
{
AddPatch(PatchType.Transpiler, typeof(DoubleCurve), nameof(DoubleCurve.RecomputeTangents));
if (!KSPCommunityFixes.cleanedDll)
{
AddPatch(PatchType.Transpiler, typeof(DoubleCurve), nameof(DoubleCurve.RecomputeTangents));
}
else
{
AddPatch(PatchType.Prefix, typeof(DoubleCurve), nameof(DoubleCurve.RecomputeTangents));
}
}

static IEnumerable<CodeInstruction> DoubleCurve_RecomputeTangents_Transpiler(IEnumerable<CodeInstruction> instructions)
Expand All @@ -37,5 +44,44 @@ static IEnumerable<CodeInstruction> DoubleCurve_RecomputeTangents_Transpiler(IEn

return code;
}

static bool DoubleCurve_RecomputeTangents_Prefix(DoubleCurve __instance)
Comment thread
R-T-B marked this conversation as resolved.
Outdated
{
int count = __instance.keys.Count;
DoubleKeyframe doubleKeyframe;
if (count == 1)
{
return false;
}
doubleKeyframe = __instance.keys[0];
if (doubleKeyframe.autoTangent)
{
doubleKeyframe.inTangent = 0.0;
doubleKeyframe.outTangent = (__instance.keys[1].value - doubleKeyframe.value) / (__instance.keys[1].time - doubleKeyframe.time);
__instance.keys[0] = doubleKeyframe;
}
int num3 = count - 1;
doubleKeyframe = __instance.keys[num3];
if (doubleKeyframe.autoTangent)
{
doubleKeyframe.inTangent = (doubleKeyframe.value - __instance.keys[num3 - 1].value) / (doubleKeyframe.time - __instance.keys[num3 - 1].value);
doubleKeyframe.outTangent = 0.0;
__instance.keys[num3] = doubleKeyframe;
}
if (count > 2)
{
for (int i = 1; i < num3; i++)
{
doubleKeyframe = __instance.keys[i];
if (doubleKeyframe.autoTangent)
{
double num4 = (doubleKeyframe.value - __instance.keys[i - 1].value) / (doubleKeyframe.time - __instance.keys[i - 1].value);
double num5 = (__instance.keys[i + 1].value - doubleKeyframe.value) / (__instance.keys[i + 1].time - doubleKeyframe.time);
doubleKeyframe.inTangent = (doubleKeyframe.outTangent = (num4 + num5) * 0.5);
}
}
}
return false;
}
}
}
43 changes: 40 additions & 3 deletions KSPCommunityFixes/BugFixes/ExtendedDeployableParts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ protected override void ApplyPatches()
{
AddPatch(PatchType.Transpiler, typeof(ModuleDeployablePart), nameof(ModuleDeployablePart.startFSM));

AddPatch(PatchType.Transpiler, typeof(ModuleDeployableSolarPanel), nameof(ModuleDeployablePart.OnStart));
if (!KSPCommunityFixes.cleanedDll)
{
AddPatch(PatchType.Transpiler, typeof(ModuleDeployableSolarPanel), nameof(ModuleDeployablePart.OnStart));
}
else
{
AddPatch(PatchType.Prefix, typeof(ModuleDeployableSolarPanel), nameof(ModuleDeployablePart.OnStart));
}
}

static IEnumerable<CodeInstruction> ModuleDeployablePart_startFSM_Transpiler(IEnumerable<CodeInstruction> instructions)
Expand Down Expand Up @@ -90,11 +97,10 @@ static IEnumerable<CodeInstruction> ModuleDeployableSolarPanel_OnStart_Transpile
FieldInfo ModuleDeployablePart_deployState = AccessTools.Field(typeof(ModuleDeployablePart), nameof(ModuleDeployablePart.deployState));

List<CodeInstruction> code = new List<CodeInstruction>(instructions);

for (int i = 0; i < code.Count; i++)
{
if (code[i].opcode == OpCodes.Ldarg_0
&& code[i + 1].opcode == OpCodes.Ldfld && ReferenceEquals(code[i+1].operand, ModuleDeployablePart_deployState)
&& code[i + 1].opcode == OpCodes.Ldfld && ReferenceEquals(code[i + 1].operand, ModuleDeployablePart_deployState)
&& code[i + 2].opcode == OpCodes.Brtrue_S)
{
code[i].opcode = OpCodes.Br_S;
Expand All @@ -105,5 +111,36 @@ static IEnumerable<CodeInstruction> ModuleDeployableSolarPanel_OnStart_Transpile

return code;
}
static bool ModuleDeployableSolarPanel_OnStart_Prefix(ModuleDeployableSolarPanel __instance, PartModule.StartState state)
{
if (!HighLogic.LoadedSceneIsEditor && !HighLogic.LoadedSceneIsFlight)
{
return false;
}
GameEvents.onPartRepaired.Add(new EventData<Part>.OnEvent(__instance.OnPartRepaired));
__instance.Fields["flowRate"].guiFormat = __instance.flowFormat;
__instance.Fields["flowRate"].guiUnits = (__instance.flowUnitsUseSpace ? " " : "") + __instance.flowUnits;
__instance.OnStart(state);
if (__instance.secondaryTransform == null)
{
Debug.LogError("Couldn't access secondaryTransform for raycasts");
}
if (__instance.useRaycastForTrackingDot)
{
__instance.trackingDotTransform = __instance.secondaryTransform;
}
else
{
__instance.trackingDotTransform = __instance.panelRotationTransform;
}
if (HighLogic.LoadedSceneIsFlight && __instance.anim != null)
{
float num = ((__instance.deployState == ModuleDeployablePart.DeployState.EXTENDED) ? 1f : 0f);
__instance.anim[__instance.animationName].normalizedTime = num;
__instance.anim[__instance.animationName].enabled = true;
__instance.anim[__instance.animationName].weight = 1f;
}
return false;
}
}
}
114 changes: 87 additions & 27 deletions KSPCommunityFixes/BugFixes/ModuleIndexingMismatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public class ModuleIndexingMismatch : BasePatch
{
private const string VALUENAME_MODULEPARTCONFIGID = "modulePartConfigId";
private static readonly HashSet<string> multiModules = new HashSet<string>();
private static readonly Dictionary<string, Type> allModuleTypes = new Dictionary<string, Type>();

private static readonly Dictionary<string, Type> allModuleTypes = new Dictionary<string, Type>();
protected override Version VersionMin => new Version(1, 8, 0);

protected override void ApplyPatches()
Expand All @@ -44,7 +43,6 @@ protected override void ApplyPatches()
{
AddPatch(PatchType.Transpiler, typeof(ProtoPartSnapshot), "ConfigurePart");
}

AddPatch(PatchType.Transpiler, typeof(ShipConstruct), "LoadShip", new Type[] { typeof(ConfigNode), typeof(uint), typeof(bool), typeof(string).MakeByRefType() });

Type multiModuleType = typeof(IMultipleModuleInPart);
Expand Down Expand Up @@ -208,8 +206,8 @@ static IEnumerable<CodeInstruction> ProtoPartSnapshot_ConfigurePart_Transpiler(I

return code;
}

/// <summary>
/// <summary>
/// Our own version of ProtoPartModuleSnapshot.Load(). We reimplement it because :
/// - Stock would do again all the indice / module matching that we just did
/// - That stock logic would prevent our handling of loading derived into base / base into derived modules.
Expand All @@ -222,6 +220,7 @@ private static void LoadProtoPartSnapshotModule(ProtoPartModuleSnapshot protoMod
protoModule.moduleRef = module;
}


static void LoadModules(ProtoPartSnapshot protoPart, Part part)
Comment thread
R-T-B marked this conversation as resolved.
{
int protoModuleCount = protoPart.modules.Count;
Expand Down Expand Up @@ -379,31 +378,92 @@ static IEnumerable<CodeInstruction> ShipConstruct_LoadShip_Transpiler(IEnumerabl

// first, remove the original module load call
bool originalFound = false;
for (int i = 0; i < code.Count - 6; i++)

if (!KSPCommunityFixes.cleanedDll)
{
//// part.LoadModule(configNode2, ref moduleIndex);
// ldloc.s 6
// ldloc.3
// ldloca.s 39
// callvirt instance class PartModule Part::LoadModule(class ConfigNode, int32&)
// dup
// pop
// pop
if (code[i].opcode == OpCodes.Ldloc_S
&& code[i + 1].opcode == OpCodes.Ldloc_3
&& code[i + 2].opcode == OpCodes.Ldloca_S
&& code[i + 3].opcode == OpCodes.Callvirt && ReferenceEquals(code[i + 3].operand, Part_LoadModule)
&& code[i + 4].opcode == OpCodes.Dup
&& code[i + 5].opcode == OpCodes.Pop
&& code[i + 6].opcode == OpCodes.Pop)
for (int i = 0; i < code.Count - 6; i++)
{
originalFound = true;
for (int j = i; j < i + 7; j++)
//// part.LoadModule(configNode2, ref moduleIndex);
// ldloc.s 6
// ldloc.3
// ldloca.s 39
// callvirt instance class PartModule Part::LoadModule(class ConfigNode, int32&)
// dup
// pop
// pop
if (code[i].opcode == OpCodes.Ldloc_S
&& code[i + 1].opcode == OpCodes.Ldloc_3
&& code[i + 2].opcode == OpCodes.Ldloca_S
&& code[i + 3].opcode == OpCodes.Callvirt && ReferenceEquals(code[i + 3].operand, Part_LoadModule)
&& code[i + 4].opcode == OpCodes.Dup
&& code[i + 5].opcode == OpCodes.Pop
&& code[i + 6].opcode == OpCodes.Pop)
{
code[j].opcode = OpCodes.Nop;
code[j].operand = null;
originalFound = true;
for (int j = i; j < i + 7; j++)
{
code[j].opcode = OpCodes.Nop;
code[j].operand = null;
}
break;
}
}
}
else
{
for (int i = 0; i < code.Count - 5; i++)
{
//// part.LoadModule(configNode2, ref moduleIndex);
// ldloc.s 6
// ldloc.3
// ldloca.s 39
// callvirt instance class PartModule Part::LoadModule(class ConfigNode, int32&)
// pop
// br
if (code[i].opcode == OpCodes.Ldloc_S
&& code[i + 1].opcode == OpCodes.Ldloc_3
&& code[i + 2].opcode == OpCodes.Ldloca_S
&& code[i + 3].opcode == OpCodes.Callvirt && ReferenceEquals(code[i + 3].operand, Part_LoadModule)
&& code[i + 4].opcode == OpCodes.Pop
&& code[i + 5].opcode == OpCodes.Br)
{
originalFound = true;
for (int j = i; j < i + 6; j++)
{
code[j].opcode = OpCodes.Nop;
code[j].operand = null;
}
break;
}
}
if (!originalFound)
{
code = new List<CodeInstruction>(instructions);
for (int i = 0; i < code.Count - 5; i++)
{
//// part.LoadModule(configNode2, ref moduleIndex);
// ldloc.s 6
// ldloc.3
// ldloca.s 39
// callvirt instance class PartModule Part::LoadModule(class ConfigNode, int32&)
// pop
// br
if (code[i].opcode == OpCodes.Ldloc_S
&& code[i + 1].opcode == OpCodes.Ldloc_S
&& code[i + 2].opcode == OpCodes.Ldloca_S
&& code[i + 3].opcode == OpCodes.Callvirt && ReferenceEquals(code[i + 3].operand, Part_LoadModule)
&& code[i + 4].opcode == OpCodes.Pop
&& code[i + 5].opcode == OpCodes.Br_S)
{
originalFound = true;
for (int j = i; j < i + 6; j++)
{
code[j].opcode = OpCodes.Nop;
code[j].operand = null;
}
break;
}
}
break;
}
}

Expand Down Expand Up @@ -611,4 +671,4 @@ static void LoadShipModuleNodes(Part part, ConfigNode partNode)
}
}
}
}
}
83 changes: 79 additions & 4 deletions KSPCommunityFixes/BugFixes/RefundingOnRecovery.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using HarmonyLib;
using KSP.UI.Screens;
using KSP.UI.Screens.SpaceCenter.MissionSummaryDialog;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using HarmonyLib;

namespace KSPCommunityFixes.BugFixes
{
Expand Down Expand Up @@ -56,7 +58,14 @@ class RefundingOnRecovery : BasePatch

protected override void ApplyPatches()
{
AddPatch(PatchType.Transpiler, typeof(Funding), "onVesselRecoveryProcessing");
if (!KSPCommunityFixes.cleanedDll)
{
AddPatch(PatchType.Transpiler, typeof(Funding), "onVesselRecoveryProcessing");
}
else
{
AddPatch(PatchType.Prefix, typeof(Funding), "onVesselRecoveryProcessing");
Comment thread
R-T-B marked this conversation as resolved.
Outdated
}

moduleInventoryPartDerivatives.Clear();
moduleInventoryPartDerivatives.Add(nameof(ModuleInventoryPart));
Expand Down Expand Up @@ -119,11 +128,77 @@ static IEnumerable<CodeInstruction> Funding_onVesselRecoveryProcessing_Transpile
return code;
}

static bool Funding_onVesselRecoveryProcessing_Prefix(Funding __instance, ProtoVessel pv, MissionRecoveryDialog mrDialog, float recoveryScore)
{
if (pv == null)
{
return false;
}
bool flag = mrDialog != null;
double num = 0.0;
List<ProtoPartSnapshot> allProtoPartsIncludingCargo = pv.GetAllProtoPartsIncludingCargo();
for (int i = 0; i < allProtoPartsIncludingCargo.Count; i++)
{
ProtoPartSnapshot protoPartSnapshot = allProtoPartsIncludingCargo[i];
AvailablePart availablePart = null;
if (protoPartSnapshot.partInfo == null)
{
if (!string.IsNullOrEmpty(protoPartSnapshot.partName))
{
availablePart = PartLoader.getPartInfoByName(protoPartSnapshot.partName);
}
}
else
{
availablePart = protoPartSnapshot.partInfo;
}
if (availablePart != null)
{
float num2;
float num3;
ShipConstruction.GetPartCosts(protoPartSnapshot, true, availablePart, out num2, out num3);
num2 *= recoveryScore;
num3 *= recoveryScore;
num += (double)(num2 + num3);
if (flag)
{
if (!string.Equals(availablePart.name, "kerbalEVA"))
{
mrDialog.AddPartWidget(PartWidget.Create(availablePart, num2, num3, mrDialog));
}
for (int j = 0; j < protoPartSnapshot.resources.Count; j++)
{
ProtoPartResourceSnapshot protoPartResourceSnapshot = protoPartSnapshot.resources[j];
PartResourceDefinition definition = PartResourceLibrary.Instance.GetDefinition(protoPartResourceSnapshot.resourceName);
if (definition != null)
{
mrDialog.AddResourceWidget(ResourceWidget.Create(definition, (float)protoPartResourceSnapshot.amount, definition.unitCost * recoveryScore, mrDialog));
}
else
{
UnityEngine.Debug.LogError("[ShipTemplate]: No Resource definition found for " + protoPartResourceSnapshot.resourceName);
}
}
}
}
else if (flag)
{
UnityEngine.Debug.Log("[Funding]: Cannot recover " + protoPartSnapshot.partName + ". Part has no entry in PartLoader catalog. That is only OK if the part is an EVA.");
}
}
if (flag)
{
mrDialog.fundsEarned = num;
}
__instance.AddFunds(num, TransactionReasons.VesselRecovery);
return false;
}


// Derived from the ModuleInventoryPart.OnLoad() code, get stored parts cost
static float GetStoredPartsCosts(ProtoPartSnapshot protoPart)
{
ConstructorInfo storedPartCtor = AccessTools.Constructor(typeof(StoredPart), new[] {typeof(ConfigNode)});
ConstructorInfo storedPartCtor = AccessTools.Constructor(typeof(StoredPart), new[] { typeof(ConfigNode) });

float cost = 0f;
foreach (ProtoPartModuleSnapshot protoModule in protoPart.modules)
Expand Down
Loading