Skip to content
Permalink
Browse files

Declare 1 constant for string length, instead of constantly hardcodin…

…g its value of 64.
  • Loading branch information...
UnknownShadow200 committed Jun 19, 2017
1 parent 43792a9 commit 772862fe75069a509cb0f581cc50063ebd14addd
@@ -3038,8 +3038,8 @@ static void WorldClearHandler(Player player, CommandReader cmd)
}
return;
}
if (value.Length > 64)
value = value.Substring(0, 64);
if (value.Length > Packet.StringSize)
value = value.Substring(0, Packet.StringSize);

if (value.CaselessEquals("remove") || value.CaselessEquals("delete") || value.CaselessEquals("reset")) {
player.Message("MOTD for \"{0}\" has been removed", world.Name);
@@ -11,6 +11,10 @@ public partial struct Packet {
/// When used in AddEntity packet, sets player's own respawn point.
/// When used in Teleport packet, teleports the player. </summary>
public const sbyte SelfId = -1;

/// <summary> Maximum number of characters allowed in a string. </summary>
public const int StringSize = 64;


/// <summary> Raw bytes of this packet. </summary>
public readonly byte[] Bytes;
@@ -29,11 +29,11 @@ sealed class PacketReader : BinaryReader {
}


char[] characters = new char[64];
char[] characters = new char[Packet.StringSize];
public override string ReadString() {
int length = 0;
byte[] data = ReadBytes( 64 );
for( int i = 63; i >= 0; i-- ) {
byte[] data = ReadBytes( Packet.StringSize );
for( int i = Packet.StringSize - 1; i >= 0; i-- ) {
byte code = data[i];
if( length == 0 && !(code == 0 || code == 0x20) )
length = i + 1;
@@ -28,8 +28,8 @@ public PacketWriter( [NotNull] Stream stream )

public override void Write( string str ) {
if( str == null ) throw new ArgumentNullException( "str" );
if( str.Length > 64 ) throw new ArgumentException( "String is too long (>64).", "str" );
Write( Encoding.ASCII.GetBytes( str.PadRight( 64 ) ) );
if( str.Length > Packet.StringSize ) throw new ArgumentException( "String is too long (>64).", "str" );
Write( Encoding.ASCII.GetBytes( str.PadRight( Packet.StringSize ) ) );
}


@@ -38,22 +38,21 @@ public PacketWriter( [NotNull] Stream stream )
else WriteAscii(str, array, offset);
}

const int StringSize = 64;
static void WriteAscii(string str, byte[] array, int offset) {
int count = Math.Min(str.Length, StringSize);
int count = Math.Min(str.Length, Packet.StringSize);
for (int i = 0; i < count; i++) {
char raw = str[i].UnicodeToCp437();
array[offset + i] = raw >= '\u0080' ? (byte)'?' : (byte)raw;
}
for (int i = count; i < StringSize; i++)
for (int i = count; i < Packet.StringSize; i++)
array[offset + i] = (byte)' ';
}

static void WriteCP437(string str, byte[] array, int offset) {
int count = Math.Min(str.Length, StringSize);
int count = Math.Min(str.Length, Packet.StringSize);
for (int i = 0; i < count; i++)
array[offset + i] = (byte)str[i].UnicodeToCp437();
for (int i = count; i < StringSize; i++)
for (int i = count; i < Packet.StringSize; i++)
array[offset + i] = (byte)' ';
}
}
@@ -810,7 +810,8 @@ bool LoginSequence()
Random random = new Random();
int index = random.Next(0, MOTDlist.Length);
motd = MOTDlist[index];
if (motd.Length > 64) {

if (motd.Length > Packet.StringSize) {
motd = "&0=&c=&e= Welcome to our server! &e=&c=&0=";
} else {
LastMotdMessage = motd;
@@ -404,7 +404,7 @@ public FontFamily LoadFontFamily(string fileName)
partialMessage = rawMessage.Substring(0, rawMessage.Length - 1);
// Spaces at the end are trimmed by default, so we need to insert one.
// +1 to length to account for λ at end
if (originalMessage.Length != (64 + 1)) partialMessage += " ";
if (originalMessage.Length != (Packet.StringSize + 1)) partialMessage += " ";
break;

case RawMessageType.Invalid:

0 comments on commit 772862f

Please sign in to comment.
You can’t perform that action at this time.