Skip to content

Commit f37ea3c

Browse files
Additional warnings and error checking for msupcm++ info
1 parent 60369ea commit f37ea3c

File tree

9 files changed

+222
-116
lines changed

9 files changed

+222
-116
lines changed

MSUScripter/Configs/MsuSongMsuPcmInfo.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ public bool AreFilesValid()
8181

8282
public List<string> GetFiles()
8383
{
84+
if (!string.IsNullOrEmpty(Output))
85+
{
86+
var outputFile = Output;
87+
}
88+
8489
List<string> files = new List<string>();
8590

8691
if (!string.IsNullOrEmpty(File))
@@ -105,6 +110,26 @@ public bool HasBothSubTracksAndSubChannels
105110
}
106111
}
107112

113+
[YamlIgnore]
114+
public bool HasValidSubChannelCount
115+
{
116+
get
117+
{
118+
return SubChannels.Count != 1 && SubChannels.All(x => x.HasValidSubChannelCount) &&
119+
SubTracks.All(x => x.HasValidSubChannelCount);
120+
}
121+
}
122+
123+
[YamlIgnore]
124+
public bool HasValidChildTypes
125+
{
126+
get
127+
{
128+
return SubChannels.All(x => x.SubChannels.Count == 0 && x.HasValidChildTypes) &&
129+
SubTracks.All(x => x.SubTracks.Count == 0 && x.HasValidChildTypes);
130+
}
131+
}
132+
108133
public bool HasData()
109134
{
110135
return Loop > 0 || TrimStart > 0 || TrimEnd > 0 || FadeIn > 0 || FadeOut > 0 || CrossFade > 0 || PadStart > 0 ||

MSUScripter/Configs/MsuTrackInfo.cs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,31 +77,21 @@ public void RemoveSong(MsuSongInfo song)
7777
public void MoveSong(MsuProject project, MsuSongInfo song, int index)
7878
{
7979
var oldTrack = project.Tracks.First(x => x.TrackNumber == song.TrackNumber);
80-
var currentIndex = oldTrack.Songs.IndexOf(song);
81-
var newIndex = index;
82-
83-
for (var i = oldTrack.Songs.Count - 1; i >= currentIndex + 1; i--)
84-
{
85-
oldTrack.Songs[i].OutputPath = oldTrack.Songs[i - 1].OutputPath;
86-
oldTrack.Songs[i].MsuPcmInfo.Output = oldTrack.Songs[i - 1].MsuPcmInfo.Output;
87-
oldTrack.Songs[i].IsAlt = oldTrack.Songs[i - 1].IsAlt;
88-
}
8980

9081
oldTrack.Songs.Remove(song);
91-
Songs.Insert(newIndex, song);
92-
93-
var lastIndex = Songs.Count - 1;
94-
for (var i = newIndex; i < lastIndex; i++)
82+
for (var i = 0; i < oldTrack.Songs.Count; i++)
9583
{
96-
Songs[i].OutputPath = Songs[i + 1].OutputPath;
97-
Songs[i].MsuPcmInfo.Output = Songs[i + 1].MsuPcmInfo.Output;
98-
Songs[i].IsAlt = Songs[i + 1].IsAlt;
84+
UpdateSongPath(project, oldTrack.Songs[i], i);
85+
}
86+
87+
Songs.Insert(index, song);
88+
for (var i = 0; i < Songs.Count; i++)
89+
{
90+
UpdateSongPath(project, Songs[i], i);
9991
}
10092

10193
song.TrackName = TrackName;
10294
song.TrackNumber = TrackNumber;
103-
104-
UpdateSongPath(project, Songs[lastIndex], lastIndex);
10595
}
10696

10797
private void UpdateSongPath(MsuProject project, MsuSongInfo song, int? index = null)

MSUScripter/Services/ControlServices/MsuSongPanelService.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ public void CheckFileErrors(MsuSongAdvancedPanelViewModel model)
104104
var song = model.CurrentSongInfo;
105105

106106
var hasBothSubTracksAndSubChannels = false;
107+
var hasSoloSubChannel = false;
108+
var hasSameParentChildType = false;
109+
var hasIgnoredFile = false;
107110

108111
List<string> files = [];
109112
foreach (var treeItem in model.TreeItems.Where(x => x.MsuPcmInfo != null))
@@ -116,23 +119,40 @@ public void CheckFileErrors(MsuSongAdvancedPanelViewModel model)
116119
var subTracks = treeItem.ChildrenTreeData.FirstOrDefault();
117120
var subChannels = treeItem.ChildrenTreeData.LastOrDefault();
118121

119-
if (subChannels != null && subTracks != null && subTracks != subChannels && subTracks.ChildrenTreeData.Count > 0 && subChannels.ChildrenTreeData.Count > 0)
122+
if (subChannels != null && subTracks != null && subTracks != subChannels &&
123+
subTracks.ChildrenTreeData.Count > 0 && subChannels.ChildrenTreeData.Count > 0)
120124
{
121125
hasBothSubTracksAndSubChannels = true;
126+
127+
if (subChannels.ChildrenTreeData.Count == 1)
128+
{
129+
hasSoloSubChannel = true;
130+
}
131+
}
132+
133+
if (treeItem is { IsSubChannel: true, ParentTreeData.ParentTreeData.IsSubChannel: true } or { IsSubTrack: true, ParentTreeData.ParentTreeData.IsSubTrack: true })
134+
{
135+
hasSameParentChildType = true;
136+
}
137+
138+
if (!string.IsNullOrEmpty(treeItem.MsuPcmInfo.File) &&
139+
(subTracks?.ChildrenTreeData.Count > 0 || subChannels?.ChildrenTreeData.Count > 0))
140+
{
141+
hasIgnoredFile = true;
122142
}
123143
}
124144

