Main.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Diagnostics;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Security;
  8. using System.Reflection;
  9. using System.Runtime.InteropServices;
  10. using System.Security.Cryptography.X509Certificates;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using MediaBrowser.Common.Configuration;
  14. using MediaBrowser.Common.Implementations.IO;
  15. using MediaBrowser.Common.Implementations.Logging;
  16. using MediaBrowser.Model.Logging;
  17. using MediaBrowser.Server.Implementations;
  18. using MediaBrowser.Server.Startup.Common;
  19. using MediaBrowser.Server.Startup.Common.Browser;
  20. using Microsoft.Win32;
  21. using MonoMac.AppKit;
  22. using MonoMac.Foundation;
  23. using MonoMac.ObjCRuntime;
  24. namespace MediaBrowser.Server.Mac
  25. {
  26. class MainClass
  27. {
  28. private static ApplicationHost _appHost;
  29. private static ILogger _logger;
  30. static void Main (string[] args)
  31. {
  32. var applicationPath = Assembly.GetEntryAssembly().Location;
  33. var options = new StartupOptions();
  34. // Allow this to be specified on the command line.
  35. var customProgramDataPath = options.GetOption("-programdata");
  36. var appPaths = CreateApplicationPaths(applicationPath, customProgramDataPath);
  37. var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
  38. logManager.ReloadLogger(LogSeverity.Info);
  39. logManager.AddConsoleOutput();
  40. var logger = _logger = logManager.GetLogger("Main");
  41. ApplicationHost.LogEnvironmentInfo(logger, appPaths, true);
  42. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  43. StartApplication(appPaths, logManager, options);
  44. NSApplication.Init ();
  45. NSApplication.Main (args);
  46. }
  47. public static void AddDependencies(MenuBarIcon appController){
  48. appController.AppHost = _appHost;
  49. appController.Logger = _logger;
  50. appController.ConfigurationManager = _appHost.ServerConfigurationManager;
  51. appController.Localization = _appHost.LocalizationManager;
  52. }
  53. private static ServerApplicationPaths CreateApplicationPaths(string applicationPath, string programDataPath)
  54. {
  55. if (string.IsNullOrEmpty(programDataPath))
  56. {
  57. // TODO: Use CommonApplicationData? Will we always have write access?
  58. programDataPath = Path.Combine(Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "mediabrowser-server");
  59. }
  60. // Within the mac bundle, go uo two levels then down into Resources folder
  61. var resourcesPath = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName (applicationPath)), "Resources");
  62. return new ServerApplicationPaths(programDataPath, applicationPath, resourcesPath);
  63. }
  64. /// <summary>
  65. /// Runs the application.
  66. /// </summary>
  67. /// <param name="appPaths">The app paths.</param>
  68. /// <param name="logManager">The log manager.</param>
  69. /// <param name="options">The options.</param>
  70. private static void StartApplication(ServerApplicationPaths appPaths,
  71. ILogManager logManager,
  72. StartupOptions options)
  73. {
  74. SystemEvents.SessionEnding += SystemEvents_SessionEnding;
  75. // Allow all https requests
  76. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
  77. var fileSystem = new CommonFileSystem(logManager.GetLogger("FileSystem"), false, true);
  78. var nativeApp = new NativeApp();
  79. _appHost = new ApplicationHost(appPaths, logManager, options, fileSystem, "MBServer.Mono", false, nativeApp);
  80. if (options.ContainsOption("-v")) {
  81. Console.WriteLine (_appHost.ApplicationVersion.ToString());
  82. return;
  83. }
  84. Console.WriteLine ("appHost.Init");
  85. Task.Run (() => StartServer(CancellationToken.None));
  86. }
  87. private static async void StartServer(CancellationToken cancellationToken)
  88. {
  89. var initProgress = new Progress<double>();
  90. await _appHost.Init (initProgress).ConfigureAwait (false);
  91. //await _appHost.RunStartupTasks ().ConfigureAwait (false);
  92. }
  93. /// <summary>
  94. /// Handles the SessionEnding event of the SystemEvents control.
  95. /// </summary>
  96. /// <param name="sender">The source of the event.</param>
  97. /// <param name="e">The <see cref="SessionEndingEventArgs"/> instance containing the event data.</param>
  98. static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
  99. {
  100. if (e.Reason == SessionEndReasons.SystemShutdown)
  101. {
  102. Shutdown();
  103. }
  104. }
  105. public static void Shutdown()
  106. {
  107. ShutdownApp();
  108. }
  109. private static void ShutdownApp()
  110. {
  111. _logger.Info ("Calling ApplicationHost.Dispose");
  112. _appHost.Dispose ();
  113. _logger.Info("AppController.Terminate");
  114. MenuBarIcon.Instance.Terminate ();
  115. }
  116. /// <summary>
  117. /// Handles the UnhandledException event of the CurrentDomain control.
  118. /// </summary>
  119. /// <param name="sender">The source of the event.</param>
  120. /// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
  121. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  122. {
  123. var exception = (Exception)e.ExceptionObject;
  124. new UnhandledExceptionWriter(_appHost.ServerConfigurationManager.ApplicationPaths, _logger, _appHost.LogManager).Log(exception);
  125. if (!Debugger.IsAttached)
  126. {
  127. Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
  128. }
  129. }
  130. }
  131. class NoCheckCertificatePolicy : ICertificatePolicy
  132. {
  133. public bool CheckValidationResult (ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
  134. {
  135. return true;
  136. }
  137. }
  138. }