From 5727fcf177fc61aff6a2366a757fed026862028e Mon Sep 17 00:00:00 2001 From: PopSlime Date: Tue, 31 Jan 2023 22:49:04 +0800 Subject: [PATCH] Introduce UnsafeIL for IL2CPP compatibility. --- .../StringFormatter/StringFormatter.cs | 76 +++++++++--------- Assets/Plugins/UnsafeIL.dll | Bin 0 -> 3072 bytes Assets/Plugins/UnsafeIL.dll.meta | 33 ++++++++ 3 files changed, 69 insertions(+), 40 deletions(-) create mode 100644 Assets/Plugins/UnsafeIL.dll create mode 100644 Assets/Plugins/UnsafeIL.dll.meta diff --git a/Assets/Plugins/StringFormatter/StringFormatter.cs b/Assets/Plugins/StringFormatter/StringFormatter.cs index fbfb664..358d56c 100644 --- a/Assets/Plugins/StringFormatter/StringFormatter.cs +++ b/Assets/Plugins/StringFormatter/StringFormatter.cs @@ -18,20 +18,16 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; -using System.Collections.Generic; using System.Globalization; -using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; -using System.Text; -using System.Threading.Tasks; +using UnsafeIL; namespace System.Text.Formatting { - /// - /// Specifies an interface for types that act as a set of formatting arguments. - /// - public interface IArgSet { + /// + /// Specifies an interface for types that act as a set of formatting arguments. + /// + public interface IArgSet { /// /// The number of arguments in the set. /// @@ -547,37 +543,37 @@ namespace System.Text.Formatting { [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void AppendGeneric(T value, StringView format) { - // this looks gross, but T is known at JIT-time so this call tree - // gets compiled down to a direct call with no branching - if (typeof(T) == typeof(sbyte)) - Append(__refvalue(__makeref(value), sbyte), format); - else if (typeof(T) == typeof(byte)) - Append(__refvalue(__makeref(value), byte), format); - else if (typeof(T) == typeof(short)) - Append(__refvalue(__makeref(value), short), format); - else if (typeof(T) == typeof(ushort)) - Append(__refvalue(__makeref(value), ushort), format); - else if (typeof(T) == typeof(int)) - Append(__refvalue(__makeref(value), int), format); - else if (typeof(T) == typeof(uint)) - Append(__refvalue(__makeref(value), uint), format); - else if (typeof(T) == typeof(long)) - Append(__refvalue(__makeref(value), long), format); - else if (typeof(T) == typeof(ulong)) - Append(__refvalue(__makeref(value), ulong), format); - else if (typeof(T) == typeof(float)) - Append(__refvalue(__makeref(value), float), format); - else if (typeof(T) == typeof(double)) - Append(__refvalue(__makeref(value), double), format); - else if (typeof(T) == typeof(decimal)) - Append(__refvalue(__makeref(value), decimal), format); - else if (typeof(T) == typeof(bool)) - Append(__refvalue(__makeref(value), bool)); - else if (typeof(T) == typeof(char)) - Append(__refvalue(__makeref(value), char), format); - else if (typeof(T) == typeof(string)) - Append((string)(object)value); - else { + // this looks gross, but T is known at JIT-time so this call tree + // gets compiled down to a direct call with no branching + if (typeof(T) == typeof(sbyte)) + Append(*(sbyte*)Unsafe.AsPointer(ref value), format); + else if (typeof(T) == typeof(byte)) + Append(*(byte*)Unsafe.AsPointer(ref value), format); + else if (typeof(T) == typeof(short)) + Append(*(short*)Unsafe.AsPointer(ref value), format); + else if (typeof(T) == typeof(ushort)) + Append(*(ushort*)Unsafe.AsPointer(ref value), format); + else if (typeof(T) == typeof(int)) + Append(*(int*)Unsafe.AsPointer(ref value), format); + else if (typeof(T) == typeof(uint)) + Append(*(uint*)Unsafe.AsPointer(ref value), format); + else if (typeof(T) == typeof(long)) + Append(*(long*)Unsafe.AsPointer(ref value), format); + else if (typeof(T) == typeof(ulong)) + Append(*(ulong*)Unsafe.AsPointer(ref value), format); + else if (typeof(T) == typeof(float)) + Append(*(float*)Unsafe.AsPointer(ref value), format); + else if (typeof(T) == typeof(double)) + Append(*(double*)Unsafe.AsPointer(ref value), format); + else if (typeof(T) == typeof(decimal)) + Append(*(decimal*)Unsafe.AsPointer(ref value), format); + else if (typeof(T) == typeof(bool)) + Append(*(bool*)Unsafe.AsPointer(ref value)); + else if (typeof(T) == typeof(char)) + Append(*(char*)Unsafe.AsPointer(ref value), format); + else if (typeof(T) == typeof(string)) + Append(Unsafe.As(value)); + else { // first, check to see if it's a value type implementing IStringFormattable var formatter = ValueHelper.Formatter; if (formatter != null) diff --git a/Assets/Plugins/UnsafeIL.dll b/Assets/Plugins/UnsafeIL.dll new file mode 100644 index 0000000000000000000000000000000000000000..4b385a3bf931e9523224e6800125e66b46268cc4 GIT binary patch literal 3072 zcmeHJO>9(E6#nj;pU$+>nF2*BIJ{c1WDFAo5``F@mKG>!rRhwCkRrU9xt%_I^WOO0 zo!Vk-Xo4|lLLfFI#)S(P8W*@RLQHUBVxmS?ZZKidjfpE4CTv_3&%N(gtFz8e7HDA?#^vrcjC+B&ED&5^k#2y#tzK7 z?^S)PW|l42^@LgCrr&T)+ck%d7R;Jg;kk9|+Mm{`$A^JYW*~0i`mD zF+%IbenOLap1Kq!0r_OzvaKRc6VYQL$iD-m)zd;v6NHf{Rhmv!CsFz&0=G2Ak)=b9 zV_eCyl1o^`27E?QaeRYoB)=mmTR2v(c9Es=JMN%|bcS(LdaqywPZ2xWWIO4Nx0~z* zs%ciU9<~#0cuTY0Yye3tYBtP9(Sh?#R-iliu4YAc66^53W)(J#PF!p<4=G&M>a=aB~#HYio3wli_WWhO$TS-hJL1O)taRoMAovEQuqOql;vmDwVRA zWIOiZCt81w_Vo*r8|dqs!xr2i*@xS-PtkL7?zQa>k>iJ8xEj}`?He?9YwXduMdOniw`uIv*hiSa=V3iBj(L@a!(T#SJ`lW? zn`pSguJPOfuU5Am?iaW}YnOR|&|nGMb0@fC%_%Jy5cDpV8iFHN7M_pcIljlCt2N>d|Mzv~{9Nr2GZS>Ytxl~Lz3P($4cv)b?730E( zS3fm8S9k2PEimZ@)-)d(%~c!+wV>?zj$P8$rV|5|3W^B4hF|934zF=n9G#vHxR9>F zdBHU+wbTKh7pxkG!>0v&yE;R$0X!5B`d$siqDOSxvt7Y`%vw%^6QAJIRHQBFLsWi! z(zP7B>hcN#Vflh4@_7(IPI17a-NyvCDoV5>3-(zm1;>0_@RpT!LfEc42#f?e(I_P= z&R8xTZ^Tq*T=`>ZU)#cuW9Mr-hYv1X&mw9vmLMmKkKY196Anz0PFCesE>XXpV1QHWLuQY=XpQs`+zgr#G#WSbh4 zVj-qu(KJIsW+!h3tr|fWnPzevN+>=Zw8El zGGk