125145
if (files.Count == 0)
126146
{
127-
model.UpdateTrackWarnings(false, false, false);
147+
model.UpdateTrackWarnings(false, false, false, false, false, false);
128148
return;
129149
}
130150

131151
files = files.Distinct().ToList();
132152

133153
if (files.Count > 1)
134154
{
135-
model.UpdateTrackWarnings(false, true, hasBothSubTracksAndSubChannels);
155+
model.UpdateTrackWarnings(false, true, hasBothSubTracksAndSubChannels, hasSoloSubChannel, hasSameParentChildType, hasIgnoredFile);
136156
return;
137157
}
138158

@@ -142,7 +162,7 @@ public void CheckFileErrors(MsuSongAdvancedPanelViewModel model)
142162
var sampleRate = GetSavedSampleInfo(project, path) ?? await GetSampleRateAsync(project, path);
143163
if (model.CurrentSongInfo == song)
144164
{
145-
model.UpdateTrackWarnings(sampleRate != 44100, false, hasBothSubTracksAndSubChannels);
165+
model.UpdateTrackWarnings(sampleRate != 44100, false, hasBothSubTracksAndSubChannels, hasSoloSubChannel, hasSameParentChildType, hasIgnoredFile);
146166
}
147167
});
148168
}
@@ -217,6 +237,11 @@ public async Task<GeneratePcmFileResponse> GeneratePcm(MsuProject project, MsuSo
217237
}
218238
return await msuPcmService.CreatePcm(project, song, asPrimary, false, true);
219239
}
240+
241+
public MsuPcmJsonInfo GenerateSongTracksFile(MsuProject project, MsuSongInfo song, string path)
242+
{
243+
return msuPcmService.ExportMsuPcmTracksJson(project, song, path, song.OutputPath);
244+
}
220245

