Comunicating data.lua and control.lua

Place to get help with not working mods / modding interface.
Post Reply
ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Comunicating data.lua and control.lua

Post by ficolas »

How can I comunicate the data.lua and the control.lua?

Also, can I use the file io to write in a file inside the mod folder? If so, how can I get the path?

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Comunicating data.lua and control.lua

Post by FreeER »

ficolas wrote:How can I comunicate the data.lua and the control.lua?

Also, can I use the file io to write in a file inside the mod folder? If so, how can I get the path?
how do you mean communicate? make it so that your mod does not load if the base mod is not first? If so I imagine you could use some form of io.read in data.lua to check if base is the first one if not then to add it first and remove it from further down (it's in the form of a table if you haven't noticed lol). I'll test and then edit but...

as to writing a file to the mod folder I've used f=io.open("filename.ext", "w") or a+ instead of w for append, and then you can just use f:write(print("whatever you want to show up in the file")) then close the file when you are finished with f:close() as to the path I'm almost sure that if you use the commands from the data/control.lua it create the file within the same location (though it might put it in the same place as running them from console which is where the factorio executable is factorio\bin\x64 for me, if it does create it in the bin folder then just use ../../mods/mod-name/file-name.ext to save it where you want it)
edit1: io.open from data.lua creates the file in the bin folder so.. you'll have to use ../.. to get to the mods folder
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me :)
Or drop into #factorio on irc.esper.net

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: Comunicating data.lua and control.lua

Post by ficolas »

../../mods/F-mod?

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Comunicating data.lua and control.lua

Post by FreeER »

yes for example
(I did this in console but it should work in data.lua or any other lua file actually)
f=io.open("../../mods/F-mod/f-mod.txt", "w") --remember case sensitivity
f:write(print("this is a test"))
f:close()
now go read the file :)

I got f=io.open("../../mods/mod-list.json", "w")
modlist = f:read("*a")
f:close()
to store the mod-list.json file into modlist but as to a good way to check if base is first idk lol
should be easy enough to resave it to the file
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me :)
Or drop into #factorio on irc.esper.net

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Comunicating data.lua and control.lua

Post by FreeER »

A tiny bit of testing, emphasis on tiny about to leave for my next class :(, and this might be able to work for checking mod-list.json (until it gets fixed in the next update lol)

Code: Select all

f=io.open("../../mods/mod-list.json")
modlist = f:read("*a")
basecheck=[[{
    "mods":
    [
        {
            "name": "base",
            "enabled": "true"
        },]]
if string.sub(modlist, 0, 104) ~= basecheck then
string.gsub(modlist, [[{
            "name": "base",
            "enabled": "true"
        },]], "")
        modlist=string.sub(modlist,0,23)..[[{
            "name": "base",
            "enabled": "true"
        },]]..string.sub(modlist,24)
f:write(print(modlist))
        end
f:close()
I think the main issue would be the comma at the end of the basecheck string because if base is the last entry then the , would not exist and it might cause issues lol

I'm sure there is a better way (prettier even) but this is what I came up with in that time frame lol
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me :)
Or drop into #factorio on irc.esper.net

kovarex
Factorio Staff
Factorio Staff
Posts: 8078
Joined: Wed Feb 06, 2013 12:00 am
Contact:

Re: Comunicating data.lua and control.lua

Post by kovarex »

This is nice temporary solution (if it works), but we will disable i/o operations in the lua in the future as this is huge security hole.
P.S. Tomas just made the mod dependencies, so they will be available from 0.4.

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: Comunicating data.lua and control.lua

Post by ficolas »

kovarex wrote:This is nice temporary solution (if it works), but we will disable i/o operations in the lua in the future as this is huge security hole.
P.S. Tomas just made the mod dependencies, so they will be available from 0.4.
:S so how will I make my scoreboard for the rocket gamemode?

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Comunicating data.lua and control.lua

Post by FreeER »

yeah, I was kind of assuming you would do that eventually :) If any of the modders had wanted to be mean they could have used factorio to launch a format script lol. os.execute probably needs to be removed as well. Though you could perhaps allow io operations within the individual mod directory just not outside of it maybe, to allow for logfiles or whatever lol. I was actually looking into creating a mod that would guide people through creating a mod (and create examples using io), but I can't seem to find a good way to delay the messages so... back to the original plan
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me :)
Or drop into #factorio on irc.esper.net

