Files
2025-08-01 21:10:51 +02:00

182 lines
6.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Web;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
namespace Valheim_Mod_Sync_Desktop
{
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
DispatcherUnhandledException += App_DispatcherUnhandledException;
}
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
// Erstellen Sie den Crash-Bericht.
var crashReport = CreateCrashReport(e.Exception);
// Zeigen Sie eine Benutzeroberfläche an, um den Benutzer aufzufordern, den Bericht zu senden.
var result = ShowCrashReportDialog(crashReport);
if (result == true)
{
// Senden Sie den Bericht an die URL.
SendCrashReport(crashReport, "https://vms.tbs-server.de/crashreport.php");
}
e.Handled = true;
}
private void SendCrashReport(string crashReport, string url)
{
try
{
using (WebClient client = new WebClient())
{
NameValueCollection data = new NameValueCollection();
data["crashReport"] = crashReport;
data["Lizenz"] = Valheim_Mod_Sync_Desktop.Properties.Settings.Default.Lizenz;
byte[] responseBytes = client.UploadValues(url, "POST", data);
NameValueCollection response = HttpUtility.ParseQueryString(Encoding.UTF8.GetString(responseBytes));
if (response.ToString() == "true")
{
// Zeigen Sie eine Benachrichtigung an, dass der Crash-Bericht erfolgreich gesendet wurde.
MessageBox.Show((string)Application.Current.FindResource("CrashGesendet"));
}
else
{
// Zeigen Sie eine Benachrichtigung an, dass beim Senden des Crash-Berichts ein Fehler aufgetreten ist.
MessageBox.Show((string)Application.Current.FindResource("CrashFehler"));
}
}
}
catch (WebException ex)
{
// Zeigen Sie eine Benachrichtigung an, dass beim Senden des Crash-Berichts ein Fehler aufgetreten ist.
MessageBox.Show("Error sending crash report: " + ex.Message);
}
}
private string CreateCrashReport(Exception exception)
{
var report = new StringBuilder();
report.AppendLine("Crash report:");
report.AppendLine();
report.AppendLine("Lizenz: " + Valheim_Mod_Sync_Desktop.Properties.Settings.Default.Lizenz);
report.AppendLine();
report.AppendLine("Exception type: " + exception.GetType().FullName);
report.AppendLine("Exception message: " + exception.Message);
report.AppendLine();
report.AppendLine("Stack trace:");
report.AppendLine(exception.StackTrace);
if (exception.InnerException != null)
{
report.AppendLine();
report.AppendLine("Inner exception type: " + exception.InnerException.GetType().FullName);
report.AppendLine("Inner exception message: " + exception.InnerException.Message);
report.AppendLine();
report.AppendLine("Inner stack trace:");
report.AppendLine(exception.InnerException.StackTrace);
}
string localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string configPath = Path.Combine(localAppDataPath, "tbS-server");
File.WriteAllText(configPath + "/CrashReport.txt", report.ToString());
return report.ToString();
}
private bool ShowCrashReportDialog(string crashReport)
{
var window = new Crash();
return window.ShowDialog() == true;
}
private void Application_Startup(object sender, StartupEventArgs e)
{
if (e.Args.Length == 1 && e.Args[0] == "--config-reset")
{
if (File.Exists(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath))
{
File.Delete(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath);
}
MessageBox.Show("Config deleted, please re-open VMS and Configure");
this.Shutdown();
} else if (e.Args.Length == 1 && e.Args[0] == "--crashreport-send")
{
// Senden des Letzen CrashReports
string localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string configPath = Path.Combine(localAppDataPath, "tbS-server");
string crashReport = File.ReadAllText(configPath + "/CrashReport.txt");
SendCrashReport(crashReport, "https://vms.tbs-server.de/crashreport.php");
this.Shutdown();
} else {
MainWindow wnd = new MainWindow();
//wnd.Show();
}
}
public ResourceDictionary ThemeDictionary
{
get { return Resources.MergedDictionaries[0]; }
}
public void ChangeTheme(Uri uri)
{
ThemeDictionary.MergedDictionaries.Clear();
ThemeDictionary.MergedDictionaries.Add(new ResourceDictionary() { Source = uri });
}
public ResourceDictionary LanguageDictionary
{
get { return Resources.MergedDictionaries[1]; }
}
public void ChangeLanguage(Uri uri)
{
LanguageDictionary.MergedDictionaries.Clear();
LanguageDictionary.MergedDictionaries.Add(new ResourceDictionary() { Source = uri });
}
}
}