Skip to content

Commit

Permalink
Clean up ParserSM some
Browse files Browse the repository at this point in the history
  • Loading branch information
Keripo committed Jan 2, 2015
1 parent 893e5b0 commit 5347d83
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 96 deletions.
4 changes: 2 additions & 2 deletions beats2/Assets/Scripts/Data/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace Beats2.Data
public class Event
{
public EventType type;
public int beat;
public double time;
public float beat;
public float time;
public object value;
}

Expand Down
4 changes: 2 additions & 2 deletions beats2/Assets/Scripts/Data/Note.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public struct NotePoint
public int y;
public bool coord;

public int beat;
public double time;
public float beat;
public float time;

public bool hit;
}
Expand Down
62 changes: 45 additions & 17 deletions beats2/Assets/Scripts/Parser/ParserBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

using System.Collections.Generic;
using Beats2.Data;
using System.IO;
using System;

namespace Beats2.Parser
Expand All @@ -19,30 +18,53 @@ namespace Beats2.Parser
public abstract class ParserBase
{
private const string TAG = "ParserBase";

public Simfile simfile;

protected FileInfo _inputFile;
protected DirectoryInfo _parentDirectory;
protected Simfile _simfile = new Simfile();
protected string _simfilePath;
protected string _parentFolderPath;
protected string _rawData;

protected ParserBase(FileInfo inputFile, DirectoryInfo parentDirectory)
public static ParserBase GetParser(string simfilePath)
{
_inputFile = inputFile;
_parentDirectory = parentDirectory;
}
if (!FileLoader.FileExists(simfilePath)) {
throw new ParserException(string.Format("Simfile does not exist: {0}", simfilePath));
}

public abstract void LoadMetadata();
string extension = FileLoader.GetFileExtension(simfilePath).ToLower();
switch (extension) {
case ".sm":
case ".ssc":
return new ParserSM(simfilePath);
default:
throw new ParserException(string.Format("Unable to find parser for format: {0}", extension));
}
}

public abstract void LoadCharts();
protected ParserBase(string simfilePath)
{
_simfilePath = simfilePath;
_parentFolderPath = FileLoader.GetParentFolder(simfilePath);
simfile = new Simfile();
}

