Format3DParser.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #pragma warning disable CS1591
  2. #pragma warning disable SA1600
  3. using System;
  4. using System.Linq;
  5. using Emby.Naming.Common;
  6. namespace Emby.Naming.Video
  7. {
  8. public class Format3DParser
  9. {
  10. private readonly NamingOptions _options;
  11. public Format3DParser(NamingOptions options)
  12. {
  13. _options = options;
  14. }
  15. public Format3DResult Parse(string path)
  16. {
  17. int oldLen = _options.VideoFlagDelimiters.Length;
  18. var delimeters = new char[oldLen + 1];
  19. _options.VideoFlagDelimiters.CopyTo(delimeters, 0);
  20. delimeters[oldLen] = ' ';
  21. return Parse(new FlagParser(_options).GetFlags(path, delimeters));
  22. }
  23. internal Format3DResult Parse(string[] videoFlags)
  24. {
  25. foreach (var rule in _options.Format3DRules)
  26. {
  27. var result = Parse(videoFlags, rule);
  28. if (result.Is3D)
  29. {
  30. return result;
  31. }
  32. }
  33. return new Format3DResult();
  34. }
  35. private static Format3DResult Parse(string[] videoFlags, Format3DRule rule)
  36. {
  37. var result = new Format3DResult();
  38. if (string.IsNullOrEmpty(rule.PreceedingToken))
  39. {
  40. result.Format3D = new[] { rule.Token }.FirstOrDefault(i => videoFlags.Contains(i, StringComparer.OrdinalIgnoreCase));
  41. result.Is3D = !string.IsNullOrEmpty(result.Format3D);
  42. if (result.Is3D)
  43. {
  44. result.Tokens.Add(rule.Token);
  45. }
  46. }
  47. else
  48. {
  49. var foundPrefix = false;
  50. string format = null;
  51. foreach (var flag in videoFlags)
  52. {
  53. if (foundPrefix)
  54. {
  55. result.Tokens.Add(rule.PreceedingToken);
  56. if (string.Equals(rule.Token, flag, StringComparison.OrdinalIgnoreCase))
  57. {
  58. format = flag;
  59. result.Tokens.Add(rule.Token);
  60. }
  61. break;
  62. }
  63. foundPrefix = string.Equals(flag, rule.PreceedingToken, StringComparison.OrdinalIgnoreCase);
  64. }
  65. result.Is3D = foundPrefix && !string.IsNullOrEmpty(format);
  66. result.Format3D = format;
  67. }
  68. return result;
  69. }
  70. }
  71. }