Page 1 of 1

Base of "required" files in different environments

Posted: Fri May 01, 2020 9:16 pm
by Pi-C
I've got a bug report against Bio Industries for a crash when the player wants to start a mini tutorial from the main game. I caught this in my log file:

Code: Select all

  41.098 Loading map /home/pc/GOG_Games/Factorio/test/saves/Maps_test.zip: 1895648 bytes.
  41.109 Loading level.dat: 2138610 bytes.
  41.114 Info Scenario.cpp:187: Map version 0.18.22-1
  41.117 Info PrototypeMigrationList.cpp:61: Activating migration Bio_Industries/Bio_Industries_0.17.19.json
  41.117 Info PrototypeMigrationList.cpp:61: Activating migration Bio_Industries/Bio_Industries_0.18.01.json
  41.202 Loading script.dat: 365 bytes.
  41.204 Checksum for script /home/pc/GOG_Games/Factorio/test/temp/currently-playing/control.lua: 2466797803
  41.220 Checksum for script __Bio_Industries__/control.lua: 1707007530
  41.223 Applying migration: Bio Industries: Bio_Industries_0.17.02.lua
[…]
 112.496 Supending game state
 112.496 Entering tutorial: [base]stack-transfers
 112.504 Loading level.dat: 1185316 bytes.
 112.504 Info Scenario.cpp:187: Map version 0.18.1-0
 112.506 Info PrototypeMigrationList.cpp:61: Activating migration Bio_Industries/Bio_Industries_0.17.19.json
 112.506 Info PrototypeMigrationList.cpp:61: Activating migration Bio_Industries/Bio_Industries_0.18.01.json
 112.568 Checksum for script /home/pc/GOG_Games/Factorio/test/temp/currently-playing-tutorial/control.lua: 3313316706
 112.569 Applying migration: Base Mod: 2020-01-27_Factorio_0.18.02.lua
 112.570 Applying migration: Base Mod: 2020-03-18_Factorio_0.18.14.lua
 112.572 Applying migration: Bio Industries: Bio_Industries_0.17.02.lua
 112.635 Error AppManager.cpp:687: Failed to load tutorial: Error while applying migration: Bio Industries: Bio_Industries_0.17.02.lua

__Bio_Industries__/migrations/Bio_Industries_0.17.02.lua:1: module common not found;  no such file __Bio_Industries__/migrations/common.lua no such file __core__/lualib/common.lua
stack traceback:
	[C]: in function 'require'
	__Bio_Industries__/migrations/Bio_Industries_0.17.02.lua:1: in main chunk
I use a file "__Bio_Industries__/common.lua" for some functions I want to access in the data stage, during migration, and from control.lua. This is the offending line from __Bio_Industries__/migrations/Bio_Industries_0.17.02.lua:

Code: Select all

local BioInd = require('common')
This works as expected most of the time: require('common') will look for a file relative to the mod's root dir, and as you can see in the log snippet, the file is parsed correctly when a saved game is loaded. However, this breaks once I try to start a tutorial. It seems to look for the required file only in the current directory and in a location relative to the core game's root dir. Is that behavior intended?

For me, this is bad news because I use such "common.lua" files in several mods. The obvious way to solve this problem would be to create a link from __Bio_Industries__/migrations/common.lua to __Bio_Industries__/common.lua . However, when i zip the folder, the link is replaced with a duplicate of the file. This takes up more space -- and it may cause problems in the future if I unpack the zip file and make changes in only one copy of that file. So is there any better solution?

Another problem I noticed while looking over the log again: Is there a way to enforce an order for the execution of migration files? It doesn't make sense that the migration files for 0.18 are run before the files for 0.17, and I imagine it actually could cause problems.

Re: Base of "required" files in different environments

Posted: Fri May 01, 2020 9:42 pm
by Squelch
Just a punt at this, and please excuse me if I'm wrong.
112.496 Supending game state
sic
seems to indicate that the normal game state and therefore environment has been left and a new environment specific to the tutorial has been spawned. The only suggestion I might offer is to use a migration script to add a "require" and the path to your common script to the Stack-transfers tutorial. Quite how that is possible I'm not sure as I run out of talent although there's something at the back of my mind that says it is.

