using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using Comal.Classes;
using InABox.Configuration;
using InABox.Core;
using InABox.Logging;
using InABox.Wpf;
using InABox.WPF;
using InABox.WPF.Themes;
using NDesk.Options;
using PRSDesktop;
using Syncfusion.Licensing;
using Path = System.IO.Path;
namespace PRSDesktop
{
///
/// Interaction logic for App.xaml
///
public partial class App : Application
{
public static DatabaseSettings DatabaseSettings;
public static AutoUpdateSettings AutoUpdateSettings;
public static Guid EmployeeID = Guid.Empty;
public static String EmployeeCode = "";
public static String EmployeeName = "";
public static String EmployeeEmail = "";
public static string Profile { get; set; } = "";
public static bool IsClosing { get; set; } = false;
public static bool ShouldRestart { get; set; } = false;
/*/ Guid to ensure that only one instance of PRS is running
public static Guid AppGuid = Guid.Parse("237E8828-7F5A-4298-B311-CF0FC27882EC");
private static Mutex AppMutex;*/
private readonly OptionSet _commandLineParameters = new()
{
{
"profile=",
"",
p => { Profile = p; }
}
};
public App()
{
SyncfusionLicenseProvider.RegisterLicense(CoreUtils.SyncfusionLicense(SyncfusionVersion.v25_2));
}
private void AutoDiscover(Dictionary allsettings)
{
var confirm = MessageWindow.ShowYesNo("Try to configure Server Connection Automatically?", "Auto Discover");
if (confirm)
try
{
using (new WaitCursor())
{
AutoDiscoverySettings autodiscover;
using (var client = new UdpClient())
{
client.Client.SendTimeout = 10000;
client.Client.ReceiveTimeout = 20000;
var requestData = Encoding.ASCII.GetBytes("");
var serverEndPoint = new IPEndPoint(IPAddress.Any, 0);
client.EnableBroadcast = true;
client.Send(requestData, requestData.Length, new IPEndPoint(IPAddress.Broadcast, 8888));
var serverResponseData = client.Receive(ref serverEndPoint);
var serverResponse = Encoding.ASCII.GetString(serverResponseData);
autodiscover = Serialization.Deserialize(serverResponse);
client.Close();
}
var settings = new DatabaseSettings();
settings.IsActive = true;
settings.Logo = autodiscover.Logo;
settings.Protocol = autodiscover.Protocol;
settings.DatabaseType = DatabaseType.Networked;
settings.URLs = autodiscover.URLs;
settings.LibraryLocation = autodiscover.LibraryLocation;
settings.GoogleAPIKey = autodiscover.GoogleAPIKey;
allsettings[autodiscover.Name] = settings;
new LocalConfiguration(autodiscover.Name).Save(settings);
AutoUpdateSettings = new AutoUpdateSettings
{
Channel = autodiscover.UpdateChannel,
Type = autodiscover.UpdateType,
Location = autodiscover.UpdateLocation,
Elevated = autodiscover.UpdateAdmin
};
new LocalConfiguration().Save(AutoUpdateSettings);
MessageWindow.ShowMessage($"Server found at {String.Join(";",autodiscover.URLs)}", "Success");
}
}
catch
{
MessageWindow.ShowMessage("No Server Found", "Not found", image: MessageWindow.WarningImage);
}
}
private void MoveDirectory(string[] source, string target)
{
var stack = new Stack();
stack.Push(new Folders(source[0], target));
while (stack.Count > 0)
{
var folders = stack.Pop();
Directory.CreateDirectory(folders.Target);
foreach (var file in Directory.GetFiles(folders.Source, "*.*"))
{
var targetFile = Path.Combine(folders.Target, Path.GetFileName(file));
if (!File.Exists(targetFile))
File.Move(file, targetFile);
else
File.Delete(file);
}
foreach (var folder in Directory.GetDirectories(folders.Source))
stack.Push(new Folders(folder, Path.Combine(folders.Target, Path.GetFileName(folder))));
}
Directory.Delete(source[0], true);
}
public static void SaveDatabaseSettings()
{
new LocalConfiguration(Profile).Save(DatabaseSettings);
}
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
/*AppMutex = new Mutex(false, $"Global\\{AppGuid}");
// Don't open if PRS is already running.
if(!AppMutex.WaitOne(0, false))
{
Shutdown(0);
SendToProcesses(Message.Maximise);
return;
}*/
AutoUpdateSettings = new LocalConfiguration().Load();
var oldPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Comal.Desktop");
if (Directory.Exists(oldPath))
MoveDirectory(new[] { oldPath }, CoreUtils.GetPath());
ShutdownMode = ShutdownMode.OnExplicitShutdown;
SetupExceptionHandling();
MainLogger.AddLogger(new LogFileLogger(CoreUtils.GetPath()));
Logger.OnLog += MainLogger.Send;
if (e.Args.Any())
{
DatabaseSettings = new DatabaseSettings();
var extras = _commandLineParameters.Parse(e.Args);
if (extras.Any())
{
var sw = new StringWriter();
sw.WriteLine("Unknown Parameter(s) found:");
foreach (var extra in extras)
sw.WriteLine(" " + extra);
sw.WriteLine();
sw.WriteLine("The following parameters are valid:");
_commandLineParameters.WriteOptionDescriptions(sw);
MessageWindow.ShowMessage(sw.ToString(), "PRS Desktop Startup Options");
}
}
bool bSaveSettings = false;
var allsettings = new LocalConfiguration().LoadAll();
// Try AutoDiscovery
if (!allsettings.Any())
AutoDiscover(allsettings);
// Create Default Database Entry
if (!allsettings.Any())
{
var settings = new DatabaseSettings();
settings.UserID = "GUEST";
settings.Password = "guest";
settings.Autologin = false;
allsettings["Default"] = settings;
bSaveSettings = true;
}
// Convert Blank Key to "Default"
if (allsettings.ContainsKey(""))
{
var defaultSettings = allsettings[""];
allsettings.Remove("");
defaultSettings.IsActive = true;
allsettings["Default"] = defaultSettings;
bSaveSettings = true;
}
// Check and Convert from URL + Port => URLs
foreach (var key in allsettings.Keys)
{
var settings = allsettings[key];
var oldurl = CoreUtils.GetPropertyValue(settings, "URL") as String;
var oldport = CoreUtils.GetPropertyValue(settings, "Port");
if (!String.IsNullOrWhiteSpace(oldurl))
{
bSaveSettings = true;
settings.URLs = new String[] { $"{oldurl}:{oldport}" };
CoreUtils.SetPropertyValue(settings, "URL", "");
CoreUtils.SetPropertyValue(settings, "Port", 0);
}
if (settings.URLs != null)
{
var urls = new List();
foreach (var url in settings.URLs)
{
var comps = url.Split(new[] { "://" }, StringSplitOptions.RemoveEmptyEntries);
if (comps.Length > 1)
{
bSaveSettings = true;
urls.Add(comps.Last());
}
else
urls.Add(url);
}
settings.URLs = urls.ToArray();
}
}
if (bSaveSettings)
new LocalConfiguration().SaveAll(allsettings);
DatabaseSettings = null;
if (!string.IsNullOrWhiteSpace(Profile))
{
if (allsettings.ContainsKey(Profile))
DatabaseSettings = allsettings[Profile];
else
MessageWindow.ShowMessage($"Profile {Profile} does not exist!", "Error");
}
if (DatabaseSettings == null)
{
var keys = allsettings.Keys.Where(x => allsettings[x].IsActive).ToArray();
if (keys.Length > 1)
{
var form = new SelectDatabase(allsettings);
if (form.ShowDialog() == true)
{
Profile = form.Database;
DatabaseSettings = allsettings[form.Database];
}
else
{
Shutdown(1);
return;
}
form = null;
}
else
{
Profile = keys.First();
DatabaseSettings = allsettings[Profile];
}
}
StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);
}
private static IEnumerable GetRestartArguments()
{
// Skip the first one, because that is the location.
return Environment.GetCommandLineArgs().Skip(1);
}
protected override void OnExit(ExitEventArgs e)
{
base.OnExit(e);
if (ShouldRestart)
{
Process.Start(Path.ChangeExtension(ResourceAssembly.Location, ".exe"), GetRestartArguments());
}
//AppMutex.ReleaseMutex();
}
private void SetupExceptionHandling()
{
AppDomain.CurrentDomain.UnhandledException += (s, e) =>
LogUnhandledException((Exception)e.ExceptionObject, "AppDomain.CurrentDomain.UnhandledException");
DispatcherUnhandledException += (s, e) =>
{
LogUnhandledException(e.Exception, "Application.Current.DispatcherUnhandledException");
e.Handled = true;
};
}
private class UnhandledException
{
private string _message;
public string Message
{
get => _message;
set
{
_message = value;
Hash = GenerateExceptionHash(value);
}
}
public DateTime OriginalTime { get; set; }
public DateTime LastTime { get; set; }
public DateTime LastPrinted { get; set; }
public int Occurences { get; set; }
public int Hash { get; private set; }
}
private static TimeSpan RepeatedExceptionThreshold = TimeSpan.FromSeconds(10);
private static Queue PreviousUnhandledExceptions { get; set; } = new Queue();
private static int GenerateExceptionHash(string message)
{
return message.GetHashCode();
}
private static void ClearUnhandledExceptions()
{
while (PreviousUnhandledExceptions.TryPeek(out var e) && DateTime.Now - e.LastTime > RepeatedExceptionThreshold)
{
PreviousUnhandledExceptions.Dequeue();
}
}
private void LogUnhandledException(Exception exception, string source)
{
ClearUnhandledExceptions();
var messages = new List();
var e2 = exception;
while (e2 != null)
{
messages.InsertRange(0, new[] { e2.Message, e2.StackTrace, "============================================" });
e2 = e2.InnerException;
}
var unhandled = new UnhandledException
{
Message = string.Join("\n", messages),
OriginalTime = DateTime.Now,
Occurences = 1
};
unhandled.LastTime = unhandled.OriginalTime;
unhandled.LastPrinted = unhandled.OriginalTime;
UnhandledException? found = null;
foreach(var e in PreviousUnhandledExceptions)
{
if(e.Hash == unhandled.Hash)
{
e.LastTime = unhandled.LastTime;
e.Occurences++;
found = e;
break;
}
}
if (found is null)
{
PreviousUnhandledExceptions.Enqueue(unhandled);
MainLogger.Send(LogType.Error, "", string.Join("\n", messages), Guid.Empty);
}
else
{
if((DateTime.Now - found.LastPrinted).TotalSeconds >= 1)
{
MainLogger.Send(LogType.Error, "", $"Recurrent Error occurred {found.Occurences} times; See {found.OriginalTime}", Guid.Empty);
found.LastPrinted = DateTime.Now;
}
}
// if (exception.Message.StartsWith("Dispatcher processing has been suspended"))
// try
// {
// SendKeys.Send("{ESC}");
// }
// catch (Exception e)
// {
// }
}
private class Folders
{
public Folders(string source, string target)
{
Source = source;
Target = target;
}
public string Source { get; }
public string Target { get; }
}
/*
public enum Message
{
Maximise = 0x2A76
}
private void SendToProcesses(Message message)
{
var process = Process.GetCurrentProcess();
var processes = Process.GetProcessesByName(process.ProcessName);
if(processes.Length > 1)
{
foreach(var p in processes)
{
if(p.Id != process.Id)
{
SendMessage(p.MainWindowHandle, (uint)message, IntPtr.Zero, IntPtr.Zero);
}
}
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SendMessage(IntPtr hwnd, uint Msg, IntPtr wParam, IntPtr lParam);
*/
}
}