Page 1 of 1

copy_file() function.

Posted: Wed Oct 10, 2018 9:17 pm
by credomane
We have write_file() that let mod authors write a string to a output file and we have remove_path that lets us delete a file/directory. I know devs are against read_file for deterministic reasons and I'm completely ok with that but can we get a copy_file function? To keep determinism working the function always returns nil so there is no way to determine if the copy succeeded or failed or what, if any, data is in the source file; other than browsing to script_output outside the game and checking manually.

This way I and others can just copy a file directly and get rid of all the heredoc + write_file() nonsense I currently have to do. This would be really helpful in exporting a bunch of static content from my mod (html, css, js, images) and then writing a small config file. Currently I have to turn each static file in to a heredoc which gets tedious when I update them and tricky when it is an image or other binary data.

Code: Select all

copy_file(source, destination[, mod_name[, by_player]])
By default, source and destination operate on the script_output folder just like write_file does but if you include the optional mod_name (default is nil) then source operates from "root" directory of the specified mod (the one where info.json is in) and destination still goes to the script_output folder. If the source file cannot be found it is a silent failure. If the source is a folder then copy the entire folder to destination. by_player is the same as by_player on write_file and other functions with the parameter.

Re: copy_file() function.

Posted: Wed Oct 10, 2018 9:25 pm
by L0laapk3
+1, would be a very useful feature

Re: copy_file() function.

Posted: Wed Oct 10, 2018 9:56 pm
by Rseding91
I could make a copy_file/move_file but I don't see the utility of it.

The *vast* majority of mods will be zipped which prevents copying files into them while the game is running.

Re: copy_file() function.

Posted: Thu Oct 11, 2018 12:47 pm
by credomane
I don't want to copy files into the zip files. I want to copy files out. Copying files into the zip file or an unzip mod's folder would just be asking for deterministic problems. write_file("./something.lua", data); copy the file back into the mod then include it. That is just asking for trouble and the reason read_file will never be a thing. That isn't what I'm looking for at all.

Let's take my FactorioMaps mod as an example. I've been working on a complete rewrite (taking way too long but RL problems. L0laapk3's update is awesome, btw) and in my rewrite I have a folder named "web".

The structure of my zip file/mod folder looks something like:

./info.json
./control.lua
./webfiles.lua
./genwebfiles.py
./web/index.html
./web/README.txt
./web/lib/main.css
./web/lib/leaflet.js
./web/lib/leaflet.css
./web/lib/factoriomap.js
./web/lib/images/8-9 images.png
./web/tools/rescale.py

Now I would love to do something like copy_file("web/index.html", "FactorioMaps/<player chosen name>/index.html", __FACTORIOMAPS__, "credomane"); so that the index.html inside my mod's zip gets copied to the script_output folder as "./FactorioMaps/map1/index.html"

Currently what I'm having to do is generate an extra large lua file webfiles.lua that contains all these files as heredocs and/or as base64 strings by running genwebfiles.py myself. Then "exporting" them by calling write_file a whole bunch. Sure the way I do it now works and will continue to work but it would be so much nicer to not have to do this extra manual step of generating webfiles.lua everytime I change/update a file inside the web folder.

Do I need copy_file()? No...not really but it would be oh, so convenient to have.