Skip to content

Commit

Permalink
[O] Prevent virus detect
Browse files Browse the repository at this point in the history
  • Loading branch information
clansty committed Feb 14, 2025
1 parent da692d2 commit d7a835c
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 76 deletions.
2 changes: 1 addition & 1 deletion AquaMai.Build/PostBuildPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private void CompressEmbeddedAssemblies(AssemblyDefinition assembly)
{
foreach (var resource in assembly.MainModule.Resources.ToList())
{
if (resource.Name.EndsWith(".dll") && resource is EmbeddedResource embeddedResource)
if ((resource.Name.EndsWith(".dll") || resource.Name.EndsWith(".exe")) && resource is EmbeddedResource embeddedResource)
{
using var compressedStream = new MemoryStream();
using (var deflateStream = new DeflateStream(compressedStream, CompressionLevel.Optimal))
Expand Down
2 changes: 1 addition & 1 deletion AquaMai.ErrorReport/AquaMai.ErrorReport.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<OutputType>WinExe</OutputType>
<TargetFramework>net472</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
Expand Down
18 changes: 9 additions & 9 deletions AquaMai.ErrorReport/CrashForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,6 @@ public CrashForm()

private async void CrashForm_Load(object sender, EventArgs e)
{
try
{
labelVersion.Text = "AquaMai v" + FileVersionInfo.GetVersionInfo(Application.ExecutablePath).ProductVersion;
}
catch
{
labelVersion.Text = "AquaMai (Version Unknown)";
}

labelStatus.Text = "正在生成错误报告... Gathering error log...";
var exePath = Path.GetDirectoryName(Application.ExecutablePath);
var gameDir = Path.GetDirectoryName(exePath);
Expand All @@ -69,6 +60,15 @@ private async void CrashForm_Load(object sender, EventArgs e)
Directory.CreateDirectory(errorLogPath);
}

try
{
labelVersion.Text = "AquaMai v" + FileVersionInfo.GetVersionInfo(Path.Combine(gameDir, "Mods", "AquaMai.dll")).ProductVersion;
}
catch
{
labelVersion.Text = "AquaMai (Version Unknown)";
}

try
{
var logFiles = Directory.GetFiles(errorLogPath, "*.log");
Expand Down
15 changes: 4 additions & 11 deletions AquaMai.ErrorReport/Main.cs → AquaMai.ErrorReport/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;

namespace AquaMai.ErrorReport;

public class Main
public class Program
{
[DllImport("SHCore.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
Expand All @@ -18,18 +17,12 @@ public enum DPI_AWARENESS
PER_MONITOR_AWARE = 2
}

public static void Start()
[STAThread]
public static void Main()
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
SetProcessDpiAwareness(DPI_AWARENESS.SYSTEM_AWARE);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new CrashForm());
}

// 这是魔法
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
return Assembly.GetExecutingAssembly();
}
}
34 changes: 27 additions & 7 deletions AquaMai.Mods/Utils/ShowErrorLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Text.RegularExpressions;
using System.Threading;
using AquaMai.Config.Attributes;
Expand Down Expand Up @@ -35,6 +36,7 @@ private static void ExceptionHandler(GameMain __instance, Exception e)
_errorUi = new GameObject("ErrorUI").AddComponent<Ui>();
_errorUi.gameObject.SetActive(true);
}

string logFile = $"{MAI2System.Path.ErrorLogPath}{DateTime.Now:yyyyMMddHHmmss}.log";
MelonLogger.Msg("Error Log:");
if (File.Exists(logFile))
Expand Down Expand Up @@ -66,21 +68,39 @@ private static void ApplicationOnQuitting()

public static void ApplicationOnQuittingNew()
{
var path = Path.Combine(Path.GetTempPath(), DateTime.Now.Ticks.ToString());
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write))
{
using var s = GetErrorReporterStream();
s.CopyTo(fs);
}

MelonLogger.Msg("Starting Crash Handler...");
var psi = new ProcessStartInfo
{
FileName = BuildInfo.ModAssembly.Location,
Arguments = "ErrorReport",
FileName = path,
WorkingDirectory = Path.GetDirectoryName(Application.dataPath),
UseShellExecute = false,
};
System.Diagnostics.Process.Start(psi);
}

