Main.cs 5.3 KB

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