diff --git a/AquaMai.Build/PostBuildPatch.cs b/AquaMai.Build/PostBuildPatch.cs index f2bb2a0a..e74305c1 100644 --- a/AquaMai.Build/PostBuildPatch.cs +++ b/AquaMai.Build/PostBuildPatch.cs @@ -33,7 +33,7 @@ public class PostBuildPatch : Task { 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)) diff --git a/AquaMai.ErrorReport/AquaMai.ErrorReport.csproj b/AquaMai.ErrorReport/AquaMai.ErrorReport.csproj index 59812db0..b2947ce4 100644 --- a/AquaMai.ErrorReport/AquaMai.ErrorReport.csproj +++ b/AquaMai.ErrorReport/AquaMai.ErrorReport.csproj @@ -1,7 +1,7 @@  - Library + WinExe net472 enable true diff --git a/AquaMai.ErrorReport/CrashForm.cs b/AquaMai.ErrorReport/CrashForm.cs index 1a5da775..95232c55 100644 --- a/AquaMai.ErrorReport/CrashForm.cs +++ b/AquaMai.ErrorReport/CrashForm.cs @@ -40,15 +40,6 @@ public partial class CrashForm : Form 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); @@ -69,6 +60,15 @@ public partial class CrashForm : Form 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"); diff --git a/AquaMai.ErrorReport/Main.cs b/AquaMai.ErrorReport/Program.cs similarity index 61% rename from AquaMai.ErrorReport/Main.cs rename to AquaMai.ErrorReport/Program.cs index 3519746f..b8f0e618 100644 --- a/AquaMai.ErrorReport/Main.cs +++ b/AquaMai.ErrorReport/Program.cs @@ -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)] @@ -18,18 +17,12 @@ public class Main 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(); - } } \ No newline at end of file diff --git a/AquaMai.Mods/Utils/ShowErrorLog.cs b/AquaMai.Mods/Utils/ShowErrorLog.cs index f3a0c4e2..2f359248 100644 --- a/AquaMai.Mods/Utils/ShowErrorLog.cs +++ b/AquaMai.Mods/Utils/ShowErrorLog.cs @@ -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; @@ -35,6 +36,7 @@ public class ShowErrorLog _errorUi = new GameObject("ErrorUI").AddComponent(); _errorUi.gameObject.SetActive(true); } + string logFile = $"{MAI2System.Path.ErrorLogPath}{DateTime.Now:yyyyMMddHHmmss}.log"; MelonLogger.Msg("Error Log:"); if (File.Exists(logFile)) @@ -66,21 +68,39 @@ public class ShowErrorLog 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; @@ -92,14 +112,14 @@ public class ShowErrorLog { 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; @@ -115,7 +135,7 @@ public class ShowErrorLog GUI.Label(new Rect(x + GuiSizes.PlayerWidth, y, width, height), _errorLog, labelStyle); } } - + public IEnumerator Show() { while (true) @@ -124,4 +144,4 @@ public class ShowErrorLog } } } -} +} \ No newline at end of file diff --git a/AquaMai/AquaMai.csproj b/AquaMai/AquaMai.csproj index f8cea312..fc84cea5 100644 --- a/AquaMai/AquaMai.csproj +++ b/AquaMai/AquaMai.csproj @@ -4,7 +4,7 @@ Release AnyCPU {788BC472-59F7-46F6-B760-65C18BA74389} - WinExe + Library AquaMai AquaMai net472 @@ -48,12 +48,12 @@ - + - + @@ -69,8 +69,8 @@ AquaMai.Mods.dll - - AquaMai.ErrorReport.dll + + AquaMai.ErrorReport.exe diff --git a/AquaMai/Program.cs b/AquaMai/Program.cs deleted file mode 100644 index 0fba6787..00000000 --- a/AquaMai/Program.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; -using System.Reflection; - -namespace AquaMai; - -public class Program -{ - [STAThread] - public static void Main(string[] args) - { - try - { - if (args.Length == 0) - { - return; - } - - switch (args[0]) - { - case "ErrorReport": - Console.WriteLine("Starting ErrorReport..."); - var erAssembly = AssemblyLoader.LoadAssemblyFromResource("AquaMai.ErrorReport.dll"); - erAssembly.GetType("AquaMai.ErrorReport.Main") - .GetMethod("Start", BindingFlags.Public | BindingFlags.Static) - .Invoke(null, []); - break; - } - } - catch (Exception e) - { - Console.WriteLine(e.GetType()); - Console.WriteLine(e.Message); - Console.WriteLine(e); - throw; - } - } -} \ No newline at end of file diff --git a/build.cake b/build.cake index 7159d677..2382a5fb 100644 --- a/build.cake +++ b/build.cake @@ -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);