1
0
mirror of https://github.com/lidgren/lidgren-network-gen3.git synced 2026-05-06 10:21:09 +09:00

*AllFields now reads/writes members in alphabetical order

This commit is contained in:
lidgren
2010-05-09 08:36:39 +00:00
parent c1f8eff04b
commit 421a4675c9
3 changed files with 83 additions and 9 deletions

View File

@@ -474,7 +474,7 @@ namespace Lidgren.Network
}
/// <summary>
/// Reads all public and private declared instance fields of the object in declaration order using reflection
/// Reads all public and private declared instance fields of the object in alphabetical order using reflection
/// </summary>
public void ReadAllFields(object target)
{
@@ -482,7 +482,7 @@ namespace Lidgren.Network
}
/// <summary>
/// Reads all fields with the specified binding of the object in declaration order using reflection
/// Reads all fields with the specified binding of the object in alphabetical order using reflection
/// </summary>
public void ReadAllFields(object target, BindingFlags flags)
{
@@ -491,6 +491,8 @@ namespace Lidgren.Network
Type tp = target.GetType();
FieldInfo[] fields = tp.GetFields(flags);
SortMembersList(fields);
foreach (FieldInfo fi in fields)
{
object value;
@@ -509,7 +511,7 @@ namespace Lidgren.Network
}
/// <summary>
/// Reads all public and private declared instance fields of the object in declaration order using reflection
/// Reads all public and private declared instance fields of the object in alphabetical order using reflection
/// </summary>
public void ReadAllProperties(object target)
{
@@ -517,7 +519,7 @@ namespace Lidgren.Network
}
/// <summary>
/// Reads all fields with the specified binding of the object in declaration order using reflection
/// Reads all fields with the specified binding of the object in alphabetical order using reflection
/// </summary>
public void ReadAllProperties(object target, BindingFlags flags)
{
@@ -526,6 +528,7 @@ namespace Lidgren.Network
Type tp = target.GetType();
PropertyInfo[] fields = tp.GetProperties(flags);
SortMembersList(fields);
foreach (PropertyInfo fi in fields)
{
object value;
@@ -543,6 +546,44 @@ namespace Lidgren.Network
}
}
}
// shell sort
internal static void SortMembersList(MemberInfo[] list)
{
int h;
int j;
MemberInfo tmp;
h = 1;
while (h * 3 + 1 <= list.Length)
h = 3 * h + 1;
while (h > 0)
{
for (int i = h - 1; i < list.Length; i++)
{
tmp = list[i];
j = i;
while (true)
{
if (j >= h)
{
if (list[j - h].Name.CompareTo(tmp.Name) > 0)
{
list[j] = list[j - h];
j -= h;
}
else
break;
}
else
break;
}
list[j] = tmp;
}
h /= 3;
}
}
}
}

View File

@@ -537,7 +537,7 @@ namespace Lidgren.Network
}
/// <summary>
/// Writes all public and private declared instance fields of the object in declaration order using reflection
/// Writes all public and private declared instance fields of the object in alphabetical order using reflection
/// </summary>
public void WriteAllFields(object ob)
{
@@ -545,7 +545,7 @@ namespace Lidgren.Network
}
/// <summary>
/// Writes all fields with specified binding in declaration order using reflection
/// Writes all fields with specified binding in alphabetical order using reflection
/// </summary>
public void WriteAllFields(object ob, BindingFlags flags)
{
@@ -554,6 +554,8 @@ namespace Lidgren.Network
Type tp = ob.GetType();
FieldInfo[] fields = tp.GetFields(flags);
NetIncomingMessage.SortMembersList(fields);
foreach (FieldInfo fi in fields)
{
object value = fi.GetValue(ob);
@@ -568,7 +570,7 @@ namespace Lidgren.Network
}
/// <summary>
/// Writes all public and private declared instance properties of the object in declaration order using reflection
/// Writes all public and private declared instance properties of the object in alphabetical order using reflection
/// </summary>
public void WriteAllProperties(object ob)
{
@@ -576,7 +578,7 @@ namespace Lidgren.Network
}
/// <summary>
/// Writes all properties with specified binding in declaration order using reflection
/// Writes all properties with specified binding in alphabetical order using reflection
/// </summary>
public void WriteAllProperties(object ob, BindingFlags flags)
{
@@ -585,6 +587,8 @@ namespace Lidgren.Network
Type tp = ob.GetType();
PropertyInfo[] fields = tp.GetProperties(flags);
NetIncomingMessage.SortMembersList(fields);
foreach (PropertyInfo fi in fields)
{
MethodInfo getMethod = fi.GetGetMethod((flags & BindingFlags.NonPublic) == BindingFlags.NonPublic);

View File

@@ -70,6 +70,35 @@ namespace UnitTests
if (msg.LengthBits != tmp.LengthBits * 2)
throw new NetException("NetOutgoingMessage.Write(NetOutgoingMessage) failed!");
tmp = peer.CreateMessage();
Test test = new Test();
test.Number = 42;
test.Name = "Hallon";
test.Age = 8.2f;
tmp.WriteAllFields(test);
data = tmp.PeekDataBuffer();
inc = (NetIncomingMessage)Activator.CreateInstance(typeof(NetIncomingMessage), true);
typeof(NetIncomingMessage).GetField("m_data", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(inc, data);
typeof(NetIncomingMessage).GetField("m_bitLength", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(inc, tmp.LengthBits);
Test readTest = new Test();
inc.ReadAllFields(readTest);
NetException.Assert(readTest.Number == 42);
NetException.Assert(readTest.Name == "Hallon");
NetException.Assert(readTest.Age == 8.2f);
}
}
public class Test
{
public int Number;
public float Age;
public string Name;
}
}