Skip to content

Commit

Permalink
Song delays
Browse files Browse the repository at this point in the history
  • Loading branch information
EliteAsian123 committed Dec 22, 2022
1 parent 83b2aee commit 7ffda59
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 38 deletions.
13 changes: 8 additions & 5 deletions Assets/Script/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
using YARG.Serialization;
using YARG.UI;
using YARG.Utils;

namespace YARG {
public class Game : MonoBehaviour {
Expand All @@ -15,7 +17,7 @@ public class Game : MonoBehaviour {
public static readonly DirectoryInfo SONG_FOLDER = new(@"B:\Clone Hero Alpha\Songs");
public static readonly FileInfo CACHE_FILE = new(Path.Combine(SONG_FOLDER.ToString(), "yarg_cache.json"));

public static DirectoryInfo song = new(@"B:\Clone Hero Alpha\Songs\Jane's Addiction - Been Caught Stealing");
public static SongInfo song = null;

[SerializeField]
private GameObject soundAudioPrefab;
Expand Down Expand Up @@ -44,13 +46,13 @@ private void Awake() {

// Song

StartCoroutine(StartSong(song));
StartCoroutine(StartSong());
}

private IEnumerator StartSong(DirectoryInfo songFolder) {
private IEnumerator StartSong() {
// Load audio
List<AudioSource> audioSources = new();
foreach (var file in songFolder.GetFiles("*.ogg")) {
foreach (var file in song.folder.GetFiles("*.ogg")) {
if (file.Name == "preview.ogg") {
continue;
}
Expand All @@ -69,7 +71,8 @@ private IEnumerator StartSong(DirectoryInfo songFolder) {
}

// Load midi
var parser = new MidiParser(Path.Combine(songFolder.FullName, "notes.mid"));
var parser = new MidiParser(Path.Combine(song.folder.FullName, "notes.mid"),
song.delay + SourceDelays.GetSourceDelay(song.source));
chart = new Chart();
parser.Parse(chart);

Expand Down
4 changes: 3 additions & 1 deletion Assets/Script/Serialization/AbstractParser.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace YARG.Serialization {
public abstract class AbstractParser {
protected string file;
protected float delay;

public AbstractParser(string file) {
public AbstractParser(string file, float delay) {
this.file = file;
this.delay = delay;
}

public abstract void Parse(Chart chart);
Expand Down
26 changes: 23 additions & 3 deletions Assets/Script/Serialization/MidiParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private struct NoteIR {

public MidiFile midi;

public MidiParser(string file) : base(file) {
public MidiParser(string file, float delay) : base(file, delay) {
midi = MidiFile.Read(file);
}

Expand Down Expand Up @@ -70,13 +70,33 @@ public override void Parse(Chart chart) {
}
}

// Sort by time (just in case)
// Sort by time (just in case) and add delay

foreach (var part in chart.allParts) {
foreach (var difficulty in part) {
if (difficulty == null) {
continue;
}

// Sort
difficulty?.Sort(new Comparison<NoteInfo>((a, b) => a.time.CompareTo(b.time)));

// Add delay
foreach (var note in difficulty) {
note.time -= delay;
}
}
}

if (chart.events != null) {
// Sort
chart.events.Sort(new Comparison<EventInfo>((a, b) => a.time.CompareTo(b.time)));

// Add delay
foreach (var ev in chart.events) {
ev.time -= delay;
}
}
chart.events?.Sort(new Comparison<EventInfo>((a, b) => a.time.CompareTo(b.time)));
}

private List<NoteInfo> ParseGuitar(TrackChunk trackChunk, int difficulty) {
Expand Down
55 changes: 55 additions & 0 deletions Assets/Script/Serialization/SongIni.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.IO;
using IniParser;
using UnityEngine;
using YARG.UI;

namespace YARG.Serialization {
public static class SongIni {
public static SongInfo CompleteSongInfo(SongInfo song, FileIniDataParser parser) {
if (song.fetched) {
return song;
}

var file = new FileInfo(Path.Combine(song.folder.ToString(), "song.ini"));
if (!file.Exists) {
return song;
}

song.fetched = true;
try {
var data = parser.ReadFile(file.FullName);

// Set basic info
song.SongName ??= data["song"]["name"];
song.artistName ??= data["song"]["artist"];
song.source ??= data["song"]["icon"];

// Get song length
if (data["song"].ContainsKey("song_length")) {
int rawLength = int.Parse(data["song"]["song_length"]);
song.songLength = rawLength / 1000f;
} else {
Debug.LogWarning($"No song length found for: {song.folder}");
}

// Get song delay (0 if none)
if (data["song"].ContainsKey("delay")) {
int rawDelay = int.Parse(data["song"]["delay"]);
song.delay = rawDelay / 1000f;
} else {
song.delay = 0f;
}
} catch (Exception e) {
song.errored = true;
Debug.LogException(e);
}

return song;
}

public static SongInfo CompleteSongInfo(SongInfo song) {
return CompleteSongInfo(song, new FileIniDataParser());
}
}
}
11 changes: 11 additions & 0 deletions Assets/Script/Serialization/SongIni.cs.meta

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

7 changes: 5 additions & 2 deletions Assets/Script/UI/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UIElements;
using YARG.Serialization;
using YARG.Utils;

namespace YARG.UI {
Expand Down Expand Up @@ -51,7 +52,9 @@ private void OnDisable() {

private void SignalRecieved(string signal) {
if (signal.StartsWith("DownloadDone,")) {
Game.song = new(Path.Combine(PlayerManager.client.remotePath, signal[13..^0]));
Game.song = SongIni.CompleteSongInfo(new SongInfo(
new(Path.Combine(PlayerManager.client.remotePath, signal[13..^0]))
));
SceneManager.LoadScene(1);
}
}
Expand Down Expand Up @@ -111,7 +114,7 @@ private void SetupPreSong() {
if (PlayerManager.client != null) {
Menu.DownloadSong(chosenSong);
} else {
Game.song = chosenSong.folder;
Game.song = chosenSong;
SceneManager.LoadScene(1);
}
return;
Expand Down
26 changes: 2 additions & 24 deletions Assets/Script/UI/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using YARG.Serialization;

namespace YARG.UI {
public class Menu : MonoBehaviour {
Expand Down Expand Up @@ -94,30 +95,7 @@ private static void FetchSongInfo() {
// Fetch song info manually
var parser = new FileIniDataParser();
foreach (var song in songs) {
if (song.fetched) {
return;
}

var file = new FileInfo(Path.Combine(song.folder.ToString(), "song.ini"));
if (!file.Exists) {
return;
}

song.fetched = true;
try {
var data = parser.ReadFile(file.FullName);

// Set basic info
song.SongName ??= data["song"]["name"];
song.artistName ??= data["song"]["artist"];

// Get song length
int rawLength = int.Parse(data["song"]["song_length"]);
song.songLength = rawLength / 1000f;
} catch (Exception e) {
song.errored = true;
Debug.LogException(e);
}
SongIni.CompleteSongInfo(song, parser);
}

// Create cache
Expand Down
7 changes: 5 additions & 2 deletions Assets/Script/UI/SongInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
namespace YARG.UI {
[JsonObject(MemberSerialization.Fields)]
public class SongInfo {
[JsonConverter(typeof(DirectoryInfoConverter))]
public DirectoryInfo folder;
[JsonIgnore]
public bool fetched;
[JsonIgnore]
public bool errored;

[JsonConverter(typeof(DirectoryInfoConverter))]
public DirectoryInfo folder;

[field: JsonProperty("bassPedal2xExpertPlus")]
public bool BassPedal2xExpertPlus {
private set;
Expand Down Expand Up @@ -46,8 +47,10 @@ public string SongName {
public string SongNameNoParen => SongName.Replace("(", "").Replace(")", "");

public string artistName;
public string source;

public float? songLength;
public float delay;

public SongInfo(DirectoryInfo folder) {
this.folder = folder;
Expand Down
1 change: 0 additions & 1 deletion Assets/Script/UI/SongInfoComponent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace YARG.UI {
public class SongInfoComponent : MonoBehaviour {
Expand Down
14 changes: 14 additions & 0 deletions Assets/Script/Utils/SourceDelays.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace YARG.Utils {
public static class SourceDelays {
public static float GetSourceDelay(string source) {
// Hardcode a delay for certain games (such as RB1).
// There may be a chance that I am reading the MIDI or something
// incorrectly. This is the fix for now, and it seems to work for
// the most part.
return source switch {
"rb1" or "rb1dlc" => 0.15f,
_ => 0f
};
}
}
}
11 changes: 11 additions & 0 deletions Assets/Script/Utils/SourceDelays.cs.meta

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

0 comments on commit 7ffda59

Please sign in to comment.