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
74 changes: 74 additions & 0 deletions RhythmGameUtilities/Structs/Song.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace RhythmGameUtilities
{

public class Song
{

private readonly Dictionary<string, KeyValuePair<string, string[]>[]> _sections;

public Dictionary<string, string> metaData { get; }

public int resolution { get; }

public Tempo[] tempoChanges { get; private set; }

public TimeSignature[] timeSignatureChanges { get; }

public Dictionary<Difficulty, Note[]> difficulties { get; private set; }

public BeatBar[] beatBars { get; private set; }

public Song(string contents)
{
_sections = Parsers.ParseSectionsFromChart(contents);

metaData = Parsers.ParseMetaDataFromChartSection(_sections
.First(section => section.Key == NamedSection.Song)
.Value);

resolution = int.Parse(metaData["Resolution"]);

tempoChanges = Parsers.ParseTempoChangesFromChartSection(_sections
.First(section => section.Key == NamedSection.SyncTrack)
.Value);

timeSignatureChanges = Parsers.ParseTimeSignatureChangesFromChartSection(_sections[NamedSection.SyncTrack]);

difficulties = Enum.GetValues(typeof(Difficulty))
.Cast<Difficulty>()
.Where(difficulty => _sections.ToDictionary(item => item.Key, item => item.Value)
.ContainsKey($"{difficulty}Single"))
.ToDictionary(difficulty => difficulty,
difficulty => Parsers.ParseNotesFromChartSection(_sections[$"{difficulty}Single"]));

beatBars = Utilities.CalculateBeatBars(tempoChanges, includeHalfNotes : true);
}

public void RecalculateBeatBarsWithSongLength(float songLength)
{
var lastTick = Utilities.ConvertSecondsToTicks(songLength, resolution, tempoChanges, timeSignatureChanges);

tempoChanges = Parsers.ParseTempoChangesFromChartSection(_sections
.First(section => section.Key == NamedSection.SyncTrack)
.Value);

tempoChanges = tempoChanges.Concat(new Tempo[]
{
new()
{
Position = Utilities.RoundUpToTheNearestMultiplier(lastTick, resolution),
BPM = tempoChanges.Last().BPM
}
})
.ToArray();

beatBars = Utilities.CalculateBeatBars(tempoChanges, includeHalfNotes : true);
}

}

}
46 changes: 46 additions & 0 deletions UnityPackage/Samples~/SampleSong (URP)/Scripts/CommonUtilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;

namespace RhythmGameUtilities
{

public static class CommonUtilities
{

public static async Task<string> LoadTextFileFromPath(string path)
{
using var request = UnityWebRequest.Get(path);

request.downloadHandler = new DownloadHandlerBuffer();

await request.SendWebRequest();

if (request.result == UnityWebRequest.Result.Success)
{
return request.downloadHandler.text;
}

throw new FileNotFoundException(request.result.ToString());
}

public static async Task<AudioClip> LoadAudioFileFromPath(string path,
AudioType audioType = AudioType.OGGVORBIS)
{
using var request =
UnityWebRequestMultimedia.GetAudioClip(path, audioType);

await request.SendWebRequest();

if (request.result == UnityWebRequest.Result.Success)
{
return DownloadHandlerAudioClip.GetContent(request);
}

throw new FileNotFoundException(request.result.ToString());
}

}

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading