redruin1/factorio-draftsman

Support for loading fully-qualified Lua files

cameronleger opened this issue · 1 comments

Both Krastorio2 and Space-Exploration (when Krastorio2 is loaded) reference files using a more fully-qualified path like __Krastorio2__/lib/public/data-stages/paths, and this isn't accounted for in

def python_require(mod_list, current_mod, module_name, package_path):
in either the module_name or each filepath within package_path

I believe Krastorio2 on its own will trigger failures for not finding these files in draftsman-update, but I know that both Krastorio2 and Space-Exploration combined will each trigger it.

Normalization of path names actually takes place in normalize_module_name in compatibility/interface.lua; this way it can take place before searching the filesystem as well as the loaded zip files. When a file is required, the procedure is basically:

  1. Normalize the module_name using normalize_module_name; resolve global paths, resolve mod names to paths, convert dots to slashes, etc.
  2. Manipulate package.path to add the location of the current file so the custom searchers can see those locations (this part is finicky)
  3. Call the original require function, which iterates through package.searchers:
    1. First we check to see if the module name is a special file, and supply dummy values
    2. Then we check the zip files via python_require, iterating over package.path
    3. Finally, the regular filesystem searching that Lua uses takes over, as well as package.preload and other, unused stuff

However, there were still some issues when loading Krastorio2. The first quick bug was that I forgot mods could have digits in them, and changed the pattern matching to reflect this. The second quick bug was that dots were replaced to slashes regardless of the type of path; this caused dots in "slash" filepaths to be resolved to slashes when they were actually part of valid folder names. Both of these are fixed for0.9.4.

The third bug was related to relative path loading, which has been a pain in my arse for as long as I've been developing this functionality. Basically, you need to keep track of what file you're currently in in order to correctly resolve local requires, which is very difficult to do and currently only functions due to a large amount of wrapped duct tape. You can't query debug.info for the Lua source file because it's run inside python and has "no" source files, so managing exactly where you are has to be handled by Draftsman itself. This is further compounded by the fact that Draftsman allows files to be loaded from both zip archive and folder filesystem simultaneously.

Currently this is patched and should work (tentatively), both Krastorio2 alone and Krastorio2 + Space Exploration. (Space Exploration works alone as well from previous testing.) However, this will likely cause breakages in the future until I develop an exacting procedure to manage the currently loaded file and directory.