Skip to content

Commit 772862f

Browse files
Declare 1 constant for string length, instead of constantly hardcoding its value of 64.
1 parent 43792a9 commit 772862f

File tree

6 files changed

+18
-14
lines changed

6 files changed

+18
-14
lines changed

fCraft/Commands/WorldCommands.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3038,8 +3038,8 @@ static void SetMOTD(Player player, World world, string value) {
30383038
}
30393039
return;
30403040
}
3041-
if (value.Length > 64)
3042-
value = value.Substring(0, 64);
3041+
if (value.Length > Packet.StringSize)
3042+
value = value.Substring(0, Packet.StringSize);
30433043

30443044
if (value.CaselessEquals("remove") || value.CaselessEquals("delete") || value.CaselessEquals("reset")) {
30453045
player.Message("MOTD for \"{0}\" has been removed", world.Name);

fCraft/Network/Packet.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ public partial struct Packet {
1111
/// When used in AddEntity packet, sets player's own respawn point.
1212
/// When used in Teleport packet, teleports the player. </summary>
1313
public const sbyte SelfId = -1;
14+
15+
/// <summary> Maximum number of characters allowed in a string. </summary>
16+
public const int StringSize = 64;
17+
1418

1519
/// <summary> Raw bytes of this packet. </summary>
1620
public readonly byte[] Bytes;

fCraft/Network/PacketReader.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ public override int ReadInt32() {
2929
}
3030

3131

32-
char[] characters = new char[64];
32+
char[] characters = new char[Packet.StringSize];
3333
public override string ReadString() {
3434
int length = 0;
35-
byte[] data = ReadBytes( 64 );
36-
for( int i = 63; i >= 0; i-- ) {
35+
byte[] data = ReadBytes( Packet.StringSize );
36+
for( int i = Packet.StringSize - 1; i >= 0; i-- ) {
3737
byte code = data[i];
3838
if( length == 0 && !(code == 0 || code == 0x20) )
3939
length = i + 1;

fCraft/Network/PacketWriter.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public override void Write( ushort data ) {
2828

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

3535

@@ -38,22 +38,21 @@ public static void WriteString(string str, byte[] array, int offset, bool hasCP4
3838
else WriteAscii(str, array, offset);
3939
}
4040

41-
const int StringSize = 64;
4241
static void WriteAscii(string str, byte[] array, int offset) {
43-
int count = Math.Min(str.Length, StringSize);
42+
int count = Math.Min(str.Length, Packet.StringSize);
4443
for (int i = 0; i < count; i++) {
4544
char raw = str[i].UnicodeToCp437();
4645
array[offset + i] = raw >= '\u0080' ? (byte)'?' : (byte)raw;
4746
}
48-
for (int i = count; i < StringSize; i++)
47+
for (int i = count; i < Packet.StringSize; i++)
4948
array[offset + i] = (byte)' ';
5049
}
5150

5251
static void WriteCP437(string str, byte[] array, int offset) {
53-
int count = Math.Min(str.Length, StringSize);
52+
int count = Math.Min(str.Length, Packet.StringSize);
5453
for (int i = 0; i < count; i++)
5554
array[offset + i] = (byte)str[i].UnicodeToCp437();
56-
for (int i = count; i < StringSize; i++)
55+
for (int i = count; i < Packet.StringSize; i++)
5756
array[offset + i] = (byte)' ';
5857
}
5958
}

fCraft/Network/Player.Networking.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,8 @@ bool LoginSequence()
810810
Random random = new Random();
811811
int index = random.Next(0, MOTDlist.Length);
812812
motd = MOTDlist[index];
813-
if (motd.Length > 64) {
813+
814+
if (motd.Length > Packet.StringSize) {
814815
motd = "&0=&c=&e= Welcome to our server! &e=&c=&0=";
815816
} else {
816817
LastMotdMessage = motd;

fCraft/Player/Player.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ public void ParseMessage( [NotNull] string rawMessage, bool fromConsole ) {
404404
partialMessage = rawMessage.Substring(0, rawMessage.Length - 1);
405405
// Spaces at the end are trimmed by default, so we need to insert one.
406406
// +1 to length to account for λ at end
407-
if (originalMessage.Length != (64 + 1)) partialMessage += " ";
407+
if (originalMessage.Length != (Packet.StringSize + 1)) partialMessage += " ";
408408
break;
409409

410410
case RawMessageType.Invalid:

0 commit comments

Comments
 (0)