From 76afccd9db62392e19e481cd29c3efec34cf4c7a Mon Sep 17 00:00:00 2001 From: zznty <94796179+zznty@users.noreply.github.com> Date: Tue, 1 Feb 2022 15:11:04 +0700 Subject: [PATCH] shitty gc calls removal --- Torch/FodyWeavers.xml | 1 + Torch/FodyWeavers.xsd | 1 + .../MSIL/MsilInstructionExtensions.cs | 21 ++++++++ Torch/Patches/GcCollectPatch.cs | 52 +++++++++++++++++++ Torch/Torch.csproj | 1 + 5 files changed, 76 insertions(+) create mode 100644 Torch/Patches/GcCollectPatch.cs diff --git a/Torch/FodyWeavers.xml b/Torch/FodyWeavers.xml index d5abfed..e2c5835 100644 --- a/Torch/FodyWeavers.xml +++ b/Torch/FodyWeavers.xml @@ -1,3 +1,4 @@  + \ No newline at end of file diff --git a/Torch/FodyWeavers.xsd b/Torch/FodyWeavers.xsd index 69dbe48..de66ede 100644 --- a/Torch/FodyWeavers.xsd +++ b/Torch/FodyWeavers.xsd @@ -53,6 +53,7 @@ + diff --git a/Torch/Managers/PatchManager/MSIL/MsilInstructionExtensions.cs b/Torch/Managers/PatchManager/MSIL/MsilInstructionExtensions.cs index a69ada4..b0b8532 100644 --- a/Torch/Managers/PatchManager/MSIL/MsilInstructionExtensions.cs +++ b/Torch/Managers/PatchManager/MSIL/MsilInstructionExtensions.cs @@ -221,6 +221,27 @@ namespace Torch.Managers.PatchManager.MSIL { return new MsilInstruction(argument.Position < 0xFF ? OpCodes.Ldarga_S : OpCodes.Ldarga).InlineValue(argument); } + + /// + /// Copy Instruction with null operand and new opcode. + /// + /// Instruction to copy from. + /// New opCode to assign. + /// Copy of . + public static MsilInstruction CopyWithoutOperand(this MsilInstruction instruction, OpCode opCode) + { + var copied = new MsilInstruction(opCode); + foreach (var label in instruction.Labels) + { + copied.Labels.Add(label); + } + foreach (var operation in instruction.TryCatchOperations) + { + copied.TryCatchOperations.Add(operation); + } + + return copied; + } #endregion #region Constant Utils diff --git a/Torch/Patches/GcCollectPatch.cs b/Torch/Patches/GcCollectPatch.cs new file mode 100644 index 0000000..1d4e04c --- /dev/null +++ b/Torch/Patches/GcCollectPatch.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Reflection; +using System.Reflection.Emit; +using Havok; +using Sandbox.Engine.Voxels.Planet; +using Sandbox.Game.World; +using Torch.Managers.PatchManager; +using Torch.Managers.PatchManager.MSIL; +using Torch.Utils; +using VRage; + +namespace Torch.Patches; + +[PatchShim] +internal static class GcCollectPatch +{ + // FUCK YO KEEN + // every call results in freeze for seconds + private static readonly MethodBase[] _targets = + { + Info.OfMethod(nameof(MyPlanetTextureMapProvider.GetHeightmap)), + Info.OfMethod(nameof(MyPlanetTextureMapProvider.GetDetailMap)), + Info.OfMethod(nameof(MyPlanetTextureMapProvider.GetMaps)), + Info.OfMethod(nameof(MySession.Unload)), + Info.OfMethod("HavokWrapper", "Havok.HkBaseSystem", nameof(HkBaseSystem.Quit)), + Info.OfMethod(nameof(MySimpleProfiler.LogPerformanceTestResults)), + Info.OfConstructor("MySyncLayer,Boolean") + }; + + public static void Patch(PatchContext context) + { + foreach (var target in _targets) + { + context.GetPattern(target).AddTranspiler(nameof(CollectRemovalTranspiler)); + } + } + + private static IEnumerable CollectRemovalTranspiler(IEnumerable instructions) + { + foreach (var instruction in instructions) + { + if (instruction.Operand is MsilOperandInline operand && + operand.Value.DeclaringType == typeof(GC)) + { + yield return instruction.CopyWithoutOperand(OpCodes.Nop); + continue; + } + yield return instruction; + } + } +} \ No newline at end of file diff --git a/Torch/Torch.csproj b/Torch/Torch.csproj index ec593d2..8ded7bc 100644 --- a/Torch/Torch.csproj +++ b/Torch/Torch.csproj @@ -20,6 +20,7 @@ +