Fixes code according to modern code style. Fixes a few expression bugs.

This commit is contained in:
Kamron Batman 2018-09-15 09:58:51 -07:00
parent 89eea25e5f
commit 970fd563b2
3324 changed files with 441118 additions and 433755 deletions

View file

@ -18,426 +18,420 @@
*
***************************************************************************/
using System;
using System.Text;
using System.IO;
using System.Text;
namespace Server.Network
{
public class PacketReader
{
private int m_Index;
public PacketReader( byte[] data, int size, bool fixedSize )
{
Buffer = data;
Size = size;
m_Index = fixedSize ? 1 : 3;
}
public byte[] Buffer { get; }
public int Size { get; }
public void Trace( NetState state )
{
try
{
using ( StreamWriter sw = new StreamWriter( "Packets.log", true ) )
{
byte[] buffer = Buffer;
if ( buffer.Length > 0 )
sw.WriteLine( "Client: {0}: Unhandled packet 0x{1:X2}", state, buffer[0] );
using ( MemoryStream ms = new MemoryStream( buffer ) )
Utility.FormatBuffer( sw, ms, buffer.Length );
sw.WriteLine();
sw.WriteLine();
}
}
catch
{
}
}
public int Seek( int offset, SeekOrigin origin )
{
switch ( origin )
{
case SeekOrigin.Begin: m_Index = offset; break;
case SeekOrigin.Current: m_Index += offset; break;
case SeekOrigin.End: m_Index = Size - offset; break;
}
return m_Index;
}
public class PacketReader
{
private int m_Index;
public PacketReader(byte[] data, int size, bool fixedSize)
{
Buffer = data;
Size = size;
m_Index = fixedSize ? 1 : 3;
}
public byte[] Buffer{ get; }
public int Size{ get; }
public void Trace(NetState state)
{
try
{
using (StreamWriter sw = new StreamWriter("Packets.log", true))
{
byte[] buffer = Buffer;
if (buffer.Length > 0)
sw.WriteLine("Client: {0}: Unhandled packet 0x{1:X2}", state, buffer[0]);
using (MemoryStream ms = new MemoryStream(buffer))
{
Utility.FormatBuffer(sw, ms, buffer.Length);
}
sw.WriteLine();
sw.WriteLine();
}
}
catch
{
}
}
public int Seek(int offset, SeekOrigin origin)
{
switch (origin)
{
case SeekOrigin.Begin:
m_Index = offset;
break;
case SeekOrigin.Current:
m_Index += offset;
break;
case SeekOrigin.End:
m_Index = Size - offset;
break;
}
return m_Index;
}
public int ReadInt32()
{
if (m_Index + 4 > Size)
return 0;
return (Buffer[m_Index++] << 24)
| (Buffer[m_Index++] << 16)
| (Buffer[m_Index++] << 8)
| Buffer[m_Index++];
}
public short ReadInt16()
{
if (m_Index + 2 > Size)
return 0;
return (short)((Buffer[m_Index++] << 8) | Buffer[m_Index++]);
}
public byte ReadByte()
{
if (m_Index + 1 > Size)
return 0;
return Buffer[m_Index++];
}
public uint ReadUInt32()
{
if (m_Index + 4 > Size)
return 0;
public int ReadInt32()
{
if ( (m_Index + 4) > Size )
return 0;
return (Buffer[m_Index++] << 24)
| (Buffer[m_Index++] << 16)
| (Buffer[m_Index++] << 8)
| Buffer[m_Index++];
}
public short ReadInt16()
{
if ( (m_Index + 2) > Size )
return 0;
return (uint)((Buffer[m_Index++] << 24) | (Buffer[m_Index++] << 16) | (Buffer[m_Index++] << 8) |
Buffer[m_Index++]);
}
return (short)((Buffer[m_Index++] << 8) | Buffer[m_Index++]);
}
public byte ReadByte()
{
if ( (m_Index + 1) > Size )
return 0;
public ushort ReadUInt16()
{
if (m_Index + 2 > Size)
return 0;
return (ushort)((Buffer[m_Index++] << 8) | Buffer[m_Index++]);
}
public sbyte ReadSByte()
{
if (m_Index + 1 > Size)
return 0;
return (sbyte)Buffer[m_Index++];
}
return Buffer[m_Index++];
}
public uint ReadUInt32()
{
if ( (m_Index + 4) > Size )
return 0;
public bool ReadBoolean()
{
if (m_Index + 1 > Size)
return false;
return (uint)((Buffer[m_Index++] << 24) | (Buffer[m_Index++] << 16) | (Buffer[m_Index++] << 8) | Buffer[m_Index++]);
}
return Buffer[m_Index++] != 0;
}
public ushort ReadUInt16()
{
if ( (m_Index + 2) > Size )
return 0;
public string ReadUnicodeStringLE()
{
StringBuilder sb = new StringBuilder();
return (ushort)((Buffer[m_Index++] << 8) | Buffer[m_Index++]);
}
int c;
public sbyte ReadSByte()
{
if ( (m_Index + 1) > Size )
return 0;
return (sbyte)Buffer[m_Index++];
}
public bool ReadBoolean()
{
if ( (m_Index + 1) > Size )
return false;
while (m_Index + 1 < Size && (c = Buffer[m_Index++] | (Buffer[m_Index++] << 8)) != 0)
sb.Append((char)c);
return ( Buffer[m_Index++] != 0 );
}
return sb.ToString();
}
public string ReadUnicodeStringLE()
{
StringBuilder sb = new StringBuilder();
public string ReadUnicodeStringLESafe(int fixedLength)
{
int bound = m_Index + (fixedLength << 1);
int end = bound;
int c;
if (bound > Size)
bound = Size;
while ( (m_Index + 1) < Size && (c = (Buffer[m_Index++] | (Buffer[m_Index++] << 8))) != 0 )
sb.Append( (char)c );
StringBuilder sb = new StringBuilder();
return sb.ToString();
}
int c;
public string ReadUnicodeStringLESafe( int fixedLength )
{
int bound = m_Index + (fixedLength << 1);
int end = bound;
while (m_Index + 1 < bound && (c = Buffer[m_Index++] | (Buffer[m_Index++] << 8)) != 0)
if (IsSafeChar(c))
sb.Append((char)c);
if ( bound > Size )
bound = Size;
m_Index = end;
StringBuilder sb = new StringBuilder();
return sb.ToString();
}
int c;
public string ReadUnicodeStringLESafe()
{
StringBuilder sb = new StringBuilder();
while ( (m_Index + 1) < bound && (c = (Buffer[m_Index++] | (Buffer[m_Index++] << 8))) != 0 )
{
if ( IsSafeChar( c ) )
sb.Append( (char)c );
}
int c;
m_Index = end;
while (m_Index + 1 < Size && (c = Buffer[m_Index++] | (Buffer[m_Index++] << 8)) != 0)
if (IsSafeChar(c))
sb.Append((char)c);
return sb.ToString();
}
return sb.ToString();
}
public string ReadUnicodeStringLESafe()
{
StringBuilder sb = new StringBuilder();
public string ReadUnicodeStringSafe()
{
StringBuilder sb = new StringBuilder();
int c;
int c;
while ( (m_Index + 1) < Size && (c = (Buffer[m_Index++] | (Buffer[m_Index++] << 8))) != 0 )
{
if ( IsSafeChar( c ) )
sb.Append( (char)c );
}
while (m_Index + 1 < Size && (c = (Buffer[m_Index++] << 8) | Buffer[m_Index++]) != 0)
if (IsSafeChar(c))
sb.Append((char)c);
return sb.ToString();
}
return sb.ToString();
}
public string ReadUnicodeStringSafe()
{
StringBuilder sb = new StringBuilder();
public string ReadUnicodeString()
{
StringBuilder sb = new StringBuilder();
int c;
int c;
while ( (m_Index + 1) < Size && (c = ((Buffer[m_Index++] << 8) | Buffer[m_Index++])) != 0 )
{
if ( IsSafeChar( c ) )
sb.Append( (char)c );
}
while (m_Index + 1 < Size && (c = (Buffer[m_Index++] << 8) | Buffer[m_Index++]) != 0)
sb.Append((char)c);
return sb.ToString();
}
return sb.ToString();
}
public string ReadUnicodeString()
{
StringBuilder sb = new StringBuilder();
public bool IsSafeChar(int c)
{
return c >= 0x20 && c < 0xFFFE;
}
int c;
public string ReadUTF8StringSafe(int fixedLength)
{
if (m_Index >= Size)
{
m_Index += fixedLength;
return string.Empty;
}
while ( (m_Index + 1) < Size && (c = ((Buffer[m_Index++] << 8) | Buffer[m_Index++])) != 0 )
sb.Append( (char)c );
int bound = m_Index + fixedLength;
//int end = bound;
return sb.ToString();
}
if (bound > Size)
bound = Size;
public bool IsSafeChar( int c )
{
return ( c >= 0x20 && c < 0xFFFE );
}
int count = 0;
int index = m_Index;
int start = m_Index;
public string ReadUTF8StringSafe( int fixedLength )
{
if ( m_Index >= Size )
{
m_Index += fixedLength;
return string.Empty;
}
while (index < bound && Buffer[index++] != 0)
++count;
int bound = m_Index + fixedLength;
//int end = bound;
index = 0;
if ( bound > Size )
bound = Size;
byte[] buffer = new byte[count];
int value = 0;
int count = 0;
int index = m_Index;
int start = m_Index;
while (m_Index < bound && (value = Buffer[m_Index++]) != 0)
buffer[index++] = (byte)value;
while ( index < bound && Buffer[index++] != 0 )
++count;
string s = Utility.UTF8.GetString(buffer);
index = 0;
bool isSafe = true;
byte[] buffer = new byte[count];
int value = 0;
for (int i = 0; isSafe && i < s.Length; ++i)
isSafe = IsSafeChar(s[i]);
while ( m_Index < bound && (value = Buffer[m_Index++]) != 0 )
buffer[index++] = (byte)value;
m_Index = start + fixedLength;
string s = Utility.UTF8.GetString( buffer );
if (isSafe)
return s;
bool isSafe = true;
StringBuilder sb = new StringBuilder(s.Length);
for ( int i = 0; isSafe && i < s.Length; ++i )
isSafe = IsSafeChar( (int) s[i] );
for (int i = 0; i < s.Length; ++i)
if (IsSafeChar(s[i]))
sb.Append(s[i]);
m_Index = start + fixedLength;
return sb.ToString();
}
if ( isSafe )
return s;
public string ReadUTF8StringSafe()
{
if (m_Index >= Size)
return string.Empty;
StringBuilder sb = new StringBuilder( s.Length );
int count = 0;
int index = m_Index;
for ( int i = 0; i < s.Length; ++i )
if ( IsSafeChar( (int) s[i] ) )
sb.Append( s[i] );
while (index < Size && Buffer[index++] != 0)
++count;
return sb.ToString();
}
index = 0;
public string ReadUTF8StringSafe()
{
if ( m_Index >= Size )
return string.Empty;
byte[] buffer = new byte[count];
int value = 0;
int count = 0;
int index = m_Index;
while (m_Index < Size && (value = Buffer[m_Index++]) != 0)
buffer[index++] = (byte)value;
while ( index < Size && Buffer[index++] != 0 )
++count;
string s = Utility.UTF8.GetString(buffer);
index = 0;
bool isSafe = true;
byte[] buffer = new byte[count];
int value = 0;
for (int i = 0; isSafe && i < s.Length; ++i)
isSafe = IsSafeChar(s[i]);
while ( m_Index < Size && (value = Buffer[m_Index++]) != 0 )
buffer[index++] = (byte)value;
if (isSafe)
return s;
string s = Utility.UTF8.GetString( buffer );
StringBuilder sb = new StringBuilder(s.Length);
bool isSafe = true;
for (int i = 0; i < s.Length; ++i)
if (IsSafeChar(s[i]))
sb.Append(s[i]);
for ( int i = 0; isSafe && i < s.Length; ++i )
isSafe = IsSafeChar( (int) s[i] );
return sb.ToString();
}
if ( isSafe )
return s;
public string ReadUTF8String()
{
if (m_Index >= Size)
return string.Empty;
StringBuilder sb = new StringBuilder( s.Length );
int count = 0;
int index = m_Index;
for ( int i = 0; i < s.Length; ++i )
{
if ( IsSafeChar( (int) s[i] ) )
sb.Append( s[i] );
}
while (index < Size && Buffer[index++] != 0)
++count;
return sb.ToString();
}
index = 0;
public string ReadUTF8String()
{
if ( m_Index >= Size )
return string.Empty;
byte[] buffer = new byte[count];
int value = 0;
int count = 0;
int index = m_Index;
while (m_Index < Size && (value = Buffer[m_Index++]) != 0)
buffer[index++] = (byte)value;
while ( index < Size && Buffer[index++] != 0 )
++count;
return Utility.UTF8.GetString(buffer);
}
index = 0;
public string ReadString()
{
StringBuilder sb = new StringBuilder();
byte[] buffer = new byte[count];
int value = 0;
int c;
while ( m_Index < Size && (value = Buffer[m_Index++]) != 0 )
buffer[index++] = (byte)value;
while (m_Index < Size && (c = Buffer[m_Index++]) != 0)
sb.Append((char)c);
return Utility.UTF8.GetString( buffer );
}
return sb.ToString();
}
public string ReadString()
{
StringBuilder sb = new StringBuilder();
public string ReadStringSafe()
{
StringBuilder sb = new StringBuilder();
int c;
int c;
while ( m_Index < Size && (c = Buffer[m_Index++]) != 0 )
sb.Append( (char)c );
while (m_Index < Size && (c = Buffer[m_Index++]) != 0)
if (IsSafeChar(c))
sb.Append((char)c);
return sb.ToString();
}
return sb.ToString();
}
public string ReadStringSafe()
{
StringBuilder sb = new StringBuilder();
public string ReadUnicodeStringSafe(int fixedLength)
{
int bound = m_Index + (fixedLength << 1);
int end = bound;
int c;
if (bound > Size)
bound = Size;
while ( m_Index < Size && (c = Buffer[m_Index++]) != 0 )
{
if ( IsSafeChar( c ) )
sb.Append( (char)c );
}
StringBuilder sb = new StringBuilder();
return sb.ToString();
}
int c;
public string ReadUnicodeStringSafe( int fixedLength )
{
int bound = m_Index + (fixedLength << 1);
int end = bound;
while (m_Index + 1 < bound && (c = (Buffer[m_Index++] << 8) | Buffer[m_Index++]) != 0)
if (IsSafeChar(c))
sb.Append((char)c);
if ( bound > Size )
bound = Size;
m_Index = end;
StringBuilder sb = new StringBuilder();
return sb.ToString();
}
int c;
public string ReadUnicodeString(int fixedLength)
{
int bound = m_Index + (fixedLength << 1);
int end = bound;
while ( (m_Index + 1) < bound && (c = ((Buffer[m_Index++] << 8) | Buffer[m_Index++])) != 0 )
{
if ( IsSafeChar( c ) )
sb.Append( (char)c );
}
if (bound > Size)
bound = Size;
m_Index = end;
StringBuilder sb = new StringBuilder();
return sb.ToString();
}
int c;
public string ReadUnicodeString( int fixedLength )
{
int bound = m_Index + (fixedLength << 1);
int end = bound;
while (m_Index + 1 < bound && (c = (Buffer[m_Index++] << 8) | Buffer[m_Index++]) != 0)
sb.Append((char)c);
if ( bound > Size )
bound = Size;
m_Index = end;
StringBuilder sb = new StringBuilder();
return sb.ToString();
}
int c;
public string ReadStringSafe(int fixedLength)
{
int bound = m_Index + fixedLength;
int end = bound;
while ( (m_Index + 1) < bound && (c = ((Buffer[m_Index++] << 8) | Buffer[m_Index++])) != 0 )
sb.Append( (char)c );
if (bound > Size)
bound = Size;
m_Index = end;
StringBuilder sb = new StringBuilder();
return sb.ToString();
}
int c;
public string ReadStringSafe( int fixedLength )
{
int bound = m_Index + fixedLength;
int end = bound;
while (m_Index < bound && (c = Buffer[m_Index++]) != 0)
if (IsSafeChar(c))
sb.Append((char)c);
if ( bound > Size )
bound = Size;
m_Index = end;
StringBuilder sb = new StringBuilder();
return sb.ToString();
}
int c;
public string ReadString(int fixedLength)
{
int bound = m_Index + fixedLength;
int end = bound;
while ( m_Index < bound && (c = Buffer[m_Index++]) != 0 )
{
if ( IsSafeChar( c ) )
sb.Append( (char)c );
}
if (bound > Size)
bound = Size;
m_Index = end;
StringBuilder sb = new StringBuilder();
return sb.ToString();
}
int c;
public string ReadString( int fixedLength )
{
int bound = m_Index + fixedLength;
int end = bound;
while (m_Index < bound && (c = Buffer[m_Index++]) != 0)
sb.Append((char)c);
if ( bound > Size )
bound = Size;
m_Index = end;
StringBuilder sb = new StringBuilder();
int c;
while ( m_Index < bound && (c = Buffer[m_Index++]) != 0 )
sb.Append( (char)c );
m_Index = end;
return sb.ToString();
}
}
return sb.ToString();
}
}
}