Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ int main()

auto sections = ParseSectionsFromChart(content.c_str());

auto metaData = ParseMetaDataFromChartSections(sections);
auto metaData = ParseMetaDataFromChartSection(
sections.at(ToString(NamedSection::Song)));

std::cout << metaData["Name"] << std::endl; // Example Song
std::cout << metaData["Resolution"] << std::endl; // 192
Expand Down Expand Up @@ -255,7 +256,8 @@ int main()

auto sections = ParseSectionsFromChart(content.c_str());

auto timeSignatures = ParseTimeSignaturesFromChartSections(sections);
auto timeSignatures = ParseTimeSignaturesFromChartSection(
sections.at(ToString(NamedSection::SyncTrack)));

std::cout << size(timeSignatures) << std::endl; // 4

Expand Down Expand Up @@ -295,7 +297,8 @@ int main()

auto sections = ParseSectionsFromChart(content.c_str());

auto bpm = ParseBpmFromChartSections(sections);
auto bpm = ParseBpmFromChartSection(
sections.at(ToString(NamedSection::SyncTrack)));

std::cout << size(bpm) << std::endl; // 7

Expand Down Expand Up @@ -335,7 +338,8 @@ int main()

auto sections = ParseSectionsFromChart(content.c_str());

auto notes = ParseNotesFromChartSections(sections, Expert);
auto notes = ParseNotesFromChartSection(
sections.at(ToString(Difficulty::Expert) + "Single"));

for (auto &note : notes)
{
Expand Down Expand Up @@ -383,7 +387,8 @@ int main()

auto sections = ParseSectionsFromChart(content.c_str());

auto lyrics = ParseLyricsFromChartSections(sections);
auto lyrics = ParseLyricsFromChartSection(
sections.at(ToString(NamedSection::Events)));

std::cout << size(lyrics) << std::endl; // 12

Expand Down
8 changes: 4 additions & 4 deletions RhythmGameUtilities.Tests/ParsersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void TestParseSectionsFromChart()
}

[Test]
public void TestParseValuesFromChartSections()
public void TestParseValuesFromChartSection()
{
var sections = Parsers.ParseSectionsFromChart(Mocks.SONG_CHART);

Expand Down Expand Up @@ -58,7 +58,7 @@ public void TestParseTimeSignaturesFromChartSection()
}

[Test]
public void TestParseBpmFromChartSections()
public void TestParseBpmFromChartSection()
{
var sections = Parsers.ParseSectionsFromChart(Mocks.SONG_CHART);

Expand All @@ -68,7 +68,7 @@ public void TestParseBpmFromChartSections()
}

[Test]
public void TestParseNotesFromChartSections()
public void TestParseNotesFromChartSection()
{
var sections = Parsers.ParseSectionsFromChart(Mocks.SONG_CHART);

Expand All @@ -78,7 +78,7 @@ public void TestParseNotesFromChartSections()
}

[Test]
public void TestParseLyricsFromChartSections()
public void TestParseLyricsFromChartSection()
{
var sections = Parsers.ParseSectionsFromChart(Mocks.SONG_CHART);

Expand Down
8 changes: 4 additions & 4 deletions UnityPackage/Editor/Tests/ParsersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void TestParseSectionsFromChart()
}

[Test]
public void TestParseValuesFromChartSections()
public void TestParseValuesFromChartSection()
{
var sections = Parsers.ParseSectionsFromChart(Mocks.SONG_CHART);

Expand Down Expand Up @@ -58,7 +58,7 @@ public void TestParseTimeSignaturesFromChartSection()
}

[Test]
public void TestParseBpmFromChartSections()
public void TestParseBpmFromChartSection()
{
var sections = Parsers.ParseSectionsFromChart(Mocks.SONG_CHART);

Expand All @@ -68,7 +68,7 @@ public void TestParseBpmFromChartSections()
}

[Test]
public void TestParseNotesFromChartSections()
public void TestParseNotesFromChartSection()
{
var sections = Parsers.ParseSectionsFromChart(Mocks.SONG_CHART);

Expand All @@ -78,7 +78,7 @@ public void TestParseNotesFromChartSections()
}

