Skip to content

Commit

Permalink
fix: Adjust ToolLocator for all platforms (#2142) (#2250)
Browse files Browse the repository at this point in the history
* Adjust ToolLocator for all platforms

* Introduce ToOSPath

* Apply code review suggestion

* Add ToOSPath() for msdfgen executable
  • Loading branch information
Jklawreszuk authored May 22, 2024
1 parent 761107a commit aa4811e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
14 changes: 14 additions & 0 deletions sources/core/Stride.Core.Design/IO/UPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,25 @@ public override string ToString()
/// </summary>
/// <returns>A string representation of this path in windows form.</returns>
[NotNull]
[Obsolete("Use ToOSPath() instead")]
public string ToWindowsPath()
{
return FullPath.Replace('/', '\\');
}

/// <summary>
/// Converts this path to a OS path,
/// by replacing each separator with the current operating system
/// <see cref="Path.DirectorySeparatorChar"/>
/// </summary>
///<returns>A normalized string path</returns>

[NotNull]
public string ToOSPath()
{
return FullPath.Replace('/', Path.DirectorySeparatorChar);
}

/// <summary>
/// Implements the ==.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion sources/engine/Stride.Assets/Media/SoundAssetCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public DecodeSoundFileCommand(string url, SoundAsset parameters, IAssetFinder as
protected override async Task<ResultStatus> DoCommandOverride(ICommandContext commandContext)
{
// Get path to ffmpeg
var ffmpeg = ToolLocator.LocateTool("ffmpeg.exe")?.ToWindowsPath() ?? throw new AssetException("Failed to compile a sound asset, ffmpeg was not found.");
var ffmpeg = ToolLocator.LocateTool("ffmpeg")?.ToOSPath() ?? throw new AssetException("Failed to compile a sound asset, ffmpeg was not found.");

// Get absolute path of asset source on disk
var assetDirectory = Parameters.Source.GetParent();
Expand Down
2 changes: 1 addition & 1 deletion sources/engine/Stride.Assets/Media/VideoAssetCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected override async Task<ResultStatus> DoCommandOverride(ICommandContext co
try
{
// Get path to ffmpeg
var ffmpeg = ToolLocator.LocateTool("ffmpeg.exe")?.ToWindowsPath() ?? throw new AssetException("Failed to compile a video asset, ffmpeg was not found.");
var ffmpeg = ToolLocator.LocateTool("ffmpeg")?.ToOSPath() ?? throw new AssetException("Failed to compile a video asset, ffmpeg was not found.");

// Get absolute path of asset source on disk
var assetDirectory = videoAsset.Source.GetParent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public void Import(SpriteFontAsset options, List<char> characters)
return;

// Get the msdfgen.exe location
var msdfgen = ToolLocator.LocateTool("msdfgen.exe") ?? throw new AssetException("Failed to compile a font asset, msdfgen was not found.");
var msdfgen = ToolLocator.LocateTool("msdfgen").ToOSPath() ?? throw new AssetException("Failed to compile a font asset, msdfgen was not found.");

msdfgenExe = msdfgen.FullPath;
tempDir = $"{Environment.GetEnvironmentVariable("TEMP")}\\";
Expand Down
33 changes: 22 additions & 11 deletions sources/engine/Stride.Assets/ToolLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,32 @@
using System.Threading.Tasks;
using Stride.Core.IO;

namespace Stride.Assets
namespace Stride.Assets;

static class ToolLocator
{
static class ToolLocator
public static UFile LocateTool(string toolName)
{
public static UFile LocateTool(string toolName)
if(!OperatingSystem.IsWindows())
{
var tool = UPath.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), new UFile(toolName));
if (File.Exists(tool))
return tool;

tool = UPath.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), new UFile($"../../content/{toolName}"));
if (File.Exists(tool))
return tool;
var pathDirectories = Environment.GetEnvironmentVariable("PATH")?.Split(':') ?? Array.Empty<string>();

return null;
foreach (var directory in pathDirectories)
{
var toolLocation = Path.Combine(directory, toolName);
if (File.Exists(toolLocation))
return new UFile(toolLocation);
}
}

var tool = UPath.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), new UFile($"{toolName}.exe"));
if (File.Exists(tool))
return tool;

tool = UPath.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), new UFile($"../../content/{toolName}.exe"));
if (File.Exists(tool))
return tool;

return null;
}
}

0 comments on commit aa4811e

Please sign in to comment.