Getting a LuaPlayer instance on a headless server

Place to get help with not working mods / modding interface.
Post Reply
ignatio
Long Handed Inserter
Long Handed Inserter
Posts: 87
Joined: Mon Jun 27, 2016 3:59 pm
Contact:

Getting a LuaPlayer instance on a headless server

Post by ignatio »

I'm working on a mod that only extracts some data from the game using game.write_file. I'd prefer to run it headless, so I can quickly start the game from the command line, run my mod code, and exit again.

Now, my problem is that some API's require a connected player, i.e. a LuaPlayer instance. If I load savegame I can find the player in it as the single entry in game.players, but it's not connected so many functions don't work. So, is there a way to simulate a connected player?

Which API's do I want to use? It's LuaPlayer.request_translations (or alternatively request_translation). So, alternatively, is there was a way to do translations on a headless server without connected players?

(And yes, I'm well aware game.write_file takes a LocalisedString so I can get translations directly in the output, but that's not enough for my use case - I need the translations in my lua code so I can process them a bit before writing the files.)

Thanks!

Bilka
Factorio Staff
Factorio Staff
Posts: 3140
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Getting a LuaPlayer instance on a headless server

Post by Bilka »

You cannot simulate a LuaPlayer.

Depending on what locale you're looking for, the --dump-prototype-locale command line option might do the job.

In case that doesn't have the locale you need: What processing are you doing? There might be a way to do that with LocalisedStrings that someone could try to help you with. (Rseding notes that writing the localised string to a file might not work in headless without a player connected, so maybe give that a try before explaining your processing.)
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

ignatio
Long Handed Inserter
Long Handed Inserter
Posts: 87
Joined: Mon Jun 27, 2016 3:59 pm
Contact:

Re: Getting a LuaPlayer instance on a headless server

Post by ignatio »

Thanks for the quick reply!

I'm doing various sorts of work on the translations, e.g. checking for missing ones* and constructing fallbacks, producing descriptions from them where I clean up clunky language (which is entirely English centric, but that's ok for me), etc. Sure, I could split up my code into a pre-translation stage that runs in-game and a post-translation stage that runs on the written files outside the game, but that'd make the whole thing a lot more complicated.

*) I've found that most, possibly all, translations on the form {"resource-category-name.<xxx>"} and {"recipe-category-name.<xxx>"} in LuaResourceCategoryPrototype.localised_name and LuaRecipeCategoryPrototype.localised_name are missing, at least in the en locale. I guess because they're not actually used anywhere in normal gameplay.

[Edit: Corrected category objects.]

Bilka
Factorio Staff
Factorio Staff
Posts: 3140
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Getting a LuaPlayer instance on a headless server

Post by Bilka »

Yeah, that sounds like you either need to split up your code or use non-headless because you need the connected player for request_translation.

Sidenote, I hope you are aware of game.check_prototype_translations().
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

ignatio
Long Handed Inserter
Long Handed Inserter
Posts: 87
Joined: Mon Jun 27, 2016 3:59 pm
Contact:

Re: Getting a LuaPlayer instance on a headless server

Post by ignatio »

Actually, I think I can't split the code in just one pre-translation and one post-translation stage, even if I wanted to. The reason is that I want to convert rich text tags in the translated strings to plain text.

E.g. I may end up with the string "Filter dirty water [item=iron-ore]" after translation, and I want to replace that [item=iron-ore] tag with the translation for item-name.iron-ore. That means I have to process the translations recursively, so two strict stages just won't work.

So it would be nice to be able to translate without a connected player. If I need to explicitly say which locale to use then no problem - I'd actually prefer that.

Nevertheless, I realise this is a rather niche use case.

[Edit: Sure, I can always implement the translation myself outside the game.]

Post Reply

Return to “Modding help”