App.xaml.cs 18 KB

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