protected int ParseInt(string value)
public string LoadData(bool reload = false)
{
int parsed;
if (!int.TryParse(value, out parsed)) {
Logger.Warn(TAG, "Unable to parse int: {0}", value);
if (_rawData == null || reload) {
_rawData = FileLoader.LoadText(_simfilePath);
if (string.IsNullOrEmpty(_rawData)) {
throw new ParserException("Failed to load raw data");
}
}
return parsed;
return _rawData;
}

public abstract void LoadMetadata();

public abstract void LoadLyrics();

public abstract void LoadCharts();

protected float ParseFloat(string value)
{
float parsed;
Expand Down Expand Up @@ -95,9 +117,15 @@ protected bool ParseSection(string s, string separator, out string key, out stri

protected string FindFile(string filename, string[] extensions, bool restrictExtensions)
{
string path = FileLoader.FindFile(filename, extensions, restrictExtensions);
if (string.IsNullOrEmpty(filename)) {
return null;
}

string path = FileLoader.FindFile(
FileLoader.GetPath(filename, _parentFolderPath), extensions, restrictExtensions);
if (string.IsNullOrEmpty(path)) {
path = FileLoader.FindFile(Path.Combine(filename, _parentDirectory.FullName), extensions, restrictExtensions);
// Most simfiles use relative paths, but check absolute in case
path = FileLoader.FindFile(filename, extensions, restrictExtensions);
}
return path;
}
Expand Down
116 changes: 52 additions & 64 deletions beats2/Assets/Scripts/Parser/ParserSM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

using System.Collections.Generic;
using Beats2.Data;
using System.IO;
using System;

namespace Beats2.Parser
Expand All @@ -23,55 +22,56 @@ public class ParserSM : ParserBase

private List<Event> _events = new List<Event>();
private List<string> _notesData = new List<string>();
private string _lyricsPath = null;
private bool _isMetadataLoaded = false;

public ParserSM(FileInfo inputFile, DirectoryInfo parentDirectory)
: base(inputFile, parentDirectory)
public ParserSM(string simfilePath) : base(simfilePath)
{
}

public override void LoadMetadata()
{
if (!_inputFile.Exists) {
throw new ParserException(string.Format("Input file does not exist: {0}", _inputFile.FullName));
}

if (_isMetadataLoaded) {
Logger.Warn(TAG, "Metadata was already previously loaded, reloading...");
}

try {
using (FileStream stream = _inputFile.OpenRead()) {
using (StreamReader reader = new StreamReader(stream)) {
// Assume the input file size isn't larger than 2GB
string buffer = reader.ReadToEnd();
// If performance becomes an issue (due to memory copying),
// replace String.Split with a String.indexOf implementation
string[] sections = LoadData().Split('#');
foreach (string section in sections) {
if (!string.IsNullOrEmpty(section)) {
// Clean up and ignore comment lines
string sectionCleaned = section.Trim();
int indexEnd = sectionCleaned.IndexOf(';');
if (indexEnd > 0) {
sectionCleaned = sectionCleaned.Substring(0, indexEnd);
}

// If performance becomes an issue (due to memory copying),
// replace String.Split with a String.indexOf implementation
string[] sections = buffer.Split('#');
foreach (string section in sections) {
if (!string.IsNullOrEmpty(section)) {
string sectionCleaned = section.Trim().TrimEnd(';');
string tag, value;
if (ParseSection(sectionCleaned, ":", out tag, out value) &&
!string.IsNullOrEmpty(tag) &&
!string.IsNullOrEmpty(value)) {
ParseTag(tag, value);
}
}
}
string tag, value;
if (ParseSection(sectionCleaned, ":", out tag, out value) &&
!string.IsNullOrEmpty(tag) &&
!string.IsNullOrEmpty(value)) {
ParseTag(tag, value);
}
}
}

} catch (Exception e) {
throw new ParserException(string.Format("Failed to parse input file: {0}", _inputFile.FullName), e);
_isMetadataLoaded = true;
}

public override void LoadLyrics()
{
if (!_isMetadataLoaded) {
throw new ParserException("Metadata must be loaded first before lyrics");
}

// TODO: Load from _lyricsPath
}

public override void LoadCharts()
{
if (!_isMetadataLoaded) {
throw new ParserException("Metadata must be loaded first");
throw new ParserException("Metadata must be loaded first before charts");
}

// TODO: Load from _notesData, then add _events
Expand All @@ -81,63 +81,63 @@ private void ParseTag(string tag, string value)
{
switch (tag.ToUpper()) {
case "VERSION":
_simfile.metadata.simfileVersion = value;
simfile.metadata.simfileVersion = value;
break;
case "TITLE":
_simfile.metadata.songTitle = value;
simfile.metadata.songTitle = value;
break;
case "TITLETRANSLIT":
_simfile.metadata.songTitleTranslit = value;
simfile.metadata.songTitleTranslit = value;
break;
case "SUBTITLE":
_simfile.metadata.songSubtitle = value;
simfile.metadata.songSubtitle = value;
break;
case "SUBTITLETRANSLIT":
_simfile.metadata.songSubtitleTranslit = value;
simfile.metadata.songSubtitleTranslit = value;
break;
case "ARTIST":
_simfile.metadata.songArtist = value;
simfile.metadata.songArtist = value;
break;
case "ARTISTTRANSLIT":
_simfile.metadata.songArtistTranslit = value;
simfile.metadata.songArtistTranslit = value;
break;
case "GENRE":
_simfile.metadata.songGenre = value;
simfile.metadata.songGenre = value;
break;
case "CREDIT":
_simfile.metadata.infoCredits = value;
simfile.metadata.infoCredits = value;
break;
case "BANNER":
_simfile.metadata.graphicBanner = FindImage(value);
simfile.metadata.graphicBanner = FindImage(value);
break;
case "BACKGROUND":
_simfile.metadata.graphicBackground = FindImage(value);
simfile.metadata.graphicBackground = FindImage(value);
break;
case "CDTITLE":
case "JACKET":
case "CDIMAGE":
case "DISCIMAGE":
if (_simfile.metadata.graphicCover == null) {
_simfile.metadata.graphicCover = FindImage(value);
if (simfile.metadata.graphicCover == null) {
simfile.metadata.graphicCover = FindImage(value);
}
break;
case "LYRICSPATH":
ParseLyricsPath(value);
_lyricsPath = FindLyrics(value);
break;
case "MUSIC":
_simfile.metadata.musicPath = FindAudio(value);
simfile.metadata.musicPath = FindAudio(value);
break;
case "OFFSET":
_simfile.metadata.musicOffset = ParseFloat(value);
simfile.metadata.musicOffset = ParseFloat(value);
break;
case "SAMPLESTART":
_simfile.metadata.musicSampleStart = ParseFloat(value);
simfile.metadata.musicSampleStart = ParseFloat(value);
break;
case "SAMPLELENGTH":
_simfile.metadata.musicSampleLength = ParseFloat(value);
simfile.metadata.musicSampleLength = ParseFloat(value);
break;
case "DISPLAYBPM":
_simfile.metadata.musicDisplayBpm = ParseFloats(value, ":");
simfile.metadata.musicDisplayBpm = ParseFloats(value, ":");
break;
case "BPMS":
ParseBpms(value);
Expand Down Expand Up @@ -190,22 +190,10 @@ private void ParseTag(string tag, string value)
}
}

private void ParseLyricsPath(string value)
{
string lyricPath = FindLyrics(value);
if (string.IsNullOrEmpty(lyricPath)) {
Logger.Warn(TAG, "Unable to find lyrics file: {0}", lyricPath);
} else {
Lyrics lyrics = new Lyrics();
lyrics.filePath = lyricPath;
_simfile.lyrics.Add(lyrics);
}
}

private void ParseBpms(string value)
{
foreach (Pair<string, string> pair in ParsePairs(value, ",", "=")) {
int beat = ParseInt(pair.key);
float beat = ParseFloat(pair.key);
float bpm = ParseFloat(pair.value);
if (beat < 0f) {
Logger.Warn(TAG, "Negative beat value events ignored");
Expand All @@ -224,7 +212,7 @@ private void ParseBpms(string value)
private void ParseStops(string value)
{
foreach (Pair<string, string> pair in ParsePairs(value, ",", "=")) {
int beat = ParseInt(pair.key);
float beat = ParseFloat(pair.key);
float stop = ParseFloat(pair.value);
if (beat < 0f) {
Logger.Warn(TAG, "Negative beat value events ignored");
Expand All @@ -243,7 +231,7 @@ private void ParseStops(string value)
private void ParseBgChanges(string value)
{
foreach (Pair<string, string> pair in ParsePairs(value, ",", "=")) {
int beat = ParseInt(pair.key);
float beat = ParseFloat(pair.key);
string filename = pair.value;
if (filename.Contains("=")) {
// No plans on supporting all those fancy transition flages
Expand All @@ -268,7 +256,7 @@ private void ParseBgChanges(string value)
private void ParseLabels(string value)
{
foreach (Pair<string, string> pair in ParsePairs(value, ",", "=")) {
int beat = ParseInt(pair.key);
float beat = ParseFloat(pair.key);
if (beat < 0f) {
Logger.Warn(TAG, "Negative beat value events ignored");
} else {
Expand Down
Loading

0 comments on commit 5347d83

Please sign in to comment.