Skip to content

Commit

Permalink
[Editor] Added the ability to copy imported assets automatically to t…
Browse files Browse the repository at this point in the history
…he Resources dir (stride3d#1827)
  • Loading branch information
SeleDreams authored Sep 22, 2023
1 parent 2596083 commit 5a4ebfe
Showing 1 changed file with 79 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
using Stride.Core.Presentation.Services;
using Stride.Core.Presentation.ViewModel;
using Stride.Core.Translation;
using MessageBoxButton = Stride.Core.Presentation.Services.MessageBoxButton;
using MessageBoxImage = Stride.Core.Presentation.Services.MessageBoxImage;

namespace Stride.Core.Assets.Editor.ViewModel
{
Expand Down Expand Up @@ -228,7 +226,7 @@ public AssetCollectionViewModel([NotNull] IViewModelServiceProvider serviceProvi
AddAssetFilterCommand = new AnonymousCommand<AssetFilterViewModel>(serviceProvider, AddAssetFilter);
ClearAssetFiltersCommand = new AnonymousCommand(serviceProvider, ClearAssetFilters);
RefreshAssetFilterCommand = new AnonymousCommand<AssetFilterViewModel>(serviceProvider, RefreshAssetFilter);
currentAssetFilters.CollectionChanged += (s,e) => RefreshFilters();
currentAssetFilters.CollectionChanged += (s, e) => RefreshFilters();
SelectedLocations.CollectionChanged += SelectedLocationCollectionChanged;
filteredAssets.CollectionChanged += FilteredAssetsCollectionChanged;
selectedContent.CollectionChanged += SelectedContentCollectionChanged;
Expand Down Expand Up @@ -473,7 +471,7 @@ public override void Destroy()
base.Destroy();
}

public async Task<List<AssetViewModel>> RunAssetTemplate(ITemplateDescriptionViewModel template, IEnumerable<UFile> files, PropertyContainer? customParameters = null)
public async Task<List<AssetViewModel>> RunAssetTemplate(ITemplateDescriptionViewModel template, [CanBeNull] IList<UFile> files, PropertyContainer? customParameters = null)
{
if (template == null)
return new List<AssetViewModel>();
Expand Down Expand Up @@ -537,31 +535,93 @@ private string ComputeNamespace(DirectoryBaseViewModel directory)
}
}

private async Task<List<AssetViewModel>> InvokeAddAssetTemplate(LoggerResult logger, string name, DirectoryBaseViewModel directory, TemplateAssetDescription templateDescription, IEnumerable<UFile> files, PropertyContainer? customParameters)
private async Task<string> GetAssetCopyDirectory(DirectoryBaseViewModel directory, UFile file)
{
var path = directory.Path;
var message = Tr._p("Message", "Do you want to place the resource in the default location ?");
var finalPath = Path.Combine(directory.Package.Package.ResourceFolders[0], path, file.GetFileName());
var pathResult = await Dialogs.MessageBox(message, MessageBoxButton.YesNo, MessageBoxImage.Question);
if (pathResult == MessageBoxResult.No)
{
while(true)
{
var fileDialog = Dialogs.CreateFileSaveModalDialog();
fileDialog.Filters = new List<FileDialogFilter>() { new FileDialogFilter("", file.GetFileExtension()) };
fileDialog.InitialDirectory = Path.GetFullPath(directory.Package.Package.ResourceFolders[0].FullPath);
fileDialog.DefaultFileName = file.GetFileName();
DialogResult result = await fileDialog.ShowModal();

// If the user closes the dialog, assume that they want to use the default directory
if (result != DialogResult.Ok)
{
return finalPath;
}

var fullPath = Path.GetFullPath(fileDialog.FilePath);

bool inResource = directory.Package.Package.ResourceFolders.Any(x => fullPath.StartsWith(Path.GetFullPath(x.FullPath)));
if (inResource)
{
return fullPath;
}

message = Tr._p("Message", "The selected directory is not a subdirectory of the resources folder!");
await Dialogs.MessageBox(message, MessageBoxButton.OK, MessageBoxImage.Error);
}
}
return finalPath;
}

private async Task<List<AssetViewModel>> InvokeAddAssetTemplate(LoggerResult logger, string name, DirectoryBaseViewModel directory, TemplateAssetDescription templateDescription, [CanBeNull] IList<UFile> files, PropertyContainer? customParameters)
{
List<AssetViewModel> newAssets = new List<AssetViewModel>();
if (files is not null)
{
foreach (var file in files)
for (int i = 0; i < files.Count; i++)
{
bool inResourceFolder = false;
foreach (var resourceFolder in directory.Package.Package.ResourceFolders)
var file = files[i];
bool inResourceFolder = directory.Package.Package.ResourceFolders.Any(x => file.FullPath.StartsWith(x.FullPath));

if (inResourceFolder)
continue;

var message = Tr._p("Message", "Source file '{0}' is not inside of your project's resource folders, do you want to copy it?").ToFormat(file.FullPath);

var copyResult = await Dialogs.MessageBox(message, MessageBoxButton.YesNo, MessageBoxImage.Warning);

if (copyResult != MessageBoxResult.Yes)
continue;

string finalPath = await GetAssetCopyDirectory(directory, file);

try
{
if (file.FullPath.StartsWith(resourceFolder.FullPath))
Directory.CreateDirectory(Path.GetDirectoryName(finalPath));
if (File.Exists(finalPath))
{
inResourceFolder = true;
break;
message = Tr._p("Message", "The file '{0}' already exists, it will get overwritten if you continue, do you really want to proceed?").ToFormat(file.FullPath);

copyResult = await Dialogs.MessageBox(message, MessageBoxButton.YesNo, MessageBoxImage.Warning);

// Abort if the user says no or closes the prompt
if (copyResult != MessageBoxResult.Yes)
{
return newAssets;
}
File.Copy(file.FullPath, finalPath, true);
}
else
{
File.Copy(file.FullPath, finalPath);
}
}

if (inResourceFolder == false)
files[i] = new UFile(finalPath);
}
catch (Exception ex)
{
var message = Tr._p("Message", "Source file '{0}' is not inside any of your project's resource folders, import anyway ?");
message = string.Format(message, file.FullPath);

var result = await Dialogs.MessageBox(message, MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (result == MessageBoxResult.No)
return newAssets;
message = Tr._p("Message", $"An error occurred while copying the asset to the resources folder : {ex.Message}");
await Dialogs.MessageBox(message, MessageBoxButton.OK, MessageBoxImage.Error);
return newAssets;
}
}
}
Expand Down

0 comments on commit 5a4ebfe

Please sign in to comment.