WindowsApp.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Model.Logging;
  4. using MediaBrowser.Server.Startup.Common;
  5. using MediaBrowser.ServerApplication.Networking;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Reflection;
  10. using CommonIO;
  11. using MediaBrowser.Controller.Power;
  12. using MediaBrowser.Model.System;
  13. using MediaBrowser.Server.Implementations.Persistence;
  14. using MediaBrowser.Server.Startup.Common.FFMpeg;
  15. using OperatingSystem = MediaBrowser.Server.Startup.Common.OperatingSystem;
  16. namespace MediaBrowser.ServerApplication.Native
  17. {
  18. public class WindowsApp : INativeApp
  19. {
  20. private readonly IFileSystem _fileSystem;
  21. private readonly ILogger _logger;
  22. public WindowsApp(IFileSystem fileSystem, ILogger logger)
  23. {
  24. _fileSystem = fileSystem;
  25. _logger = logger;
  26. }
  27. public List<Assembly> GetAssembliesWithParts()
  28. {
  29. var list = new List<Assembly>();
  30. if (!System.Environment.Is64BitProcess)
  31. {
  32. //list.Add(typeof(PismoIsoManager).Assembly);
  33. }
  34. list.Add(GetType().Assembly);
  35. return list;
  36. }
  37. public void AuthorizeServer(int udpPort, int httpServerPort, int httpsPort, string applicationPath, string tempDirectory)
  38. {
  39. ServerAuthorization.AuthorizeServer(udpPort, httpServerPort, httpsPort, applicationPath, tempDirectory);
  40. }
  41. public NativeEnvironment Environment
  42. {
  43. get
  44. {
  45. return new NativeEnvironment
  46. {
  47. OperatingSystem = OperatingSystem.Windows,
  48. SystemArchitecture = System.Environment.Is64BitOperatingSystem ? Architecture.X64 : Architecture.X86,
  49. OperatingSystemVersionString = System.Environment.OSVersion.VersionString
  50. };
  51. }
  52. }
  53. public bool SupportsLibraryMonitor
  54. {
  55. get { return true; }
  56. }
  57. public bool SupportsRunningAsService
  58. {
  59. get
  60. {
  61. return true;
  62. }
  63. }
  64. public bool IsRunningAsService
  65. {
  66. get;
  67. set;
  68. }
  69. public bool CanSelfRestart
  70. {
  71. get
  72. {
  73. return MainStartup.CanSelfRestart;
  74. }
  75. }
  76. public bool SupportsAutoRunAtStartup
  77. {
  78. get
  79. {
  80. return true;
  81. }
  82. }
  83. public bool CanSelfUpdate
  84. {
  85. get
  86. {
  87. return MainStartup.CanSelfUpdate;
  88. }
  89. }
  90. public void Shutdown()
  91. {
  92. MainStartup.Shutdown();
  93. }
  94. public void Restart(StartupOptions startupOptions)
  95. {
  96. MainStartup.Restart();
  97. }
  98. public void ConfigureAutoRun(bool autorun)
  99. {
  100. var shortcutPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.StartMenu), "Emby", "Emby Server.lnk");
  101. var startupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup);
  102. if (autorun)
  103. {
  104. //Copy our shortut into the startup folder for this user
  105. var targetPath = Path.Combine(startupPath, Path.GetFileName(shortcutPath) ?? "Emby Server.lnk");
  106. _fileSystem.CreateDirectory(Path.GetDirectoryName(targetPath));
  107. File.Copy(shortcutPath, targetPath, true);
  108. }
  109. else
  110. {
  111. //Remove our shortcut from the startup folder for this user
  112. _fileSystem.DeleteFile(Path.Combine(startupPath, Path.GetFileName(shortcutPath) ?? "Emby Server.lnk"));
  113. }
  114. }
  115. public INetworkManager CreateNetworkManager(ILogger logger)
  116. {
  117. return new NetworkManager(logger);
  118. }
  119. public void PreventSystemStandby()
  120. {
  121. MainStartup.Invoke(Standby.PreventSleep);
  122. }
  123. public void AllowSystemStandby()
  124. {
  125. MainStartup.Invoke(Standby.AllowSleep);
  126. }
  127. public IPowerManagement GetPowerManagement()
  128. {
  129. return new WindowsPowerManagement(_logger);
  130. }
  131. public FFMpegInstallInfo GetFfmpegInstallInfo()
  132. {
  133. var info = new FFMpegInstallInfo();
  134. info.FFMpegFilename = "ffmpeg.exe";
  135. info.FFProbeFilename = "ffprobe.exe";
  136. info.Version = "0";
  137. return info;
  138. }
  139. public void LaunchUrl(string url)
  140. {
  141. var process = new Process
  142. {
  143. StartInfo = new ProcessStartInfo
  144. {
  145. FileName = url
  146. },
  147. EnableRaisingEvents = true,
  148. };
  149. process.Exited += ProcessExited;
  150. try
  151. {
  152. process.Start();
  153. }
  154. catch (Exception ex)
  155. {
  156. _logger.ErrorException("Error launching url: {0}", ex, url);
  157. throw;
  158. }
  159. }
  160. public IDbConnector GetDbConnector()
  161. {
  162. return new DbConnector(_logger);
  163. }
  164. /// <summary>
  165. /// Processes the exited.
  166. /// </summary>
  167. /// <param name="sender">The sender.</param>
  168. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  169. private static void ProcessExited(object sender, EventArgs e)
  170. {
  171. ((Process)sender).Dispose();
  172. }
  173. public void EnableLoopback(string appName)
  174. {
  175. LoopUtil.Run(appName);
  176. }
  177. }
  178. }