2019-06-18 22:55:32 -04:00
|
|
|
|
/*
|
|
|
|
|
===========================================================================
|
|
|
|
|
Copyright (C) 2015-2019 Project Meteor Dev Team
|
|
|
|
|
|
|
|
|
|
This file is part of Project Meteor Server.
|
|
|
|
|
|
|
|
|
|
Project Meteor Server is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
Project Meteor Server 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 Affero General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
along with Project Meteor Server. If not, see <https:www.gnu.org/licenses/>.
|
|
|
|
|
===========================================================================
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
2015-09-13 14:12:41 -04:00
|
|
|
|
|
2019-06-19 00:05:18 -04:00
|
|
|
|
namespace Meteor.Common
|
2015-09-13 14:12:41 -04:00
|
|
|
|
{
|
|
|
|
|
[global::System.AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
|
2016-06-12 20:12:59 +01:00
|
|
|
|
public sealed class BitfieldLengthAttribute : Attribute
|
2015-09-13 14:12:41 -04:00
|
|
|
|
{
|
|
|
|
|
uint length;
|
|
|
|
|
|
|
|
|
|
public BitfieldLengthAttribute(uint length)
|
|
|
|
|
{
|
|
|
|
|
this.length = length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public uint Length { get { return length; } }
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-12 20:12:59 +01:00
|
|
|
|
public static class PrimitiveConversion
|
2015-09-13 14:12:41 -04:00
|
|
|
|
{
|
|
|
|
|
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++)
|
2019-05-04 20:53:08 -04:00
|
|
|
|
mask |= 1L << i;
|
2015-09-13 14:12:41 -04:00
|
|
|
|
|
|
|
|
|
r |= ((UInt32)f.GetValue(t) & mask) << offset;
|
|
|
|
|
|
|
|
|
|
offset += (int)fieldLength;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-18 22:55:32 -04:00
|
|
|
|
}
|