Skip to content

Commit 3a1b790

Browse files
committed
allow converted songs to go straight into the master list
1 parent 4e3f661 commit 3a1b790

File tree

5 files changed

+70
-43
lines changed

5 files changed

+70
-43
lines changed

TabPlayer.Songs/Convert/SongConvertManager.cs

+14-13
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,33 @@ public class SongConvertManager
1313
public enum SongType {
1414
Psarc, Midi
1515
}
16-
public static async Task<bool> ConvertFile(SongType type, string location, bool reconvert, bool copySource, Action<string> output) {
17-
bool success = false;
16+
public static async Task<DirectoryInfo> ConvertFile(SongType type, string location, bool reconvert, bool copySource, Action<string> output) {
17+
DirectoryInfo outputFolder = null;
1818

1919
try {
2020
if (type == SongType.Midi)
21-
success = ExportMidi(new DirectoryInfo(location), output);
21+
outputFolder = ExportMidi(new DirectoryInfo(location), output);
2222
if (type == SongType.Psarc)
23-
success = await ExportPsarc(new FileInfo(location), reconvert, copySource, output);
23+
outputFolder = await ExportPsarc(new FileInfo(location), reconvert, copySource, output);
2424
} catch (Exception e) {
2525
Console.WriteLine(e);
2626
Console.WriteLine($"Failed to convert file '{location}'");
27-
return false;
27+
return null;
2828
}
2929

30-
return success;
30+
return outputFolder;
3131
}
3232

33-
private static async Task<bool> ExportPsarc(FileInfo file, bool reconvert, bool copySource, Action<string> output) {
33+
private static async Task<DirectoryInfo> ExportPsarc(FileInfo file, bool reconvert, bool copySource, Action<string> output) {
3434
PsarcFile p = null;
35+
DirectoryInfo outputFolder = null;
3536
try {
3637
output("Converting: " + file.FullName);
3738
p = new PsarcFile(file.FullName);
3839

3940
var songNames = p.ExtractArrangementManifests().Select(x => x.Attributes.BlockAsset).Distinct();
4041
if (songNames.Count() <= 1) {
41-
var outputFolder = await ExportSinglePsarc(p, null, reconvert, output);
42+
outputFolder = await ExportSinglePsarc(p, null, reconvert, output);
4243
if (copySource)
4344
File.Copy(file.FullName, Path.Combine(outputFolder.FullName, file.Name), true);
4445
}
@@ -48,7 +49,7 @@ private static async Task<bool> ExportPsarc(FileInfo file, bool reconvert, bool
4849

4950
foreach (var name in songNames) {
5051
try {
51-
var outputFolder = await ExportSinglePsarc(p, name.Split(':').Last(), reconvert, output);
52+
outputFolder = await ExportSinglePsarc(p, name.Split(':').Last(), reconvert, output);
5253
if (copySource) {
5354
if (sourceOfSongBatch == null) {
5455
sourceOfSongBatch = Path.Combine(outputFolder.FullName, file.Name);
@@ -71,18 +72,18 @@ await File.WriteAllTextAsync(Path.Combine(outputFolder.FullName, file.Name + ".t
7172
output("Failed to convert file, see last error in log");
7273
Console.WriteLine(ef);
7374
Console.WriteLine($"File format was weird for {file.FullName}");
74-
return false;
75+
return null;
7576
} catch (Exception e) {
7677
output("Failed to convert file, see last error in log");
7778
Console.WriteLine(e);
7879
Console.WriteLine($"Failed to convert file: {file.FullName}");
79-
return false;
80+
return null;
8081
} finally {
8182
Console.WriteLine($"Converted {file.FullName}");
8283
p?.Dispose();
8384
}
8485

85-
return true;
86+
return outputFolder;
8687
}
8788

8889
private static async Task<DirectoryInfo> ExportSinglePsarc(PsarcFile p, string songFilter, bool reconvert, Action<string> output)
@@ -130,7 +131,7 @@ await File.WriteAllTextAsync(jsonFile, JsonConvert.SerializeObject(info, Newtons
130131
return jsonFile;
131132
}
132133

133-
private static bool ExportMidi(DirectoryInfo f, Action<string> output) {
134+
private static DirectoryInfo ExportMidi(DirectoryInfo f, Action<string> output) {
134135
throw new Exception("Midi not supported, here for reference");
135136
}
136137
}

TabPlayer.Songs/SongFileManager.cs

+30-11
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,12 @@ public static void UpdateSongList(Action<string> output) {
5656
int total = songDirs.Count;
5757
int i = 0;
5858
foreach (var songDir in songDirs) {
59-
var file = songDir.GetFiles("*.json").FirstOrDefault();
60-
if (file == null) continue;
61-
62-
var noteInfo = JsonConvert.DeserializeObject<SongInfo>(File.ReadAllText(file.FullName));
63-
output($"{i}/{total} Reading all songs, current: {noteInfo?.Metadata?.Name}");
64-
var instruments = noteInfo.Instruments.Select(x => new SongFileInstrument(x.Name, x == noteInfo.MainInstrument, x.Config.Tuning, x.TotalNoteCount(), x.Config.CapoFret)).ToArray();
65-
var lyrics = noteInfo.Lyrics != null ? new SongFileLyrics(noteInfo.Lyrics.Lines?.Sum(x => x.Words?.Count) ?? 0) : null;
66-
songList.Data.Add(new SongFile(songDir.Name, noteInfo.Metadata.Name,
67-
noteInfo.Metadata.Artist, noteInfo.Metadata.Album, noteInfo.Metadata.Year,
68-
noteInfo.MainInstrument.LastNoteTime, instruments, lyrics));
69-
59+
output($"{i}/{total} Reading all songs, current: {songDir.Name}");
60+
var song = ReadSingleSong(songDir);
61+
if (song == null)
62+
continue;
63+
songList.Data.Add(song);
64+
7065
i++;
7166
}
7267
songList.Data.Sort((x, y) => x.SongName.CompareTo(y.SongName)); // default sort of name
@@ -77,6 +72,30 @@ public static void UpdateSongList(Action<string> output) {
7772

7873
output($"Loaded all songs {total}/{total} into {PLAY_DATA_FILE}");
7974
}
75+
76+
public static bool AddSingleSong(string dataFolderName) {
77+
var songList = GetSongFileList();
78+
var song = ReadSingleSong(new DirectoryInfo(dataFolderName));
79+
if (song == null)
80+
return false;
81+
songList.Data.Add(song);
82+
var songListContents = JsonConvert.SerializeObject(songList);
83+
File.WriteAllText(PLAY_DATA_FILE, songListContents);
84+
return true;
85+
}
86+
87+
private static SongFile? ReadSingleSong(DirectoryInfo songDir) {
88+
var file = songDir.GetFiles("*.json").FirstOrDefault();
89+
if (file == null) return null;
90+
91+
var noteInfo = JsonConvert.DeserializeObject<SongInfo>(File.ReadAllText(file.FullName));
92+
93+
var instruments = noteInfo.Instruments.Select(x => new SongFileInstrument(x.Name, x == noteInfo.MainInstrument, x.Config.Tuning, x.TotalNoteCount(), x.Config.CapoFret)).ToArray();
94+
var lyrics = noteInfo.Lyrics != null ? new SongFileLyrics(noteInfo.Lyrics.Lines?.Sum(x => x.Words?.Count) ?? 0) : null;
95+
return new SongFile(songDir.Name, noteInfo.Metadata.Name,
96+
noteInfo.Metadata.Artist, noteInfo.Metadata.Album, noteInfo.Metadata.Year,
97+
noteInfo.MainInstrument.LastNoteTime, instruments, lyrics);
98+
}
8099
}
81100

82101
public class SongFileList {

godot/MainScene.cs

+16-11
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ private void LoadMenus() {
2828
_convertMenu.Closed += () => {
2929
_convertMenu.AnimateOut();
3030
_startMenu.AnimateIn();
31+
32+
// then reload the song list globally so converted songs exist
33+
ReloadSongList();
3134
};
3235

3336
_infoPage = GD.Load<PackedScene>("res://scenes/InfoPage.tscn").Instantiate<InfoPage>();
@@ -60,17 +63,7 @@ private void LoadStartMenu() {
6063
RemoveChild(_infoPage);
6164
AddChild(_songPick);
6265
};
63-
_startMenu.SongListFileChanged += () => {
64-
var loaded = _songPick.IsVisibleInTree();
65-
if (loaded) {
66-
_songPick.QueueFree();
67-
}
68-
LoadSongPick();
69-
70-
if (loaded) {
71-
AddChild(_songPick);
72-
}
73-
};
66+
_startMenu.SongListFileChanged += ReloadSongList;
7467
_startMenu.ConvertMenuOpened += () => {
7568
_convertMenu.AnimateIn();
7669
_startMenu.AnimateOut();
@@ -85,6 +78,18 @@ private void LoadStartMenu() {
8578
};
8679
}
8780

81+
private void ReloadSongList() {
82+
var loaded = _songPick.IsVisibleInTree();
83+
if (loaded) {
84+
_songPick.QueueFree();
85+
}
86+
LoadSongPick();
87+
88+
if (loaded) {
89+
AddChild(_songPick);
90+
}
91+
}
92+
8893
private void LoadSongPick() {
8994
_songPick = GD.Load<PackedScene>("res://scenes/SongPick.tscn").Instantiate<SongPick>();
9095
_songPick.Closed += () => {

godot/scenes/ConvertMenu.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Godot;
22
using murph9.TabPlayer.scenes.Services;
3+
using murph9.TabPlayer.Songs;
34
using murph9.TabPlayer.Songs.Convert;
45
using System;
56
using System.Collections.Generic;
@@ -62,11 +63,16 @@ private async Task ConvertFiles(string[] files) {
6263
var failed = new List<string>();
6364
foreach (var psarc in files) {
6465
try {
65-
var success = await SongConvertManager.ConvertFile(SongConvertManager.SongType.Psarc, psarc, recreate, copySource, (string str) => {infoLabel.Text = str;});
66-
if (success)
67-
completed.Add(psarc);
68-
else
66+
var outputFolder = await SongConvertManager.ConvertFile(SongConvertManager.SongType.Psarc, psarc, recreate, copySource, (string str) => {infoLabel.Text = str;});
67+
if (outputFolder != null) {
68+
var success = SongFileManager.AddSingleSong(outputFolder.FullName);
69+
if (success)
70+
completed.Add(psarc);
71+
else
72+
failed.Add(psarc);
73+
} else {
6974
failed.Add(psarc);
75+
}
7076
} catch (Exception e) {
7177
GD.Print(e, "ogg file not converted: " + psarc);
7278
infoLabel.Text = "Failed to convert psarc file: " + psarc;

godot/scenes/ConvertMenu.tscn

-4
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,6 @@ text = "Convert All from Downloads"
5959
layout_mode = 2
6060
text = " "
6161

62-
[node name="AfterLabel" type="Label" parent="."]
63-
layout_mode = 2
64-
text = "After converting songs they won't be loaded yet"
65-
6662
[connection signal="dir_selected" from="FileDialog" to="." method="Dir_Selected"]
6763
[connection signal="file_selected" from="FileDialog" to="." method="File_Selected"]
6864
[connection signal="files_selected" from="FileDialog" to="." method="Files_Selected"]

0 commit comments

Comments
 (0)