Skip to content

Commit 09401a2

Browse files
authored
Merge a0f98af into a12be9e
2 parents a12be9e + a0f98af commit 09401a2

File tree

4 files changed

+62
-14
lines changed

4 files changed

+62
-14
lines changed

Confuser.Core/ConfuserEngine.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,17 @@ static void OptimizeMethods(ConfuserContext context) {
379379

380380
static void EndModule(ConfuserContext context) {
381381
string output = context.Modules[context.CurrentModuleIndex].Location;
382-
if (output != null) {
382+
if (!(output is null)) {
383383
if (!Path.IsPathRooted(output))
384-
output = Path.Combine(Environment.CurrentDirectory, output);
385-
output = Utils.GetRelativePath(output, context.BaseDirectory);
384+
output = Path.Combine(context.BaseDirectory, output);
385+
string relativeOutput = Utils.GetRelativePath(output, context.BaseDirectory);
386+
if (relativeOutput is null) {
387+
context.Logger.WarnFormat("Input file is not inside the base directory. Relative path can't be created. Placing file into output root." +
388+
Environment.NewLine + "Responsible file is: {0}", output);
389+
output = Path.GetFileName(output);
390+
} else {
391+
output = relativeOutput;
392+
}
386393
}
387394
else {
388395
output = context.CurrentModule.Name;

Confuser.Core/Utils.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,33 @@ public static void AddListEntry<TKey, TValue>(this IDictionary<TKey, List<TValue
7171
/// <summary>
7272
/// Obtains the relative path from the specified base path.
7373
/// </summary>
74-
/// <param name="filespec">The file path.</param>
75-
/// <param name="folder">The base path.</param>
74+
/// <param name="fileSpec">The file path.</param>
75+
/// <param name="baseDirectory">The base path.</param>
7676
/// <returns>The path of <paramref name="filespec" /> relative to <paramref name="folder" />.</returns>
77-
public static string GetRelativePath(string filespec, string folder) {
78-
//http://stackoverflow.com/a/703292/462805
77+
public static string GetRelativePath(string fileSpec, string baseDirectory) {
78+
if (fileSpec is null) throw new ArgumentNullException(nameof(fileSpec));
79+
if (baseDirectory is null) throw new ArgumentNullException(nameof(fileSpec));
7980

80-
var pathUri = new Uri(filespec);
81-
// Folders must end in a slash
82-
if (!folder.EndsWith(Path.DirectorySeparatorChar.ToString())) {
83-
folder += Path.DirectorySeparatorChar;
81+
return GetRelativePath(new FileInfo(fileSpec), new DirectoryInfo(baseDirectory));
82+
}
83+
84+
public static string GetRelativePath(FileInfo fileSpec, DirectoryInfo baseDirectory) {
85+
if (fileSpec is null) throw new ArgumentNullException(nameof(fileSpec));
86+
if (baseDirectory is null) throw new ArgumentNullException(nameof(fileSpec));
87+
88+
if (baseDirectory.FullName.EndsWith(Path.DirectorySeparatorChar.ToString())) {
89+
baseDirectory = new DirectoryInfo(baseDirectory.FullName.TrimEnd(Path.DirectorySeparatorChar));
8490
}
85-
var folderUri = new Uri(folder);
86-
return Uri.UnescapeDataString(folderUri.MakeRelativeUri(pathUri).ToString().Replace('/', Path.DirectorySeparatorChar));
91+
92+
var relativePath = fileSpec.Name;
93+
var currentDirectory = fileSpec.Directory;
94+
while (!(currentDirectory is null) && !string.Equals(currentDirectory.FullName, baseDirectory.FullName, StringComparison.OrdinalIgnoreCase)) {
95+
relativePath = currentDirectory.Name + Path.DirectorySeparatorChar + relativePath;
96+
currentDirectory = currentDirectory.Parent;
97+
}
98+
99+
if (currentDirectory is null) return null; //file is not inside the base directory
100+
return relativePath;
87101
}
88102

89103
/// <summary>

ConfuserEx/ViewModel/UI/ProjectTabVM.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void AddModule(string file) {
117117
}
118118
var module = new ProjectModuleVM(App.Project, new ProjectModule());
119119
try {
120-
module.Path = Confuser.Core.Utils.GetRelativePath(file, App.Project.BaseDirectory);
120+
module.Path = Confuser.Core.Utils.GetRelativePath(file, App.Project.BaseDirectory) ?? file;
121121
}
122122
catch {
123123
module.Path = file;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Collections.Generic;
2+
using Xunit;
3+
4+
namespace Confuser.Core.Test {
5+
public class UtilsTest {
6+
[Theory]
7+
[MemberData(nameof(BuildRelativePathTestData))]
8+
[Trait("Issue", "https://github.com/mkaring/ConfuserEx/issues/413")]
9+
public void BuildRelativePath(string baseDirectory, string fileReference, string expectedRelativePath) =>
10+
Assert.Equal(expectedRelativePath, Utils.GetRelativePath(fileReference, baseDirectory), ignoreCase: true);
11+
12+
public static IEnumerable<object[]> BuildRelativePathTestData() {
13+
yield return new object[] { "C:\\Test", "C:\\Test\\Asm.dll", "Asm.dll" };
14+
yield return new object[] { "C:\\Test\\", "C:\\Test\\Asm.dll", "Asm.dll" };
15+
yield return new object[] { "C:\\Test", "C:\\Test\\Test2\\Asm.dll", "Test2\\Asm.dll" };
16+
yield return new object[] { "C:\\Test\\", "C:\\Test\\Test2\\Asm.dll", "Test2\\Asm.dll" };
17+
yield return new object[] { "C:\\Test", "C:\\Test\\Test2\\Test3\\Asm.dll", "Test2\\Test3\\Asm.dll" };
18+
yield return new object[] { "C:\\Test\\", "C:\\Test\\Test2\\Test3\\Asm.dll", "Test2\\Test3\\Asm.dll" };
19+
yield return new object[] { "C:\\Test", "C:\\Test2\\Asm.dll", null };
20+
yield return new object[] { "C:\\Test\\", "C:\\Test2\\Asm.dll", null };
21+
22+
// Only for case insensitive file systems (windows)
23+
yield return new object[] { "C:\\Test", "c:\\test\\test2\\test3\\asm.dll", "Test2\\Test3\\Asm.dll" };
24+
yield return new object[] { "C:\\Test", "C:\\TEST\\TEST2\\TEST3\\ASM.DLL", "Test2\\Test3\\Asm.dll" };
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)