GameXmlSaver.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using MediaBrowser.Controller.Configuration;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Model.Entities;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using System.IO;
  8. using System.Security;
  9. using System.Text;
  10. using System.Threading;
  11. namespace MediaBrowser.LocalMetadata.Savers
  12. {
  13. /// <summary>
  14. /// Saves game.xml for games
  15. /// </summary>
  16. public class GameXmlSaver : IMetadataFileSaver
  17. {
  18. public string Name
  19. {
  20. get
  21. {
  22. return XmlProviderUtils.Name;
  23. }
  24. }
  25. private readonly IServerConfigurationManager _config;
  26. public GameXmlSaver(IServerConfigurationManager config)
  27. {
  28. _config = config;
  29. }
  30. /// <summary>
  31. /// Determines whether [is enabled for] [the specified item].
  32. /// </summary>
  33. /// <param name="item">The item.</param>
  34. /// <param name="updateType">Type of the update.</param>
  35. /// <returns><c>true</c> if [is enabled for] [the specified item]; otherwise, <c>false</c>.</returns>
  36. public bool IsEnabledFor(IHasMetadata item, ItemUpdateType updateType)
  37. {
  38. if (!item.SupportsLocalMetadata)
  39. {
  40. return false;
  41. }
  42. return item is Game && updateType >= ItemUpdateType.MetadataDownload;
  43. }
  44. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  45. /// <summary>
  46. /// Saves the specified item.
  47. /// </summary>
  48. /// <param name="item">The item.</param>
  49. /// <param name="cancellationToken">The cancellation token.</param>
  50. /// <returns>Task.</returns>
  51. public void Save(IHasMetadata item, CancellationToken cancellationToken)
  52. {
  53. var builder = new StringBuilder();
  54. builder.Append("<Item>");
  55. var game = (Game)item;
  56. if (game.PlayersSupported.HasValue)
  57. {
  58. builder.Append("<Players>" + SecurityElement.Escape(game.PlayersSupported.Value.ToString(UsCulture)) + "</Players>");
  59. }
  60. if (!string.IsNullOrEmpty(game.GameSystem))
  61. {
  62. builder.Append("<GameSystem>" + SecurityElement.Escape(game.GameSystem) + "</GameSystem>");
  63. }
  64. var val = game.GetProviderId(MetadataProviders.NesBox);
  65. if (!string.IsNullOrEmpty(val))
  66. {
  67. builder.Append("<NesBox>" + SecurityElement.Escape(val) + "</NesBox>");
  68. }
  69. val = game.GetProviderId(MetadataProviders.NesBoxRom);
  70. if (!string.IsNullOrEmpty(val))
  71. {
  72. builder.Append("<NesBoxRom>" + SecurityElement.Escape(val) + "</NesBoxRom>");
  73. }
  74. XmlSaverHelpers.AddCommonNodes(game, builder);
  75. builder.Append("</Item>");
  76. var xmlFilePath = GetSavePath(item);
  77. XmlSaverHelpers.Save(builder, xmlFilePath, new List<string>
  78. {
  79. "Players",
  80. "GameSystem",
  81. "NesBox",
  82. "NesBoxRom"
  83. }, _config);
  84. }
  85. public string GetSavePath(IHasMetadata item)
  86. {
  87. return GetGameSavePath((Game)item);
  88. }
  89. public static string GetGameSavePath(Game item)
  90. {
  91. if (item.IsInMixedFolder)
  92. {
  93. return Path.ChangeExtension(item.Path, ".xml");
  94. }
  95. return Path.Combine(item.ContainingFolderPath, "game.xml");
  96. }
  97. }
  98. }