221246
public async Task<AnalysisDataOutput?> AnalyzeAudio(MsuProject project, MsuSongInfo song)
222247
{

MSUScripter/Services/ConverterService.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,15 @@ public Track_base ConvertMsuPcmTrackInfo(MsuSongMsuPcmInfo trackBase, bool isSub
186186
if (!isSubTrack && !isSubChannel && output is Track track)
187187
{
188188
if (trackBase.SubChannels.Any())
189-
track.Sub_channels = trackBase.SubChannels.Select(x => ConvertMsuPcmTrackInfo(x, false, true)).Cast<Sub_channel>().ToList();
189+
{
190+
if (string.IsNullOrEmpty(track.File))
191+
{
192+
track.File = trackBase.GetFiles().FirstOrDefault();
193+
}
194+
track.Sub_channels = trackBase.SubChannels.Select(x => ConvertMsuPcmTrackInfo(x, false, true))
195+
.Cast<Sub_channel>().ToList();
196+
}
197+
190198
if (trackBase.SubTracks.Any())
191199
track.Sub_tracks = trackBase.SubTracks.Select(x => ConvertMsuPcmTrackInfo(x, true, false)).Cast<Sub_track>().ToList();
192200
}
@@ -196,6 +204,10 @@ public Track_base ConvertMsuPcmTrackInfo(MsuSongMsuPcmInfo trackBase, bool isSub
196204
}
197205
else if (isSubTrack && output is Sub_track subTrack && trackBase.SubChannels.Any())
198206
{
207+
if (string.IsNullOrEmpty(subTrack.File))
208+
{
209+
subTrack.File = trackBase.GetFiles().FirstOrDefault();
210+
}
199211
subTrack.Sub_channels = trackBase.SubChannels.Select(x => ConvertMsuPcmTrackInfo(x, false, true)).Cast<Sub_channel>().ToList();
200212
}
201213

MSUScripter/Services/MsuPcmService.cs

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class MsuPcmJsonInfo
2626
// ReSharper disable once UnusedAutoPropertyAccessor.Global
2727
public required string JsonFilePath { get; init; }
2828
public required string JsonText { get; init; }
29+
public string? ErrorMessage { get; init; }
2930
}
3031

3132
public class MsuPcmResult
@@ -150,26 +151,6 @@ public async Task<GeneratePcmFileResponse> CreateTempPcm(MsuProject project, str
150151
return result;
151152
}
152153

153-
public void DeleteTempJsonFiles()
154-
{
155-
var jsonDirectory = Path.Combine(Directories.TempFolder, "msupcm");
156-
var tempDirectory = new DirectoryInfo(jsonDirectory);
157-
if (tempDirectory.Exists)
158-
{
159-
foreach (var tempPcm in tempDirectory.EnumerateFiles("*.json"))
160-
{
161-
try
162-
{
163-
tempPcm.Delete();
164-
}
165-
catch
166-
{
167-
logger.LogWarning("Could not delete {File}", tempPcm.FullName);
168-
}
169-
}
170-
}
171-
}
172-
173154
public async Task<GeneratePcmFileResponse> CreatePcm(MsuProject project, MsuSongInfo song, bool asPrimary, bool isBulkGeneration, bool cacheResults)
174155
{
175156
if (!IsValid)
@@ -185,22 +166,7 @@ public async Task<GeneratePcmFileResponse> CreatePcm(MsuProject project, MsuSong
185166
{
186167
await audioPlayerService.StopSongAsync(song.OutputPath);
187168
}
188-
189-
if (!song.HasAudioFiles())
190-
{
191-
WriteFailureToStatusBar();
192-
GeneratingPcm?.Invoke(this, false);
193-
return new GeneratePcmFileResponse(false, false, "No input files selected.", null);
194-
}
195169

196-
if (song.MsuPcmInfo.HasBothSubTracksAndSubChannels)
197-
{
198-
var error = "Subtracks and subchannels can't be at the same level and be generated by msupcm++.";
199-
WriteFailureToStatusBar();
200-
GeneratingPcm?.Invoke(this, false);
201-
return new GeneratePcmFileResponse(false, false, error, null);
202-
}
203-
204170
if (song.TrackNumber < 0)
205171
{
206172
logger.LogInformation("Generating PCM file for song {Song}", string.IsNullOrEmpty(song.SongName) ? song.TrackName : song.SongName);
@@ -215,15 +181,15 @@ public async Task<GeneratePcmFileResponse> CreatePcm(MsuProject project, MsuSong
215181
{
216182
Directory.CreateDirectory(tempPath);
217183
}
218-
var tempJsonPath = Path.Combine(tempPath, $"temp.json");
219-
var tempPcmPath = Path.Combine(tempPath, $"temp.pcm");
184+
var tempJsonPath = Path.Combine(tempPath, "temp.json");
185+
var tempPcmPath = Path.Combine(tempPath, "temp.pcm");
220186
var jsonResponse = ExportMsuPcmTracksJson(project, song, tempJsonPath, tempPcmPath);
221187

222188
if (string.IsNullOrEmpty(jsonResponse.JsonText))
223189
{
224190
WriteFailureToStatusBar();
225191
GeneratingPcm?.Invoke(this, false);
226-
return new GeneratePcmFileResponse(false, false, "Failed to generate MsuPcm++ JSON file", null);
192+
return new GeneratePcmFileResponse(false, false, jsonResponse.ErrorMessage ?? "Failed to generate MsuPcm++ JSON file", null);
227193
}
228194

229195
var outputPath = song.OutputPath;
@@ -696,8 +662,54 @@ private async Task<MsuPcmResult> RunMsuPcmInternalAsync(string innerCommand, str
696662
}
697663
}
698664

665+
private string? CheckSongForErrors(MsuSongInfo song)
666+
{
667+
var audioFiles = song.MsuPcmInfo.GetFiles();
668+
669+
if (audioFiles.Count == 0)
670+
{
671+
return "No input files selected.";
672+
}
673+
674+
foreach (var path in audioFiles.Where(path => !File.Exists(path)))
675+
{
676+
return $"Audio file {path} is not found.";
677+
}
678+
679+
if (song.MsuPcmInfo.HasBothSubTracksAndSubChannels)
680+
{
681+
return "Subtracks and subchannels can't be at the same level and be generated by msupcm++.";
682+
}
683+
684+
if (!song.MsuPcmInfo.HasValidSubChannelCount)
685+
{
686+
return "If subchannels are to be used, you need at least two of them.";
687+
}
688+
689+
if (!song.MsuPcmInfo.HasValidChildTypes)
690+
{
691+
return "Subchannels cannot contain more subchannels and subtracks cannot contain more subtracks. You must alternate between subchannel and subtracks when creating multiple layers.";
692+
}
693+
694+
return null;
695+
}
696+
699697
public MsuPcmJsonInfo ExportMsuPcmTracksJson(MsuProject project, MsuSongInfo? singleSong = null, string? exportPath = null, string? pcmPath = null)
700698
{
699+
if (singleSong != null)
700+
{
701+
var error = CheckSongForErrors(singleSong);
702+
if (!string.IsNullOrEmpty(error))
703+
{
704+
return new MsuPcmJsonInfo
705+
{
706+
JsonText = "",
707+
JsonFilePath = "",
708+
ErrorMessage = error,
709+
};
710+
}
711+
}
712+
701713
var msu = new FileInfo(project.MsuPath);
702714

703715
if (string.IsNullOrEmpty(exportPath))

0 commit comments

Comments
 (0)