Format3DParser.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Linq;
  3. using Emby.Naming.Common;
  4. namespace Emby.Naming.Video
  5. {
  6. /// <summary>
  7. /// Parste 3D format related flags.
  8. /// </summary>
  9. public class Format3DParser
  10. {
  11. private readonly NamingOptions _options;
  12. /// <summary>
  13. /// Initializes a new instance of the <see cref="Format3DParser"/> class.
  14. /// </summary>
  15. /// <param name="options"><see cref="NamingOptions"/> object containing VideoFlagDelimiters and passes options to <see cref="FlagParser"/>.</param>
  16. public Format3DParser(NamingOptions options)
  17. {
  18. _options = options;
  19. }
  20. /// <summary>
  21. /// Parse 3D format related flags.
  22. /// </summary>
  23. /// <param name="path">Path to file.</param>
  24. /// <returns>Returns <see cref="Format3DResult"/> object.</returns>
  25. public Format3DResult Parse(string path)
  26. {
  27. int oldLen = _options.VideoFlagDelimiters.Length;
  28. var delimiters = new char[oldLen + 1];
  29. _options.VideoFlagDelimiters.CopyTo(delimiters, 0);
  30. delimiters[oldLen] = ' ';
  31. return Parse(new FlagParser(_options).GetFlags(path, delimiters));
  32. }
  33. internal Format3DResult Parse(string[] videoFlags)
  34. {
  35. foreach (var rule in _options.Format3DRules)
  36. {
  37. var result = Parse(videoFlags, rule);
  38. if (result.Is3D)
  39. {
  40. return result;
  41. }
  42. }
  43. return new Format3DResult();
  44. }
  45. private static Format3DResult Parse(string[] videoFlags, Format3DRule rule)
  46. {
  47. var result = new Format3DResult();
  48. if (string.IsNullOrEmpty(rule.PrecedingToken))
  49. {
  50. result.Format3D = new[] { rule.Token }.FirstOrDefault(i => videoFlags.Contains(i, StringComparer.OrdinalIgnoreCase));
  51. result.Is3D = !string.IsNullOrEmpty(result.Format3D);
  52. if (result.Is3D)
  53. {
  54. result.Tokens.Add(rule.Token);
  55. }
  56. }
  57. else
  58. {
  59. var foundPrefix = false;
  60. string? format = null;
  61. foreach (var flag in videoFlags)
  62. {
  63. if (foundPrefix)
  64. {
  65. result.Tokens.Add(rule.PrecedingToken);
  66. if (string.Equals(rule.Token, flag, StringComparison.OrdinalIgnoreCase))
  67. {
  68. format = flag;
  69. result.Tokens.Add(rule.Token);
  70. }
  71. break;
  72. }
  73. foundPrefix = string.Equals(flag, rule.PrecedingToken, StringComparison.OrdinalIgnoreCase);
  74. }
  75. result.Is3D = foundPrefix && !string.IsNullOrEmpty(format);
  76. result.Format3D = format;
  77. }
  78. return result;
  79. }
  80. }
  81. }