[Edit]
Perhaps a LuaRemote script interface could work?

Re: Base of "required" files in different environments

Posted: Fri May 01, 2020 9:50 pm
by Klonan
I don't think this is related to tutorials

The base game runs lua migrations with migrations folder as the package path

Code: Select all

  try
  {
    ScopedBoolSetter guard1(script->allowRequire, true);
    // Preserve the original CRC so runtime the CRC matches as if the migration never ran.
    ScopedValueSetter guard2(script->crc, script->crc);
    script->runMigration(mod->getMigrationsPath() / Filesystem::Path(name));
  }
  catch (const std::runtime_error& error)
  {
    throw ScriptException(ssprintf("Error while applying migration: %s: %s\n\n%s", mod->title.c_str(), name.c_str(), error.what()));
  }

Re: Base of "required" files in different environments

Posted: Fri May 01, 2020 9:54 pm
by Pi-C
Squelch wrote: Fri May 01, 2020 9:42 pm The only suggestion I might offer is to use a migration script to add a "require" and the path to your common script to the Stack-transfers tutorial.
Thanks, that was the pointer I needed! I know that my mod is loaded, and I know where its root dir is, so I just add this:

Code: Select all

local BioInd = require('__Bio_Industries__/common')
[Edit]
Perhaps a LuaRemote script interface could work?
Too complicated, replacing the path is way shorter. :-D

Re: Base of "required" files in different environments

Posted: Fri May 01, 2020 10:11 pm
by Pi-C
Klonan wrote: Fri May 01, 2020 9:50 pm I don't think this is related to tutorials

The base game runs lua migrations with migrations folder as the package path
Well, the environment seems to change! Let me pull just the relevant lines from the log:

Code: Select all

 112.496 Supending game state
 112.496 Entering tutorial: [base]stack-transfers

 112.572 Applying migration: Bio Industries: Bio_Industries_0.17.02.lua
 112.635 Error AppManager.cpp:687: Failed to load tutorial: Error while applying migration: Bio Industries: Bio_Industries_0.17.02.lua

__Bio_Industries__/migrations/Bio_Industries_0.17.02.lua:1: module common not found;  no such file __Bio_Industries__/migrations/common.lua no such file __core__/lualib/common.lua
Please note the last line especially!

Moreover, the crash happens with

Code: Select all

require('common')
but not with

Code: Select all

require('__Bio_Industries__/common')
Apparently, when the tutorial is entered, root is moved down from / to /migrations (because require is relative to the root dir).

About your code: Sorry, that's not really helpful. I've played around a bit with C, but that was in the early 1990s, so I don't quite get it anymore. :-D

Re: Base of "required" files in different environments

Posted: Fri May 01, 2020 10:17 pm
by Squelch
Pi-C wrote: Fri May 01, 2020 9:54 pm
Squelch wrote: Fri May 01, 2020 9:42 pm The only suggestion I might offer is to use a migration script to add a "require" and the path to your common script to the Stack-transfers tutorial.
Thanks, that was the pointer I needed! I know that my mod is loaded, and I know where its root dir is, so I just add this:

Code: Select all

local BioInd = require('__Bio_Industries__/common')
Woohoo, sometimes fresh eyes are all that is needed. I've also learned something today too. Thanks.
[Edit]
Perhaps a LuaRemote script interface could work?
Too complicated, replacing the path is way shorter. :-D
That was my fallback suggestion made in the heat of the moment in case the simple one (that I couldn't remember) didn't work :D

Re: Base of "required" files in different environments

Posted: Fri May 01, 2020 10:55 pm
by Pi-C
Squelch wrote: Fri May 01, 2020 10:17 pm Woohoo, sometimes fresh eyes are all that is needed. I've also learned something today too. Thanks.
Turns out the tutorials cause even more trouble. I'll have to check all migration scripts of all my mods now, already found some crashes due to undefined tables. Oh, that's gonna be a lot of fun -- boohooo! :(