From 55eac7ecbf082ba3ee7816c833df66635e7559ae Mon Sep 17 00:00:00 2001 From: zznty <94796179+zznty@users.noreply.github.com> Date: Mon, 31 Jan 2022 20:28:43 +0700 Subject: [PATCH 1/3] test for try/finally transpiler --- Torch.Tests/PatchTest.cs | 77 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/Torch.Tests/PatchTest.cs b/Torch.Tests/PatchTest.cs index fdb5671..8e53d3d 100644 --- a/Torch.Tests/PatchTest.cs +++ b/Torch.Tests/PatchTest.cs @@ -513,6 +513,83 @@ namespace Torch.Tests } } + [PatchTest] + private class StaticTryFinallyInject + { + private static bool _injectionHit; + private static bool _normalHit; + + [MethodImpl(MethodImplOptions.NoInlining)] + public static void Target() + { + _normalHit = true; + } + public static void Reset() + { + _injectionHit = _normalHit = false; + } + + public static void InjectionTarget() + { + _injectionHit = true; + } + + public static void AssertTranspile() + { + Assert.True(_injectionHit, "Failed to execute transpile"); + } + + public static void AssertNormal() + { + Assert.True(_normalHit, "Failed to execute normal"); + } + + public static IEnumerable Transpile(IEnumerable instructions, MethodBase __methodBase, Func __localCreator) + { + var returnHasValue = (__methodBase as MethodInfo)?.ReturnType != typeof(void); + var returnLabel = new MsilLabel(); + + var original = instructions.ToList(); + + MsilLocal returnLocal = null; + if (returnHasValue) + returnLocal = __localCreator(((MethodInfo)__methodBase).ReturnType); + + // begin try block at start of method + var firstInstruction = original.First(); + firstInstruction.TryCatchOperations.Add(new(MsilTryCatchOperationType.BeginExceptionBlock)); + yield return firstInstruction; + + // copy original except first + foreach (var instruction in original.Skip(1)) + { + if (instruction.OpCode == OpCodes.Ret) + { + if (returnHasValue) + yield return instruction.CopyWith(OpCodes.Stloc).InlineValue(returnLocal); + + yield return new MsilInstruction(OpCodes.Leave).InlineTarget(returnLabel); + continue; + } + yield return instruction; + } + + var injectionCall = new MsilInstruction(OpCodes.Call).InlineValue(new Action(InjectionTarget).Method); + // split begin and end to separate instructions if needed. endfinally instruction will be generated automatically + injectionCall.TryCatchOperations.AddRange(new MsilTryCatchOperation[] + { + new(MsilTryCatchOperationType.BeginFinallyBlock), + new(MsilTryCatchOperationType.EndExceptionBlock) + }); + yield return injectionCall; + + if (returnHasValue) + yield return new MsilInstruction(OpCodes.Ldloc).InlineValue(returnLocal); + + yield return new MsilInstruction(OpCodes.Ret).LabelWith(returnLabel); + } + } + #endregion } #pragma warning restore 414 From 9a6253d0ef289f8c79be829271528f6bb6b02f7a Mon Sep 17 00:00:00 2001 From: z__ Date: Tue, 1 Feb 2022 11:39:00 +0700 Subject: [PATCH 2/3] NRE fixes --- Torch.Server/Views/Extensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Torch.Server/Views/Extensions.cs b/Torch.Server/Views/Extensions.cs index d9aea82..47aeb5c 100644 --- a/Torch.Server/Views/Extensions.cs +++ b/Torch.Server/Views/Extensions.cs @@ -13,12 +13,12 @@ namespace Torch.Server.Views public static bool GetScrollContainer(this UIElement ui) { - return (bool)ui.GetValue(ScrollContainerProperty); + return (bool) (ui?.GetValue(ScrollContainerProperty) ?? false); } public static void SetScrollContainer(this UIElement ui, bool value) { - ui.SetValue(ScrollContainerProperty, value); + ui?.SetValue(ScrollContainerProperty, value); } } } From 4cab2146350e8a3656aef030315c99c2b9e09ee7 Mon Sep 17 00:00:00 2001 From: z__ Date: Tue, 1 Feb 2022 12:01:29 +0700 Subject: [PATCH 3/3] fixed planet heightmap loading --- Torch.sln | 3 +++ Torch/Torch.csproj | 1 + 2 files changed, 4 insertions(+) diff --git a/Torch.sln b/Torch.sln index fba04a5..e44265d 100644 --- a/Torch.sln +++ b/Torch.sln @@ -38,12 +38,15 @@ Global {7E01635C-3B67-472E-BCD6-C5539564F214}.Release|Any CPU.ActiveCfg = Release|Any CPU {7E01635C-3B67-472E-BCD6-C5539564F214}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {7E01635C-3B67-472E-BCD6-C5539564F214}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7E01635C-3B67-472E-BCD6-C5539564F214}.Release|Any CPU.Build.0 = Release|Any CPU {FBA5D932-6254-4A1E-BAF4-E229FA94E3C2}.Release|Any CPU.ActiveCfg = Release|Any CPU {FBA5D932-6254-4A1E-BAF4-E229FA94E3C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {FBA5D932-6254-4A1E-BAF4-E229FA94E3C2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FBA5D932-6254-4A1E-BAF4-E229FA94E3C2}.Release|Any CPU.Build.0 = Release|Any CPU {CA50886B-7B22-4CD8-93A0-C06F38D4F77D}.Release|Any CPU.ActiveCfg = Release|Any CPU {CA50886B-7B22-4CD8-93A0-C06F38D4F77D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {CA50886B-7B22-4CD8-93A0-C06F38D4F77D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CA50886B-7B22-4CD8-93A0-C06F38D4F77D}.Release|Any CPU.Build.0 = Release|Any CPU {9EFD1D91-2FA2-47ED-B537-D8BC3B0E543E}.Release|Any CPU.ActiveCfg = Release|Any CPU {9EFD1D91-2FA2-47ED-B537-D8BC3B0E543E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {C3C8B671-6AD1-44AA-A8DA-E0C0DC0FEDF5}.Release|Any CPU.ActiveCfg = Release|Any CPU diff --git a/Torch/Torch.csproj b/Torch/Torch.csproj index f868d56..ec593d2 100644 --- a/Torch/Torch.csproj +++ b/Torch/Torch.csproj @@ -25,6 +25,7 @@ +