/// <summary> /// Reads a GameMessage from the BitStream. /// </summary> /// <param name="bitStream">BitStream to read from.</param> /// <param name="gameMessages">Collection of GameMessages to use to grab the message.</param> /// <returns>String of the parsed GameMessage read from the BitStream.</returns> /// <exception cref="ArgumentNullException"><paramref name="gameMessages" /> is <c>null</c>.</exception> public static string ReadGameMessage(this BitStream bitStream, GameMessageCollection gameMessages) { if (gameMessages == null) { throw new ArgumentNullException("gameMessages"); } var messageID = bitStream.ReadUInt(_gameMessageIDBits); var paramCount = bitStream.ReadByte(); // Parse the parameters string[] parameters = null; if (paramCount > 0) { parameters = new string[paramCount]; for (var i = 0; i < paramCount; i++) { parameters[i] = bitStream.ReadString(GameData.MaxServerMessageParameterLength); } } // Parse the message and return it var gameMessage = (GameMessage)messageID; var message = gameMessages.GetMessage(gameMessage, parameters); return(message); }
/// <summary> /// Gets a message from a <see cref="GameMessage"/> packed into a string using <see cref="GameMessageHelper.AsString"/>. /// </summary> /// <param name="coll">The <see cref="GameMessageCollection"/>.</param> /// <param name="s">The <see cref="GameMessage"/> and arguments packed into a string.</param> /// <returns>The parsed <see cref="GameMessage"/>, or null if the parsing failed.</returns> public static string TryGetMessageFromString(this GameMessageCollection coll, string s) { try { var kvp = GameMessageHelper.FromString(s); return(coll.GetMessage(kvp.Key, kvp.Value)); } catch (ArgumentException) { return(null); } catch (FormatException) { return(null); } }
/// <summary> /// Gets a message from a <see cref="GameMessage"/> packed into a string using <see cref="GameMessageHelper.AsString"/>. /// </summary> /// <param name="coll">The <see cref="GameMessageCollection"/>.</param> /// <param name="s">The <see cref="GameMessage"/> and arguments packed into a string.</param> /// <returns>The parsed <see cref="GameMessage"/>.</returns> public static string GetMessageFromString(this GameMessageCollection coll, string s) { var kvp = GameMessageHelper.FromString(s); return(coll.GetMessage(kvp.Key, kvp.Value)); }