Skip to content

Commit

Permalink
Log a user-friendly message when Rakaly is blocked by an antivirus (#…
Browse files Browse the repository at this point in the history
…1902) #patch
  • Loading branch information
IhateTrains authored Apr 24, 2024
1 parent 8959144 commit bbf5a79
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions ImperatorToCK3/Helpers/RakalyCaller.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using commonItems;
using ImperatorToCK3.Exceptions;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -59,6 +60,13 @@ public static string GetJson(string filePath) {
return plainText;
}

private static bool IsFileFlaggedAsInfected(Win32Exception ex) {
// The error code name is ERROR_VIRUS_INFECTED:
// https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/18d8fbe8-a967-4f1c-ae50-99ca8e491d2d
const int nativeErrorCode = 0x000000E1;
return ex.NativeErrorCode == nativeErrorCode;
}

public static void MeltSave(string savePath) {
string arguments = $"melt --unknown-key stringify \"{savePath}\"";

Expand All @@ -68,8 +76,21 @@ public static void MeltSave(string savePath) {
process.StartInfo.Arguments = arguments;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
process.WaitForExit();

try {
process.Start();
process.WaitForExit();
}
catch (Win32Exception e) when (IsFileFlaggedAsInfected(e)) {
Logger.Debug("Message: " + e.Message);
Logger.Debug("HResult: " + e.HResult);
Logger.Debug("NativeErrorCode: " + e.NativeErrorCode);

string absoluteRakalyPath = Path.Combine(Directory.GetCurrentDirectory(), RelativeRakalyPath);
throw new UserErrorException($"Failed to run Rakaly because the antivirus blocked it.\n" +
$"Add an exclusion for \"{absoluteRakalyPath}\" to the antivirus and try again.");
}

int returnCode = process.ExitCode;
if (returnCode != 0 && returnCode != 1) {
Logger.Debug($"Save path: {savePath}");
Expand Down

0 comments on commit bbf5a79

Please sign in to comment.