Skip to content
Open
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
9 changes: 9 additions & 0 deletions WitcherScriptMerger/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace WitcherScriptMerger
{
using System.IO;

static class Extensions
{
#region Strings
Expand Down Expand Up @@ -56,6 +58,13 @@ public static string GetPluralS(this int num)

#endregion

#region FileInfo
public static string ResolveTargetFileFullName(this FileInfo fileInfo)
{
return fileInfo.Exists ? SimLink.GetSymbolicLinkTarget(fileInfo) : fileInfo.FullName;
}
#endregion

#region Tree & Context Menu

public static IEnumerable<ToolStripItem> GetAvailableItems(this ContextMenuStrip menu)
Expand Down
61 changes: 61 additions & 0 deletions WitcherScriptMerger/SimLink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32.SafeHandles;

namespace WitcherScriptMerger
{
/// <summary>
/// Provides access to NTFS junction points in .Net.
/// </summary>
public static class SimLink
{
private const int CREATION_DISPOSITION_OPEN_EXISTING = 3;

private const int FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;

// http://msdn.microsoft.com/en-us/library/aa364962%28VS.85%29.aspx
[DllImport("kernel32.dll", EntryPoint = "GetFinalPathNameByHandleW", CharSet = CharSet.Unicode,
SetLastError = true)]
public static extern int GetFinalPathNameByHandle(IntPtr handle,
[In, Out] StringBuilder path,
int bufLen,
int flags);

// http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx
[DllImport("kernel32.dll", EntryPoint = "CreateFileW", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern SafeFileHandle CreateFile(string lpFileName,
int dwDesiredAccess,
int dwShareMode,
IntPtr SecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
IntPtr hTemplateFile);

public static string GetSymbolicLinkTarget(FileInfo symlink)
{
var fileHandle = CreateFile(symlink.FullName, 0, 2, IntPtr.Zero, CREATION_DISPOSITION_OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero);
if (fileHandle.IsInvalid)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}

var path = new StringBuilder(512);
var size = GetFinalPathNameByHandle(fileHandle.DangerousGetHandle(), path, path.Capacity, 0);
if (size < 0)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
// The remarks section of GetFinalPathNameByHandle mentions the return being prefixed with "\\?\"
// More information about "\\?\" here -> http://msdn.microsoft.com/en-us/library/aa365247(v=VS.85).aspx
if (path[0] == '\\' && path[1] == '\\' && path[2] == '?' && path[3] == '\\')
{
return path.ToString().Substring(4);
}
return path.ToString();
}
}
}
8 changes: 6 additions & 2 deletions WitcherScriptMerger/Tools/KDiff3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ public static int Run(
? "\"" + vanillaFile.FullName + "\" "
: "");

// resolve any simlinked files
var source1FullName = source1.TextFile.ResolveTargetFileFullName();
var source2FullName = source2.TextFile.ResolveTargetFileFullName();

args +=
$"\"{source1.TextFile.FullName}\" \"{source2.TextFile.FullName}\" " +
$"\"{source1FullName}\" \"{source2FullName}\" " +
$"-o \"{outputPath}\" " +
"--cs \"WhiteSpace3FileMergeDefault=2\" " +
"--cs \"CreateBakFiles=0\" " +
Expand All @@ -51,7 +55,7 @@ public static int Run(

if (!Program.Settings.Get<bool>("ReviewEachMerge") && hasVanillaVersion)
{
if (source1.TextFile.FullName.EqualsIgnoreCase(outputPath)
if (source1FullName.EqualsIgnoreCase(outputPath)
&& source2.Hash != null && source2.Hash.IsOutdated)
{
Program.MainForm.ShowMessage(
Expand Down
1 change: 1 addition & 0 deletions WitcherScriptMerger/WitcherScriptMerger.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<Compile Include="Forms\OptionsForm.Designer.cs">
<DependentUpon>OptionsForm.cs</DependentUpon>
</Compile>
<Compile Include="SimLink.cs" />
<Compile Include="Tools\xxHash.cs" />
<Compile Include="FileIndex\ModFile.cs" />
<Compile Include="FileIndex\ModFileCategory.cs" />
Expand Down