jellyfin/Emby.Naming/Video/Format3DParser.cs

98 lines
3.0 KiB
C#
Raw Normal View History

2018-09-12 10:26:21 -07:00
using System;
using System.Linq;
2019-01-13 12:17:29 -07:00
using Emby.Naming.Common;
2018-09-12 10:26:21 -07:00
namespace Emby.Naming.Video
{
2020-11-10 11:23:10 -07:00
/// <summary>
/// Parste 3D format related flags.
/// </summary>
2018-09-12 10:26:21 -07:00
public class Format3DParser
{
private readonly NamingOptions _options;
2020-11-10 11:23:10 -07:00
/// <summary>
/// Initializes a new instance of the <see cref="Format3DParser"/> class.
/// </summary>
/// <param name="options"><see cref="NamingOptions"/> object containing VideoFlagDelimiters and passes options to <see cref="FlagParser"/>.</param>
2018-09-12 10:26:21 -07:00
public Format3DParser(NamingOptions options)
{
_options = options;
}
2020-11-10 11:23:10 -07:00
/// <summary>
/// Parse 3D format related flags.
/// </summary>
/// <param name="path">Path to file.</param>
/// <returns>Returns <see cref="Format3DResult"/> object.</returns>
2018-09-12 10:26:21 -07:00
public Format3DResult Parse(string path)
{
2019-05-10 11:37:42 -07:00
int oldLen = _options.VideoFlagDelimiters.Length;
2020-11-01 03:19:22 -07:00
var delimiters = new char[oldLen + 1];
_options.VideoFlagDelimiters.CopyTo(delimiters, 0);
delimiters[oldLen] = ' ';
2018-09-12 10:26:21 -07:00
2020-11-01 03:19:22 -07:00
return Parse(new FlagParser(_options).GetFlags(path, delimiters));
2018-09-12 10:26:21 -07:00
}
internal Format3DResult Parse(string[] videoFlags)
{
foreach (var rule in _options.Format3DRules)
{
var result = Parse(videoFlags, rule);
if (result.Is3D)
{
return result;
}
}
return new Format3DResult();
}
private static Format3DResult Parse(string[] videoFlags, Format3DRule rule)
2018-09-12 10:26:21 -07:00
{
var result = new Format3DResult();
2020-11-01 03:19:22 -07:00
if (string.IsNullOrEmpty(rule.PrecedingToken))
2018-09-12 10:26:21 -07:00
{
result.Format3D = new[] { rule.Token }.FirstOrDefault(i => videoFlags.Contains(i, StringComparer.OrdinalIgnoreCase));
result.Is3D = !string.IsNullOrEmpty(result.Format3D);
if (result.Is3D)
{
result.Tokens.Add(rule.Token);
}
}
else
{
var foundPrefix = false;
2020-11-01 02:47:31 -07:00
string? format = null;
2018-09-12 10:26:21 -07:00
foreach (var flag in videoFlags)
{
if (foundPrefix)
{
2020-11-01 03:19:22 -07:00
result.Tokens.Add(rule.PrecedingToken);
2018-09-12 10:26:21 -07:00
if (string.Equals(rule.Token, flag, StringComparison.OrdinalIgnoreCase))
{
format = flag;
result.Tokens.Add(rule.Token);
}
2019-05-10 11:37:42 -07:00
2018-09-12 10:26:21 -07:00
break;
}
2019-05-10 11:37:42 -07:00
2020-11-01 03:19:22 -07:00
foundPrefix = string.Equals(flag, rule.PrecedingToken, StringComparison.OrdinalIgnoreCase);
2018-09-12 10:26:21 -07:00
}
result.Is3D = foundPrefix && !string.IsNullOrEmpty(format);
result.Format3D = format;
}
return result;
}
}
}