Skip to content

Commit 5347d83

Browse files
committed
Clean up ParserSM some
1 parent 893e5b0 commit 5347d83

5 files changed

Lines changed: 128 additions & 96 deletions

File tree

beats2/Assets/Scripts/Data/Event.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ namespace Beats2.Data
1010
public class Event
1111
{
1212
public EventType type;
13-
public int beat;
14-
public double time;
13+
public float beat;
14+
public float time;
1515
public object value;
1616
}
1717

beats2/Assets/Scripts/Data/Note.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public struct NotePoint
5555
public int y;
5656
public bool coord;
5757

58-
public int beat;
59-
public double time;
58+
public float beat;
59+
public float time;
6060

6161
public bool hit;
6262
}

beats2/Assets/Scripts/Parser/ParserBase.cs

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
using System.Collections.Generic;
99
using Beats2.Data;
10-
using System.IO;
1110
using System;
1211

1312
namespace Beats2.Parser
@@ -19,30 +18,53 @@ namespace Beats2.Parser
1918
public abstract class ParserBase
2019
{
2120
private const string TAG = "ParserBase";
21+
22+
public Simfile simfile;
2223

23-
protected FileInfo _inputFile;
24-
protected DirectoryInfo _parentDirectory;
25-
protected Simfile _simfile = new Simfile();
24+
protected string _simfilePath;
25+
protected string _parentFolderPath;
26+
protected string _rawData;
2627

27-
protected ParserBase(FileInfo inputFile, DirectoryInfo parentDirectory)
28+
public static ParserBase GetParser(string simfilePath)
2829
{
29-
_inputFile = inputFile;
30-
_parentDirectory = parentDirectory;
31-
}
30+
if (!FileLoader.FileExists(simfilePath)) {
31+
throw new ParserException(string.Format("Simfile does not exist: {0}", simfilePath));
32+
}
3233

33-
public abstract void LoadMetadata();
34+
string extension = FileLoader.GetFileExtension(simfilePath).ToLower();
35+
switch (extension) {
36+
case ".sm":
37+
case ".ssc":
38+
return new ParserSM(simfilePath);
39+
default:
40+
throw new ParserException(string.Format("Unable to find parser for format: {0}", extension));
41+
}
42+
}
3443

35-
public abstract void LoadCharts();
44+
protected ParserBase(string simfilePath)
45+
{
46+
_simfilePath = simfilePath;
47+
_parentFolderPath = FileLoader.GetParentFolder(simfilePath);
48+
simfile = new Simfile();
49+
}
3650

37-
protected int ParseInt(string value)
51+
public string LoadData(bool reload = false)
3852
{
39-
int parsed;
40-
if (!int.TryParse(value, out parsed)) {
41-
Logger.Warn(TAG, "Unable to parse int: {0}", value);
53+
if (_rawData == null || reload) {
54+
_rawData = FileLoader.LoadText(_simfilePath);
55+
if (string.IsNullOrEmpty(_rawData)) {
56+
throw new ParserException("Failed to load raw data");
57+
}
4258
}
43-
return parsed;
59+
return _rawData;
4460
}
4561

62+
public abstract void LoadMetadata();
63+
64+
public abstract void LoadLyrics();
65+
66+
public abstract void LoadCharts();
67+
4668
protected float ParseFloat(string value)
4769
{
4870
float parsed;
@@ -95,9 +117,15 @@ protected bool ParseSection(string s, string separator, out string key, out stri
95117

96118
protected string FindFile(string filename, string[] extensions, bool restrictExtensions)
97119
{
98-
string path = FileLoader.FindFile(filename, extensions, restrictExtensions);
120+
if (string.IsNullOrEmpty(filename)) {
121+
return null;
122+
}
123+
124+
string path = FileLoader.FindFile(
125+
FileLoader.GetPath(filename, _parentFolderPath), extensions, restrictExtensions);
99126
if (string.IsNullOrEmpty(path)) {
100-
path = FileLoader.FindFile(Path.Combine(filename, _parentDirectory.FullName), extensions, restrictExtensions);
127+
// Most simfiles use relative paths, but check absolute in case
128+
path = FileLoader.FindFile(filename, extensions, restrictExtensions);
101129
}
102130
return path;
103131
}

beats2/Assets/Scripts/Parser/ParserSM.cs

Lines changed: 52 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
using System.Collections.Generic;
99
using Beats2.Data;
10-
using System.IO;
1110
using System;
1211

1312
namespace 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

Comments
 (0)