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
6 changes: 6 additions & 0 deletions JiayiLauncher/JiayiLauncher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,10 @@
</Compile>
</ItemGroup>

<ItemGroup>
<None Update="upload.bat">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
72 changes: 49 additions & 23 deletions JiayiLauncher/Platforms/Windows/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using System.Diagnostics;
using JiayiLauncher.Utils;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using Blazored.Toast;
using Blazored.Toast.Services;
using JiayiLauncher.Shared.Components.Toasts;
using JiayiLauncher.Utils;
using Microsoft.AspNetCore.Components;
using static JiayiLauncher.Utils.Imports;

namespace JiayiLauncher.Platforms.Windows;
Expand All @@ -18,15 +14,15 @@ public partial class App : MauiWinUIApplication
public App()
{
InitializeComponent();

// the first singletons
var log = Singletons.Add<Log>();
var arguments = Singletons.Add<Arguments>();

// log current version
var version = Assembly.GetExecutingAssembly().GetName().Version ?? new Version(0, 0, 0);
log.Write("JiayiLauncher", $"Running version {version.Major}.{version.Minor}.{version.Build}");

_mutex = new Mutex(true, "Global\\JiayiLauncher", out var createdNew);

if (createdNew) // only one instance of the launcher
Expand All @@ -41,53 +37,83 @@ public App()
"at Microsoft.AspNetCore.Components.WebView.WebView2.WebView2WebViewManager.SendMessage(String message)",
"System.Net.Sockets.SocketException (995): The I/O operation has been aborted because of either a thread exit or an application request."
};
AppDomain.CurrentDomain.FirstChanceException += (_, ex) =>

AppDomain.CurrentDomain.FirstChanceException += async (_, ex) =>
{
var exception = ex.Exception.ToString();
if (suppressed.Any(exception.Contains)) return;

log.Write(ex.Exception, exception, Log.LogLevel.Error);

var windowHandle = FindWindowW(null, "Jiayi Launcher");
var result = MessageBoxW(
windowHandle,
windowHandle,
"""
Jiayi has ran into an issue and needs to close. Please send your log file to the nearest developer.

Would you like to open the log folder? Your log file is saved as 'Current.log'.
""",
"Crash",
Would you like to anonymously upload your current log file?
""",
"Crash",
0x00000004 | 0x00000010);

if (result == 6) // yes
{
string current_log = Path.Join(Log.LogPath, "Current.log");
if (File.Exists(current_log))
{
string upload_bat = Path.Join(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "upload.bat");

ProcessStartInfo psi = new ProcessStartInfo
{
FileName = upload_bat,
WindowStyle = ProcessWindowStyle.Minimized,
CreateNoWindow = true,
UseShellExecute = true
};
Process.Start(psi);
}
}

result = MessageBoxW(
windowHandle,
"""
Would you like to open the log folder?

Your log file is saved as 'Current.log'.
""",
"Crash",
0x00000004 | 0x00000010);

if (result == 6) // yes
{
Process.Start("explorer.exe", Log.LogPath);
}




Environment.Exit(1);
};

return;
}

// send arguments to the running instance
var ptr = Marshal.StringToHGlobalUni(arguments.Get());

var hWnd = FindWindowW(null, "Jiayi Launcher");
if (hWnd != nint.Zero)
{
CopyData cds;
cds.dwData = 1;
cds.cbData = (uint)((arguments.Get().Length + 1) * 2);
cds.lpData = ptr;

var pCds = Marshal.AllocHGlobal(Marshal.SizeOf<CopyData>());
Marshal.StructureToPtr(cds, pCds, false);

SendMessage(hWnd, 0x004A, 0, pCds);
}

Marshal.FreeHGlobal(ptr);
Environment.Exit(0);
}
Expand Down
32 changes: 32 additions & 0 deletions JiayiLauncher/upload.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@echo off
setlocal

:: Check if file path is provided
if "%~1"=="" (
echo Usage: upload_log.bat "C:\path\to\Current.log"
exit /b 1
)

:: Store the file path
set FILE_PATH=%~1

:: Check if file exists
if not exist "%FILE_PATH%" (
echo Error: File not found - %FILE_PATH%
exit /b 1
)

:: API endpoint
set API_URL=https://jiayi-api.vercel.app/api/v1/webhook/error-from-file

:: Perform the file upload using curl
curl -X POST "%API_URL%" -F "file=@%FILE_PATH%" -H "Content-Type: multipart/form-data"

:: Check if the command was successful
if %ERRORLEVEL% NEQ 0 (
echo Upload failed.
exit /b %ERRORLEVEL%
)

echo Upload successful!
exit /b 0