Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of relative paths #447

Merged
merged 1 commit into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions Confuser.Core/ConfuserEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,17 @@ static void OptimizeMethods(ConfuserContext context) {

static void EndModule(ConfuserContext context) {
string output = context.Modules[context.CurrentModuleIndex].Location;
if (output != null) {
if (!(output is null)) {
if (!Path.IsPathRooted(output))
output = Path.Combine(Environment.CurrentDirectory, output);
output = Utils.GetRelativePath(output, context.BaseDirectory);
output = Path.Combine(context.BaseDirectory, output);
string relativeOutput = Utils.GetRelativePath(output, context.BaseDirectory);
if (relativeOutput is null) {
context.Logger.WarnFormat("Input file is not inside the base directory. Relative path can't be created. Placing file into output root." +
Environment.NewLine + "Responsible file is: {0}", output);
output = Path.GetFileName(output);
} else {
output = relativeOutput;
}
}
else {
output = context.CurrentModule.Name;
Expand Down
34 changes: 24 additions & 10 deletions Confuser.Core/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,33 @@ public static void AddListEntry<TKey, TValue>(this IDictionary<TKey, List<TValue
/// <summary>
/// Obtains the relative path from the specified base path.
/// </summary>
/// <param name="filespec">The file path.</param>
/// <param name="folder">The base path.</param>
/// <param name="fileSpec">The file path.</param>
/// <param name="baseDirectory">The base path.</param>
/// <returns>The path of <paramref name="filespec" /> relative to <paramref name="folder" />.</returns>
public static string GetRelativePath(string filespec, string folder) {
//http://stackoverflow.com/a/703292/462805
public static string GetRelativePath(string fileSpec, string baseDirectory) {
if (fileSpec is null) throw new ArgumentNullException(nameof(fileSpec));
if (baseDirectory is null) throw new ArgumentNullException(nameof(fileSpec));

var pathUri = new Uri(filespec);
// Folders must end in a slash
if (!folder.EndsWith(Path.DirectorySeparatorChar.ToString())) {
folder += Path.DirectorySeparatorChar;
return GetRelativePath(new FileInfo(fileSpec), new DirectoryInfo(baseDirectory));
}

public static string GetRelativePath(FileInfo fileSpec, DirectoryInfo baseDirectory) {
if (fileSpec is null) throw new ArgumentNullException(nameof(fileSpec));
if (baseDirectory is null) throw new ArgumentNullException(nameof(fileSpec));

if (baseDirectory.FullName.EndsWith(Path.DirectorySeparatorChar.ToString())) {
baseDirectory = new DirectoryInfo(baseDirectory.FullName.TrimEnd(Path.DirectorySeparatorChar));
}
var folderUri = new Uri(folder);
return Uri.UnescapeDataString(folderUri.MakeRelativeUri(pathUri).ToString().Replace('/', Path.DirectorySeparatorChar));

var relativePath = fileSpec.Name;
var currentDirectory = fileSpec.Directory;
while (!(currentDirectory is null) && !string.Equals(currentDirectory.FullName, baseDirectory.FullName, StringComparison.OrdinalIgnoreCase)) {
relativePath = currentDirectory.Name + Path.DirectorySeparatorChar + relativePath;
currentDirectory = currentDirectory.Parent;
}

if (currentDirectory is null) return null; //file is not inside the base directory
return relativePath;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion ConfuserEx/ViewModel/UI/ProjectTabVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void AddModule(string file) {
}
var module = new ProjectModuleVM(App.Project, new ProjectModule());
try {
module.Path = Confuser.Core.Utils.GetRelativePath(file, App.Project.BaseDirectory);
module.Path = Confuser.Core.Utils.GetRelativePath(file, App.Project.BaseDirectory) ?? file;
}
catch {
module.Path = file;
Expand Down
27 changes: 27 additions & 0 deletions Tests/Confuser.Core.Test/UtilsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Collections.Generic;
using Xunit;

namespace Confuser.Core.Test {
public class UtilsTest {
[Theory]
[MemberData(nameof(BuildRelativePathTestData))]
[Trait("Issue", "https://github.com/mkaring/ConfuserEx/issues/413")]
public void BuildRelativePath(string baseDirectory, string fileReference, string expectedRelativePath) =>
Assert.Equal(expectedRelativePath, Utils.GetRelativePath(fileReference, baseDirectory), ignoreCase: true);

public static IEnumerable<object[]> BuildRelativePathTestData() {
yield return new object[] { "C:\\Test", "C:\\Test\\Asm.dll", "Asm.dll" };
yield return new object[] { "C:\\Test\\", "C:\\Test\\Asm.dll", "Asm.dll" };
yield return new object[] { "C:\\Test", "C:\\Test\\Test2\\Asm.dll", "Test2\\Asm.dll" };
yield return new object[] { "C:\\Test\\", "C:\\Test\\Test2\\Asm.dll", "Test2\\Asm.dll" };
yield return new object[] { "C:\\Test", "C:\\Test\\Test2\\Test3\\Asm.dll", "Test2\\Test3\\Asm.dll" };
yield return new object[] { "C:\\Test\\", "C:\\Test\\Test2\\Test3\\Asm.dll", "Test2\\Test3\\Asm.dll" };
yield return new object[] { "C:\\Test", "C:\\Test2\\Asm.dll", null };
yield return new object[] { "C:\\Test\\", "C:\\Test2\\Asm.dll", null };

// Only for case insensitive file systems (windows)
yield return new object[] { "C:\\Test", "c:\\test\\test2\\test3\\asm.dll", "Test2\\Test3\\Asm.dll" };
yield return new object[] { "C:\\Test", "C:\\TEST\\TEST2\\TEST3\\ASM.DLL", "Test2\\Test3\\Asm.dll" };
}
}
}