Core: Removed not needed faststruct
This commit is contained in:
@@ -14,6 +14,7 @@
|
|||||||
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
|
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
|
||||||
<PackageReference Include="MySqlConnector" Version="0.48.2" />
|
<PackageReference Include="MySqlConnector" Version="0.48.2" />
|
||||||
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.3.0" />
|
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.3.0" />
|
||||||
|
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -1,91 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2012-2018 CypherCore <http://github.com/CypherCore>
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
using System.Reflection.Emit;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
|
|
||||||
namespace Framework.IO
|
|
||||||
{
|
|
||||||
public static class FastStruct<T> where T : struct
|
|
||||||
{
|
|
||||||
private delegate T LoadFromByteRefDelegate(ref byte source);
|
|
||||||
private delegate void CopyMemoryDelegate(ref T dest, ref byte src, int count);
|
|
||||||
|
|
||||||
private static readonly LoadFromByteRefDelegate LoadFromByteRef = BuildLoadFromByteRefMethod();
|
|
||||||
private static readonly CopyMemoryDelegate CopyMemory = BuildCopyMemoryMethod();
|
|
||||||
|
|
||||||
public static readonly int Size = Marshal.SizeOf<T>();
|
|
||||||
|
|
||||||
private static LoadFromByteRefDelegate BuildLoadFromByteRefMethod()
|
|
||||||
{
|
|
||||||
var methodLoadFromByteRef = new DynamicMethod("LoadFromByteRef<" + typeof(T).FullName + ">",
|
|
||||||
typeof(T), new[] { typeof(byte).MakeByRefType() }, typeof(FastStruct<T>));
|
|
||||||
|
|
||||||
ILGenerator generator = methodLoadFromByteRef.GetILGenerator();
|
|
||||||
generator.Emit(OpCodes.Ldarg_0);
|
|
||||||
generator.Emit(OpCodes.Ldobj, typeof(T));
|
|
||||||
generator.Emit(OpCodes.Ret);
|
|
||||||
|
|
||||||
return (LoadFromByteRefDelegate)methodLoadFromByteRef.CreateDelegate(typeof(LoadFromByteRefDelegate));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static CopyMemoryDelegate BuildCopyMemoryMethod()
|
|
||||||
{
|
|
||||||
var methodCopyMemory = new DynamicMethod("CopyMemory<" + typeof(T).FullName + ">",
|
|
||||||
typeof(void), new[] { typeof(T).MakeByRefType(), typeof(byte).MakeByRefType(), typeof(int) }, typeof(FastStruct<T>));
|
|
||||||
|
|
||||||
ILGenerator generator = methodCopyMemory.GetILGenerator();
|
|
||||||
generator.Emit(OpCodes.Ldarg_0);
|
|
||||||
generator.Emit(OpCodes.Ldarg_1);
|
|
||||||
generator.Emit(OpCodes.Ldarg_2);
|
|
||||||
generator.Emit(OpCodes.Cpblk);
|
|
||||||
generator.Emit(OpCodes.Ret);
|
|
||||||
|
|
||||||
return (CopyMemoryDelegate)methodCopyMemory.CreateDelegate(typeof(CopyMemoryDelegate));
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static T ArrayToStructure(byte[] src)
|
|
||||||
{
|
|
||||||
return LoadFromByteRef(ref src[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static T ArrayToStructure(byte[] src, int offset)
|
|
||||||
{
|
|
||||||
return LoadFromByteRef(ref src[offset]);
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static T ArrayToStructure(ref byte src)
|
|
||||||
{
|
|
||||||
return LoadFromByteRef(ref src);
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static T[] ReadArray(byte[] source)
|
|
||||||
{
|
|
||||||
T[] buffer = new T[source.Length / Size];
|
|
||||||
|
|
||||||
if (source.Length > 0)
|
|
||||||
CopyMemory(ref buffer[0], ref source[0], source.Length);
|
|
||||||
|
|
||||||
return buffer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -25,6 +25,7 @@ using System.Reflection.Emit;
|
|||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
namespace System
|
namespace System
|
||||||
{
|
{
|
||||||
@@ -157,50 +158,6 @@ namespace System
|
|||||||
right = temp;
|
right = temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Func<object, object> CompileGetter(this FieldInfo field)
|
|
||||||
{
|
|
||||||
string methodName = field.ReflectedType.FullName + ".get_" + field.Name;
|
|
||||||
DynamicMethod setterMethod = new DynamicMethod(methodName, typeof(object), new[] { typeof(object) }, true);
|
|
||||||
ILGenerator gen = setterMethod.GetILGenerator();
|
|
||||||
if (field.IsStatic)
|
|
||||||
{
|
|
||||||
gen.Emit(OpCodes.Ldsfld, field);
|
|
||||||
gen.Emit(field.FieldType.IsClass ? OpCodes.Castclass : OpCodes.Box, field.FieldType);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
gen.Emit(OpCodes.Ldarg_0);
|
|
||||||
gen.Emit(OpCodes.Castclass, field.DeclaringType);
|
|
||||||
gen.Emit(OpCodes.Ldfld, field);
|
|
||||||
gen.Emit(field.FieldType.IsClass ? OpCodes.Castclass : OpCodes.Box, field.FieldType);
|
|
||||||
}
|
|
||||||
gen.Emit(OpCodes.Ret);
|
|
||||||
return (Func<object, object>)setterMethod.CreateDelegate(typeof(Func<object, object>));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Action<object, object> CompileSetter(this FieldInfo field)
|
|
||||||
{
|
|
||||||
string methodName = field.ReflectedType.FullName + ".set_" + field.Name;
|
|
||||||
DynamicMethod setterMethod = new DynamicMethod(methodName, null, new[] { typeof(object), typeof(object) }, true);
|
|
||||||
ILGenerator gen = setterMethod.GetILGenerator();
|
|
||||||
if (field.IsStatic)
|
|
||||||
{
|
|
||||||
gen.Emit(OpCodes.Ldarg_1);
|
|
||||||
gen.Emit(field.FieldType.IsClass ? OpCodes.Castclass : OpCodes.Unbox_Any, field.FieldType);
|
|
||||||
gen.Emit(OpCodes.Stsfld, field);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
gen.Emit(OpCodes.Ldarg_0);
|
|
||||||
gen.Emit(OpCodes.Castclass, field.DeclaringType);
|
|
||||||
gen.Emit(OpCodes.Ldarg_1);
|
|
||||||
gen.Emit(field.FieldType.IsClass ? OpCodes.Castclass : OpCodes.Unbox_Any, field.FieldType);
|
|
||||||
gen.Emit(OpCodes.Stfld, field);
|
|
||||||
}
|
|
||||||
gen.Emit(OpCodes.Ret);
|
|
||||||
return (Action<object, object>)setterMethod.CreateDelegate(typeof(Action<object, object>));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static uint[] SerializeObject<T>(this T obj)
|
public static uint[] SerializeObject<T>(this T obj)
|
||||||
{
|
{
|
||||||
//if (obj.GetType()<StructLayoutAttribute>() == null)
|
//if (obj.GetType()<StructLayoutAttribute>() == null)
|
||||||
@@ -318,18 +275,28 @@ namespace System
|
|||||||
|
|
||||||
public static T[] ReadArray<T>(this BinaryReader reader, uint size) where T : struct
|
public static T[] ReadArray<T>(this BinaryReader reader, uint size) where T : struct
|
||||||
{
|
{
|
||||||
int numBytes = FastStruct<T>.Size * (int)size;
|
int numBytes = Unsafe.SizeOf<T>() * (int)size;
|
||||||
|
|
||||||
byte[] result = reader.ReadBytes(numBytes);
|
byte[] source = reader.ReadBytes(numBytes);
|
||||||
|
|
||||||
return FastStruct<T>.ReadArray(result);
|
T[] result = new T[source.Length / Unsafe.SizeOf<T>()];
|
||||||
|
|
||||||
|
if (source.Length > 0)
|
||||||
|
{
|
||||||
|
unsafe
|
||||||
|
{
|
||||||
|
Unsafe.CopyBlockUnaligned(Unsafe.AsPointer(ref result[0]), Unsafe.AsPointer(ref source[0]), (uint)source.Length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static T Read<T>(this BinaryReader reader) where T : struct
|
public static T Read<T>(this BinaryReader reader) where T : struct
|
||||||
{
|
{
|
||||||
byte[] result = reader.ReadBytes(FastStruct<T>.Size);
|
byte[] result = reader.ReadBytes(Unsafe.SizeOf<T>());
|
||||||
|
|
||||||
return FastStruct<T>.ArrayToStructure(result);
|
return Unsafe.ReadUnaligned<T>(ref result[0]);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,10 +7,6 @@
|
|||||||
<LangVersion>7.3</LangVersion>
|
<LangVersion>7.3</LangVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.2" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\Framework\Framework.csproj" />
|
<ProjectReference Include="..\Framework\Framework.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user