summaryrefslogtreecommitdiff
path: root/lib/utils/simfile.dart
diff options
context:
space:
mode:
Diffstat (limited to 'lib/utils/simfile.dart')
-rw-r--r--lib/utils/simfile.dart23
1 files changed, 18 insertions, 5 deletions
diff --git a/lib/utils/simfile.dart b/lib/utils/simfile.dart
index c528084..71613a9 100644
--- a/lib/utils/simfile.dart
+++ b/lib/utils/simfile.dart
@@ -55,6 +55,7 @@ class Simfile {
Simfile(this.simfilePath);
+ /// parses a chart tag with metadata [keys] and note data [value]
void _parseChart({required List<String> keys, required String value}) {
Chart chart = Chart();
chart.chartType = keys[1];
@@ -63,6 +64,7 @@ class Simfile {
chart.numericalMeter = int.parse(keys[4]);
chart.radarValues = keys[5];
+ // find simplest chart
if (chartSimplest == null ||
(chart.difficulty!.index <= chartSimplest!.difficulty!.index &&
chart.numericalMeter! <= chartSimplest!.numericalMeter!)) {
@@ -78,8 +80,10 @@ class Simfile {
measures.add(measure);
}
+ // for now only use the first bpm value
double bpm = bpms.entries.first.value;
+ // calculate timing for all notes based on offset, bpm and measure
for (final (measureIndex, measure) in measures.indexed) {
for (final (noteIndex, noteData) in measure.indexed) {
double beat = measureIndex * 4.0 +
@@ -95,10 +99,13 @@ class Simfile {
}
}
+ /// parse a tag based on a regex match [fieldData] and parsing the value based
+ /// on the key
void _parseTag(RegExpMatch fieldData) {
List<String> keys =
fieldData[1]!.split(':').map((key) => key.trim()).toList();
String value = fieldData[2]!;
+
if (keys[0] == "BPMS") {
for (final pairRaw in value.split(',')) {
List<String> pair = pairRaw.split('=');
@@ -122,14 +129,18 @@ class Simfile {
_parseChart(keys: keys, value: value);
}
+ /// load the simfile
Future<bool> load() async {
directoryPath = File(simfilePath).parent.path;
lines = File(simfilePath).readAsStringSync();
+ // remove comments
RegExp commentsRegExp = RegExp(r'//.*$');
lines = lines?.replaceAll(commentsRegExp, '');
+ // find all tags
RegExp fieldDataRegExp = RegExp(r'#([^;]+):([^;]*);');
+ // parse all tags
for (final fieldData in fieldDataRegExp.allMatches(lines!)) {
try {
_parseTag(fieldData);
@@ -138,11 +149,8 @@ class Simfile {
}
}
- String? musicFileName = tags["MUSIC"];
- if (musicFileName == null) return false;
- String? bannerFileName = tags["BANNER"];
- if (bannerFileName == null) return false;
-
+ // searching for audio and banned in the directory is more robust than using
+ // values from metadata as they are wrong more often
for (FileSystemEntity entity in Directory(directoryPath!).listSync()) {
if (entity.path.endsWith('.ogg')) {
audioPath = entity.path;
@@ -152,6 +160,11 @@ class Simfile {
}
}
+ // dont use this simfile of files are missing
+ if (audioPath == null) return false;
+ if (bannerPath == null) return false;
+
+ // get duration from audio
AudioPlayer audioplayer = AudioPlayer();
await audioplayer.setSource(DeviceFileSource(audioPath!));
duration = await audioplayer.getDuration();