User avatar
rk84
Filter Inserter
Filter Inserter
Posts: 556
Joined: Wed Feb 13, 2013 9:15 am
Contact:

Re: Comunicating data.lua and control.lua

Post by rk84 »

kovarex wrote:This is nice temporary solution (if it works), but we will disable i/o operations in the lua in the future as this is huge security hole.
P.S. Tomas just made the mod dependencies, so they will be available from 0.4.
how will it react if mod is not found. Can I have optional dependencies?
Test mode
Searching Flashlight
[WIP]Fluid handling expansion
[WIP]PvP gamescript
[WIP]Rocket Express
Autofill: The torch has been pass to Nexela

kovarex
Factorio Staff
Factorio Staff
Posts: 8078
Joined: Wed Feb 06, 2013 12:00 am
Contact:

Re: Comunicating data.lua and control.lua

Post by kovarex »

rk84 wrote:Can I have optional dependencies?
Yes.
You can also specify condition of the mod version you depend on ... more info in the 0.4 update.

slpwnd
Factorio Staff
Factorio Staff
Posts: 1835
Joined: Sun Feb 03, 2013 2:51 pm
Contact:

Re: Comunicating data.lua and control.lua

Post by slpwnd »

rk84 wrote:
kovarex wrote:This is nice temporary solution (if it works), but we will disable i/o operations in the lua in the future as this is huge security hole.
P.S. Tomas just made the mod dependencies, so they will be available from 0.4.
how will it react if mod is not found. Can I have optional dependencies?
The changes will be as follows:

1) Mod can have dependencies on other mods
2) Dependency can be vague (name of other mod) or parametrized by the version (= < > <= >=)
3) Dependencies can be optional (dependency will be checked only if the other mod is enabled and has valid dependencies)
4) Mods ordering will be specified primarily by the dependencies and secondarily by the name
Custom ordering will not be possible anymore (would cause issues with replay, multiplayer, etc.)

drs9999
Filter Inserter
Filter Inserter
Posts: 831
Joined: Wed Mar 06, 2013 11:16 pm
Contact:

Re: Comunicating data.lua and control.lua

Post by drs9999 »

Sounds great!

But what is about "not optional" dependencies? I think about (optional) mod-addons. I think I should use an example to point it out more clearly.

I.e. I move the fertilizer-production-line into an optional addon for the treefarm-mod. Obviously the addon needs the "root" treefarm-mod.
What will happen if the user forgot to install the treefarm-mod? Maybe it is not a common scenario, but a possible imo.

slpwnd
Factorio Staff
Factorio Staff
Posts: 1835
Joined: Sun Feb 03, 2013 2:51 pm
Contact:

Re: Comunicating data.lua and control.lua

Post by slpwnd »

In this case the game starts normally, however the dependency for the addon is not satisfied (the treefarm-mod is missing). Therefore the addon mod is marked as not valid (will be red in the mods gui) and is not used in the game.

Basically we added a new mod state "valid". So now the mod can be enabled / disabled (user setting) and valid / invalid (some dependency missing or dependency version not satisfied or dependency not valid or dependency not enabled). Only valid enabled mods are used for the game.

There are small changes in the mod manager gui related to this as well:

Mods that are not valid are marked with red color (as opposed to disabled mods now).
Mods that are not enabled are grey.
Mods always have the same ordering (independent of the valid / enabled setting).
Dependencies are displayed and dependencies causing invalidity are marked with red (so the user can see what is wrong).

drs9999
Filter Inserter
Filter Inserter
Posts: 831
Joined: Wed Mar 06, 2013 11:16 pm
Contact:

Re: Comunicating data.lua and control.lua

Post by drs9999 »

Well-wrought idea! (But honestly I did not expect anything else from you guys ;) )

SilverWarior
Filter Inserter
Filter Inserter
Posts: 559
Joined: Mon Mar 04, 2013 9:23 am
Contact:

Re: Comunicating data.lua and control.lua

