From 7a04efba50c1f2606dae0edc6c4d42bc1662410c Mon Sep 17 00:00:00 2001 From: gotmachine <24925209+gotmachine@users.noreply.github.com> Date: Wed, 29 Jun 2022 23:53:52 +0200 Subject: [PATCH] New patch : RescaledRoboticParts, fix rescaled servo parts progating their scale to child parts in the editor (issue #48). Version bump to 1.18 --- .../KSPCommunityFixes.version | 2 +- GameData/KSPCommunityFixes/Settings.cfg | 3 + .../BugFixes/RescaledRoboticParts.cs | 63 +++++++++++++++++++ KSPCommunityFixes/KSPCommunityFixes.csproj | 1 + KSPCommunityFixes/Properties/AssemblyInfo.cs | 4 +- README.md | 14 +++-- 6 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 KSPCommunityFixes/BugFixes/RescaledRoboticParts.cs diff --git a/GameData/KSPCommunityFixes/KSPCommunityFixes.version b/GameData/KSPCommunityFixes/KSPCommunityFixes.version index 273c570..b0818c5 100644 --- a/GameData/KSPCommunityFixes/KSPCommunityFixes.version +++ b/GameData/KSPCommunityFixes/KSPCommunityFixes.version @@ -2,7 +2,7 @@ "NAME": "KSPCommunityFixes", "URL": "https://raw.githubusercontent.com/KSPModdingLibs/KSPCommunityFixes/master/GameData/KSPCommunityFixes/KSPCommunityFixes.version", "DOWNLOAD": "https://github.com/KSPModdingLibs/KSPCommunityFixes/releases", - "VERSION": {"MAJOR": 1, "MINOR": 17, "PATCH": 0, "BUILD": 0}, + "VERSION": {"MAJOR": 1, "MINOR": 18, "PATCH": 0, "BUILD": 0}, "KSP_VERSION": {"MAJOR": 1, "MINOR": 12, "PATCH": 3}, "KSP_VERSION_MIN": {"MAJOR": 1, "MINOR": 8, "PATCH": 0}, "KSP_VERSION_MAX": {"MAJOR": 1, "MINOR": 12, "PATCH": 3} diff --git a/GameData/KSPCommunityFixes/Settings.cfg b/GameData/KSPCommunityFixes/Settings.cfg index 8da56fb..100468d 100644 --- a/GameData/KSPCommunityFixes/Settings.cfg +++ b/GameData/KSPCommunityFixes/Settings.cfg @@ -106,6 +106,9 @@ KSP_COMMUNITY_FIXES // science data they carry not being recovered, depending on the EVA kerbal variant/suit. EVAKerbalRecovery = true + // Fix rescaled servo parts propagating their scale to childrens after actuating the servo in the editor + RescaledRoboticParts = true + // Fix Admin Building not using HeadImage if that is defined for a Department but a kerbal prefab is not DepartmentHeadImage = true diff --git a/KSPCommunityFixes/BugFixes/RescaledRoboticParts.cs b/KSPCommunityFixes/BugFixes/RescaledRoboticParts.cs new file mode 100644 index 0000000..a6240ae --- /dev/null +++ b/KSPCommunityFixes/BugFixes/RescaledRoboticParts.cs @@ -0,0 +1,63 @@ +using Expansions.Serenity; +using HarmonyLib; +using System; +using System.Collections.Generic; +using System.Reflection; +using System.Reflection.Emit; +using UnityEngine; + +// fix scaled servo parts propagating their scale to childrens after actuating the servo in the editor +// see issue #48 : https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/48 + +namespace KSPCommunityFixes.BugFixes +{ + class RescaledRoboticParts : BasePatch + { + protected override Version VersionMin => new Version(1, 8, 0); + + protected override void ApplyPatches(List patches) + { + patches.Add(new PatchInfo( + PatchMethodType.Transpiler, + AccessTools.Method(typeof(BaseServo), nameof(BaseServo.SetChildParentTransform)), + this)); + } + + private static IEnumerable BaseServo_SetChildParentTransform_Transpiler(IEnumerable instructions) + { + MethodInfo mInfo_TransformSetParent = AccessTools.Method(typeof(Transform), nameof(Transform.SetParent), new Type[] { typeof(Transform) }); + MethodInfo mInfo_TransformLocalScale = AccessTools.PropertySetter(typeof(Transform), nameof(Transform.localScale)); + FieldInfo fInfo_BaseServoMovingPartObject = AccessTools.Field(typeof(BaseServo), nameof(BaseServo.movingPartObject)); + + List code = new List(instructions); + + for (int i = 0; i < code.Count; i++) + { + if (code[i].opcode == OpCodes.Ldfld && ReferenceEquals(code[i].operand, fInfo_BaseServoMovingPartObject)) + { + for (int j = i + 1; j < i + 6; j++) + { + if (code[j].opcode == OpCodes.Callvirt && ReferenceEquals(code[j].operand, mInfo_TransformSetParent)) + { + int k = j; + bool end = false; + do + { + k++; + end = code[k].opcode == OpCodes.Callvirt && ReferenceEquals(code[k].operand, mInfo_TransformLocalScale); + code[k].opcode = OpCodes.Nop; + code[k].operand = null; + } + while (!end); + + i = k; + break; + } + } + } + } + + return code; + } + } +} diff --git a/KSPCommunityFixes/KSPCommunityFixes.csproj b/KSPCommunityFixes/KSPCommunityFixes.csproj index 5a3b347..901d686 100644 --- a/KSPCommunityFixes/KSPCommunityFixes.csproj +++ b/KSPCommunityFixes/KSPCommunityFixes.csproj @@ -94,6 +94,7 @@ + diff --git a/KSPCommunityFixes/Properties/AssemblyInfo.cs b/KSPCommunityFixes/Properties/AssemblyInfo.cs index cfc784f..445168f 100644 --- a/KSPCommunityFixes/Properties/AssemblyInfo.cs +++ b/KSPCommunityFixes/Properties/AssemblyInfo.cs @@ -29,5 +29,5 @@ // Build Number // Revision // -[assembly: AssemblyVersion("1.17.0.0")] -[assembly: AssemblyFileVersion("1.17.0.0")] +[assembly: AssemblyVersion("1.18.0.0")] +[assembly: AssemblyFileVersion("1.18.0.0")] diff --git a/README.md b/README.md index 774457a..d3b9788 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ User options are available from the "ESC" in-game settings menu :