Server.Network.PacketReader.ReadUTF8StringSafe C# (CSharp) Méthode

ReadUTF8StringSafe() public méthode

public ReadUTF8StringSafe ( ) : string
Résultat string
		public string ReadUTF8StringSafe()
		{
			if ( m_Index >= m_Size )
				return String.Empty;

			int count = 0;
			int index = m_Index;

			while ( index < m_Size && m_Data[index++] != 0 )
				++count;

			index = 0;

			byte[] buffer = new byte[count];
			int value = 0;

			while ( m_Index < m_Size && (value = m_Data[m_Index++]) != 0 )
				buffer[index++] = (byte)value;

			string s = Utility.UTF8.GetString( buffer );

			bool isSafe = true;

			for ( int i = 0; isSafe && i < s.Length; ++i )
				isSafe = IsSafeChar( (int) s[i] );

			if ( isSafe )
				return s;

			StringBuilder sb = new StringBuilder( s.Length );

			for ( int i = 0; i < s.Length; ++i )
			{
				if ( IsSafeChar( (int) s[i] ) )
					sb.Append( s[i] );
			}

			return sb.ToString();
		}

Same methods

PacketReader::ReadUTF8StringSafe ( int fixedLength ) : string

Usage Example

		public static void UnicodeSpeech( NetState state, PacketReader pvSrc )
		{
			Mobile from = state.Mobile;

			MessageType type = (MessageType)pvSrc.ReadByte();
			int hue = pvSrc.ReadInt16();
			pvSrc.ReadInt16(); // font
			string lang = pvSrc.ReadString( 4 );
			string text;

			bool isEncoded = (type & MessageType.Encoded) != 0;
			int[] keywords;

			if ( isEncoded )
			{
				int value = pvSrc.ReadInt16();
				int count = (value & 0xFFF0) >> 4;
				int hold = value & 0xF;

				if ( count < 0 || count > 50 )
					return;

				KeywordList keyList = m_KeywordList;

				for ( int i = 0; i < count; ++i )
				{
					int speechID;

					if ( (i & 1) == 0 )
					{
						hold <<= 8;
						hold |= pvSrc.ReadByte();
						speechID = hold;
						hold = 0;
					}
					else
					{
						value = pvSrc.ReadInt16();
						speechID = (value & 0xFFF0) >> 4;
						hold = value & 0xF;
					}

					if ( !keyList.Contains( speechID ) )
						keyList.Add( speechID );
				}

				text = pvSrc.ReadUTF8StringSafe();

				keywords = keyList.ToArray();
			}
			else
			{
				text = pvSrc.ReadUnicodeStringSafe();

				keywords = m_EmptyInts;
			}

			text = text.Trim();

			if ( text.Length <= 0 || text.Length > 128 )
				return;

			type &= ~MessageType.Encoded;

			if ( !Enum.IsDefined( typeof( MessageType ), type ) )
				type = MessageType.Regular;

			from.Language = lang;
			from.DoSpeech( text, keywords, type, Utility.ClipDyedHue( hue ) );
		}
All Usage Examples Of Server.Network.PacketReader::ReadUTF8StringSafe