From 28d0fe7d8c3a38d7c2ded86c30b549ed6be48f3c Mon Sep 17 00:00:00 2001 From: Orangerot Date: Tue, 14 Jan 2025 16:26:57 +0100 Subject: feat: show duration in level selection + more robust simfile loader --- lib/utils/simfile.dart | 61 +++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 25 deletions(-) (limited to 'lib/utils') diff --git a/lib/utils/simfile.dart b/lib/utils/simfile.dart index 0af734f..c528084 100644 --- a/lib/utils/simfile.dart +++ b/lib/utils/simfile.dart @@ -1,5 +1,7 @@ import 'dart:io'; +import 'package:audioplayers/audioplayers.dart'; + enum Difficulty { Beginner, Easy, Medium, Hard, Challenge, Edit } // These are the standard note values: @@ -36,12 +38,13 @@ class Chart { } class Simfile { - String directoryPath; - String? simfilePath; + String? directoryPath; + String simfilePath; String? audioPath; String? bannerPath; String? lines; + Duration? duration; // tags of simfile Map tags = {}; @@ -50,7 +53,7 @@ class Simfile { Map bpms = {}; double offset = 0; - Simfile(this.directoryPath); + Simfile(this.simfilePath); void _parseChart({required List keys, required String value}) { Chart chart = Chart(); @@ -119,33 +122,41 @@ class Simfile { _parseChart(keys: keys, value: value); } - void load() { - simfilePath = Directory(directoryPath) - .listSync() - .firstWhere((entity) => entity.path.endsWith('.sm'), - orElse: () => File('')) - .path; - - audioPath = Directory(directoryPath) - .listSync() - .firstWhere((entity) => entity.path.endsWith('.ogg'), - orElse: () => File('')) - .path; - - bannerPath = Directory(directoryPath) - .listSync() - .firstWhere((file) => file.path.toLowerCase().endsWith('banner.png'), - orElse: () => File('')) - .path; - - lines = File(simfilePath!).readAsStringSync(); - + Future load() async { + directoryPath = File(simfilePath).parent.path; + lines = File(simfilePath).readAsStringSync(); + RegExp commentsRegExp = RegExp(r'//.*$'); lines = lines?.replaceAll(commentsRegExp, ''); RegExp fieldDataRegExp = RegExp(r'#([^;]+):([^;]*);'); for (final fieldData in fieldDataRegExp.allMatches(lines!)) { - _parseTag(fieldData); + try { + _parseTag(fieldData); + } catch (err) { + return false; + } } + + String? musicFileName = tags["MUSIC"]; + if (musicFileName == null) return false; + String? bannerFileName = tags["BANNER"]; + if (bannerFileName == null) return false; + + for (FileSystemEntity entity in Directory(directoryPath!).listSync()) { + if (entity.path.endsWith('.ogg')) { + audioPath = entity.path; + } + if (entity.path.endsWith('anner.png')) { + bannerPath = entity.path; + } + } + + AudioPlayer audioplayer = AudioPlayer(); + await audioplayer.setSource(DeviceFileSource(audioPath!)); + duration = await audioplayer.getDuration(); + audioplayer.dispose(); + + return true; } } -- cgit v1.2.3