mirror of
https://bitbucket.org/Ioncannon/project-meteor-server.git
synced 2025-05-20 08:26:59 -04:00
Cleaned up namespaces (still have to do Map Project) and removed references to FFXIV Classic from the code. Removed the Launcher Editor project as it is no longer needed (host file editing is cleaner).
This commit is contained in:
95
Common Class Lib/Bitfield.cs
Normal file
95
Common Class Lib/Bitfield.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
===========================================================================
|
||||
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;
|
||||
|
||||
namespace Meteor.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 |= 1L << i;
|
||||
|
||||
r |= ((UInt32)f.GetValue(t) & mask) << offset;
|
||||
|
||||
offset += (int)fieldLength;
|
||||
}
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user