WindowsAppHost.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Runtime.InteropServices.ComTypes;
  7. using Emby.Server.CinemaMode;
  8. using Emby.Server.Connect;
  9. using Emby.Server.Core;
  10. using Emby.Server.Implementations;
  11. using Emby.Server.Implementations.EntryPoints;
  12. using Emby.Server.Implementations.FFMpeg;
  13. using Emby.Server.Implementations.IO;
  14. using Emby.Server.Sync;
  15. using MediaBrowser.Controller.Connect;
  16. using MediaBrowser.Controller.Sync;
  17. using MediaBrowser.Model.IO;
  18. using MediaBrowser.Model.Logging;
  19. using MediaBrowser.Model.System;
  20. using MediaBrowser.Model.Updates;
  21. using MediaBrowser.Server.Startup.Common;
  22. using MediaBrowser.ServerApplication.Native;
  23. namespace MediaBrowser.ServerApplication
  24. {
  25. public class WindowsAppHost : ApplicationHost
  26. {
  27. public WindowsAppHost(ServerApplicationPaths applicationPaths, ILogManager logManager, StartupOptions options, IFileSystem fileSystem, IPowerManagement powerManagement, string releaseAssetFilename, IEnvironmentInfo environmentInfo, MediaBrowser.Controller.Drawing.IImageEncoder imageEncoder, ISystemEvents systemEvents, MediaBrowser.Common.Net.INetworkManager networkManager)
  28. : base(applicationPaths, logManager, options, fileSystem, powerManagement, releaseAssetFilename, environmentInfo, imageEncoder, systemEvents, networkManager)
  29. {
  30. fileSystem.AddShortcutHandler(new LnkShortcutHandler());
  31. }
  32. public override bool IsRunningAsService
  33. {
  34. get { return MainStartup.IsRunningAsService; }
  35. }
  36. protected override IConnectManager CreateConnectManager()
  37. {
  38. return new ConnectManager();
  39. }
  40. protected override ISyncManager CreateSyncManager()
  41. {
  42. return new SyncManager();
  43. }
  44. protected override void RestartInternal()
  45. {
  46. MainStartup.Restart();
  47. }
  48. public override void EnableLoopback(string appName)
  49. {
  50. LoopUtil.Run(appName);
  51. }
  52. protected override List<Assembly> GetAssembliesWithPartsInternal()
  53. {
  54. var list = new List<Assembly>();
  55. list.Add(typeof(DefaultIntroProvider).Assembly);
  56. list.Add(typeof(ConnectManager).Assembly);
  57. list.Add(typeof(SyncManager).Assembly);
  58. list.Add(GetType().Assembly);
  59. return list;
  60. }
  61. protected override void ShutdownInternal()
  62. {
  63. MainStartup.Shutdown();
  64. }
  65. protected override void AuthorizeServer()
  66. {
  67. ServerAuthorization.AuthorizeServer(UdpServerEntryPoint.PortNumber,
  68. ServerConfigurationManager.Configuration.HttpServerPortNumber,
  69. ServerConfigurationManager.Configuration.HttpsPortNumber,
  70. MainStartup.ApplicationPath,
  71. ConfigurationManager.CommonApplicationPaths.TempDirectory);
  72. }
  73. protected override void ConfigureAutoRunInternal(bool autorun)
  74. {
  75. var startupPath = Environment.GetFolderPath(System.Environment.SpecialFolder.Startup);
  76. if (autorun && !MainStartup.IsRunningAsService)
  77. {
  78. //Copy our shortut into the startup folder for this user
  79. var targetPath = Path.Combine(startupPath, "Emby Server.lnk");
  80. IShellLinkW link = (IShellLinkW)new ShellLink();
  81. var appPath = Process.GetCurrentProcess().MainModule.FileName;
  82. // setup shortcut information
  83. link.SetDescription(Name);
  84. link.SetPath(appPath);
  85. link.SetWorkingDirectory(Path.GetDirectoryName(appPath));
  86. // save it
  87. IPersistFile file = (IPersistFile)link;
  88. file.Save(targetPath, true);
  89. }
  90. else
  91. {
  92. //Remove our shortcut from the startup folder for this user
  93. FileSystemManager.DeleteFile(Path.Combine(startupPath, "Emby Server.lnk"));
  94. }
  95. }
  96. public override bool SupportsRunningAsService
  97. {
  98. get
  99. {
  100. return true;
  101. }
  102. }
  103. public override bool CanSelfRestart
  104. {
  105. get
  106. {
  107. return MainStartup.CanSelfRestart;
  108. }
  109. }
  110. public override bool CanSelfUpdate
  111. {
  112. get
  113. {
  114. return MainStartup.CanSelfUpdate;
  115. }
  116. }
  117. }
  118. }