jamerst/AutoTag

Error parsing config file when using parsePattern on Windows

livinggreener opened this issue · 3 comments

I'm getting the following error on a Windows machine when trying to use the parsePattern field of the config file. If I set the config file parsePatten field back to "" and use the same pattern instead specified through the command line --pattern option the error is not thrown:
AutoTag v3.1.1
https://jtattersall.net
Error parsing config file 'C:\Users\Administrator\AppData\Roaming\autotag\conf.json'
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at autotag.Core.AutoTagSettings..ctor(String configPath)
at autotag.cli.Program.OnExecuteAsync()
at McMaster.Extensions.CommandLineUtils.Conventions.ExecuteMethodConvention.InvokeAsync(MethodInfo method, Object instance, Object[] arguments)
at McMaster.Extensions.CommandLineUtils.Conventions.ExecuteMethodConvention.OnExecute(ConventionContext context, CancellationToken cancellationToken)
at McMaster.Extensions.CommandLineUtils.Conventions.ExecuteMethodConvention.<>c__DisplayClass0_0.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at McMaster.Extensions.CommandLineUtils.CommandLineApplication.ExecuteAsync(String[] args, CancellationToken cancellationToken)
at McMaster.Extensions.CommandLineUtils.CommandLineApplication.ExecuteAsync[TApp](CommandLineContext context, CancellationToken cancellationToken)
at McMaster.Extensions.CommandLineUtils.CommandLineApplication.Execute[TApp](CommandLineContext context)
at McMaster.Extensions.CommandLineUtils.CommandLineApplication.Execute[TApp](IConsole console, String[] args)
at McMaster.Extensions.CommandLineUtils.CommandLineApplication.Execute[TApp](String[] args)
at autotag.cli.Program.Main(String[] args)

Are you setting the value of parsePattern manually or using the --set-default option?

If you're doing it manually any backslashes in the regex will have to be escaped as double backslashes, otherwise the JSON isn't valid.

In my testing the --set-default option automatically escapes the backslashes.

I was setting parsePattern manually within conf.json using a text editor. I'd escaped the backslashes that repent the Windows directory delimiters but not any that need to be interpreted as RegEx special characters. For the directory structure of:
"\Series-name\Season #\Episode #.mkv"
I was using the pattern:
".*\(?.+)\Season (?\d+)\Episode (?\d+)"

I tried --set-default to see how it wrote my above pattern to the JSON and see now that even the special character backslash needs to be escaped. So, \d must instead be \\d. However, I was surprised to see that any Windows directory delimiters need to be triple-escaped. I was theorizing I might see them be double escaped based on your comment, but not triple escaped. So, \\ must instead be written \\\\. I was also even more surprised to see that a number of other characters were rewritten into their C/C++/Java Unicode versions:
< becomes \u003C
> ... \u003E
+ ... \u002B
So, this is the full pattern that was written to the JSON by using --set-default and --pattern .*\\(?<SeriesName>.+)\\Season (?<Season>\d+)\\Episode (?<Episode>\d+) at the command line:
.*\\\\(?\u003CSeriesName\u003E.\u002B)\\\\Season (?\u003CSeason\u003E\\d\u002B)\\\\Episode (?\u003CEpisode\u003E\\d\u002B)

Now the RegEx pattern within the JSON works without error. It seems the best way to edit the JSON parsePattern field - at least on a Windows machine - is via CLI with --pattern and --set-default rather than manually with a text editor.

The angle brackets also being escaped/encoded has been fixed in v3.1.2. There's a "feature" in System.Text.Json that escapes some characters to prevent XSS attacks by default.

Obviously that feature is useless in this application, so it's now disabled.