[Test]
public void TestParseLyricsFromChartSections()
public void TestParseLyricsFromChartSection()
{
var sections = Parsers.ParseSectionsFromChart(Mocks.SONG_CHART);

Expand Down
15 changes: 10 additions & 5 deletions UnityPackage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ int main()

auto sections = ParseSectionsFromChart(content.c_str());

auto metaData = ParseMetaDataFromChartSections(sections);
auto metaData = ParseMetaDataFromChartSection(
sections.at(ToString(NamedSection::Song)));

std::cout << metaData["Name"] << std::endl; // Example Song
std::cout << metaData["Resolution"] << std::endl; // 192
Expand Down Expand Up @@ -255,7 +256,8 @@ int main()

auto sections = ParseSectionsFromChart(content.c_str());

auto timeSignatures = ParseTimeSignaturesFromChartSections(sections);
auto timeSignatures = ParseTimeSignaturesFromChartSection(
sections.at(ToString(NamedSection::SyncTrack)));

std::cout << size(timeSignatures) << std::endl; // 4

Expand Down Expand Up @@ -295,7 +297,8 @@ int main()

auto sections = ParseSectionsFromChart(content.c_str());

auto bpm = ParseBpmFromChartSections(sections);
auto bpm = ParseBpmFromChartSection(
sections.at(ToString(NamedSection::SyncTrack)));

std::cout << size(bpm) << std::endl; // 7

Expand Down Expand Up @@ -335,7 +338,8 @@ int main()

auto sections = ParseSectionsFromChart(content.c_str());

auto notes = ParseNotesFromChartSections(sections, Expert);
auto notes = ParseNotesFromChartSection(
sections.at(ToString(Difficulty::Expert) + "Single"));

for (auto &note : notes)
{
Expand Down Expand Up @@ -383,7 +387,8 @@ int main()

auto sections = ParseSectionsFromChart(content.c_str());

auto lyrics = ParseLyricsFromChartSections(sections);
auto lyrics = ParseLyricsFromChartSection(
sections.at(ToString(NamedSection::Events)));

std::cout << size(lyrics) << std::endl; // 12

Expand Down
121 changes: 24 additions & 97 deletions includes/RhythmGameUtilities/Parsers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,22 @@ typedef struct
int lineCount;
} ChartSectionInternal;

typedef struct
{
std::string name;
std::vector<std::pair<std::string, std::vector<std::string>>> lines;
} ChartSection;

std::regex CHART_SECTION_PATTERN("\\[([a-z]+)\\]\\s*\\{([^\\}]+)\\}",
std::regex_constants::icase);

std::regex CHART_SECTION_LINE_PATTERN("([^=]+)\\s*=([^\\r\\n]+)");

std::regex JSON_VALUE_PATTERN("(\"[^\"]+\"|\\S+)");

std::vector<ChartSection> ParseSectionsFromChart(const char *contents)
std::map<std::string,
std::vector<std::pair<std::string, std::vector<std::string>>>>
ParseSectionsFromChart(const char *contents)
{
auto matches = FindAllMatches(contents, CHART_SECTION_PATTERN);

auto sections = std::vector<ChartSection>();
std::map<std::string,
std::vector<std::pair<std::string, std::vector<std::string>>>>
sections;

for (auto i = 0; i < matches.size(); i += 1)
{
Expand All @@ -66,15 +64,10 @@ std::vector<ChartSection> ParseSectionsFromChart(const char *contents)
continue;
}

ChartSection section;

section.name = parts[1].c_str();

auto lines =
FindAllMatches(parts[2].c_str(), CHART_SECTION_LINE_PATTERN);

section.lines =
std::vector<std::pair<std::string, std::vector<std::string>>>();
std::vector<std::pair<std::string, std::vector<std::string>>> items;

for (auto j = 0; j < lines.size(); j += 1)
{
Expand All @@ -91,47 +84,34 @@ std::vector<ChartSection> ParseSectionsFromChart(const char *contents)
std::regex_replace(values[k], std::regex("^\"|\"$"), "");
}

section.lines.push_back(std::make_pair(key, values));
items.push_back(std::make_pair(key, values));
}

sections.push_back(section);
sections.insert({parts[1].c_str(), items});
}

