App.xaml.cs 18 KB

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