App.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.Text;
  9. using System.Windows;
  10. using Comal.Classes;
  11. using InABox.Configuration;
  12. using InABox.Core;
  13. using InABox.Logging;
  14. using InABox.Wpf;
  15. using InABox.WPF;
  16. using NDesk.Options;
  17. using Syncfusion.Licensing;
  18. using Path = System.IO.Path;
  19. namespace PRSDesktop
  20. {
  21. /// <summary>
  22. /// Interaction logic for App.xaml
  23. /// </summary>
  24. public partial class App : Application
  25. {
  26. public static DatabaseSettings DatabaseSettings;
  27. public static AutoUpdateSettings AutoUpdateSettings;
  28. public static Guid EmployeeID = Guid.Empty;
  29. public static String EmployeeCode = "";
  30. public static String EmployeeName = "";
  31. public static String EmployeeEmail = "";
  32. public static string Profile { get; set; } = "";
  33. public static bool IsClosing { get; set; } = false;
  34. public static bool ShouldRestart { get; set; } = false;
  35. /*/ Guid to ensure that only one instance of PRS is running
  36. public static Guid AppGuid = Guid.Parse("237E8828-7F5A-4298-B311-CF0FC27882EC");
  37. private static Mutex AppMutex;*/
  38. private readonly OptionSet _commandLineParameters = new()
  39. {
  40. {
  41. "profile=",
  42. "",
  43. p => { Profile = p; }
  44. }
  45. };
  46. public App()
  47. {
  48. SyncfusionLicenseProvider.RegisterLicense(CoreUtils.SyncfusionLicense(SyncfusionVersion.v25_2));
  49. }
  50. private void AutoDiscover(Dictionary<string, DatabaseSettings> allsettings)
  51. {
  52. var confirm = MessageWindow.ShowYesNo("Try to configure Server Connection Automatically?", "Auto Discover");
  53. if (confirm)
  54. try
  55. {
  56. using (new WaitCursor())
  57. {
  58. AutoDiscoverySettings autodiscover;
  59. using (var client = new UdpClient())
  60. {
  61. client.Client.SendTimeout = 10000;
  62. client.Client.ReceiveTimeout = 20000;
  63. var requestData = Encoding.ASCII.GetBytes("");
  64. var serverEndPoint = new IPEndPoint(IPAddress.Any, 0);
  65. client.EnableBroadcast = true;
  66. client.Send(requestData, requestData.Length, new IPEndPoint(IPAddress.Broadcast, 8888));
  67. var serverResponseData = client.Receive(ref serverEndPoint);
  68. var serverResponse = Encoding.ASCII.GetString(serverResponseData);
  69. autodiscover = Serialization.Deserialize<AutoDiscoverySettings>(serverResponse);
  70. client.Close();
  71. }
  72. var settings = new DatabaseSettings();
  73. settings.IsActive = true;
  74. settings.Logo = autodiscover.Logo;
  75. settings.Protocol = autodiscover.Protocol;
  76. settings.DatabaseType = DatabaseType.Networked;
  77. settings.URLs = autodiscover.URLs;
  78. settings.LibraryLocation = autodiscover.LibraryLocation;
  79. settings.GoogleAPIKey = autodiscover.GoogleAPIKey;
  80. allsettings[autodiscover.Name] = settings;
  81. new LocalConfiguration<DatabaseSettings>(autodiscover.Name).Save(settings);
  82. AutoUpdateSettings = new AutoUpdateSettings
  83. {
  84. Channel = autodiscover.UpdateChannel,
  85. Type = autodiscover.UpdateType,
  86. Location = autodiscover.UpdateLocation,
  87. Elevated = autodiscover.UpdateAdmin
  88. };
  89. new LocalConfiguration<AutoUpdateSettings>().Save(AutoUpdateSettings);
  90. MessageWindow.ShowMessage($"Server found at {String.Join(";",autodiscover.URLs)}", "Success");
  91. }
  92. }
  93. catch
  94. {
  95. MessageWindow.ShowMessage("No Server Found", "Not found", image: MessageWindow.WarningImage);
  96. }
  97. }
  98. private void MoveDirectory(string[] source, string target)
  99. {
  100. var stack = new Stack<Folders>();
  101. stack.Push(new Folders(source[0], target));
  102. while (stack.Count > 0)
  103. {
  104. var folders = stack.Pop();
  105. Directory.CreateDirectory(folders.Target);
  106. foreach (var file in Directory.GetFiles(folders.Source, "*.*"))
  107. {
  108. var targetFile = Path.Combine(folders.Target, Path.GetFileName(file));
  109. if (!File.Exists(targetFile))
  110. File.Move(file, targetFile);
  111. else
  112. File.Delete(file);
  113. }
  114. foreach (var folder in Directory.GetDirectories(folders.Source))
  115. stack.Push(new Folders(folder, Path.Combine(folders.Target, Path.GetFileName(folder))));
  116. }
  117. Directory.Delete(source[0], true);
  118. }
  119. public static void SaveDatabaseSettings()
  120. {
  121. new LocalConfiguration<DatabaseSettings>(Profile).Save(DatabaseSettings);
  122. }
  123. protected override void OnStartup(StartupEventArgs e)
  124. {
  125. base.OnStartup(e);
  126. /*AppMutex = new Mutex(false, $"Global\\{AppGuid}");
  127. // Don't open if PRS is already running.
  128. if(!AppMutex.WaitOne(0, false))
  129. {
  130. Shutdown(0);
  131. SendToProcesses(Message.Maximise);
  132. return;
  133. }*/
  134. AutoUpdateSettings = new LocalConfiguration<AutoUpdateSettings>().Load();
  135. var oldPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Comal.Desktop");
  136. if (Directory.Exists(oldPath))
  137. MoveDirectory(new[] { oldPath }, CoreUtils.GetPath());
  138. ShutdownMode = ShutdownMode.OnExplicitShutdown;
  139. SetupExceptionHandling();
  140. MainLogger.AddLogger(new LogFileLogger(CoreUtils.GetPath()));
  141. Logger.OnLog += MainLogger.Send;
  142. if (e.Args.Any())
  143. {
  144. DatabaseSettings = new DatabaseSettings();
  145. var extras = _commandLineParameters.Parse(e.Args);
  146. if (extras.Any())
  147. {
  148. var sw = new StringWriter();
  149. sw.WriteLine("Unknown Parameter(s) found:");
  150. foreach (var extra in extras)
  151. sw.WriteLine(" " + extra);
  152. sw.WriteLine();
  153. sw.WriteLine("The following parameters are valid:");
  154. _commandLineParameters.WriteOptionDescriptions(sw);
  155. MessageWindow.ShowMessage(sw.ToString(), "PRS Desktop Startup Options");
  156. }
  157. }
  158. bool bSaveSettings = false;
  159. var allsettings = new LocalConfiguration<DatabaseSettings>().LoadAll();
  160. // Try AutoDiscovery
  161. if (!allsettings.Any())
  162. AutoDiscover(allsettings);
  163. // Create Default Database Entry
  164. if (!allsettings.Any())
  165. {
  166. var settings = new DatabaseSettings();
  167. settings.UserID = "GUEST";
  168. settings.Password = "guest";
  169. settings.Autologin = false;
  170. allsettings["Default"] = settings;
  171. bSaveSettings = true;
  172. }
  173. // Convert Blank Key to "Default"
  174. if (allsettings.ContainsKey(""))
  175. {
  176. var defaultSettings = allsettings[""];
  177. allsettings.Remove("");
  178. defaultSettings.IsActive = true;
  179. allsettings["Default"] = defaultSettings;
  180. bSaveSettings = true;
  181. }
  182. // Check and Convert from URL + Port => URLs
  183. foreach (var key in allsettings.Keys)
  184. {
  185. var settings = allsettings[key];
  186. var oldurl = CoreUtils.GetPropertyValue(settings, "URL") as String;
  187. var oldport = CoreUtils.GetPropertyValue(settings, "Port");
  188. if (!String.IsNullOrWhiteSpace(oldurl))
  189. {
  190. bSaveSettings = true;
  191. settings.URLs = new String[] { $"{oldurl}:{oldport}" };
  192. CoreUtils.SetPropertyValue(settings, "URL", "");
  193. CoreUtils.SetPropertyValue(settings, "Port", 0);
  194. }
  195. if (settings.URLs != null)
  196. {
  197. var urls = new List<String>();
  198. foreach (var url in settings.URLs)
  199. {
  200. var comps = url.Split(new[] { "://" }, StringSplitOptions.RemoveEmptyEntries);
  201. if (comps.Length > 1)
  202. {
  203. bSaveSettings = true;
  204. urls.Add(comps.Last());
  205. }
  206. else
  207. urls.Add(url);
  208. }
  209. settings.URLs = urls.ToArray();
  210. }
  211. }
  212. if (bSaveSettings)
  213. new LocalConfiguration<DatabaseSettings>().SaveAll(allsettings);
  214. DatabaseSettings = null;
  215. if (!string.IsNullOrWhiteSpace(Profile))
  216. {
  217. if (allsettings.ContainsKey(Profile))
  218. DatabaseSettings = allsettings[Profile];
  219. else
  220. MessageWindow.ShowMessage($"Profile {Profile} does not exist!", "Error");
  221. }
  222. if (DatabaseSettings == null)
  223. {
  224. var keys = allsettings.Keys.Where(x => allsettings[x].IsActive).ToArray();
  225. if (keys.Length > 1)
  226. {
  227. var form = new SelectDatabase(allsettings);
  228. if (form.ShowDialog() == true)
  229. {
  230. Profile = form.Database;
  231. DatabaseSettings = allsettings[form.Database];
  232. }
  233. else
  234. {
  235. Shutdown(1);
  236. return;
  237. }
  238. form = null;
  239. }
  240. else
  241. {
  242. Profile = keys.First();
  243. DatabaseSettings = allsettings[Profile];
  244. }
  245. }
  246. StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);
  247. }
  248. private static IEnumerable<string> GetRestartArguments()
  249. {
  250. // Skip the first one, because that is the location.
  251. return Environment.GetCommandLineArgs().Skip(1);
  252. }
  253. protected override void OnExit(ExitEventArgs e)
  254. {
  255. base.OnExit(e);
  256. if (ShouldRestart)
  257. {
  258. Process.Start(Path.ChangeExtension(ResourceAssembly.Location, ".exe"), GetRestartArguments());
  259. }
  260. //AppMutex.ReleaseMutex();
  261. }
  262. private void SetupExceptionHandling()
  263. {
  264. AppDomain.CurrentDomain.UnhandledException += (s, e) =>
  265. LogUnhandledException((Exception)e.ExceptionObject, "AppDomain.CurrentDomain.UnhandledException");
  266. DispatcherUnhandledException += (s, e) =>
  267. {
  268. LogUnhandledException(e.Exception, "Application.Current.DispatcherUnhandledException");
  269. e.Handled = true;
  270. };
  271. }
  272. private class UnhandledException
  273. {
  274. private string _message;
  275. public string Message
  276. {
  277. get => _message;
  278. set
  279. {
  280. _message = value;
  281. Hash = GenerateExceptionHash(value);
  282. }
  283. }
  284. public DateTime OriginalTime { get; set; }
  285. public DateTime LastTime { get; set; }
  286. public DateTime LastPrinted { get; set; }
  287. public int Occurences { get; set; }
  288. public int Hash { get; private set; }
  289. }
  290. private static TimeSpan RepeatedExceptionThreshold = TimeSpan.FromSeconds(10);
  291. private static Queue<UnhandledException> PreviousUnhandledExceptions { get; set; } = new Queue<UnhandledException>();
  292. private static int GenerateExceptionHash(string message)
  293. {
  294. return message.GetHashCode();
  295. }
  296. private static void ClearUnhandledExceptions()
  297. {
  298. while (PreviousUnhandledExceptions.TryPeek(out var e) && DateTime.Now - e.LastTime > RepeatedExceptionThreshold)
  299. {
  300. PreviousUnhandledExceptions.Dequeue();
  301. }
  302. }
  303. private void LogUnhandledException(Exception exception, string source)
  304. {
  305. ClearUnhandledExceptions();
  306. var messages = new List<string>();
  307. var e2 = exception;
  308. while (e2 != null)
  309. {
  310. messages.InsertRange(0, new[] { e2.Message, e2.StackTrace, "============================================" });
  311. e2 = e2.InnerException;
  312. }
  313. var unhandled = new UnhandledException
  314. {
  315. Message = string.Join("\n", messages),
  316. OriginalTime = DateTime.Now,
  317. Occurences = 1
  318. };
  319. unhandled.LastTime = unhandled.OriginalTime;
  320. unhandled.LastPrinted = unhandled.OriginalTime;
  321. UnhandledException? found = null;
  322. foreach(var e in PreviousUnhandledExceptions)
  323. {
  324. if(e.Hash == unhandled.Hash)
  325. {
  326. e.LastTime = unhandled.LastTime;
  327. e.Occurences++;
  328. found = e;
  329. break;
  330. }
  331. }
  332. if (found is null)
  333. {
  334. PreviousUnhandledExceptions.Enqueue(unhandled);
  335. MainLogger.Send(LogType.Error, "", string.Join("\n", messages), Guid.Empty);
  336. }
  337. else
  338. {
  339. if((DateTime.Now - found.LastPrinted).TotalSeconds >= 1)
  340. {
  341. MainLogger.Send(LogType.Error, "", $"Recurrent Error occurred {found.Occurences} times; See {found.OriginalTime}", Guid.Empty);
  342. found.LastPrinted = DateTime.Now;
  343. }
  344. }
  345. // if (exception.Message.StartsWith("Dispatcher processing has been suspended"))
  346. // try
  347. // {
  348. // SendKeys.Send("{ESC}");
  349. // }
  350. // catch (Exception e)
  351. // {
  352. // }
  353. }
  354. private class Folders
  355. {
  356. public Folders(string source, string target)
  357. {
  358. Source = source;
  359. Target = target;
  360. }
  361. public string Source { get; }
  362. public string Target { get; }
  363. }
  364. /*
  365. public enum Message
  366. {
  367. Maximise = 0x2A76
  368. }
  369. private void SendToProcesses(Message message)
  370. {
  371. var process = Process.GetCurrentProcess();
  372. var processes = Process.GetProcessesByName(process.ProcessName);
  373. if(processes.Length > 1)
  374. {
  375. foreach(var p in processes)
  376. {
  377. if(p.Id != process.Id)
  378. {
  379. SendMessage(p.MainWindowHandle, (uint)message, IntPtr.Zero, IntPtr.Zero);
  380. }
  381. }
  382. }
  383. }
  384. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  385. private static extern IntPtr SendMessage(IntPtr hwnd, uint Msg, IntPtr wParam, IntPtr lParam);
  386. */
  387. }
  388. }