jellyfin/Emby.Naming/Video/Format3DParser.cs

86 lines
2.4 KiB
C#
Raw Normal View History

2018-09-12 13:26:21 -04:00
using System;
using System.Linq;
2019-01-13 14:17:29 -05:00
using Emby.Naming.Common;
2018-09-12 13:26:21 -04:00
namespace Emby.Naming.Video
{
public class Format3DParser
{
private readonly NamingOptions _options;
public Format3DParser(NamingOptions options)
{
_options = options;
}
public Format3DResult Parse(string path)
{
2019-05-10 14:37:42 -04:00
int oldLen = _options.VideoFlagDelimiters.Length;
2020-11-01 05:19:22 -05:00
var delimiters = new char[oldLen + 1];
_options.VideoFlagDelimiters.CopyTo(delimiters, 0);
delimiters[oldLen] = ' ';
2018-09-12 13:26:21 -04:00
2020-11-01 05:19:22 -05:00
return Parse(new FlagParser(_options).GetFlags(path, delimiters));
2018-09-12 13:26:21 -04: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 13:26:21 -04:00
{
var result = new Format3DResult();
2020-11-01 05:19:22 -05:00
if (string.IsNullOrEmpty(rule.PrecedingToken))
2018-09-12 13:26:21 -04: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 04:47:31 -05:00
string? format = null;
2018-09-12 13:26:21 -04:00
foreach (var flag in videoFlags)
{
if (foundPrefix)
{
2020-11-01 05:19:22 -05:00
result.Tokens.Add(rule.PrecedingToken);
2018-09-12 13:26:21 -04:00
if (string.Equals(rule.Token, flag, StringComparison.OrdinalIgnoreCase))
{
format = flag;
result.Tokens.Add(rule.Token);
}
2019-05-10 14:37:42 -04:00
2018-09-12 13:26:21 -04:00
break;
}
2019-05-10 14:37:42 -04:00
2020-11-01 05:19:22 -05:00
foundPrefix = string.Equals(flag, rule.PrecedingToken, StringComparison.OrdinalIgnoreCase);
2018-09-12 13:26:21 -04:00
}
result.Is3D = foundPrefix && !string.IsNullOrEmpty(format);
result.Format3D = format;
}
return result;
}
}
}