jellyfin/MediaBrowser.Server.Mac/Main.cs

212 lines
6.5 KiB
C#
Raw Normal View History

2014-11-21 10:08:50 -07:00
using System;
2014-11-23 17:02:01 -07:00
using System.Diagnostics;
2014-11-21 10:08:50 -07:00
using System.Drawing;
2014-11-23 17:02:01 -07:00
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Implementations.IO;
using MediaBrowser.Common.Implementations.Logging;
using MediaBrowser.Model.Logging;
using MediaBrowser.Server.Implementations;
using MediaBrowser.Server.Startup.Common;
using MediaBrowser.Server.Startup.Common.Browser;
using Microsoft.Win32;
2014-11-21 10:08:50 -07:00
using MonoMac.AppKit;
2014-11-23 17:02:01 -07:00
using MonoMac.Foundation;
2014-11-21 10:08:50 -07:00
using MonoMac.ObjCRuntime;
2015-10-26 21:13:12 -07:00
using CommonIO;
using MediaBrowser.Server.Implementations.Logging;
2014-11-21 10:08:50 -07:00
namespace MediaBrowser.Server.Mac
{
class MainClass
{
2014-11-28 21:03:47 -07:00
internal static ApplicationHost AppHost;
2014-11-23 17:02:01 -07:00
private static ILogger _logger;
2014-11-21 10:08:50 -07:00
static void Main (string[] args)
{
2014-11-23 17:02:01 -07:00
var applicationPath = Assembly.GetEntryAssembly().Location;
var options = new StartupOptions();
// Allow this to be specified on the command line.
var customProgramDataPath = options.GetOption("-programdata");
var appPaths = CreateApplicationPaths(applicationPath, customProgramDataPath);
var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
logManager.ReloadLogger(LogSeverity.Info);
logManager.AddConsoleOutput();
var logger = _logger = logManager.GetLogger("Main");
ApplicationHost.LogEnvironmentInfo(logger, appPaths, true);
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
StartApplication(appPaths, logManager, options);
2014-11-21 10:08:50 -07:00
NSApplication.Init ();
NSApplication.Main (args);
2014-11-25 21:27:55 -07:00
}
2014-11-23 17:02:01 -07:00
private static ServerApplicationPaths CreateApplicationPaths(string applicationPath, string programDataPath)
{
if (string.IsNullOrEmpty(programDataPath))
{
2014-11-26 14:16:04 -07:00
// TODO: Use CommonApplicationData? Will we always have write access?
programDataPath = Path.Combine(Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "mediabrowser-server");
2015-04-16 08:41:23 -07:00
if (!Directory.Exists (programDataPath)) {
programDataPath = Path.Combine(Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "emby-server");
}
2014-11-23 17:02:01 -07:00
}
2014-11-26 18:56:14 -07:00
// Within the mac bundle, go uo two levels then down into Resources folder
var resourcesPath = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName (applicationPath)), "Resources");
2014-11-23 17:02:01 -07:00
2014-11-26 14:16:04 -07:00
return new ServerApplicationPaths(programDataPath, applicationPath, resourcesPath);
2014-11-23 17:02:01 -07:00
}
/// <summary>
/// Runs the application.
/// </summary>
/// <param name="appPaths">The app paths.</param>
/// <param name="logManager">The log manager.</param>
/// <param name="options">The options.</param>
private static void StartApplication(ServerApplicationPaths appPaths,
ILogManager logManager,
StartupOptions options)
{
SystemEvents.SessionEnding += SystemEvents_SessionEnding;
// Allow all https requests
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
2015-10-26 21:13:12 -07:00
var fileSystem = new ManagedFileSystem(new PatternsLogger(logManager.GetLogger("FileSystem")), false, true);
2015-10-29 18:55:42 -07:00
fileSystem.AddShortcutHandler(new MbLinkShortcutHandler(fileSystem));
2014-11-23 17:02:01 -07:00
var nativeApp = new NativeApp();
2016-01-22 11:31:57 -07:00
AppHost = new ApplicationHost(appPaths, logManager, options, fileSystem, "Emby.Server.Mac.pkg", nativeApp);
2014-11-23 17:02:01 -07:00
if (options.ContainsOption("-v")) {
2014-11-28 21:03:47 -07:00
Console.WriteLine (AppHost.ApplicationVersion.ToString());
2014-11-23 17:02:01 -07:00
return;
}
Console.WriteLine ("appHost.Init");
2014-11-28 10:40:20 -07:00
Task.Run (() => StartServer(CancellationToken.None));
}
2014-11-23 17:02:01 -07:00
2014-11-28 10:40:20 -07:00
private static async void StartServer(CancellationToken cancellationToken)
{
var initProgress = new Progress<double>();
2014-11-23 17:02:01 -07:00
2014-11-28 21:03:47 -07:00
await AppHost.Init (initProgress).ConfigureAwait (false);
await AppHost.RunStartupTasks ().ConfigureAwait (false);
2014-11-26 18:56:14 -07:00
2014-11-28 21:03:47 -07:00
if (MenuBarIcon.Instance != null)
{
MenuBarIcon.Instance.Localize ();
}
2014-11-23 17:02:01 -07:00
}
/// <summary>
/// Handles the SessionEnding event of the SystemEvents control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="SessionEndingEventArgs"/> instance containing the event data.</param>
static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
{
if (e.Reason == SessionEndReasons.SystemShutdown)
{
Shutdown();
}
}
public static void Shutdown()
{
ShutdownApp();
}
private static void ShutdownApp()
{
_logger.Info ("Calling ApplicationHost.Dispose");
2014-11-28 21:03:47 -07:00
AppHost.Dispose ();
2014-11-23 17:02:01 -07:00
_logger.Info("AppController.Terminate");
2014-11-28 10:40:20 -07:00
MenuBarIcon.Instance.Terminate ();
2014-11-23 17:02:01 -07:00
}
2015-05-23 15:01:13 -07:00
public static void Restart()
{
_logger.Info("Disposing app host");
AppHost.Dispose();
_logger.Info("Starting new instance");
var args = Environment.GetCommandLineArgs()
2015-05-23 15:27:05 -07:00
.Skip(1)
2015-05-23 15:01:13 -07:00
.Select(NormalizeCommandLineArgument);
var commandLineArgsString = string.Join(" ", args.ToArray());
2015-05-23 15:27:05 -07:00
var module = Environment.GetCommandLineArgs().First();
_logger.Info ("Executable: {0}", module);
_logger.Info ("Arguments: {0}", commandLineArgsString);
2015-05-23 15:01:13 -07:00
2015-05-23 15:27:05 -07:00
Process.Start(module, commandLineArgsString);
2015-05-23 15:01:13 -07:00
_logger.Info("AppController.Terminate");
MenuBarIcon.Instance.Terminate();
}
2015-05-23 15:08:50 -07:00
private static string NormalizeCommandLineArgument(string arg)
{
if (arg.IndexOf(" ", StringComparison.OrdinalIgnoreCase) == -1)
{
return arg;
}
return "\"" + arg + "\"";
}
2014-11-23 17:02:01 -07:00
/// <summary>
/// Handles the UnhandledException event of the CurrentDomain control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var exception = (Exception)e.ExceptionObject;
2014-11-28 21:03:47 -07:00
new UnhandledExceptionWriter(AppHost.ServerConfigurationManager.ApplicationPaths, _logger, AppHost.LogManager).Log(exception);
2014-11-23 17:02:01 -07:00
if (!Debugger.IsAttached)
{
Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
}
}
}
class NoCheckCertificatePolicy : ICertificatePolicy
{
public bool CheckValidationResult (ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
{
return true;
}
2014-11-21 10:08:50 -07:00
}
}