Skip to content

Commit

Permalink
Rework new chart loaders (YARC-Official#258)
Browse files Browse the repository at this point in the history
* Turn IChartLoader into an abstract class

* Encapsulate chart property info into ChartLoader

* Encapsulate event loading into ChartLoader

* Add some protected helper methods to ChartLoader

* Implement drums SP activation loading

* Create static instances for chart loaders

* Use null coalescence to initialize YargChart instrument properties
  • Loading branch information
TheNathannator authored May 6, 2023
1 parent 83358ed commit 7f684eb
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 116 deletions.
15 changes: 15 additions & 0 deletions Assets/Script/Chart/Loaders/ChartLoader.Instances.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using MoonscraperChartEditor.Song;

namespace YARG.Chart {
public static class ChartLoader {
public static readonly GuitarChartLoader GuitarLoader = new(MoonSong.MoonInstrument.Guitar);
public static readonly GuitarChartLoader GuitarCoopLoader = new(MoonSong.MoonInstrument.GuitarCoop);
public static readonly GuitarChartLoader RhythmLoader = new(MoonSong.MoonInstrument.Rhythm);
public static readonly GuitarChartLoader BassLoader = new(MoonSong.MoonInstrument.Bass);
public static readonly GuitarChartLoader KeysLoader = new(MoonSong.MoonInstrument.Keys);

public static readonly FourLaneDrumsChartLoader DrumsLoader = new(pro: false);
public static readonly FourLaneDrumsChartLoader ProDrumsLoader = new(pro: true);
public static readonly FiveLaneDrumsChartLoader FiveLaneDrumsLoader = new();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions Assets/Script/Chart/Loaders/ChartLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using MoonscraperChartEditor.Song;
using YARG.Data;

namespace YARG.Chart {
public abstract class ChartLoader<T> {
public MoonSong.MoonInstrument Instrument { get; protected set; }
public string InstrumentName { get; protected set; }
public Difficulty MaxDifficulty { get; protected set; } = Difficulty.EXPERT;

public abstract List<T> GetNotesFromChart(MoonSong song, Difficulty chart);

public virtual List<EventInfo> GetEventsFromChart(MoonSong song)
{
var events = new List<EventInfo>();
var chart = song.GetChart(Instrument, MoonSong.Difficulty.Expert);

// Star Power
foreach (var sp in chart.starPower) {
if (sp.flags != Starpower.Flags.None) {
continue;
}

events.Add(new EventInfo($"starpower_{InstrumentName}", (float) sp.time, (float) GetStarpowerLength(song, sp)));
}

// Solos
for (int i = 0; i < chart.events.Count; i++) {
var chartEvent = chart.events[i];
if (chartEvent.eventName == "solo") {
for (int k = i; k < chart.events.Count; k++) {
var chartEvent2 = chart.events[k];
if (chartEvent2.eventName == "soloend") {
events.Add(new EventInfo($"solo_{InstrumentName}", (float) chartEvent.time, (float) (chartEvent2.time - chartEvent.time)));
break;
}
}
}
}

return events;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected double GetLength(MoonSong song, double startTime, uint tick, uint tickLength) {
return song.TickToTime(tick + tickLength, song.resolution) - startTime;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected double GetNoteLength(MoonSong song, MoonNote note) {
return GetLength(song, note.time, note.tick, note.length);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected double GetStarpowerLength(MoonSong song, Starpower sp) {
return GetLength(song, sp.time, sp.tick, sp.length - 1);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected MoonChart GetChart(MoonSong song, Difficulty difficulty) {
if (difficulty > Difficulty.EXPERT) {
difficulty = Difficulty.EXPERT;
} else if (difficulty < Difficulty.EASY) {
difficulty = Difficulty.EASY;
}

return song.GetChart(Instrument, MoonSong.Difficulty.Easy - (int) difficulty);
}
}
}
11 changes: 11 additions & 0 deletions Assets/Script/Chart/Loaders/ChartLoader.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 34 additions & 12 deletions Assets/Script/Chart/Loaders/FiveLaneDrumsChartLoader.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using MoonscraperChartEditor.Song;
using YARG.Data;

namespace YARG.Chart {
public class FiveLaneDrumsChartLoader : IChartLoader<NoteInfo> {
public List<NoteInfo> GetNotesFromChart(MoonSong song, Difficulty difficulty) {
public class FiveLaneDrumsChartLoader : ChartLoader<NoteInfo> {
public FiveLaneDrumsChartLoader() {
Instrument = MoonSong.MoonInstrument.Drums;
InstrumentName = "ghDrums";
MaxDifficulty = Difficulty.EXPERT_PLUS;
}

public override List<NoteInfo> GetNotesFromChart(MoonSong song, Difficulty difficulty) {
var notes = new List<NoteInfo>();
bool doubleBass = false;
if (difficulty == Difficulty.EXPERT_PLUS) {
difficulty = Difficulty.EXPERT;
doubleBass = true;
}
var chart = song.GetChart(MoonSong.MoonInstrument.Drums, MoonSong.Difficulty.Easy - (int) difficulty);
var chart = GetChart(song, difficulty);
bool doubleBass = difficulty == Difficulty.EXPERT_PLUS;

foreach (var moonNote in chart.notes) {
// Ignore double-kicks if not Expert+
Expand All @@ -23,12 +26,9 @@ public List<NoteInfo> GetNotesFromChart(MoonSong song, Difficulty difficulty) {
if (pad == -1)
continue;

// Length of the note in realtime
double timeLength = song.TickToTime(moonNote.tick + moonNote.length, song.resolution) - moonNote.time;

var note = new NoteInfo {
time = (float) moonNote.time,
length = (float) timeLength,
length = (float) GetNoteLength(song, moonNote),
fret = pad,
hopo = moonNote.type == MoonNote.MoonNoteType.Cymbal
};
Expand All @@ -40,6 +40,28 @@ public List<NoteInfo> GetNotesFromChart(MoonSong song, Difficulty difficulty) {
return notes;
}

public override List<EventInfo> GetEventsFromChart(MoonSong song) {
var events = base.GetEventsFromChart(song);
var chart = GetChart(song, Difficulty.EXPERT);

// SP activations
// Not typically present on 5-lane, but if they're there we'll take 'em!
foreach (var sp in chart.starPower) {
if ((sp.flags & Starpower.Flags.ProDrums_Activation) == 0) {
continue;
}

events.Add(new EventInfo($"fill_{InstrumentName}", (float) sp.time, (float) GetDrumFillLength(song, sp)));
}

return events;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected double GetDrumFillLength(MoonSong song, Starpower sp) {
return GetLength(song, sp.time, sp.tick, sp.length);
}

private int MoonDrumNoteToPad(MoonNote note) {
return note.drumPad switch {
MoonNote.DrumPad.Kick => 5,
Expand Down
43 changes: 31 additions & 12 deletions Assets/Script/Chart/Loaders/FourLaneDrumsChartLoader.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using MoonscraperChartEditor.Song;
using YARG.Data;

namespace YARG.Chart {
public class FourLaneDrumsChartLoader : IChartLoader<NoteInfo> {
public class FourLaneDrumsChartLoader : ChartLoader<NoteInfo> {
private bool _proDrums;

public FourLaneDrumsChartLoader(bool pro) {
_proDrums = pro;

Instrument = MoonSong.MoonInstrument.Drums;
InstrumentName = _proDrums ? "realDrums" : "drums";
MaxDifficulty = Difficulty.EXPERT_PLUS;
}

public List<NoteInfo> GetNotesFromChart(MoonSong song, Difficulty difficulty) {
public override List<NoteInfo> GetNotesFromChart(MoonSong song, Difficulty difficulty) {
var notes = new List<NoteInfo>();
bool doubleBass = false;
if (difficulty == Difficulty.EXPERT_PLUS) {
difficulty = Difficulty.EXPERT;
doubleBass = true;
}
var chart = song.GetChart(MoonSong.MoonInstrument.Drums, MoonSong.Difficulty.Easy - (int) difficulty);
var chart = GetChart(song, difficulty);
bool doubleBass = difficulty == Difficulty.EXPERT_PLUS;

foreach (var moonNote in chart.notes) {
// Ignore double-kicks if not Expert+
Expand All @@ -29,12 +30,9 @@ public List<NoteInfo> GetNotesFromChart(MoonSong song, Difficulty difficulty) {
if (pad == -1)
continue;

// Length of the note in realtime
double timeLength = song.TickToTime(moonNote.tick + moonNote.length, song.resolution) - moonNote.time;

var note = new NoteInfo {
time = (float) moonNote.time,
length = (float) timeLength,
length = (float) GetNoteLength(song, moonNote),
fret = pad,
hopo = _proDrums && moonNote.type == MoonNote.MoonNoteType.Cymbal
};
Expand All @@ -46,6 +44,27 @@ public List<NoteInfo> GetNotesFromChart(MoonSong song, Difficulty difficulty) {
return notes;
}

public override List<EventInfo> GetEventsFromChart(MoonSong song) {
var events = base.GetEventsFromChart(song);
var chart = GetChart(song, Difficulty.EXPERT);

// SP activations
foreach (var sp in chart.starPower) {
if ((sp.flags & Starpower.Flags.ProDrums_Activation) == 0) {
continue;
}

events.Add(new EventInfo($"fill_{InstrumentName}", (float) sp.time, (float) GetDrumFillLength(song, sp)));
}

return events;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected double GetDrumFillLength(MoonSong song, Starpower sp) {
return GetLength(song, sp.time, sp.tick, sp.length);
}

private int MoonDrumNoteToPad(MoonNote note) {
return note.drumPad switch {
MoonNote.DrumPad.Kick => 4,
Expand Down
32 changes: 20 additions & 12 deletions Assets/Script/Chart/Loaders/GuitarChartLoader.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using MoonscraperChartEditor.Song;
using YARG.Data;

namespace YARG.Chart {
public class GuitarChartLoader : IChartLoader<NoteInfo> {
public List<NoteInfo> GetNotesFromChart(MoonSong song, Difficulty difficulty) {
public class GuitarChartLoader : ChartLoader<NoteInfo> {
public GuitarChartLoader(MoonSong.MoonInstrument instrument) {
InstrumentName = instrument switch {
MoonSong.MoonInstrument.Guitar => "guitar",
MoonSong.MoonInstrument.GuitarCoop => "guitarCoop",
MoonSong.MoonInstrument.Rhythm => "rhythm",
MoonSong.MoonInstrument.Bass => "bass",
MoonSong.MoonInstrument.Keys => "keys",
_ => throw new Exception("Instrument not supported!")
};

Instrument = instrument;
}

public override List<NoteInfo> GetNotesFromChart(MoonSong song, Difficulty difficulty) {
var notes = new List<NoteInfo>();
if (difficulty == Difficulty.EXPERT_PLUS) {
difficulty = Difficulty.EXPERT;
}
var chart = song.GetChart(MoonSong.MoonInstrument.Guitar, MoonSong.Difficulty.Easy - (int) difficulty);
var chart = GetChart(song, difficulty);

foreach (var moonNote in chart.notes) {
// Length of the note in realtime
double timeLength = song.TickToTime(moonNote.tick + moonNote.length, song.resolution) - moonNote.time;

var note = new NoteInfo {
time = (float)moonNote.time,
length = (float)timeLength,
time = (float) moonNote.time,
length = (float) GetNoteLength(song, moonNote),
fret = moonNote.rawNote,
hopo = moonNote.type == MoonNote.MoonNoteType.Hopo,
tap = moonNote.type == MoonNote.MoonNoteType.Tap,
Expand Down
11 changes: 0 additions & 11 deletions Assets/Script/Chart/Loaders/IChartLoader.cs

This file was deleted.

17 changes: 8 additions & 9 deletions Assets/Script/Chart/Loaders/New/NewDrumChartLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@
using YARG.Data;

namespace YARG.Chart {
public class NewDrumChartLoader : IChartLoader<DrumNote> {

public class NewDrumChartLoader : ChartLoader<DrumNote> {
private readonly bool _isPro;

public NewDrumChartLoader(bool isPro) {
_isPro = isPro;

Instrument = MoonSong.MoonInstrument.Drums;
InstrumentName = _isPro ? "realDrums" : "drums";
MaxDifficulty = Difficulty.EXPERT_PLUS;
}

public List<DrumNote> GetNotesFromChart(MoonSong song, Difficulty difficulty) {
public override List<DrumNote> GetNotesFromChart(MoonSong song, Difficulty difficulty) {
var notes = new List<DrumNote>();
bool doubleBass = false;
if (difficulty == Difficulty.EXPERT_PLUS) {
difficulty = Difficulty.EXPERT;
doubleBass = true;
}
var chart = song.GetChart(MoonSong.MoonInstrument.Drums, MoonSong.Difficulty.Easy - (int) difficulty);
var chart = GetChart(song, difficulty);
bool doubleBass = difficulty == Difficulty.EXPERT_PLUS;

// do star power later lol idk how it works

Expand Down
29 changes: 18 additions & 11 deletions Assets/Script/Chart/Loaders/New/NewGuitarChartLoader.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
using System;
using System.Collections.Generic;
using MoonscraperChartEditor.Song;
using YARG.Data;

namespace YARG.Chart {
public class NewGuitarChartLoader : IChartLoader<GuitarNote> {
public class NewGuitarChartLoader : ChartLoader<GuitarNote> {
public NewGuitarChartLoader(MoonSong.MoonInstrument instrument) {
InstrumentName = instrument switch {
MoonSong.MoonInstrument.Guitar => "guitar",
MoonSong.MoonInstrument.GuitarCoop => "guitarCoop",
MoonSong.MoonInstrument.Rhythm => "rhythm",
MoonSong.MoonInstrument.Bass => "bass",
MoonSong.MoonInstrument.Keys => "keys",
_ => throw new Exception("Instrument not supported!")
};

Instrument = instrument;
}

public List<GuitarNote> GetNotesFromChart(MoonSong song, Difficulty difficulty) {
public override List<GuitarNote> GetNotesFromChart(MoonSong song, Difficulty difficulty) {
var notes = new List<GuitarNote>();
if (difficulty == Difficulty.EXPERT_PLUS) {
difficulty = Difficulty.EXPERT;
}
var chart = song.GetChart(MoonSong.MoonInstrument.Guitar, MoonSong.Difficulty.Easy - (int) difficulty);
var chart = GetChart(song, difficulty);

var starpowers = chart.starPower.ToArray();

Expand Down Expand Up @@ -58,12 +68,9 @@ public List<GuitarNote> GetNotesFromChart(MoonSong song, Difficulty difficulty)
flags |= NoteFlags.Chord;
}

// Length of the note in realtime
double timeLength = song.TickToTime(moonNote.tick + moonNote.length, song.resolution) - moonNote.time;

int fret = MoonGuitarNoteToFret(moonNote);
var currentNote = new GuitarNote(previousSeparateGameNote, moonNote.time, timeLength, moonNote.tick,
moonNote.length, fret, moonNote.type, flags);
var currentNote = new GuitarNote(previousSeparateGameNote, moonNote.time, GetNoteLength(song, moonNote),
moonNote.tick, moonNote.length, fret, moonNote.type, flags);

// First note, must be a parent note
if (previousGameNote is null) {
Expand Down
Loading

0 comments on commit 7f684eb

Please sign in to comment.