Skip to content

Commit

Permalink
Merge pull request #2676 from ivan-mogilko/362--batchrebasesourcefile
Browse files Browse the repository at this point in the history
Editor: implement "Replace source paths" operation for sprites and audio clips
  • Loading branch information
ivan-mogilko authored Feb 10, 2025
2 parents b6e0c6c + 301ed30 commit 0c13c5a
Show file tree
Hide file tree
Showing 7 changed files with 567 additions and 6 deletions.
9 changes: 9 additions & 0 deletions Editor/AGS.Editor/AGSEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@
<Compile Include="GUI\Progress.Designer.cs">
<DependentUpon>Progress.cs</DependentUpon>
</Compile>
<Compile Include="GUI\ReplaceFolderDialog.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="GUI\ReplaceFolderDialog.Designer.cs">
<DependentUpon>ReplaceFolderDialog.cs</DependentUpon>
</Compile>
<Compile Include="GUI\SplashScreen.cs">
<SubType>Form</SubType>
</Compile>
Expand Down Expand Up @@ -472,6 +478,9 @@
<EmbeddedResource Include="GUI\Progress.resx">
<DependentUpon>Progress.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="GUI\ReplaceFolderDialog.resx">
<DependentUpon>ReplaceFolderDialog.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="GUI\SpriteExportDialog.resx">
<DependentUpon>SpriteExportDialog.cs</DependentUpon>
</EmbeddedResource>
Expand Down
59 changes: 57 additions & 2 deletions 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 @@ -908,11 +961,13 @@ protected override ProjectTreeItem CreateTreeItemForItem(AudioClip item)
protected override void AddNewItemCommandsToFolderContextMenu(string controlID, IList<MenuCommand> menu)
{
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));
}

protected override void AddExtraCommandsToFolderContextMenu(string controlID, IList<MenuCommand> menu)
{
menu.Add(MenuCommand.Separator);
menu.Add(new MenuCommand(COMMAND_REPLACE_AUDIO_SOURCE_FOLDER, "Replace source paths for audio clips...", null));
menu.Add(new MenuCommand(COMMAND_REIMPORT_ALL, "Force reimport all file(s)", null));
menu.Add(MenuCommand.Separator);
menu.Add(new MenuCommand(COMMAND_PROPERTIES, "Properties", null));
}
Expand Down
165 changes: 165 additions & 0 deletions Editor/AGS.Editor/GUI/ReplaceFolderDialog.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 91 additions & 0 deletions Editor/AGS.Editor/GUI/ReplaceFolderDialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AGS.Editor
{
public partial class ReplaceFolderDialog : Form
{
private string _makeRelativeToThisDir;

public ReplaceFolderDialog()
{
InitializeComponent();
}

private ReplaceFolderDialog(string titleBar, string headerText, string oldPath, string newPath, string makeRelativeToThisDir = null)
{
InitializeComponent();
Text = titleBar;
lblOperationDescription.Text = headerText;
_makeRelativeToThisDir = makeRelativeToThisDir;
OldPath = oldPath;
NewPath = newPath;
}

public string OldPath
{
get { return tbOldPath.Text; }
set
{
string path = value;
if (!string.IsNullOrEmpty(_makeRelativeToThisDir))
path = Utilities.GetRelativeToBasePath(path, _makeRelativeToThisDir);
tbOldPath.Text = path;
}
}

public string NewPath
{
get { return tbNewPath.Text; }
set
{
string path = value;
if (!string.IsNullOrEmpty(_makeRelativeToThisDir))
path = Utilities.GetRelativeToBasePath(path, _makeRelativeToThisDir);
tbNewPath.Text = path;
}
}

public static Tuple<string, string> Show(string titleBar, string headerText, string oldPath, string newPath, string makeRelativeToThisDir = null)
{
ReplaceFolderDialog dialog = new ReplaceFolderDialog(titleBar, headerText, oldPath, newPath, makeRelativeToThisDir);
Tuple<string, string> result = null;
if (dialog.ShowDialog() == DialogResult.OK)
{
result = new Tuple<string, string>(dialog.OldPath, dialog.NewPath);
}
dialog.Dispose();
return result;
}

private void btnOK_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
Close();
}

private void btnCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}

private void btnBrowseOldPath_Click(object sender, EventArgs e)
{
OldPath = Factory.GUIController.ShowSelectFolderOrDefaultDialog("Select folder", tbOldPath.Text);
}

private void btnBrowseNewPath_Click(object sender, EventArgs e)
{
NewPath = Factory.GUIController.ShowSelectFolderOrDefaultDialog("Select folder", tbNewPath.Text);
}
}
}
Loading

0 comments on commit 0c13c5a

Please sign in to comment.