From 4cd624c42c801bfb98af6c1af4504a1f334c6450 Mon Sep 17 00:00:00 2001 From: lidgren Date: Sat, 14 Jan 2012 10:12:13 +0000 Subject: [PATCH] ReadSingle(out float) and ReadInt32(out int) added --- Lidgren.Network/NetIncomingMessage.Read.cs | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/Lidgren.Network/NetIncomingMessage.Read.cs b/Lidgren.Network/NetIncomingMessage.Read.cs index 5eb0b2f..76a8c19 100644 --- a/Lidgren.Network/NetIncomingMessage.Read.cs +++ b/Lidgren.Network/NetIncomingMessage.Read.cs @@ -232,6 +232,22 @@ namespace Lidgren.Network return (Int32)retval; } + /// + /// Reads a 32 bit signed integer written using Write(Int32) + /// + public bool ReadInt32(out Int32 result) + { + if (m_bitLength - m_readPosition < 32) + { + result = 0; + return false; + } + + result = (Int32)NetBitWriter.ReadUInt32(m_data, 32, m_readPosition); + m_readPosition += 32; + return true; + } + /// /// Reads a signed integer stored in 1 to 32 bits, written using Write(Int32, Int32) /// @@ -391,6 +407,29 @@ namespace Lidgren.Network return BitConverter.ToSingle(bytes, 0); } + /// + /// Reads a 32 bit floating point value written using Write(Single) + /// + public bool ReadSingle(out float result) + { + if (m_bitLength - m_readPosition < 32) + { + result = 0.0f; + return false; + } + + if ((m_readPosition & 7) == 0) // read directly + { + result = BitConverter.ToSingle(m_data, m_readPosition >> 3); + m_readPosition += 32; + return true; + } + + byte[] bytes = ReadBytes(4); + result = BitConverter.ToSingle(bytes, 0); + return true; + } + /// /// Reads a 64 bit floating point value written using Write(Double) ///