return sections;
}

std::map<std::string, std::string>
ParseMetaDataFromChartSection(ChartSection section)
std::map<std::string, std::string> ParseMetaDataFromChartSection(
std::vector<std::pair<std::string, std::vector<std::string>>> section)
{
auto data = std::map<std::string, std::string>();

for (auto &line : section.lines)
for (auto &line : section)
{
data.insert({line.first, line.second.front()});
}

return data;
}

std::map<std::string, std::string>
ParseMetaDataFromChartSections(std::vector<ChartSection> sections)
{
for (auto &section : sections)
{
if (section.name == ToString(NamedSection::Song))
{
return ParseMetaDataFromChartSection(section);
}
}

return std::map<std::string, std::string>();
}

std::map<int, int> ParseTimeSignaturesFromChartSection(ChartSection section)
std::map<int, int> ParseTimeSignaturesFromChartSection(
std::vector<std::pair<std::string, std::vector<std::string>>> section)
{
auto timeSignatures = std::map<int, int>();

for (auto &line : section.lines)
for (auto &line : section)
{
if (line.second.front() == ToString(TypeCode::TimeSignatureMarker))
{
Expand All @@ -143,25 +123,12 @@ std::map<int, int> ParseTimeSignaturesFromChartSection(ChartSection section)
return timeSignatures;
}

std::map<int, int>
ParseTimeSignaturesFromChartSections(std::vector<ChartSection> sections)
{
for (auto &section : sections)
{
if (section.name == ToString(NamedSection::SyncTrack))
{
return ParseTimeSignaturesFromChartSection(section);
}
}

return std::map<int, int>();
}

std::map<int, int> ParseBpmFromChartSection(ChartSection section)
std::map<int, int> ParseBpmFromChartSection(
std::vector<std::pair<std::string, std::vector<std::string>>> section)
{
auto bpm = std::map<int, int>();

for (auto &line : section.lines)
for (auto &line : section)
{
if (line.second.front() == ToString(TypeCode::BPM_Marker))
{
Expand All @@ -172,24 +139,12 @@ std::map<int, int> ParseBpmFromChartSection(ChartSection section)
return bpm;
}

std::map<int, int> ParseBpmFromChartSections(std::vector<ChartSection> sections)
{
for (auto &section : sections)
{
if (section.name == ToString(NamedSection::SyncTrack))
{
return ParseBpmFromChartSection(section);
}
}

return std::map<int, int>();
}

std::vector<Note> ParseNotesFromChartSection(ChartSection section)
std::vector<Note> ParseNotesFromChartSection(
std::vector<std::pair<std::string, std::vector<std::string>>> section)
{
auto notes = std::vector<Note>();

for (auto &line : section.lines)
for (auto &line : section)
{
if (line.second.front() == ToString(TypeCode::NoteMarker))
{
Expand All @@ -202,26 +157,12 @@ std::vector<Note> ParseNotesFromChartSection(ChartSection section)
return notes;
}

std::vector<Note>
ParseNotesFromChartSections(std::vector<ChartSection> sections,
Difficulty difficulty)
{
for (auto &section : sections)
{
if (section.name == ToString(difficulty) + "Single")
{
return ParseNotesFromChartSection(section);
}
}

return std::vector<Note>();
}

std::map<int, std::string> ParseLyricsFromChartSection(ChartSection section)
std::map<int, std::string> ParseLyricsFromChartSection(
std::vector<std::pair<std::string, std::vector<std::string>>> section)
{
auto lyrics = std::map<int, std::string>();

for (auto &line : section.lines)
for (auto &line : section)
{
if (line.second.back().rfind("lyric", 0) == 0)
{
Expand All @@ -232,18 +173,4 @@ std::map<int, std::string> ParseLyricsFromChartSection(ChartSection section)
return lyrics;
}

std::map<int, std::string>
ParseLyricsFromChartSections(std::vector<ChartSection> sections)
{
for (auto &section : sections)
{
if (section.name == ToString(NamedSection::Events))
{
return ParseLyricsFromChartSection(section);
}
}

return std::map<int, std::string>();
}

} // namespace RhythmGameUtilities
Loading