mirror of
https://bitbucket.org/Ioncannon/project-meteor-server.git
synced 2025-06-08 05:24:34 +02:00
PROJECT: added common library to make common files actually common
- renamed sln to FFXIVClassic.sln - threaded logging - todo: print packets using Log.Packet
This commit is contained in:
parent
16d4779970
commit
c23f9c7ca9
53 changed files with 547 additions and 1817 deletions
74
FFXIVClassic Common Class Lib/Bitfield.cs
Normal file
74
FFXIVClassic Common Class Lib/Bitfield.cs
Normal file
|
@ -0,0 +1,74 @@
|
|||
using System;
|
||||
|
||||
namespace FFXIVClassic.Common
|
||||
{
|
||||
[global::System.AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
|
||||
public sealed class BitfieldLengthAttribute : Attribute
|
||||
{
|
||||
uint length;
|
||||
|
||||
public BitfieldLengthAttribute(uint length)
|
||||
{
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
public uint Length { get { return length; } }
|
||||
}
|
||||
|
||||
public static class PrimitiveConversion
|
||||
{
|
||||
public static UInt32 ToUInt32<T>(T t) where T : struct
|
||||
{
|
||||
UInt32 r = 0;
|
||||
int offset = 0;
|
||||
|
||||
// For every field suitably attributed with a BitfieldLength
|
||||
foreach (System.Reflection.FieldInfo f in t.GetType().GetFields())
|
||||
{
|
||||
object[] attrs = f.GetCustomAttributes(typeof(BitfieldLengthAttribute), false);
|
||||
if (attrs.Length == 1)
|
||||
{
|
||||
uint fieldLength = ((BitfieldLengthAttribute)attrs[0]).Length;
|
||||
|
||||
// Calculate a bitmask of the desired length
|
||||
uint mask = 0;
|
||||
for (int i = 0; i < fieldLength; i++)
|
||||
mask |= (UInt32)1 << i;
|
||||
|
||||
r |= ((UInt32)f.GetValue(t) & mask) << offset;
|
||||
|
||||
offset += (int)fieldLength;
|
||||
}
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
public static long ToLong<T>(T t) where T : struct
|
||||
{
|
||||
long r = 0;
|
||||
int offset = 0;
|
||||
|
||||
// For every field suitably attributed with a BitfieldLength
|
||||
foreach (System.Reflection.FieldInfo f in t.GetType().GetFields())
|
||||
{
|
||||
object[] attrs = f.GetCustomAttributes(typeof(BitfieldLengthAttribute), false);
|
||||
if (attrs.Length == 1)
|
||||
{
|
||||
uint fieldLength = ((BitfieldLengthAttribute)attrs[0]).Length;
|
||||
|
||||
// Calculate a bitmask of the desired length
|
||||
long mask = 0;
|
||||
for (int i = 0; i < fieldLength; i++)
|
||||
mask |= 1 << i;
|
||||
|
||||
r |= ((UInt32)f.GetValue(t) & mask) << offset;
|
||||
|
||||
offset += (int)fieldLength;
|
||||
}
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue