Calling arbitrary scripts and passing information between them

rlbond86

Member
I'm trying to execute a script whose name is known at runtime. So far the only way I know to do this is by saying

Code:
cli_execute(filename);

but there's a problem. I want to pass some information to this script. So far I've come up with two similar ways to do this, but I'd like to know if anyone knows another method.

The first way is to store this data in some KoLmafia properties. The called script imports the original script, which loads these properties into some global variables, which the called script can use. This works well but spams the CLI quite a bit.

The second way is to use file_to_map() and map_to_file() to do the same thing. However, I am concerned that this might do some harm to users with SSDs, since this script will be called quite a bit. However, if set_property and get_property write to disk as well, I guess this is preferable?

I had thought of using static variables, but apparently they are static only to the scope of the main executing script. If script1 imports script2 with a static variable, script1 gets its own copy of the variable.

I've also tried doing
Code:
cli_execute(filename + " " + paramString);
, but this doesn't work either because paramString contains semicolons which mess up the argument passing.

Does anyone know any other ways?
 
Last edited:

Theraze

Active member
If the scripts are written in a way that they're meant to be used externally, you could do import <filename> at the top of your script and then call the functions you're wanting directly. Many of the scripts on this site work well like this.

If the scripts ask for multiple variables... you're stuck. Calling a script with a variable only works for single-variable executions. Unless you're wanting to automate outside of mafia, and then AutoIt and many other options will allow your automation desires to be met.
 

rlbond86

Member
If the scripts are written in a way that they're meant to be used externally, you could do import <filename> at the top of your script and then call the functions you're wanting directly. Many of the scripts on this site work well like this.

The problem is that "filename" is not known until runtime. I was under the impression that the import command must use a literal (like most languages)
 

xKiv

Active member
how about:
Code:
cli_execute('ashq import<' + filename + '>; main("' + parameter +'");');
?

(note the quotes *and* apostrophes around +parameter+)
 

Theraze

Active member
No, because when you're importing, main would conflict as a matter of course. If you're needing main specifically, you need to replicate its internals either inside the original script and call the new function, or just put all of its function calls into your (post-original-file-import) new script.
 

rlbond86

Member
how about:
Code:
cli_execute('ashq import<' + filename + '>; main("' + parameter +'");');
?

(note the quotes *and* apostrophes around +parameter+)

This would still fail if the string contains a " or ' character. I mean, it could be possible to escape those characters, but things would start to get hairy...
 

rlbond86

Member
how about:
Code:
cli_execute('ashq import<' + filename + '>; main("' + parameter +'");');
?

(note the quotes *and* apostrophes around +parameter+)

Actually, this works great if I do

Code:
cli_execute('ashq import<' + filename + '>; func(url_decode("' + url_encode(parameter) +'"));');

So thanks!
 
Last edited:

holatuwol

Developer
Couldn't you use something like zlib to setup properties that are read in by whatever it is that you're calling?
 

Paragon

Member
I did something similiar when i did the remote ash scripts, I read the whole script as a string, then used a regex to see what parameters main took, then dynamically rewrote the first few lines of the main function so that it was parameterless and added lines at the top of the main function to set the values... the whole thing was working pretty well, the whole thing was working pretty well until I ran into an issue with multiple imported libs importing the same tertiary lib... I would just need to have written some logic to defer lib loading until the proper sequence of lib loading could be determined.... but I took a break from it before I got that done.
 
Top