private static Stream GetErrorReporterStream()
{
var s = BuildInfo.ModAssembly.Assembly.GetManifestResourceStream("AquaMai.ErrorReport.exe");
if (s != null)
{
return s;
}

s = BuildInfo.ModAssembly.Assembly.GetManifestResourceStream("AquaMai.ErrorReport.exe.compressed");
return new DeflateStream(s, CompressionMode.Decompress);
}

private class Ui : MonoBehaviour
{
private string _errorLog = "";

public void SetErrorLog(string text)
{
_errorLog = "Error Log:\n" + text;
Expand All @@ -92,14 +112,14 @@ public void OnGUI()
{
fontSize = GuiSizes.FontSize,
alignment = TextAnchor.MiddleLeft,
normal = new GUIStyleState(){textColor = Color.black}
normal = new GUIStyleState() { textColor = Color.black }
};

var boxStyle = new GUIStyle(GUI.skin.box)
{
normal = new GUIStyleState() { background = Texture2D.whiteTexture }
};

int logLineCount = Regex.Matches(_errorLog, "\n").Count + 1;
float offset = GuiSizes.PlayerCenter * 0.12f;
var x = GuiSizes.PlayerCenter / 2f + offset / 2f;
Expand All @@ -115,7 +135,7 @@ public void OnGUI()
GUI.Label(new Rect(x + GuiSizes.PlayerWidth, y, width, height), _errorLog, labelStyle);
}
}

public IEnumerator Show()
{
while (true)
Expand All @@ -124,4 +144,4 @@ public IEnumerator Show()
}
}
}
}
}
10 changes: 5 additions & 5 deletions AquaMai/AquaMai.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{788BC472-59F7-46F6-B760-65C18BA74389}</ProjectGuid>
<OutputType>WinExe</OutputType>
<OutputType>Library</OutputType>
<RootNamespace>AquaMai</RootNamespace>
<AssemblyName>AquaMai</AssemblyName>
<TargetFramework>net472</TargetFramework>
Expand Down Expand Up @@ -48,12 +48,12 @@

<UsingTask TaskName="PostBuildPatch" AssemblyFile="$(OutputPath)AquaMai.Build.dll" />
<Target Name="PostBuildPatch" AfterTargets="AfterBuild" Condition=" '$(Configuration)' == 'Release' ">
<PostBuildPatch DllPath="$(OutputPath)$(AssemblyName).exe" />
<PostBuildPatch DllPath="$(OutputPath)$(AssemblyName).dll" />
</Target>

<UsingTask TaskName="GenerateExampleConfig" AssemblyFile="$(OutputPath)AquaMai.Build.dll" />
<Target Name="GenerateExampleConfig" AfterTargets="AfterBuild">
<GenerateExampleConfig DllPath="$(OutputPath)$(AssemblyName).exe" OutputPath="$(OutputPath)" />
<GenerateExampleConfig DllPath="$(OutputPath)$(AssemblyName).dll" OutputPath="$(OutputPath)" />
</Target>

<ItemGroup>
Expand All @@ -69,8 +69,8 @@
<EmbeddedResource Include="$(OutputPath)AquaMai.Mods.dll">
<LogicalName>AquaMai.Mods.dll</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="$(OutputPath)AquaMai.ErrorReport.dll">
<LogicalName>AquaMai.ErrorReport.dll</LogicalName>
<EmbeddedResource Include="$(OutputPath)AquaMai.ErrorReport.exe">
<LogicalName>AquaMai.ErrorReport.exe</LogicalName>
</EmbeddedResource>
</ItemGroup>

Expand Down
37 changes: 0 additions & 37 deletions AquaMai/Program.cs

This file was deleted.

5 changes: 0 additions & 5 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,11 @@ Task("Build")
.IsDependentOn("Restore")
.Does(() =>
{
if (FileExists("Output/AquaMai.dll"))
{
DeleteFile("Output/AquaMai.dll");
}
// 使用 dotnet build 进行构建
DotNetBuild("./AquaMai.sln", new DotNetBuildSettings
{
Configuration = configuration
});
MoveFile("Output/AquaMai.exe", "Output/AquaMai.dll");
});

RunTarget(target);

0 comments on commit d7a835c

Please sign in to comment.