GameXmlSaver.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System.Security;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Providers.Movies;
  6. using System;
  7. using System.Globalization;
  8. using System.IO;
  9. using System.Text;
  10. using System.Threading;
  11. namespace MediaBrowser.Providers.Savers
  12. {
  13. /// <summary>
  14. /// Saves game.xml for games
  15. /// </summary>
  16. public class GameXmlSaver : IMetadataSaver
  17. {
  18. private readonly IServerConfigurationManager _config;
  19. public GameXmlSaver(IServerConfigurationManager config)
  20. {
  21. _config = config;
  22. }
  23. /// <summary>
  24. /// Determines whether [is enabled for] [the specified item].
  25. /// </summary>
  26. /// <param name="item">The item.</param>
  27. /// <param name="updateType">Type of the update.</param>
  28. /// <returns><c>true</c> if [is enabled for] [the specified item]; otherwise, <c>false</c>.</returns>
  29. public bool IsEnabledFor(BaseItem item, ItemUpdateType updateType)
  30. {
  31. var wasMetadataEdited = (updateType & ItemUpdateType.MetadataEdit) == ItemUpdateType.MetadataEdit;
  32. var wasMetadataDownloaded = (updateType & ItemUpdateType.MetadataDownload) == ItemUpdateType.MetadataDownload;
  33. // If new metadata has been downloaded and save local is on, OR metadata was manually edited, proceed
  34. if ((_config.Configuration.SaveLocalMeta && (wasMetadataEdited || wasMetadataDownloaded)) || wasMetadataEdited)
  35. {
  36. return item is Game;
  37. }
  38. return false;
  39. }
  40. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  41. /// <summary>
  42. /// Saves the specified item.
  43. /// </summary>
  44. /// <param name="item">The item.</param>
  45. /// <param name="cancellationToken">The cancellation token.</param>
  46. /// <returns>Task.</returns>
  47. public void Save(BaseItem item, CancellationToken cancellationToken)
  48. {
  49. var builder = new StringBuilder();
  50. builder.Append("<Item>");
  51. var game = (Game)item;
  52. if (game.PlayersSupported.HasValue)
  53. {
  54. builder.Append("<Players>" + SecurityElement.Escape(game.PlayersSupported.Value.ToString(UsCulture)) + "</Players>");
  55. }
  56. if (!string.IsNullOrEmpty(game.GameSystem))
  57. {
  58. builder.Append("<GameSystem><![CDATA[" + game.GameSystem + "]]></GameSystem>");
  59. }
  60. XmlSaverHelpers.AddCommonNodes(item, builder);
  61. builder.Append("</Item>");
  62. var xmlFilePath = GetSavePath(item);
  63. XmlSaverHelpers.Save(builder, xmlFilePath, new[]
  64. {
  65. "Players",
  66. "GameSystem"
  67. });
  68. // Set last refreshed so that the provider doesn't trigger after the file save
  69. MovieProviderFromXml.Current.SetLastRefreshed(item, DateTime.UtcNow);
  70. }
  71. public string GetSavePath(BaseItem item)
  72. {
  73. if (item.ResolveArgs.IsDirectory)
  74. {
  75. var path = Directory.Exists(item.Path) ? item.Path : Path.GetDirectoryName(item.Path);
  76. return Path.Combine(path, "game.xml");
  77. }
  78. return Path.ChangeExtension(item.Path, ".xml");
  79. }
  80. }
  81. }