Skip to content

Commit

Permalink
Editor: implement "Replace source paths" operation for audio clips
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-mogilko committed Feb 8, 2025
1 parent 49175a5 commit 034494a
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion Editor/AGS.Editor/Components/AudioComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Linq;
using AGS.Types;
using System.Windows.Forms;

namespace AGS.Editor.Components
{
Expand All @@ -13,6 +14,7 @@ class AudioComponent : BaseComponentWithFolders<AudioClip, AudioClipFolder>, IPr
private const string COMPILED_AUDIO_FILENAME_PREFIX = "au";
private const string COMMAND_ADD_AUDIO = "AddAudioClipCmd";
private const string COMMAND_REIMPORT_ALL = "ReimportAllAudioClipCmd";
private const string COMMAND_REPLACE_AUDIO_SOURCE_FOLDER = "ReplaceAudioClipSourceFolderCmd";
private const string COMMAND_PROPERTIES = "PropertiesAudioClip";
private const string COMMAND_RENAME = "RenameAudioClip";
private const string COMMAND_REIMPORT = "ReimportAudioClip";
Expand Down Expand Up @@ -114,6 +116,10 @@ protected override void ItemCommandClick(string controlID)
{
CommandForceReimportOfAllAudioClips();
}
else if (controlID == COMMAND_REPLACE_AUDIO_SOURCE_FOLDER)
{
CommandReplaceSourceFolderForAudioClips();
}
else if (controlID == COMMAND_RENAME)
{
_guiController.ProjectTree.BeginLabelEdit(this, _rightClickedID);
Expand Down Expand Up @@ -508,7 +514,7 @@ private void UpdateScoreSound(IList<AudioClip> allAudio)

private void UpdateViewFrameSounds(IList<AudioClip> allAudio, ViewFolder views)
{
foreach (View view in views.Views)
foreach (AGS.Types.View view in views.Views)
{
foreach (ViewLoop loop in view.Loops)
{
Expand Down Expand Up @@ -702,6 +708,53 @@ public void ReplaceAudioClipSource(AudioClip clip)
}
}

private void CommandReplaceSourceFolderForAudioClips()
{
var allAudioClips = _agsEditor.CurrentGame.RootAudioClipFolder.AllItemsFlat;
string firstFoundSourceFile = null;
AudioClip foundClip = allAudioClips.Where(s => !string.IsNullOrEmpty(s.SourceFileName)).FirstOrDefault();
if (foundClip != null)
firstFoundSourceFile = foundClip.SourceFileName;

if (string.IsNullOrEmpty(firstFoundSourceFile))
{
Factory.GUIController.ShowMessage("None of the audio clips has a source filename.", MessageBoxIcon.Warning);
return;
}

string parentDir = Path.GetDirectoryName(firstFoundSourceFile);
var replaceDirs = ReplaceFolderDialog.Show("Replace audio clip(s) source path",
"Please choose which part of the parent path should be replaced and provide a replacement. Relative paths will be assumed relative to your game's project folder.",
parentDir, parentDir, Factory.AGSEditor.CurrentGame.DirectoryPath);

if (replaceDirs == null || replaceDirs.Item1 == replaceDirs.Item2)
return;

int itemCount = 0;
foreach (var clip in allAudioClips)
{
if (string.IsNullOrEmpty(clip.SourceFileName))
continue;

string newPath;
if (Utilities.ReplacePathBaseProjectRelative(clip.SourceFileName, replaceDirs.Item1, replaceDirs.Item2, out newPath))
{
clip.SourceFileName = newPath;
itemCount++;
}
}

if (itemCount > 0)
{
Factory.GUIController.ShowMessage($"{itemCount} audio clip(s) had their source paths updated.", MessageBoxIcon.Information);
Factory.GUIController.RefreshPropertyGrid();
}
else
{
Factory.GUIController.ShowMessage($"No audio clips with the matching old paths found, no changes were made.", MessageBoxIcon.Information);
}
}

private void AddAudioClipToListIfFileNeedsToBeCopiedFromSource(AudioClip clip, PreCompileGameEventArgs evArgs, List<AudioClip> filesToCopy, List<string> fileNamesToUpdate)
{
string compiledFileName = clip.CacheFileName;
Expand Down Expand Up @@ -909,6 +962,7 @@ protected override void AddNewItemCommandsToFolderContextMenu(string controlID,
{
menu.Add(new MenuCommand(COMMAND_ADD_AUDIO, "Add audio file(s)...", null));
menu.Add(new MenuCommand(COMMAND_REIMPORT_ALL, "Force reimport all file(s)", null));
menu.Add(new MenuCommand(COMMAND_REPLACE_AUDIO_SOURCE_FOLDER, "Replace source paths for audio clips...", null));
}

protected override void AddExtraCommandsToFolderContextMenu(string controlID, IList<MenuCommand> menu)
Expand Down

0 comments on commit 034494a

Please sign in to comment.