/// <summary> /// Tries to parse the MusicID from a string. /// </summary> /// <param name="parser">The Parser to use.</param> /// <param name="value">The string to parse.</param> /// <param name="outValue">If this method returns true, contains the parsed MusicID.</param> /// <returns>True if the parsing was successfully; otherwise false.</returns> public static bool TryParse(this Parser parser, string value, out MusicID outValue) { ushort tmp; var ret = parser.TryParse(value, out tmp); outValue = new MusicID(tmp); return(ret); }
/// <summary> /// Gets the <see cref="IMusicInfo"/> for a music track. /// </summary> /// <param name="id">The id of the <see cref="IMusicInfo"/> to get.</param> /// <returns>The <see cref="IMusicInfo"/> for the given <paramref name="id"/>, or null if the value /// was invalid.</returns> public IMusicInfo GetMusicInfo(MusicID id) { var i = (int)id; if (i < 0 || i >= _infos.Length) { return(null); } return(_infos[i]); }
/// <summary> /// Plays a music track by the given <see cref="MusicID"/>. /// </summary> /// <param name="id">The ID of the music to play.</param> /// <returns> /// True if the music played successfully; otherwise false. /// </returns> public bool Play(MusicID id) { try { // If the music is already playing, continue to play it if (_playingInfo != null && _playingInfo.ID == id) { if (_playing.Status != SoundStatus.Playing) { _playing.Play(); } return(true); } // Stop the old music Stop(); // Get the info for the music to play var info = GetMusicInfo(id); if (info == null) { return(false); } // Start the new music _playingInfo = info; var file = GetFilePath(info); try { _playing = new Music(file); } catch (LoadingFailedException ex) { const string errmsg = "Failed to load music `{0}`: {1}"; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, info, ex); } Debug.Fail(string.Format(errmsg, info, ex)); _playing = null; _playingInfo = null; return(false); } // Set the values for the music and start playing it _playing.Volume = Volume; _playing.Loop = Loop; _playing.RelativeToListener = true; _playing.Play(); } catch (Exception ex) { const string errmsg = "Failed to play music with ID `{0}`. Exception: {1}"; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, id, ex); } Debug.Fail(string.Format(errmsg, id, ex)); } return(true); }
/// <summary> /// Writes a MusicID to a IValueWriter. /// </summary> /// <param name="valueWriter">IValueWriter to write to.</param> /// <param name="name">Unique name of the MusicID that will be used to distinguish it /// from other values when reading.</param> /// <param name="value">MusicID to write.</param> public static void Write(this IValueWriter valueWriter, string name, MusicID value) { value.Write(valueWriter, name); }
/// <summary> /// Writes a MusicID to a BitStream. /// </summary> /// <param name="bitStream">BitStream to write to.</param> /// <param name="value">MusicID to write.</param> public static void Write(this BitStream bitStream, MusicID value) { value.Write(bitStream); }
/// <summary> /// Reads the MusicID from an IValueReader. /// </summary> /// <param name="valueReader">IValueReader to read the MusicID from.</param> /// <param name="name">The unique name of the value to read.</param> /// <returns>The MusicID read from the IValueReader.</returns> public static MusicID ReadMusicID(this IValueReader valueReader, string name) { return(MusicID.Read(valueReader, name)); }
/// <summary> /// Reads the MusicID from a BitStream. /// </summary> /// <param name="bitStream">BitStream to read the MusicID from.</param> /// <returns>The MusicID read from the BitStream.</returns> public static MusicID ReadMusicID(this BitStream bitStream) { return(MusicID.Read(bitStream)); }
/// <summary> /// Reads the MusicID from an <see cref="IDataRecord"/>. /// </summary> /// <param name="r"><see cref="IDataRecord"/> to read the MusicID from.</param> /// <param name="name">The name of the field to read the value from.</param> /// <returns>The MusicID read from the <see cref="IDataRecord"/>.</returns> public static MusicID GetMusicID(this IDataRecord r, string name) { return(MusicID.Read(r, name)); }
/// <summary> /// Reads the MusicID from an <see cref="IDataRecord"/>. /// </summary> /// <param name="r"><see cref="IDataRecord"/> to read the MusicID from.</param> /// <param name="i">The field index to read.</param> /// <returns>The MusicID read from the <see cref="IDataRecord"/>.</returns> public static MusicID GetMusicID(this IDataRecord r, int i) { return(MusicID.Read(r, i)); }
/// <summary> /// Tries to get the value in the <paramref name="dict"/> entry at the given <paramref name="key"/> as type MusicID. /// </summary> /// <typeparam name="T">The key Type.</typeparam> /// <param name="dict">The IDictionary.</param> /// <param name="key">The key for the value to get.</param> /// <param name="defaultValue">The value to use if the value at the <paramref name="key"/> could not be parsed.</param> /// <returns>The value at the given <paramref name="key"/> parsed as an int, or the /// <paramref name="defaultValue"/> if the <paramref name="key"/> did not exist in the <paramref name="dict"/> /// or the value at the given <paramref name="key"/> could not be parsed.</returns> public static MusicID AsMusicID <T>(this IDictionary <T, string> dict, T key, MusicID defaultValue) { string value; if (!dict.TryGetValue(key, out value)) { return(defaultValue); } MusicID parsed; if (!Parser.Invariant.TryParse(value, out parsed)) { return(defaultValue); } return(parsed); }
/// <summary> /// Initializes a new instance of the <see cref="MusicInfo"/> class. /// </summary> /// <param name="name">The name.</param> /// <param name="id">The id.</param> public MusicInfo(string name, MusicID id) { _name = name; _id = id; }