jellyfin-server/Emby.Naming/Video/Format3DParser.cs

89 lines
2.5 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
#pragma warning disable SA1600
2018-09-12 17:26:21 +00:00
using System;
using System.Linq;
2019-01-13 19:17:29 +00:00
using Emby.Naming.Common;
2018-09-12 17:26:21 +00: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 18:37:42 +00:00
int oldLen = _options.VideoFlagDelimiters.Length;
var delimeters = new char[oldLen + 1];
_options.VideoFlagDelimiters.CopyTo(delimeters, 0);
delimeters[oldLen] = ' ';
2018-09-12 17:26:21 +00:00
2019-05-10 18:37:42 +00:00
return Parse(new FlagParser(_options).GetFlags(path, delimeters));
2018-09-12 17:26:21 +00: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 17:26:21 +00:00
{
var result = new Format3DResult();
if (string.IsNullOrEmpty(rule.PreceedingToken))
{
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;
string format = null;
foreach (var flag in videoFlags)
{
if (foundPrefix)
{
result.Tokens.Add(rule.PreceedingToken);
if (string.Equals(rule.Token, flag, StringComparison.OrdinalIgnoreCase))
{
format = flag;
result.Tokens.Add(rule.Token);
}
2019-05-10 18:37:42 +00:00
2018-09-12 17:26:21 +00:00
break;
}
2019-05-10 18:37:42 +00:00
2018-09-12 17:26:21 +00:00
foundPrefix = string.Equals(flag, rule.PreceedingToken, StringComparison.OrdinalIgnoreCase);
}
result.Is3D = foundPrefix && !string.IsNullOrEmpty(format);
result.Format3D = format;
}
return result;
}
}
}