Skip to content

Commit 173adcc

Browse files
committed
Massively simplify.
1 parent 41aee2f commit 173adcc

File tree

1 file changed

+7
-77
lines changed

1 file changed

+7
-77
lines changed

src/Simfile/SaveSm.cpp

Lines changed: 7 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -468,83 +468,20 @@ static inline bool TestSectionCompression(const char* section, int width, int qu
468468
return quant >= MIN_SECTIONS_PER_MEASURE;
469469
}
470470

471-
static bool GetSectionCompression(const char* section, int width, std::list<uint> quantVec, int& count, int& pitch)
471+
static void GetSectionCompression(const char* section, int width, int& count, int& pitch)
472472
{
473+
count = ROWS_PER_NOTE_SECTION;
473474
// Determines the best compression for the given section.
474-
bool error = false;
475-
int best = ROWS_PER_NOTE_SECTION;
476-
String zeroline(width, '0');
477-
std::list<uint>::iterator it;
478-
int lcm = 1;
479-
for (it = quantVec.begin(); it != quantVec.end(); it++)
475+
// We actually don't care about custom snaps for this at all, since we want to save 192nds for non-standard-compressible snaps.
476+
for (int i = 1; i < NUM_MEASURE_SUBDIV - 1; i++)
480477
{
481-
if (*it <= 0 || *it > 192)
482-
{
483-
// If there's a quantization error assume nothing
484-
error = true;
485-
lcm = ROWS_PER_NOTE_SECTION;
486-
break;
487-
}
488-
lcm = lcm * *it / gcd(lcm, *it);
489-
if (lcm > ROWS_PER_NOTE_SECTION)
478+
if (TestSectionCompression(section, width, MEASURE_SUBDIV[i]))
490479
{
491-
lcm = ROWS_PER_NOTE_SECTION;
480+
count = MEASURE_SUBDIV[i];
492481
break;
493482
}
494483
}
495-
496-
// Set whole and half step measures to be quarter notes by default
497-
if (lcm <= MIN_SECTIONS_PER_MEASURE)
498-
{
499-
count = MIN_SECTIONS_PER_MEASURE;
500-
}
501-
else
502-
{
503-
// Determines the best compression for the given section.
504-
// Maybe lcm is the best factor, so just keep that.
505-
count = lcm;
506-
bool valid = false;
507-
//The factor list is small, just check them all by hand, but only up to 96 at most since there won't be factors otherwise
508-
for (int i = 4; i <= lcm / 2; i++)
509-
{
510-
// Skip anything that isn't a lcm factor
511-
if (lcm % i > 0) continue;
512-
513-
// The first (smallest) match is always the best
514-
if (TestSectionCompression(section, width, i))
515-
{
516-
valid = true;
517-
count = i;
518-
break;
519-
}
520-
}
521-
522-
// If no factor was found, double-check we won't have any data loss from our lcm guess
523-
// Why not check all factors? Saving files would be several times slower otherwise
524-
if (!valid)
525-
{
526-
valid = TestSectionCompression(section, width, lcm);
527-
}
528-
if (!valid)
529-
{
530-
// Okay, we WOULD have had data loss, so set the rows to 192 and error
531-
error = true;
532-
count = ROWS_PER_NOTE_SECTION;
533-
}
534-
}
535-
// Is our factor a standard snap? If so, use it.
536-
// If not, save the measure as 192nds for SM5 Editor compatibility.
537-
if (ROWS_PER_NOTE_SECTION % count != 0)
538-
{
539-
count = ROWS_PER_NOTE_SECTION;
540-
}
541-
// Yes it is weird... but we don't save 6ths even though they factor into 192 evenly.
542-
if (count == 6)
543-
{
544-
count = 12;
545-
}
546484
pitch = (ROWS_PER_NOTE_SECTION * width) / count;
547-
return error;
548485
}
549486

550487
static void WriteSections(ExportData& data)
@@ -594,20 +531,17 @@ static void WriteSections(ExportData& data)
594531
if(it->row == it->endrow)
595532
{
596533
section[pos] = GetNoteChar(it->type);
597-
quantVec.push_front(it->quant);
598534
}
599535
else
600536
{
601537
section[pos] = GetHoldChar(it->type);
602-
quantVec.push_front(it->quant);
603538
auto hold = holds[it->col];
604539
if(hold)
605540
{
606541
if((int)hold->endrow >= startRow && (int)hold->endrow < endRow)
607542
{
608543
int pos = ((int)hold->endrow - startRow) * numCols + (int)hold->col;
609544
section[pos] = '3';
610-
quantVec.push_front(holds[it->col]->quant);
611545
--remainingHolds;
612546
}
613547
}
@@ -629,7 +563,6 @@ static void WriteSections(ExportData& data)
629563
{
630564
int pos = (hold->endrow - startRow) * numCols + hold->col;
631565
section[pos] = '3';
632-
quantVec.push_front(holds[col]->quant);
633566
holds[col] = nullptr;
634567
--remainingHolds;
635568
}
@@ -641,10 +574,7 @@ static void WriteSections(ExportData& data)
641574
int count, pitch;
642575
const char* m = section;
643576
quantVec.unique();
644-
if (GetSectionCompression(m, numCols, quantVec, count, pitch))
645-
{
646-
HudError("Bug: invalid quantization recorded in chart in measure starting at row %d", startRow);
647-
}
577+
GetSectionCompression(m, numCols, count, pitch);
648578
quantVec.clear();
649579
if (ROWS_PER_NOTE_SECTION % count != 0)
650580
{

0 commit comments

Comments
 (0)