Skip to content

Commit 8efb4db

Browse files
authored
Merge pull request #99 from neogeek/feature/csharp-song-class
[feat] Added new song class to C# libraries.
2 parents 2eeed8b + 1991f51 commit 8efb4db

File tree

9 files changed

+489
-408
lines changed

9 files changed

+489
-408
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace RhythmGameUtilities
6+
{
7+
8+
public class Song
9+
{
10+
11+
private readonly Dictionary<string, KeyValuePair<string, string[]>[]> _sections;
12+
13+
public Dictionary<string, string> metaData { get; }
14+
15+
public int resolution { get; }
16+
17+
public Tempo[] tempoChanges { get; private set; }
18+
19+
public TimeSignature[] timeSignatureChanges { get; }
20+
21+
public Dictionary<Difficulty, Note[]> difficulties { get; private set; }
22+
23+
public BeatBar[] beatBars { get; private set; }
24+
25+
public Song(string contents)
26+
{
27+
_sections = Parsers.ParseSectionsFromChart(contents);
28+
29+
metaData = Parsers.ParseMetaDataFromChartSection(_sections
30+
.First(section => section.Key == NamedSection.Song)
31+
.Value);
32+
33+
resolution = int.Parse(metaData["Resolution"]);
34+
35+
tempoChanges = Parsers.ParseTempoChangesFromChartSection(_sections
36+
.First(section => section.Key == NamedSection.SyncTrack)
37+
.Value);
38+
39+
timeSignatureChanges = Parsers.ParseTimeSignatureChangesFromChartSection(_sections[NamedSection.SyncTrack]);
40+
41+
difficulties = Enum.GetValues(typeof(Difficulty))
42+
.Cast<Difficulty>()
43+
.Where(difficulty => _sections.ToDictionary(item => item.Key, item => item.Value)
44+
.ContainsKey($"{difficulty}Single"))
45+
.ToDictionary(difficulty => difficulty,
46+
difficulty => Parsers.ParseNotesFromChartSection(_sections[$"{difficulty}Single"]));
47+
48+
beatBars = Utilities.CalculateBeatBars(tempoChanges, includeHalfNotes : true);
49+
}
50+
51+
public void RecalculateBeatBarsWithSongLength(float songLength)
52+
{
53+
var lastTick = Utilities.ConvertSecondsToTicks(songLength, resolution, tempoChanges, timeSignatureChanges);
54+
55+
tempoChanges = Parsers.ParseTempoChangesFromChartSection(_sections
56+
.First(section => section.Key == NamedSection.SyncTrack)
57+
.Value);
58+
59+
tempoChanges = tempoChanges.Concat(new Tempo[]
60+
{
61+
new()
62+
{
63+
Position = Utilities.RoundUpToTheNearestMultiplier(lastTick, resolution),
64+
BPM = tempoChanges.Last().BPM
65+
}
66+
})
67+
.ToArray();
68+
69+
beatBars = Utilities.CalculateBeatBars(tempoChanges, includeHalfNotes : true);
70+
}
71+
72+
}
73+
74+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.IO;
2+
using System.Threading.Tasks;
3+
using UnityEngine;
4+
using UnityEngine.Networking;
5+
6+
namespace RhythmGameUtilities
7+
{
8+
9+
public static class CommonUtilities
10+
{
11+
12+
public static async Task<string> LoadTextFileFromPath(string path)
13+
{
14+
using var request = UnityWebRequest.Get(path);
15+
16+
request.downloadHandler = new DownloadHandlerBuffer();
17+
18+
await request.SendWebRequest();
19+
20+
if (request.result == UnityWebRequest.Result.Success)
21+
{
22+
return request.downloadHandler.text;
23+
}
24+
25+
throw new FileNotFoundException(request.result.ToString());
26+
}
27+
28+
public static async Task<AudioClip> LoadAudioFileFromPath(string path,
29+
AudioType audioType = AudioType.OGGVORBIS)
30+
{
31+
using var request =
32+
UnityWebRequestMultimedia.GetAudioClip(path, audioType);
33+
34+
await request.SendWebRequest();
35+
36+
if (request.result == UnityWebRequest.Result.Success)
37+
{
38+
return DownloadHandlerAudioClip.GetContent(request);
39+
}
40+
41+
throw new FileNotFoundException(request.result.ToString());
42+
}
43+
44+
}
45+
46+
}

UnityPackage/Samples~/SampleSong (URP)/Scripts/CommonUtilities.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)