Format3DParser.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. var delimeters = _options.VideoFlagDelimiters.ToList();
  16. delimeters.Add(' ');
  17. return Parse(new FlagParser(_options).GetFlags(path, delimeters.ToArray()));
  18. }
  19. internal Format3DResult Parse(string[] videoFlags)
  20. {
  21. foreach (var rule in _options.Format3DRules)
  22. {
  23. var result = Parse(videoFlags, rule);
  24. if (result.Is3D)
  25. {
  26. return result;
  27. }
  28. }
  29. return new Format3DResult();
  30. }
  31. private static Format3DResult Parse(string[] videoFlags, Format3DRule rule)
  32. {
  33. var result = new Format3DResult();
  34. if (string.IsNullOrEmpty(rule.PreceedingToken))
  35. {
  36. result.Format3D = new[] { rule.Token }.FirstOrDefault(i => videoFlags.Contains(i, StringComparer.OrdinalIgnoreCase));
  37. result.Is3D = !string.IsNullOrEmpty(result.Format3D);
  38. if (result.Is3D)
  39. {
  40. result.Tokens.Add(rule.Token);
  41. }
  42. }
  43. else
  44. {
  45. var foundPrefix = false;
  46. string format = null;
  47. foreach (var flag in videoFlags)
  48. {
  49. if (foundPrefix)
  50. {
  51. result.Tokens.Add(rule.PreceedingToken);
  52. if (string.Equals(rule.Token, flag, StringComparison.OrdinalIgnoreCase))
  53. {
  54. format = flag;
  55. result.Tokens.Add(rule.Token);
  56. }
  57. break;
  58. }
  59. foundPrefix = string.Equals(flag, rule.PreceedingToken, StringComparison.OrdinalIgnoreCase);
  60. }
  61. result.Is3D = foundPrefix && !string.IsNullOrEmpty(format);
  62. result.Format3D = format;
  63. }
  64. return result;
  65. }
  66. }
  67. }