Bug bug + feature: adding a way to assert that a path starts with ROOT_LOCATION

fredg1

Member
Suppose you have two files.

<root>/scripts/my_script.ash
<root>/scripts/a_folder/however_many_layers_you_want/scripts.ash

It is currently impossible to point to the former without using an absolute path, as submitting "scripts/my_script.ash" to KoLmafiaCLI.findScriptFile( string ) will always find both.
On top of that, even if the latter didn't exist, even once the former is found, the method will still scan the rest of your files for potential matches. If you have a lot of folders, this can take some time (this is why this thread is also a feature request, in a way. Fixing/implementing this would also act as a way to speed up scripts).

The solution would be a way to indicate to KoLmafiaCLI.findScriptFile( string ) that it only needs to search in the root location.



I think we should use characters not allowed in file names (i.e. \ / : * ? " < > | ) to achieve this.
These characters are not allowed in file names because, AFAIK, they are used in command lines to indicate high priority operations (i.e. a priority higher than "string").


I gave it a go, using the asterisk (*), but feel free to change this is you think of something more suitable.
 

Attachments

  • KoLmafiaCLI.java.patch
    4.1 KB · Views: 1

philmasterplus

Active member
Does ASH support relative path imports (e.g. import "../foo/bar/stuff.ash";)? If not, that could be an alternative for bypassing the normal script lookup logic.
 

fronobulax

Developer
Staff member
A problem with the script finding is that the same code has several use cases, specifically 1) find something typed in a command line; 2) find something used in an import; and 3) find something called at runtime.

I am not smart enough to have found a solution that is smarter about things that might be duplicates but are in different directories but doesn't break existing code. Perhaps smart is not the right word because I can fix it but the cost in my time is higher than I want to pay and the benefit is low to me personally.

It should be noted that the assumption that file names are unique across the entire allowed script directory tree is pretty deeply embedded and eliminating the special exceptions that make JS development easier is also a solution :)
 

fredg1

Member
A problem with the script finding is that the same code has several use cases, specifically 1) find something typed in a command line; 2) find something used in an import; and 3) find something called at runtime.
I can assert that what I did in the patch I submitted helps for n°2, without breaking n°1 or n°3.
This is because I went straight for KoLmafiaCLI.findScriptFile, which doesn't alter the string it receives in any way/use it in any way other than search for that exactly-named file.

Now, what I don't know is if the two other cases you mention do anything to the input they receive before sending it to KoLmafiaCLI.findScriptFile.

So TL;DR:
1- I can confidently say that what I did will not break anything.
2- I can confidently say that what I did will work with scripts' import statements.
3- I do not know for certain if what I did will make any difference in all the other situations, because I would need to dig deeper into if the input they (the other situations/contexts that expect a file name) receive gets altered before being sent to KoLmafiaCLI.findScriptFile.
 

lostcalpolydude

Developer
Staff member
Does ASH support relative path imports (e.g. import "../foo/bar/stuff.ash";)? If not, that could be an alternative for bypassing the normal script lookup logic.
Given that the code assumes scripts have unique names, and also given the effort to prevent any access to files outside of KoLmafia's directories, my first assumption would be that any leading ../ is stripped away before further processing. I know I added something like that in at least one place, forever ago. I haven't looked at any of this code in a long time though.
 
Top