Exemplo n.º 1
0
 /// <summary>
 /// This method will remove a note from the list of custom notes
 /// </summary>
 public static void RemoveNote(Note note)
 {
     if (CheckNoteExists(note))
     {
         CustomNotes.Remove(note);
     }
     else
     {
         throw new AutoplayerCustomNoteException("Trying to remove non-existent note");
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// This method will add a note to the list of custom notes
 /// </summary>
 public static void AddNote(Note note, Note newNote)
 {
     if (!CheckNoteExists(note, newNote))
     {
         CustomNotes.Add(note, newNote);
     }
     else
     {
         throw new AutoplayerCustomNoteException("Trying to add already existing note");
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// This method will check if a note exists in the list of custom notes
        /// as well as the connection between the note and the new note of the pair
        /// </summary>
        public static bool CheckNoteExists(Note note, Note newNote)
        {
            Note checkNote;

            CustomNotes.TryGetValue(note, out checkNote);
            if (checkNote != null && checkNote == newNote)
            {
                return(true);
            }
            return(false);
        }
Exemplo n.º 4
0
 /// <summary>
 /// This method will set a new note to the value of a specified note from the list of custom notes
 /// </summary>
 public static void ChangeNote(Note note, Note newNote)
 {
     if (CheckNoteExists(note))
     {
         CustomNotes.Remove(note);
         CustomNotes.Add(note, newNote);
     }
     else
     {
         throw new AutoplayerCustomNoteException("Trying to modify non-existent note");
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// This method will load a song and its settings from a file at the "path" variable's destination
        /// This loading method handles all previous save formats for backwards compatibility
        /// </summary>
        public static void LoadSong(string path)
        {
            Song.Clear();
            bool         errorWhileLoading = true;
            StreamReader sr        = new StreamReader(path);
            string       firstLine = sr.ReadLine();

            #region 2.1+ save format
            if (SupportedVersionsSave.Contains(firstLine))
            {
                if (sr.ReadLine() == "DELAYS")
                {
                    int delayCount = 0;
                    if (int.TryParse(sr.ReadLine(), out delayCount) && delayCount > 0)
                    {
                        for (int i = 0; i < delayCount; i++)
                        {
                            char delayChar;
                            int  delayTime = 0;
                            if (char.TryParse(sr.ReadLine(), out delayChar))
                            {
                                if (int.TryParse(sr.ReadLine(), out delayTime))
                                {
                                    Delays.Add(new Delay(delayChar, delayTime));
                                }
                            }
                        }
                    }
                }
                if (sr.ReadLine() == "CUSTOM NOTES")
                {
                    int noteCount = 0;
                    if (int.TryParse(sr.ReadLine(), out noteCount) && noteCount > 0)
                    {
                        for (int i = 0; i < noteCount; i++)
                        {
                            char origNoteChar;
                            char replaceNoteChar;
                            if (char.TryParse(sr.ReadLine(), out origNoteChar))
                            {
                                if (char.TryParse(sr.ReadLine(), out replaceNoteChar))
                                {
                                    WindowsInput.Native.VirtualKeyCode vkOld;
                                    WindowsInput.Native.VirtualKeyCode vkNew;
                                    try
                                    {
                                        VirtualDictionary.TryGetValue(origNoteChar, out vkOld);
                                        VirtualDictionary.TryGetValue(replaceNoteChar, out vkNew);

                                        if (vkOld == 0 || vkNew == 0)
                                        {
                                            return;
                                        }
                                    }
                                    catch (ArgumentNullException)
                                    {
                                        return;
                                    }

                                    CustomNotes.Add(new Note(origNoteChar, vkOld, char.IsUpper(origNoteChar)), new Note(replaceNoteChar, vkNew, char.IsUpper(replaceNoteChar)));
                                }
                            }
                        }
                    }
                }
                if (sr.ReadLine() == "SPEEDS")
                {
                    int normalSpeed, fastSpeed;
                    int.TryParse(sr.ReadLine(), out normalSpeed);
                    int.TryParse(sr.ReadLine(), out fastSpeed);
                    DelayAtNormalSpeed = normalSpeed;
                    DelayAtFastSpeed   = fastSpeed;
                }
                if (sr.ReadLine() == "NOTES")
                {
                    int noteCount = 0;
                    if (int.TryParse(sr.ReadLine(), out noteCount) && noteCount > 0)
                    {
                        AddNotesFromString(sr.ReadToEnd());
                    }
                    errorWhileLoading = false;
                }
            }
            #endregion
            #region 2.0 save format (for backwards compatibility)
            if (firstLine == "DELAYS")
            {
                int delayCount = 0;
                if (int.TryParse(sr.ReadLine(), out delayCount) && delayCount > 0)
                {
                    for (int i = 0; i < delayCount; i++)
                    {
                        char delayChar;
                        int  delayTime = 0;
                        if (char.TryParse(sr.ReadLine(), out delayChar))
                        {
                            if (int.TryParse(sr.ReadLine(), out delayTime))
                            {
                                Delays.Add(new Delay(delayChar, delayTime));
                            }
                        }
                    }
                }
                if (sr.ReadLine() == "NOTES")
                {
                    int noteCount = 0;
                    if (int.TryParse(sr.ReadLine(), out noteCount) && noteCount > 0)
                    {
                        AddNotesFromString(sr.ReadToEnd());
                    }
                    errorWhileLoading = false;
                }
            }
            #endregion
            #region 1.2.2 save format (For backwards compatibility)
            else if (firstLine == "CUSTOM DELAYS")
            {
                int delayCount = 0;
                if (int.TryParse(sr.ReadLine(), out delayCount))
                {
                    if (sr.ReadLine() == "NORMAL DELAY")
                    {
                        if (int.TryParse(sr.ReadLine(), out delayAtNormalSpeed))
                        {
                            Delays.Add(new Delay(' ', DelayAtNormalSpeed));
                            if (sr.ReadLine() == "FAST DELAY")
                            {
                                if (int.TryParse(sr.ReadLine(), out delayAtFastSpeed))
                                {
                                    if (delayCount != 0)
                                    {
                                        for (int i = 0; i < delayCount; i++)
                                        {
                                            int  customDelayIndex = 0;
                                            int  customDelayTime  = 0;
                                            char customDelayChar;

                                            if (sr.ReadLine() == "CUSTOM DELAY INDEX")
                                            {
                                                if (int.TryParse(sr.ReadLine(), out customDelayIndex))
                                                {
                                                    if (sr.ReadLine() == "CUSTOM DELAY CHARACTER")
                                                    {
                                                        if (char.TryParse(sr.ReadLine(), out customDelayChar))
                                                        {
                                                            if (sr.ReadLine() == "CUSTOM DELAY TIME")
                                                            {
                                                                if (int.TryParse(sr.ReadLine(), out customDelayTime))
                                                                {
                                                                    Delays.Add(new Delay(customDelayChar, customDelayTime));
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    if (sr.ReadLine() == "NOTES")
                                    {
                                        AddNotesFromString(sr.ReadToEnd());
                                        errorWhileLoading = false;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            #endregion
            #region 1.0 save format (For backwards compatibility)
            else if (firstLine == "NORMAL DELAY")
            {
                if (int.TryParse(sr.ReadLine(), out delayAtNormalSpeed))
                {
                    if (sr.ReadLine() == "FAST DELAY")
                    {
                        if (int.TryParse(sr.ReadLine(), out delayAtFastSpeed))
                        {
                            if (sr.ReadLine() == "NOTES")
                            {
                                AddNotesFromString(sr.ReadToEnd());
                                errorWhileLoading = false;
                            }
                        }
                    }
                }
            }
            #endregion
            sr.Close();
            if (errorWhileLoading)
            {
                LoadFailed?.Invoke();
                throw new AutoplayerLoadFailedException("No compatible save format was found!");
            }
            LoadCompleted?.Invoke();
        }
Exemplo n.º 6
0
 /// <summary>
 /// This method will play the notes in the song in sequence
 /// </summary>
 public static void PlaySong()
 {
     Stop = false;
     foreach (INote note in Song)
     {
         if (!Stop)
         {
             try
             {
                 if (note is Note)
                 {
                     if (CustomNotes.ContainsKey((Note)note))
                     {
                         Note customNote;
                         CustomNotes.TryGetValue((Note)note, out customNote);
                         customNote.Play();
                     }
                     else
                     {
                         note.Play();
                     }
                 }
                 else
                 {
                     note.Play();
                 }
                 if (note is Note || note is MultiNote)
                 {
                     if (isFastSpeed)
                     {
                         Thread.Sleep(DelayAtFastSpeed);
                     }
                     else
                     {
                         Thread.Sleep(DelayAtNormalSpeed);
                     }
                 }
             }
             catch (AutoplayerTargetNotFoundException error)
             {
                 Stop = true;
                 SongWasInteruptedByException?.Invoke(error);
             }
             catch (ArgumentException)
             {
                 Stop = true;
                 SongWasInteruptedByException?.Invoke(new AutoplayerException($"The program encountered an invalid note. Please inform the developer of this incident so it can be added to the list of invalid characters. Info: '{note.ToString()}'"));
             }
         }
         else
         {
             //The foreach loop here is to avoid any keys getting stuck when the song is stopped by the stop keybinding
             foreach (INote n in Song)
             {
                 if (n is Note)
                 {
                     ((Note)n).Stop();
                 }
                 if (n is MultiNote)
                 {
                     ((MultiNote)n).Stop();
                 }
             }
             SongWasStopped?.Invoke();
         }
     }
     if (Loop)
     {
         PlaySong();
     }
     SongFinishedPlaying?.Invoke();
 }
Exemplo n.º 7
0
 /// <summary>
 /// This method will remove all notes from the list of custom notes
 /// </summary>
 public static void ResetNotes()
 {
     CustomNotes.Clear();
 }
Exemplo n.º 8
0
 /// <summary>
 /// This method will check if a note exists in the list of custom notes
 /// If it does, it returns true, otherwise it returns false
 /// </summary>
 public static bool CheckNoteExists(Note note)
 {
     return(CustomNotes.ContainsKey(note));
 }