Skip to content

Commit

Permalink
Merge branch 'JayDiddyThaGOAT-post-song-goes-to-song-selection' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
EliteAsian123 committed May 12, 2023
2 parents 28a1f97 + 6932320 commit 5532222
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 68 deletions.
4 changes: 2 additions & 2 deletions Assets/Scenes/MenuScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -12427,7 +12427,7 @@ MonoBehaviour:
m_Calls:
- m_Target: {fileID: 874813134}
m_TargetAssemblyTypeName: YARG.UI.MainMenu, Assembly-CSharp
m_MethodName: ShowMainMenu
m_MethodName: ShowSongSelect
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
Expand Down Expand Up @@ -16964,7 +16964,7 @@ MonoBehaviour:
m_Calls:
- m_Target: {fileID: 874813134}
m_TargetAssemblyTypeName: YARG.UI.MainMenu, Assembly-CSharp
m_MethodName: ShowMainMenu
m_MethodName: ShowSongSelect
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
Expand Down
2 changes: 2 additions & 0 deletions Assets/Script/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class GameManager : MonoBehaviour {
private AudioMixerGroup vocalGroup;

public SceneIndex CurrentScene { get; private set; } = SceneIndex.PERSISTANT;

public SongEntry SelectedSong { get; set; }

private void Awake() {
Instance = this;
Expand Down
30 changes: 15 additions & 15 deletions Assets/Script/PlayMode/Play.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public static Play Instance {

public const float SONG_START_OFFSET = -2f;

public static SongEntry song = null;

public delegate void BeatAction();
public static event BeatAction BeatEvent;

Expand Down Expand Up @@ -106,6 +104,8 @@ public bool Paused {
}
}

private SongEntry Song => GameManager.Instance.SelectedSong;

private bool playingRhythm = false;

private void Awake() {
Expand All @@ -127,10 +127,10 @@ private void StartSong() {
bool isSpeedUp = Math.Abs(speed - 1) > float.Epsilon;

// Load MOGG if CON, otherwise load stems
if (song is ExtractedConSongEntry rawConSongEntry) {
if (Song is ExtractedConSongEntry rawConSongEntry) {
GameManager.AudioManager.LoadMogg(rawConSongEntry, isSpeedUp);
} else {
var stems = AudioHelpers.GetSupportedStems(song.Location);
var stems = AudioHelpers.GetSupportedStems(Song.Location);

GameManager.AudioManager.LoadSong(stems, isSpeedUp);
}
Expand Down Expand Up @@ -193,13 +193,13 @@ private void StartSong() {
}
}

OnSongStart?.Invoke(song);
OnSongStart?.Invoke(Song);
}

private void LoadBackground() {
// Try a yarground first

string backgroundPath = Path.Combine(song.Location, "bg.yarground");
string backgroundPath = Path.Combine(Song.Location, "bg.yarground");
if (File.Exists(backgroundPath)) {
var bundle = AssetBundle.LoadFromFile(backgroundPath);

Expand All @@ -222,7 +222,7 @@ private void LoadBackground() {
};

foreach (var file in videoPaths) {
var path = Path.Combine(song.Location, file);
var path = Path.Combine(Song.Location, file);

if (File.Exists(path)) {
GameUI.Instance.videoPlayer.url = path;
Expand All @@ -241,7 +241,7 @@ private void LoadBackground() {
};

foreach (var file in imagePaths) {
var path = Path.Combine(song.Location, file);
var path = Path.Combine(Song.Location, file);

if (File.Exists(path)) {
var png = ImageHelper.LoadTextureFromFile(path);
Expand All @@ -255,7 +255,7 @@ private void LoadBackground() {
private void LoadChart() {
// Add main file
var files = new List<string> {
Path.Combine(song.Location, song.NotesFile)
Path.Combine(Song.Location, Song.NotesFile)
};

// Look for upgrades and add
Expand All @@ -269,18 +269,18 @@ private void LoadChart() {
// Parse

MoonSong moonSong = null;
if (song.NotesFile.EndsWith(".chart")) {
if (Song.NotesFile.EndsWith(".chart")) {
Debug.Log("Reading .chart file");
moonSong = ChartReader.ReadChart(files[0]);
}

chart = new YargChart(moonSong);
if (song.NotesFile.EndsWith(".mid")) {
if (Song.NotesFile.EndsWith(".mid")) {
// Parse
var parser = new MidiParser(song, files.ToArray());
var parser = new MidiParser(Song, files.ToArray());
chart.InitializeArrays();
parser.Parse(chart);
} else if (song.NotesFile.EndsWith(".chart")) {
} else if (Song.NotesFile.EndsWith(".chart")) {
var handler = new BeatHandler(moonSong);
handler.GenerateBeats();
chart.beats = handler.Beats;
Expand Down Expand Up @@ -491,15 +491,15 @@ public void Exit() {
GameManager.AudioManager.UnloadSong();

// Call events
OnSongEnd?.Invoke(song);
OnSongEnd?.Invoke(Song);

// Unpause just in case
Time.timeScale = 1f;

backgroundRenderTexture.ClearTexture();
_tracks.Clear();

OnSongEnd?.Invoke(song);
OnSongEnd?.Invoke(Song);
GameManager.Instance.LoadScene(SceneIndex.MENU);
}

Expand Down
20 changes: 16 additions & 4 deletions Assets/Script/Song/Preparsers/ChartPreparser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,29 @@ public static class ChartPreparser {
// { Instrument.GHLiveCoop, "GHLCoop" }
};

public static ulong GetAvailableTracks(byte[] chartData) {
public static bool GetAvailableTracks(byte[] chartData, out ulong tracks) {
using var stream = new MemoryStream(chartData);

using var reader = new StreamReader(stream);
return ReadStream(reader);
try {
tracks = ReadStream(reader);
return true;
} catch {
tracks = 0;
return false;
}
}

public static ulong GetAvailableTracks(SongEntry song) {
public static bool GetAvailableTracks(SongEntry song, out ulong tracks) {
using var reader = File.OpenText(Path.Combine(song.Location, song.NotesFile));

return ReadStream(reader);
try {
tracks = ReadStream(reader);
return true;
} catch {
tracks = 0;
return false;
}
}

private static ulong ReadStream(StreamReader reader) {
Expand Down
25 changes: 13 additions & 12 deletions Assets/Script/Song/Preparsers/MidPreparser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.IO;
using Melanchall.DryWetMidi.Core;
using MoonscraperChartEditor.Song.IO;
using UnityEngine;
using YARG.Data;

namespace YARG.Song.Preparsers {
Expand Down Expand Up @@ -31,26 +30,28 @@ public static class MidPreparser {

};

public static ulong GetAvailableTracks(byte[] chartData) {
public static bool GetAvailableTracks(byte[] chartData, out ulong tracks) {
using var stream = new MemoryStream(chartData);
try {
var midi = MidiFile.Read(stream, ReadSettings);
return ReadStream(midi);
tracks = ReadStream(midi);
return true;
} catch (Exception e) {
Debug.LogError(e.Message);
Debug.LogError(e.StackTrace);
return ulong.MaxValue;
// Debug.LogError(e.Message);
// Debug.LogError(e.StackTrace);
tracks = 0;
return false;
}
}

public static ulong GetAvailableTracks(SongEntry song) {
public static bool GetAvailableTracks(SongEntry song, out ulong tracks) {
try {
var midi = MidiFile.Read(Path.Combine(song.Location, song.NotesFile), ReadSettings);
return ReadStream(midi);
} catch (Exception e) {
Debug.LogError(e.Message);
Debug.LogError(e.StackTrace);
return ulong.MaxValue;
tracks = ReadStream(midi);
return true;
} catch {
tracks = 0;
return false;
}
}

Expand Down
3 changes: 3 additions & 0 deletions Assets/Script/Song/ScanHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public static class ScanHelpers {

public static ScanResult ParseSongIni(string iniFile, IniSongEntry entry) {
var file = new IniFile(iniFile);

// Had some reports that ini parsing might throw an exception, leaving this in for now
// in as I don't know the cause just yet and I want to investigate it further.
file.Parse();

string sectionName = file.ContainsSection("song") ? "song" : "Song";
Expand Down
20 changes: 15 additions & 5 deletions Assets/Script/Song/Scanning/SongScanThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private void ScanSubDirectory(string cacheFolder, string subDir, ICollection<Son
_errorsEncountered++;
errorsEncountered = _errorsEncountered;
_songErrors[cacheFolder].Add(new SongError(subDir, result));
Debug.LogWarning($"Error encountered with {subDir}");
Debug.LogWarning($"Error encountered with {subDir}: {result}");
return;
}

Expand Down Expand Up @@ -251,9 +251,15 @@ private static ScanResult ScanIniSong(string cache, string directory, out SongEn
var tracks = ulong.MaxValue;

if (notesFile.EndsWith(".mid")) {
tracks = MidPreparser.GetAvailableTracks(bytes);
if (!MidPreparser.GetAvailableTracks(bytes, out ulong availTracks)) {
return ScanResult.CorruptedNotesFile;
}
tracks = availTracks;
} else if (notesFile.EndsWith(".chart")) {
tracks = ChartPreparser.GetAvailableTracks(bytes);
if (!ChartPreparser.GetAvailableTracks(bytes, out ulong availTracks)) {
return ScanResult.CorruptedNotesFile;
}
tracks = availTracks;
}

// We have a song.ini, notes file and audio. The song is scannable.
Expand Down Expand Up @@ -289,7 +295,9 @@ private static ScanResult ScanExConSong(string cache, ExtractedConSongEntry file

string checksum = BitConverter.ToString(SHA1.Create().ComputeHash(bytes)).Replace("-", "");

ulong tracks = MidPreparser.GetAvailableTracks(bytes);
if (!MidPreparser.GetAvailableTracks(bytes, out ulong tracks)) {
return ScanResult.CorruptedNotesFile;
}

file.CacheRoot = cache;
file.Checksum = checksum;
Expand Down Expand Up @@ -321,7 +329,9 @@ private static ScanResult ScanConSong(string cache, ConSongEntry file) {

string checksum = BitConverter.ToString(SHA1.Create().ComputeHash(bytes)).Replace("-", "");

ulong tracks = MidPreparser.GetAvailableTracks(bytes);
if (!MidPreparser.GetAvailableTracks(bytes, out ulong tracks)) {
return ScanResult.CorruptedNotesFile;
}

file.CacheRoot = cache;
file.Checksum = checksum;
Expand Down
7 changes: 6 additions & 1 deletion Assets/Script/Song/Scanning/SongScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public enum ScanResult {
NotASong,
NoNotesFile,
NoAudioFile,
EncryptedMogg
EncryptedMogg,
CorruptedNotesFile,
CorruptedMetadataFile
}

public readonly struct SongError {
Expand Down Expand Up @@ -231,6 +233,9 @@ await writer.WriteLineAsync(
case ScanResult.EncryptedMogg:
await writer.WriteLineAsync("These songs contain encrypted moggs!");
break;
case ScanResult.CorruptedNotesFile:
await writer.WriteLineAsync("These songs contain a corrupted notes.chart/notes.mid file!");
break;
}
lastResult = error.Result;
}
Expand Down
22 changes: 7 additions & 15 deletions Assets/Script/UI/DifficultySelect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ private void IncreasePlayerIndex() {
}

// Play song
Play.song = MainMenu.Instance.chosenSong;
GameManager.Instance.LoadScene(SceneIndex.PLAY);
} else {
UpdateInstrument();
Expand All @@ -224,7 +223,9 @@ private void UpdateInstrument() {

// Get available instruments
var availableInstruments = allInstruments
.Where(instrument => MainMenu.Instance.chosenSong.HasInstrument(instrument)).ToList();
.Where(instrument => GameManager.Instance.SelectedSong.HasInstrument(instrument)).ToList();

Debug.Log(GameManager.Instance.SelectedSong.AvailableParts);

// Force add pro drums and five lane
if (availableInstruments.Contains(Instrument.DRUMS)) {
Expand All @@ -242,25 +243,16 @@ private void UpdateInstrument() {
// Filter out to only allowed instruments
availableInstruments.RemoveAll(i => !player.inputStrategy.GetAllowedInstruments().Contains(i));

bool showSitOut = availableInstruments.Count <= 0 || PlayerManager.players.Count > 1;

optionCount = availableInstruments.Count + (showSitOut ? 1 : 0);
if (showSitOut) {
optionCount++;
}
optionCount = availableInstruments.Count + 1;

// Add to options
var ops = new string[optionCount];
var ops = new string[availableInstruments.Count + 1];
instruments = new string[availableInstruments.Count];
for (int i = 0; i < instruments.Length; i++) {
instruments[i] = availableInstruments[i].ToStringName();
ops[i] = availableInstruments[i].ToLocalizedName();
}

// Add sit out (only if there are more than 1 player)
if (showSitOut) {
ops[^1] = "Sit Out";
}
ops[^1] = "Sit Out";

// Set text and sprites
for (int i = 0; i < 6; i++) {
Expand Down Expand Up @@ -294,7 +286,7 @@ private void UpdateDifficulty(string chosenInstrument, bool showExpertPlus) {
// Get the available difficulties
var availableDifficulties = new List<Difficulty>();
for (int i = 0; i < (int) Difficulty.EXPERT_PLUS; i++) {
if (!MainMenu.Instance.chosenSong.HasPart(instrument, (Difficulty) i)) {
if (!GameManager.Instance.SelectedSong.HasPart(instrument, (Difficulty) i)) {
continue;
}
availableDifficulties.Add((Difficulty) i);
Expand Down
8 changes: 4 additions & 4 deletions Assets/Script/UI/GameUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ private void Awake() {

private void Start() {
if (Play.speed == 1f) {
songTitle.text = $"{Play.song.Name}";
bandName.text = $"{Play.song.Artist}";
songTitle.text = $"{GameManager.Instance.SelectedSong.Name}";
bandName.text = $"{GameManager.Instance.SelectedSong.Artist}";
} else {
songTitle.text = $"{Play.song.Name} ({Play.speed * 100}%)";
bandName.text = $"{Play.song.Artist}";
songTitle.text = $"{GameManager.Instance.SelectedSong.Name} ({Play.speed * 100}%)";
bandName.text = $"{GameManager.Instance.SelectedSong.Artist}";
}
}

Expand Down
2 changes: 0 additions & 2 deletions Assets/Script/UI/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ private enum ButtonIndex {
EXIT
}

public SongEntry chosenSong = null;

[SerializeField]
private Canvas mainMenu;
[SerializeField]
Expand Down
Loading

0 comments on commit 5532222

Please sign in to comment.