WindowsApp.cs 6.0 KB

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