77
88using System . Collections . Generic ;
99using Beats2 . Data ;
10- using System . IO ;
1110using System ;
1211
1312namespace Beats2 . Parser
@@ -23,55 +22,56 @@ public class ParserSM : ParserBase
2322
2423 private List < Event > _events = new List < Event > ( ) ;
2524 private List < string > _notesData = new List < string > ( ) ;
25+ private string _lyricsPath = null ;
2626 private bool _isMetadataLoaded = false ;
2727
28- public ParserSM ( FileInfo inputFile , DirectoryInfo parentDirectory )
29- : base ( inputFile , parentDirectory )
28+ public ParserSM ( string simfilePath ) : base ( simfilePath )
3029 {
3130 }
3231
3332 public override void LoadMetadata ( )
3433 {
35- if ( ! _inputFile . Exists ) {
36- throw new ParserException ( string . Format ( "Input file does not exist: {0}" , _inputFile . FullName ) ) ;
37- }
38-
3934 if ( _isMetadataLoaded ) {
4035 Logger . Warn ( TAG , "Metadata was already previously loaded, reloading..." ) ;
4136 }
4237
43- try {
44- using ( FileStream stream = _inputFile . OpenRead ( ) ) {
45- using ( StreamReader reader = new StreamReader ( stream ) ) {
46- // Assume the input file size isn't larger than 2GB
47- string buffer = reader . ReadToEnd ( ) ;
38+ // If performance becomes an issue (due to memory copying),
39+ // replace String.Split with a String.indexOf implementation
40+ string [ ] sections = LoadData ( ) . Split ( '#' ) ;
41+ foreach ( string section in sections ) {
42+ if ( ! string . IsNullOrEmpty ( section ) ) {
43+ // Clean up and ignore comment lines
44+ string sectionCleaned = section . Trim ( ) ;
45+ int indexEnd = sectionCleaned . IndexOf ( ';' ) ;
46+ if ( indexEnd > 0 ) {
47+ sectionCleaned = sectionCleaned . Substring ( 0 , indexEnd ) ;
48+ }
4849
49- // If performance becomes an issue (due to memory copying),
50- // replace String.Split with a String.indexOf implementation
51- string [ ] sections = buffer . Split ( '#' ) ;
52- foreach ( string section in sections ) {
53- if ( ! string . IsNullOrEmpty ( section ) ) {
54- string sectionCleaned = section . Trim ( ) . TrimEnd ( ';' ) ;
55- string tag , value ;
56- if ( ParseSection ( sectionCleaned , ":" , out tag , out value ) &&
57- ! string . IsNullOrEmpty ( tag ) &&
58- ! string . IsNullOrEmpty ( value ) ) {
59- ParseTag ( tag , value ) ;
60- }
61- }
62- }
50+ string tag , value ;
51+ if ( ParseSection ( sectionCleaned , ":" , out tag , out value ) &&
52+ ! string . IsNullOrEmpty ( tag ) &&
53+ ! string . IsNullOrEmpty ( value ) ) {
54+ ParseTag ( tag , value ) ;
6355 }
6456 }
57+ }
6558
66- } catch ( Exception e ) {
67- throw new ParserException ( string . Format ( "Failed to parse input file: {0}" , _inputFile . FullName ) , e ) ;
59+ _isMetadataLoaded = true ;
60+ }
61+
62+ public override void LoadLyrics ( )
63+ {
64+ if ( ! _isMetadataLoaded ) {
65+ throw new ParserException ( "Metadata must be loaded first before lyrics" ) ;
6866 }
67+
68+ // TODO: Load from _lyricsPath
6969 }
7070
7171 public override void LoadCharts ( )
7272 {
7373 if ( ! _isMetadataLoaded ) {
74- throw new ParserException ( "Metadata must be loaded first" ) ;
74+ throw new ParserException ( "Metadata must be loaded first before charts " ) ;
7575 }
7676
7777 // TODO: Load from _notesData, then add _events
@@ -81,63 +81,63 @@ private void ParseTag(string tag, string value)
8181 {
8282 switch ( tag . ToUpper ( ) ) {
8383 case "VERSION" :
84- _simfile . metadata . simfileVersion = value ;
84+ simfile . metadata . simfileVersion = value ;
8585 break ;
8686 case "TITLE" :
87- _simfile . metadata . songTitle = value ;
87+ simfile . metadata . songTitle = value ;
8888 break ;
8989 case "TITLETRANSLIT" :
90- _simfile . metadata . songTitleTranslit = value ;
90+ simfile . metadata . songTitleTranslit = value ;
9191 break ;
9292 case "SUBTITLE" :
93- _simfile . metadata . songSubtitle = value ;
93+ simfile . metadata . songSubtitle = value ;
9494 break ;
9595 case "SUBTITLETRANSLIT" :
96- _simfile . metadata . songSubtitleTranslit = value ;
96+ simfile . metadata . songSubtitleTranslit = value ;
9797 break ;
9898 case "ARTIST" :
99- _simfile . metadata . songArtist = value ;
99+ simfile . metadata . songArtist = value ;
100100 break ;
101101 case "ARTISTTRANSLIT" :
102- _simfile . metadata . songArtistTranslit = value ;
102+ simfile . metadata . songArtistTranslit = value ;
103103 break ;
104104 case "GENRE" :
105- _simfile . metadata . songGenre = value ;
105+ simfile . metadata . songGenre = value ;
106106 break ;
107107 case "CREDIT" :
108- _simfile . metadata . infoCredits = value ;
108+ simfile . metadata . infoCredits = value ;
109109 break ;
110110 case "BANNER" :
111- _simfile . metadata . graphicBanner = FindImage ( value ) ;
111+ simfile . metadata . graphicBanner = FindImage ( value ) ;
112112 break ;
113113 case "BACKGROUND" :
114- _simfile . metadata . graphicBackground = FindImage ( value ) ;
114+ simfile . metadata . graphicBackground = FindImage ( value ) ;
115115 break ;
116116 case "CDTITLE" :
117117 case "JACKET" :
118118 case "CDIMAGE" :
119119 case "DISCIMAGE" :
120- if ( _simfile . metadata . graphicCover == null ) {
121- _simfile . metadata . graphicCover = FindImage ( value ) ;
120+ if ( simfile . metadata . graphicCover == null ) {
121+ simfile . metadata . graphicCover = FindImage ( value ) ;
122122 }
123123 break ;
124124 case "LYRICSPATH" :
125- ParseLyricsPath ( value ) ;
125+ _lyricsPath = FindLyrics ( value ) ;
126126 break ;
127127 case "MUSIC" :
128- _simfile . metadata . musicPath = FindAudio ( value ) ;
128+ simfile . metadata . musicPath = FindAudio ( value ) ;
129129 break ;
130130 case "OFFSET" :
131- _simfile . metadata . musicOffset = ParseFloat ( value ) ;
131+ simfile . metadata . musicOffset = ParseFloat ( value ) ;
132132 break ;
133133 case "SAMPLESTART" :
134- _simfile . metadata . musicSampleStart = ParseFloat ( value ) ;
134+ simfile . metadata . musicSampleStart = ParseFloat ( value ) ;
135135 break ;
136136 case "SAMPLELENGTH" :
137- _simfile . metadata . musicSampleLength = ParseFloat ( value ) ;
137+ simfile . metadata . musicSampleLength = ParseFloat ( value ) ;
138138 break ;
139139 case "DISPLAYBPM" :
140- _simfile . metadata . musicDisplayBpm = ParseFloats ( value , ":" ) ;
140+ simfile . metadata . musicDisplayBpm = ParseFloats ( value , ":" ) ;
141141 break ;
142142 case "BPMS" :
143143 ParseBpms ( value ) ;
@@ -190,22 +190,10 @@ private void ParseTag(string tag, string value)
190190 }
191191 }
192192
193- private void ParseLyricsPath ( string value )
194- {
195- string lyricPath = FindLyrics ( value ) ;
196- if ( string . IsNullOrEmpty ( lyricPath ) ) {
197- Logger . Warn ( TAG , "Unable to find lyrics file: {0}" , lyricPath ) ;
198- } else {
199- Lyrics lyrics = new Lyrics ( ) ;
200- lyrics . filePath = lyricPath ;
201- _simfile . lyrics . Add ( lyrics ) ;
202- }
203- }
204-
205193 private void ParseBpms ( string value )
206194 {
207195 foreach ( Pair < string , string > pair in ParsePairs ( value , "," , "=" ) ) {
208- int beat = ParseInt ( pair . key ) ;
196+ float beat = ParseFloat ( pair . key ) ;
209197 float bpm = ParseFloat ( pair . value ) ;
210198 if ( beat < 0f ) {
211199 Logger . Warn ( TAG , "Negative beat value events ignored" ) ;
@@ -224,7 +212,7 @@ private void ParseBpms(string value)
224212 private void ParseStops ( string value )
225213 {
226214 foreach ( Pair < string , string > pair in ParsePairs ( value , "," , "=" ) ) {
227- int beat = ParseInt ( pair . key ) ;
215+ float beat = ParseFloat ( pair . key ) ;
228216 float stop = ParseFloat ( pair . value ) ;
229217 if ( beat < 0f ) {
230218 Logger . Warn ( TAG , "Negative beat value events ignored" ) ;
@@ -243,7 +231,7 @@ private void ParseStops(string value)
243231 private void ParseBgChanges ( string value )
244232 {
245233 foreach ( Pair < string , string > pair in ParsePairs ( value , "," , "=" ) ) {
246- int beat = ParseInt ( pair . key ) ;
234+ float beat = ParseFloat ( pair . key ) ;
247235 string filename = pair . value ;
248236 if ( filename . Contains ( "=" ) ) {
249237 // No plans on supporting all those fancy transition flages
@@ -268,7 +256,7 @@ private void ParseBgChanges(string value)
268256 private void ParseLabels ( string value )
269257 {
270258 foreach ( Pair < string , string > pair in ParsePairs ( value , "," , "=" ) ) {
271- int beat = ParseInt ( pair . key ) ;
259+ float beat = ParseFloat ( pair . key ) ;
272260 if ( beat < 0f ) {
273261 Logger . Warn ( TAG , "Negative beat value events ignored" ) ;
274262 } else {
0 commit comments