Main.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. internal 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. private static ServerApplicationPaths CreateApplicationPaths(string applicationPath, string programDataPath)
  48. {
  49. if (string.IsNullOrEmpty(programDataPath))
  50. {
  51. // TODO: Use CommonApplicationData? Will we always have write access?
  52. programDataPath = Path.Combine(Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "mediabrowser-server");
  53. }
  54. // Within the mac bundle, go uo two levels then down into Resources folder
  55. var resourcesPath = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName (applicationPath)), "Resources");
  56. return new ServerApplicationPaths(programDataPath, applicationPath, resourcesPath);
  57. }
  58. /// <summary>
  59. /// Runs the application.
  60. /// </summary>
  61. /// <param name="appPaths">The app paths.</param>
  62. /// <param name="logManager">The log manager.</param>
  63. /// <param name="options">The options.</param>
  64. private static void StartApplication(ServerApplicationPaths appPaths,
  65. ILogManager logManager,
  66. StartupOptions options)
  67. {
  68. SystemEvents.SessionEnding += SystemEvents_SessionEnding;
  69. // Allow all https requests
  70. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
  71. var fileSystem = new CommonFileSystem(logManager.GetLogger("FileSystem"), false, true);
  72. var nativeApp = new NativeApp();
  73. AppHost = new ApplicationHost(appPaths, logManager, options, fileSystem, "MBServer.Mono", false, nativeApp);
  74. if (options.ContainsOption("-v")) {
  75. Console.WriteLine (AppHost.ApplicationVersion.ToString());
  76. return;
  77. }
  78. Console.WriteLine ("appHost.Init");
  79. Task.Run (() => StartServer(CancellationToken.None));
  80. }
  81. private static async void StartServer(CancellationToken cancellationToken)
  82. {
  83. var initProgress = new Progress<double>();
  84. await AppHost.Init (initProgress).ConfigureAwait (false);
  85. await AppHost.RunStartupTasks ().ConfigureAwait (false);
  86. if (MenuBarIcon.Instance != null)
  87. {
  88. MenuBarIcon.Instance.Localize ();
  89. }
  90. }
  91. /// <summary>
  92. /// Handles the SessionEnding event of the SystemEvents control.
  93. /// </summary>
  94. /// <param name="sender">The source of the event.</param>
  95. /// <param name="e">The <see cref="SessionEndingEventArgs"/> instance containing the event data.</param>
  96. static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
  97. {
  98. if (e.Reason == SessionEndReasons.SystemShutdown)
  99. {
  100. Shutdown();
  101. }
  102. }
  103. public static void Shutdown()
  104. {
  105. ShutdownApp();
  106. }
  107. private static void ShutdownApp()
  108. {
  109. _logger.Info ("Calling ApplicationHost.Dispose");
  110. AppHost.Dispose ();
  111. _logger.Info("AppController.Terminate");
  112. MenuBarIcon.Instance.Terminate ();
  113. }
  114. /// <summary>
  115. /// Handles the UnhandledException event of the CurrentDomain control.
  116. /// </summary>
  117. /// <param name="sender">The source of the event.</param>
  118. /// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
  119. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  120. {
  121. var exception = (Exception)e.ExceptionObject;
  122. new UnhandledExceptionWriter(AppHost.ServerConfigurationManager.ApplicationPaths, _logger, AppHost.LogManager).Log(exception);
  123. if (!Debugger.IsAttached)
  124. {
  125. Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
  126. }
  127. }
  128. }
  129. class NoCheckCertificatePolicy : ICertificatePolicy
  130. {
  131. public bool CheckValidationResult (ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
  132. {
  133. return true;
  134. }
  135. }
  136. }