Post by SilverWarior »

kovarex wrote:This is nice temporary solution (if it works), but we will disable i/o operations in the lua in the future as this is huge security hole.
P.S. Tomas just made the mod dependencies, so they will be available from 0.4.
If you do so please provide your own function which will encapsulate the low level I/O functionality so that modders could still read or write certain data in their files. Doung this you could esily limit where theese files can bee.

kovarex
Factorio Staff
Factorio Staff
Posts: 8078
Joined: Wed Feb 06, 2013 12:00 am
Contact:

Re: Comunicating data.lua and control.lua

Post by kovarex »

SilverWarior wrote:
kovarex wrote:This is nice temporary solution (if it works), but we will disable i/o operations in the lua in the future as this is huge security hole.
P.S. Tomas just made the mod dependencies, so they will be available from 0.4.
If you do so please provide your own function which will encapsulate the low level I/O functionality so that modders could still read or write certain data in their files. Doung this you could esily limit where theese files can bee.
This is problematic. The game must be deterministic, always. When you are replaying game, everything that changes the game state must be the same the second time. That is the reason why the script shouldn't be able to access anything that could act differently when you replay the game (IO/Network/non-deterministic random number generator etc), this will be especially important in the network game.

What is the reason for the mod to write/read files anyway?

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: Comunicating data.lua and control.lua

Post by ficolas »

kovarex wrote:
SilverWarior wrote:
kovarex wrote:This is nice temporary solution (if it works), but we will disable i/o operations in the lua in the future as this is huge security hole.
P.S. Tomas just made the mod dependencies, so they will be available from 0.4.
If you do so please provide your own function which will encapsulate the low level I/O functionality so that modders could still read or write certain data in their files. Doung this you could esily limit where theese files can bee.
This is problematic. The game must be deterministic, always. When you are replaying game, everything that changes the game state must be the same the second time. That is the reason why the script shouldn't be able to access anything that could act differently when you replay the game (IO/Network/non-deterministic random number generator etc), this will be especially important in the network game.

What is the reason for the mod to write/read files anyway?
For example, Im making a new gamemode where you need to build a rocket to scape the planet, and I want to make a highscore board, but I cant without io or sonething :S

User avatar
rk84
Filter Inserter
Filter Inserter
Posts: 556
Joined: Wed Feb 13, 2013 9:15 am
Contact:

Re: Comunicating data.lua and control.lua

Post by rk84 »

Could player-data.json have place for mods to write/read (via methods) scores and achievements?
Test mode
Searching Flashlight
[WIP]Fluid handling expansion
[WIP]PvP gamescript
[WIP]Rocket Express
Autofill: The torch has been pass to Nexela

slpwnd
Factorio Staff
Factorio Staff
Posts: 1835
Joined: Sun Feb 03, 2013 2:51 pm
Contact:

Re: Comunicating data.lua and control.lua

Post by slpwnd »

rk84 wrote:Could player-data.json have place for mods to write/read (via methods) scores and achievements?
.

Yes that would be possible.

vzybilly
Fast Inserter
Fast Inserter
Posts: 143
Joined: Thu May 14, 2015 6:10 pm
Contact:

Re: Comunicating data.lua and control.lua

Post by vzybilly »

*pokes even more dead thread with stick*
kovarex wrote:This is nice temporary solution (if it works), but we will disable i/o operations in the lua in the future as this is huge security hole.
P.S. Tomas just made the mod dependencies, so they will be available from 0.4.
is there a way we could pass some values or something over to our control scripts from the data scripts side? or maybe even have data.raw as a read only setup? I've seen a fair amount of mods wanting data.raw values in their control script, some hard coding those values in from their personal installs, which is horrible because a mod could change those values leading to a separation...

I could really see having a table per mod that they could save things to, which is passed into control... only simple values (no functions or anything, just string and numbers and such) I could also see a read only data.raw, if it can't be read only, it doesn't really matter too much at that point if it's edited, right? it being contained in that one control script and reset each time the game starts up... maybe a warning/error when it is changed? anyways, I'm going crazy from lack of sleep, good night.
Will code for Food. I also have 11+ mods!

Post Reply

Return to “Modding help”