cwtools / cwtools-vscode

A VS Code extension providing language server support for paradox script files using cwtools
Other
88 stars 12 forks source link

filepath checks if file exists, but not if it matches criteria #81

Closed yakau closed 1 year ago

yakau commented 1 year ago

I've been having trouble with filepath, so I read your doc, and then ran a test:

Config File:

## cardinality = 1..2
texture = filepath[some/path/,.ext]

Game File:

building_example = {
    texture = common/decisions/000_decisions_help.txt
    texture = gui/goods_panel.gui
    texture = foo

Result:

All of the texture files should fail, because none of them match the permitted file's path or extension.

The cardinality fails correctly ("too many texture") "texture = foo" fails correctly ("foo.ext not found")

However, the first two texture values pass, when they should have failed.


It appears that filepath is correctly checking the existence of the file, but is not checking if it matches the criteria [in here].

I looked through cwtools-vscode and the Vic3/CK3/IR/Stellaris configs to see what filepath[] was expecting, but I couldn't find much relevant outside of the doc.

Your changelog for v0.8.3 mentions * Add configu rule support for "filepath[prefix]" - That doesn't seem to be working for me here. Any ideas?

I'm running Vic3 through Steam. This is my first time cloning a repository, using VSCode or CWTools. I'm not a coder. Please forgive me if I've made a dumb error.

tboby commented 1 year ago

You're right, it does behave weirdly.

From memory those options were written as ways to make it more general, rather than more strict, to match the game engine's ability to generally find the right file even if you left off the full path or the extension.

So it's checking all of:

    let checkFilepathField (files : Collections.Set<string>) (ids : StringTokens) (prefix : string option) (extension : string option) (leafornode) errors =
        let key = getOriginalKey ids
        let file = (trimQuote key).Replace("\\","/")
        let file2 = file.Replace(".lua",".shader").Replace(".tga",".dds")
        let file = if extension.IsSome then file + extension.Value else file
        match prefix with
        | Some pre ->
            if files.Contains file || files.Contains (pre + file) || files.Contains file2 || files.Contains (pre + file2) then errors else inv (ErrorCodes.MissingFile file) leafornode <&&&> errors
        | None ->
            if files.Contains file || files.Contains file2 then errors else inv (ErrorCodes.MissingFile file) leafornode <&&&> errors

If you wanted to work around this, you could try making a type with the type_per_file setting. One benefit of this would be that "go to definition" would take users to the file (which filepath probably wouldn't do).

yakau commented 1 year ago

When I browsed through the "type/subtype" section in your guidance doc, my brain melted. :-0

I'll give it another go, now that I've got something specific to solve. :)

Would it be possible for me to grab all of the file names from a directory (eg. some/path/ *.ext) and dump them into a complex enum so that

These 3 would be valid selections: some/path/foo.ext some/path/foo2.ext some/path/foo3.ext

And this would not: other/path/wrong.ini

I'm not sure how I'd go about scraping file names.

tboby commented 1 year ago
    type[diplomacy_economy] = {
        path = "some/path"
        type_per_file = yes
    }

Type is more powerful than complex enum here I think

yakau commented 1 year ago

From memory those options were written as ways to make it more general, rather than more strict, to match the game engine's ability to generally find the right file even if you left off the full path or the extension.

That makes sense. I ran another test:

texture = filepath[common/decisions/,.txt]

texture = common/decisions/000_decisions_help.txt
texture = common/decisions/000_decisions_help
texture = 000_decisions_help.txt
texture = 000_decisions_help    
texture = 000_decisions_help.foo

The first four were all passed okay, and the last one failed. I see what it's doing now. Not a bug after all. Thanks, mate.