Skip to content

Commit

Permalink
Unblock Internet files on startup (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
SadPencil authored Sep 9, 2024
1 parent d4957bd commit 5a4f6f4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
10 changes: 10 additions & 0 deletions NativeConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace CnCNet.LauncherStub;

internal static class NativeConstants
{
/// ERROR_FILE_NOT_FOUND -> 2L
public const int ERROR_FILE_NOT_FOUND = 2;

/// ERROR_ACCESS_DENIED -> 5L
public const int ERROR_ACCESS_DENIED = 5;
}
13 changes: 13 additions & 0 deletions NativeMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace CnCNet.LauncherStub;

using System.Runtime.InteropServices;

internal static class NativeMethods
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
#if NET45_OR_GREATER || NETSTANDARD || NET
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
#endif
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteFile(string name);
}
47 changes: 47 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Win32;

internal sealed class Program
Expand Down Expand Up @@ -87,6 +88,21 @@ private static void RequireDotNetFramework()
[STAThread]
private static void Main(string[] args)
{
try
{
RemoveZoneIdentifer(CurrentDirectory);
}
catch (Exception ex)
{
bool ignoreUnblocking = AdvancedMessageBoxHelper.ShowYesNoMessageBox(
"An error occured when the launcher tried to unblock files. Re-running the launcher with administrator privileges might help.\n\n" + ex.ToString(),
"Client Launcher Warning",
yesText: "Continue", noText: "Exit");

if (!ignoreUnblocking)
Environment.Exit(1);
}

try
{
foreach (string arg in args)
Expand Down Expand Up @@ -163,6 +179,37 @@ private static void RunDialogTest()
msgbox.ShowDialog();
}

private static void RemoveZoneIdentifer(string directory)
{
// https://stackoverflow.com/a/6375373

List<string> failedMessages = [];

// Enumerate all files recursively
string[] files = Directory.GetFiles(directory, "*", SearchOption.AllDirectories);
string[] directories = Directory.GetDirectories(directory, "*", SearchOption.AllDirectories);

// For each file or directory, remove the Zone.Identifier alternate data stream
foreach (string file in files.Concat(directories))
{
string zoneIdentifier = file + ":Zone.Identifier";
bool success = NativeMethods.DeleteFile(zoneIdentifier);
if (!success)
{
int error = Marshal.GetLastWin32Error();
if (error == NativeConstants.ERROR_FILE_NOT_FOUND)
continue;

string errorMessage = new Win32Exception(error).Message;

failedMessages.Add($"{file}: {errorMessage}");
}
}

if (failedMessages.Count > 0)
throw new Exception("Failed to remove Zone.Identifier from the following files:\n" + string.Join("\n", failedMessages));
}

private static void RunXNA()
{
RequireXna();
Expand Down

0 comments on commit 5a4f6f4

Please sign in to comment.