Format3DParser.cs 2.5 KB

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