From 0a313b508790b0051d65c7e1c2e5aa549d9d0a24 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 26 Sep 2013 11:15:18 -0400 Subject: [PATCH] added game system xml saver --- .../Games/GameSystemProviderFromXml.cs | 101 ++++++++++++++++++ .../MediaBrowser.Providers.csproj | 2 + .../MediaInfo/FFProbeVideoInfoProvider.cs | 13 ++- .../Savers/FolderXmlSaver.cs | 7 +- .../Savers/GameSystemXmlSaver.cs | 75 +++++++++++++ .../Savers/SeriesXmlSaver.cs | 4 +- 6 files changed, 194 insertions(+), 8 deletions(-) create mode 100644 MediaBrowser.Providers/Games/GameSystemProviderFromXml.cs create mode 100644 MediaBrowser.Providers/Savers/GameSystemXmlSaver.cs diff --git a/MediaBrowser.Providers/Games/GameSystemProviderFromXml.cs b/MediaBrowser.Providers/Games/GameSystemProviderFromXml.cs new file mode 100644 index 0000000000..e01bed88e4 --- /dev/null +++ b/MediaBrowser.Providers/Games/GameSystemProviderFromXml.cs @@ -0,0 +1,101 @@ +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Providers; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Logging; +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.Providers.Games +{ + public class GameSystemProviderFromXml : BaseMetadataProvider + { + internal static GameSystemProviderFromXml Current { get; private set; } + + public GameSystemProviderFromXml(ILogManager logManager, IServerConfigurationManager configurationManager) + : base(logManager, configurationManager) + { + Current = this; + } + + /// + /// Supportses the specified item. + /// + /// The item. + /// true if XXXX, false otherwise + public override bool Supports(BaseItem item) + { + return item is GameSystem && item.LocationType == LocationType.FileSystem; + } + + /// + /// Gets the priority. + /// + /// The priority. + public override MetadataProviderPriority Priority + { + get { return MetadataProviderPriority.First; } + } + + /// + /// Override this to return the date that should be compared to the last refresh date + /// to determine if this provider should be re-fetched. + /// + /// The item. + /// DateTime. + protected override DateTime CompareDate(BaseItem item) + { + var entry = item.ResolveArgs.GetMetaFileByPath(Path.Combine(item.MetaLocation, "gamesystem.xml")); + return entry != null ? entry.LastWriteTimeUtc : DateTime.MinValue; + } + + /// + /// Fetches metadata and returns true or false indicating if any work that requires persistence was done + /// + /// The item. + /// if set to true [force]. + /// The cancellation token. + /// Task{System.Boolean}. + public override Task FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken) + { + return Fetch(item, cancellationToken); + } + + /// + /// Fetches the specified item. + /// + /// The item. + /// The cancellation token. + /// true if XXXX, false otherwise + private async Task Fetch(BaseItem item, CancellationToken cancellationToken) + { + cancellationToken.ThrowIfCancellationRequested(); + + var metadataFile = item.ResolveArgs.GetMetaFileByPath(Path.Combine(item.MetaLocation, "gamesystem.xml")); + + if (metadataFile != null) + { + var path = metadataFile.FullName; + + await XmlParsingResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); + + try + { + new BaseItemXmlParser(Logger).Fetch((GameSystem)item, path, cancellationToken); + } + finally + { + XmlParsingResourcePool.Release(); + } + + SetLastRefreshed(item, DateTime.UtcNow); + + return true; + } + + return false; + } + } +} diff --git a/MediaBrowser.Providers/MediaBrowser.Providers.csproj b/MediaBrowser.Providers/MediaBrowser.Providers.csproj index ef94d77d16..7e90d41f5f 100644 --- a/MediaBrowser.Providers/MediaBrowser.Providers.csproj +++ b/MediaBrowser.Providers/MediaBrowser.Providers.csproj @@ -52,6 +52,7 @@ + @@ -91,6 +92,7 @@ + diff --git a/MediaBrowser.Providers/MediaInfo/FFProbeVideoInfoProvider.cs b/MediaBrowser.Providers/MediaInfo/FFProbeVideoInfoProvider.cs index c28d06cbb7..727768b9f7 100644 --- a/MediaBrowser.Providers/MediaInfo/FFProbeVideoInfoProvider.cs +++ b/MediaBrowser.Providers/MediaInfo/FFProbeVideoInfoProvider.cs @@ -330,8 +330,17 @@ namespace MediaBrowser.Providers.MediaInfo await Kernel.Instance.FFMpegManager.PopulateChapterImages(video, chapters, false, false, cancellationToken).ConfigureAwait(false); - // Only save chapters if forcing or there are not already any saved ones - if (force || _itemRepo.GetChapter(video.Id, 0) == null) + + BaseProviderInfo providerInfo; + var videoFileChanged = false; + + if (video.ProviderData.TryGetValue(Id, out providerInfo)) + { + videoFileChanged = CompareDate(video) > providerInfo.LastRefreshed; + } + + // Only save chapters if forcing, if the video changed, or if there are not already any saved ones + if (force || videoFileChanged || _itemRepo.GetChapter(video.Id, 0) == null) { await _itemRepo.SaveChapters(video.Id, chapters, cancellationToken).ConfigureAwait(false); } diff --git a/MediaBrowser.Providers/Savers/FolderXmlSaver.cs b/MediaBrowser.Providers/Savers/FolderXmlSaver.cs index 6e95cc8c5e..f98d27b45f 100644 --- a/MediaBrowser.Providers/Savers/FolderXmlSaver.cs +++ b/MediaBrowser.Providers/Savers/FolderXmlSaver.cs @@ -1,12 +1,11 @@ -using System; -using System.Collections.Generic; -using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Audio; using MediaBrowser.Controller.Entities.Movies; using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Library; -using MediaBrowser.Model.Entities; +using System; +using System.Collections.Generic; using System.IO; using System.Text; using System.Threading; diff --git a/MediaBrowser.Providers/Savers/GameSystemXmlSaver.cs b/MediaBrowser.Providers/Savers/GameSystemXmlSaver.cs new file mode 100644 index 0000000000..b2837fd58e --- /dev/null +++ b/MediaBrowser.Providers/Savers/GameSystemXmlSaver.cs @@ -0,0 +1,75 @@ +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; +using MediaBrowser.Providers.Games; +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Threading; + +namespace MediaBrowser.Providers.Savers +{ + public class GameSystemXmlSaver : IMetadataSaver + { + private readonly IServerConfigurationManager _config; + + public GameSystemXmlSaver(IServerConfigurationManager config) + { + _config = config; + } + + /// + /// Determines whether [is enabled for] [the specified item]. + /// + /// The item. + /// Type of the update. + /// true if [is enabled for] [the specified item]; otherwise, false. + public bool IsEnabledFor(BaseItem item, ItemUpdateType updateType) + { + var wasMetadataEdited = (updateType & ItemUpdateType.MetadataEdit) == ItemUpdateType.MetadataEdit; + var wasMetadataDownloaded = (updateType & ItemUpdateType.MetadataDownload) == ItemUpdateType.MetadataDownload; + + // If new metadata has been downloaded and save local is on, OR metadata was manually edited, proceed + if ((_config.Configuration.SaveLocalMeta && (wasMetadataEdited || wasMetadataDownloaded)) || wasMetadataEdited) + { + return item is GameSystem; + } + + return false; + } + + /// + /// Saves the specified item. + /// + /// The item. + /// The cancellation token. + /// Task. + public void Save(BaseItem item, CancellationToken cancellationToken) + { + var builder = new StringBuilder(); + + builder.Append(""); + + XmlSaverHelpers.AddCommonNodes(item, builder); + + builder.Append(""); + + var xmlFilePath = GetSavePath(item); + + XmlSaverHelpers.Save(builder, xmlFilePath, new List { }); + + GameSystemProviderFromXml.Current.SetLastRefreshed(item, DateTime.UtcNow); + } + + /// + /// Gets the save path. + /// + /// The item. + /// System.String. + public string GetSavePath(BaseItem item) + { + return Path.Combine(item.Path, "gamesystem.xml"); + } + } +} diff --git a/MediaBrowser.Providers/Savers/SeriesXmlSaver.cs b/MediaBrowser.Providers/Savers/SeriesXmlSaver.cs index a4ff9c7d8f..8ccf23ddca 100644 --- a/MediaBrowser.Providers/Savers/SeriesXmlSaver.cs +++ b/MediaBrowser.Providers/Savers/SeriesXmlSaver.cs @@ -1,11 +1,11 @@ -using System.Collections.Generic; -using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Library; using MediaBrowser.Model.Entities; using MediaBrowser.Providers.TV; using System; +using System.Collections.Generic; using System.IO; using System.Security; using System.Text;