Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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;
}
}
}
128 changes: 98 additions & 30 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 All @@ -412,9 +472,17 @@ static IEnumerable<CodeInstruction> ShipConstruct_LoadShip_Transpiler(IEnumerabl
Debug.LogError($"Error applying ModuleIndexingMismatch patch : couldn't find Part.LoadModule() call in ShipConstruct.LoadShip()");
return instructions;
}

// then, insert our own module loading call before the original part nodes parsing loop
for (int i = 0; i < code.Count - 6; i++)
uint minusCount;
if (!KSPCommunityFixes.cleanedDll)
{
minusCount = 6;
}
else
{
minusCount = 5;
}
// then, insert our own module loading call before the original part nodes parsing loop
for (int i = 0; i < code.Count - minusCount; i++)
{
//// int moduleIndex = 0;
// ldc.i4.0 NULL
Expand Down Expand Up @@ -611,4 +679,4 @@ static void LoadShipModuleNodes(Part part, ConfigNode partNode)
}
}
}
}
}
Loading