Skip to content

Commit

Permalink
Move some code from ParserSM to ParserBase
Browse files Browse the repository at this point in the history
  • Loading branch information
Keripo committed Jan 2, 2015
1 parent c39ecaa commit 70bbe1a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
30 changes: 22 additions & 8 deletions beats2/Assets/Scripts/Parser/ParserBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ public abstract class ParserBase
{
private const string TAG = "ParserBase";

protected FileInfo _inputFile;
protected DirectoryInfo _parentDirectory;
protected Simfile _simfile = new Simfile();

protected ParserBase(FileInfo inputFile, DirectoryInfo parentDirectory)
{
_inputFile = inputFile;
_parentDirectory = parentDirectory;
}

public abstract void LoadMetadata();

public abstract void LoadCharts();

protected int ParseInt(string value)
{
int parsed;
Expand Down Expand Up @@ -79,28 +93,28 @@ protected bool ParseSection(string s, string separator, out string key, out stri
}
}

protected string FindFile(string filename, string[] extensions, bool restrictExtensions, DirectoryInfo parentDirectory)
protected string FindFile(string filename, string[] extensions, bool restrictExtensions)
{
string path = FileLoader.FindFile(filename, extensions, restrictExtensions);
if (string.IsNullOrEmpty(path)) {
path = FileLoader.FindFile(Path.Combine(filename, parentDirectory.FullName), extensions, restrictExtensions);
path = FileLoader.FindFile(Path.Combine(filename, _parentDirectory.FullName), extensions, restrictExtensions);
}
return path;
}

protected string FindImage(string filename, DirectoryInfo parentDirectory)
protected string FindImage(string filename)
{
return FindFile(filename, FileLoader.IMAGE_EXTENSIONS, false, parentDirectory);
return FindFile(filename, FileLoader.IMAGE_EXTENSIONS, false);
}

protected string FindLyrics(string filename, DirectoryInfo parentDirectory)
protected string FindLyrics(string filename)
{
return FindFile(filename, FileLoader.LYRICS_EXTENSIONS, false, parentDirectory);
return FindFile(filename, FileLoader.LYRICS_EXTENSIONS, false);
}

protected string FindAudio(string filename, DirectoryInfo parentDirectory)
protected string FindAudio(string filename)
{
return FindFile(filename, FileLoader.AUDIO_EXTENSIONS, true, parentDirectory);
return FindFile(filename, FileLoader.AUDIO_EXTENSIONS, true);
}
}
}
24 changes: 10 additions & 14 deletions beats2/Assets/Scripts/Parser/ParserSM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,18 @@ namespace Beats2.Parser
/// </summary>
public class ParserSM : ParserBase
{
private const string TAG = "SMParser";
private const string TAG = "ParserSM";

private FileInfo _inputFile;
private DirectoryInfo _parentDirectory;
private Simfile _simfile = new Simfile();
private List<Event> _events = new List<Event>();
private List<string> _notesData = new List<string>();
private bool _isMetadataLoaded = false;

public ParserSM(FileInfo inputFile, DirectoryInfo parentDirectory)
: base(inputFile, parentDirectory)
{
_inputFile = inputFile;
_parentDirectory = parentDirectory;
}

public void LoadMetadata()
public override void LoadMetadata()
{
if (!_inputFile.Exists) {
throw new ParserException(string.Format("Input file does not exist: {0}", _inputFile.FullName));
Expand Down Expand Up @@ -72,7 +68,7 @@ public void LoadMetadata()
}
}

public void LoadCharts()
public override void LoadCharts()
{
if (!_isMetadataLoaded) {
throw new ParserException("Metadata must be loaded first");
Expand Down Expand Up @@ -112,24 +108,24 @@ private void ParseTag(string tag, string value)
_simfile.metadata.infoCredits = value;
break;
case "BANNER":
_simfile.metadata.graphicBanner = FindImage(value, _parentDirectory);
_simfile.metadata.graphicBanner = FindImage(value);
break;
case "BACKGROUND":
_simfile.metadata.graphicBackground = FindImage(value, _parentDirectory);
_simfile.metadata.graphicBackground = FindImage(value);
break;
case "CDTITLE":
case "JACKET":
case "CDIMAGE":
case "DISCIMAGE":
if (_simfile.metadata.graphicCover == null) {
_simfile.metadata.graphicCover = FindImage(value, _parentDirectory);
_simfile.metadata.graphicCover = FindImage(value);
}
break;
case "LYRICSPATH":
ParseLyricsPath(value);
break;
case "MUSIC":
_simfile.metadata.musicPath = FindAudio(value, _parentDirectory);
_simfile.metadata.musicPath = FindAudio(value);
break;
case "OFFSET":
_simfile.metadata.musicOffset = ParseFloat(value);
Expand Down Expand Up @@ -196,7 +192,7 @@ private void ParseTag(string tag, string value)

private void ParseLyricsPath(string value)
{
string lyricPath = FindLyrics(value, _parentDirectory);
string lyricPath = FindLyrics(value);
if (string.IsNullOrEmpty(lyricPath)) {
Logger.Warn(TAG, "Unable to find lyrics file: {0}", lyricPath);
} else {
Expand Down Expand Up @@ -254,7 +250,7 @@ private void ParseBgChanges(string value)
// See http://www.stepmania.com/forums/general-stepmania/show/1393#post3757
filename = filename.Substring(0, filename.IndexOf('='));
}
string filePath = FindImage(filename, _parentDirectory);
string filePath = FindImage(filename);
if (beat < 0f) {
Logger.Warn(TAG, "Negative beat value events ignored");
} else if (string.IsNullOrEmpty(filename)) {
Expand Down

0 comments on commit 70bbe1a

Please sign in to comment.