garcia / simfile

A modern simfile parsing & editing library for Python 3
MIT License
62 stars 7 forks source link

Early Edits fail to process #37

Open bjfranks opened 1 year ago

bjfranks commented 1 year ago

So, I was just going through files trying to count game elements and noticed that whenever an empty edit came up, the library crashed due to a list index being out of range. Here are three similar errors. For each, I give the file name and the index of the chart failing, followed by the stack trace. Find the files attached:

Dance Vibrations 2
Traceback (most recent call last):
  File "...", line 10, in calculate_statistics
    note_data = NoteData(simfileInstance.charts[chart])
  File "D:\Meatball\venv\lib\site-packages\simfile\notes\__init__.py", line 136, in __init__
    self._columns = NoteData._get_columns(self._notedata)
  File "D:\Meatball\venv\lib\site-packages\simfile\notes\__init__.py", line 142, in _get_columns
    first_line = first_measure.strip().splitlines()[0].strip()
IndexError: list index out of range

That Is Fair 4
Traceback (most recent call last):
  File "...", line 10, in calculate_statistics
    note_data = NoteData(simfileInstance.charts[chart])
  File "D:\Meatball\venv\lib\site-packages\simfile\notes\__init__.py", line 136, in __init__
    self._columns = NoteData._get_columns(self._notedata)
  File "D:\Meatball\venv\lib\site-packages\simfile\notes\__init__.py", line 142, in _get_columns
    first_line = first_measure.strip().splitlines()[0].strip()
IndexError: list index out of range

Virtual Emotion 0
Traceback (most recent call last):
  File "...", line 10, in calculate_statistics
    note_data = NoteData(simfileInstance.charts[chart])
  File "D:\Meatball\venv\lib\site-packages\simfile\notes\__init__.py", line 136, in __init__
    self._columns = NoteData._get_columns(self._notedata)
  File "D:\Meatball\venv\lib\site-packages\simfile\notes\__init__.py", line 142, in _get_columns
    first_line = first_measure.strip().splitlines()[0].strip()
IndexError: list index out of range

files.zip

garcia commented 1 year ago

The reason this happens is because NoteData determines the number of columns by looking at the first non-empty line of note data. This doesn't work when the note data is completely empty. I might set it to 0 for these cases. Another option would be to define a static mapping of stepstypes to column counts (dance-single → 4, dance-double → 8, etc.) but I'd like to avoid that pattern as much as possible.

garcia commented 4 months ago

Another option would be to define a static mapping of stepstypes to column counts (dance-single → 4, dance-double → 8, etc.)

Upon further reflection, I think that this is not only a reasonable fix for the bug, but makes more sense as the default method for determining column count than the current behavior ("looking at the first non-empty line of note data"). Will have to think some more about whether empty note data should even be considered a strict parse error.