App.xaml.cs 18 KB

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