Format3DParser.cs 2.4 KB

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