From 0610f0cf36a9deb4ffd5ad34870d3be4aa21aba0 Mon Sep 17 00:00:00 2001 From: Dominic Date: Fri, 25 Dec 2020 12:44:45 -0600 Subject: [PATCH] Sped up some reflection --- Utilities/CalamityUtils.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Utilities/CalamityUtils.cs b/Utilities/CalamityUtils.cs index d9c1f5fdef..ffaf987d64 100644 --- a/Utilities/CalamityUtils.cs +++ b/Utilities/CalamityUtils.cs @@ -3803,10 +3803,17 @@ namespace CalamityMod return Color.Lerp(currentColor, nextColor, increment * colors.Length % 1f); } - // TODO: Find some way to speed this up. + // Cached for efficiency purposes. + internal static readonly FieldInfo UImageField = typeof(MiscShaderData).GetField("_uImage", BindingFlags.NonPublic | BindingFlags.Instance); + + /// + /// Manually sets the texture of a instance, since vanilla's implementation only supports strings that access vanilla textures. + /// + /// The shader to bind the texture to. + /// The texture to bind. public static void SetShaderTexture(this MiscShaderData shader, Texture2D texture) { - typeof(MiscShaderData).GetField("_uImage", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(shader, new Ref(texture)); + UImageField.SetValue(shader, new Ref(texture)); } #endregion