Skip to content

Commit

Permalink
Fixed possible crash pushing STOP on song playback in FRB Editor
Browse files Browse the repository at this point in the history
Fixed NAudio codegen bug when loaded only when referenced
  • Loading branch information
vchelaru committed Jan 27, 2024
1 parent 3f89dcd commit abb5e83
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static void RefreshGlobalContentDictionary()

}

#region Generate Fields (Static Members)
#region Fields (Static Members)

public override ICodeBlock GenerateFields(ICodeBlock codeBlock, SaveClasses.IElement element)
{
Expand All @@ -91,8 +91,9 @@ public override ICodeBlock GenerateFields(ICodeBlock codeBlock, SaveClasses.IEl

for (int i = 0; i < element.ReferencedFiles.Count; i++)
{
var file = element.ReferencedFiles[i];
AppendFieldOrPropertyForReferencedFile(codeBlock,
element.ReferencedFiles[i], element, contentManagerName);
file, element, contentManagerName);

//stringBuilder.AppendLine(GetFieldForReferencedFile(mSaveObject.ReferencedFiles[i]));

Expand Down Expand Up @@ -178,6 +179,8 @@ public static void AppendFieldOrPropertyForReferencedFile(ICodeBlock codeBlock,

#endregion

#region Initialize

public override ICodeBlock GenerateInitialize(ICodeBlock codeBlock, SaveClasses.IElement element)
{
for (int i = 0; i < element.ReferencedFiles.Count; i++)
Expand All @@ -195,6 +198,8 @@ public override ICodeBlock GenerateInitialize(ICodeBlock codeBlock, SaveClasses
return codeBlock;
}

#endregion

public static void GenerateAddToManagersStatic(ICodeBlock codeBlock, SaveClasses.IElement element)
{
for (int i = 0; i < element.ReferencedFiles.Count; i++)
Expand Down
7 changes: 5 additions & 2 deletions FRBDK/Glue/NAudioPlugin/Managers/AssetTypeInfoManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,15 @@ private static string GetLoadSongCode(IElement screenOrEntity, NamedObjectSave n
ReferencedFileSave file, string contentManager)
{
var instanceName = file.GetInstanceName();

if(file.LoadedOnlyWhenReferenced)
{
instanceName = "m" + instanceName;
}
var relativeFileName = file.Name.ToLowerInvariant();

var path = $"Content/{relativeFileName}";

var contentManagerName = "contentManagerName";
var contentManagerName = contentManagerName ?? "contentManagerName";

Check failure on line 133 in FRBDK/Glue/NAudioPlugin/Managers/AssetTypeInfoManager.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Cannot use local variable 'contentManagerName' before it is declared

Check failure on line 133 in FRBDK/Glue/NAudioPlugin/Managers/AssetTypeInfoManager.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Cannot use local variable 'contentManagerName' before it is declared

if(file.DestroyOnUnload == false)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FlatRedBall.IO;
using FlatRedBall.Glue.Plugins.ExportedImplementations;
using FlatRedBall.IO;
using NAudio.Vorbis;
using NAudio.Wave;
using OfficialPlugins.SongPlugin.ViewModels;
Expand Down Expand Up @@ -96,15 +97,27 @@ private void StopButton_Click(object sender, RoutedEventArgs e)

internal void StopPlaying()
{

if (mediaPlayer != null)
// There seems to be some threading issue here, so let's try multiple times, then
// tolerate the error so we don't lose the plugin:
try
{
mediaPlayer.Stop();
mediaPlayer.Position = TimeSpan.Zero;
GlueCommands.Self.TryMultipleTimes(() =>
{

}
if (mediaPlayer != null)
{
mediaPlayer.Stop();
mediaPlayer.Position = TimeSpan.Zero;

nAudioOutputDevice?.Stop();
}

nAudioOutputDevice?.Stop();
});
}
catch(Exception ex)
{
GlueCommands.Self.PrintError("Error stopping song:\n" + ex);
}
}

internal void PlaySong()
Expand Down

0 comments on commit abb5e83

Please sign in to comment.