[2.0.11] Lua Key/Value Tables Don't Work For Some Users.

Place for things which are bugs but we have no idea how to solve them. Things related to hardware, libraries, strange setups, etc.
ChaosGaming_
Manual Inserter
Manual Inserter
Posts: 3
Joined: Sat Oct 26, 2024 11:45 am
Contact:

[2.0.11] Lua Key/Value Tables Don't Work For Some Users.

Post by ChaosGaming_ »

Hello, I'm the author of Unlimited Resources, recently I had a user by the name of Arcanical reach out about an error in the experimental branch. Upon investigating his issue we learned that the lua tables were not having their values assigned despite the function behaving as it should have. Furthermore, upon loading the experimental branch myself and having the same mod instance, which we confirmed by comparing all the code within the files, only Arcanical had the issue.

This was the code that was producing the issue:

Code: Select all

function string:split(inSplitPattern)
    local outResults = {}

    if not self or self == "" then
        return outResults
    end

    local str = ""
    for i = 1, #self do
        local char = self:sub(i, i)
        if char == inSplitPattern then
            outResults[str] = true
            str = ""
        else
            str = str ..char
        end
    end
    outResults[str] = true
    
    return outResults
end
My Output (Correct):

Code: Select all

@Unlimited-Resources-Oil-Refresh-Cargo-Ship-Compat/data-final-fixes.lua:42: InfiniteOreResources (7 items): {
   calcite = true,
   coal = true,
   ["copper-ore"] = true,
   ["iron-ore"] = true,
   stone = true,
   ["tungsten-ore"] = true,
   ["uranium-ore"] = true
}
Arcanical's Output (Incorrect):

Code: Select all

@Unlimited-Resources-Oil-Refresh-Cargo-Ship-Compat/data-final-fixes.lua:42: InfiniteOreResources (7 items): {
   "iron-ore",
   "copper-ore",
   "stone",
   "coal",
   "uranium-ore",
   "tungsten-ore",
   "calcite"
}
These outputs show that the split function was working, but Arcanical's table didn't include the boolean values it should've.

In the end, despite everything we tried, we couldn't figure out why his lua produced different results and sometimes crashed when using an attempted patch that worked fine for me.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14893
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [2.0.11] Lua Key/Value Tables Don't Work For Some Users.

Post by Rseding91 »

Thanks for the report. Do you have a piece of Lua code I can insert into a test mod to try to reproduce this issue?
If you want to get ahold of me I'm almost always on Discord.
ChaosGaming_
Manual Inserter
Manual Inserter
Posts: 3
Joined: Sat Oct 26, 2024 11:45 am
Contact:

Re: [2.0.11] Lua Key/Value Tables Don't Work For Some Users.

Post by ChaosGaming_ »

I attached the version of the mod where the issue was discovered.

This would be how the code block I attached to the original would be used:

Code: Select all

require("scripts.string")

---@type [string]
-- settings.startup["ignored-resources"].value is a string like this "resourceName, resourceName"
local IgnoredResourceIDs = settings.startup["ignored-resources"].value:gsub("%s+", ""):split(",")
Attachments
Unlimited-Resources-Oil-Refresh-Cargo-Ship-Compat_2.1.1.zip
(149.76 KiB) Downloaded 14 times
robot256
Smart Inserter
Smart Inserter
Posts: 1063
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: [2.0.11] Lua Key/Value Tables Don't Work For Some Users.

Post by robot256 »

The folder inside the ZIP you attached has a different name than info.json. Are you sure the user was running the same code as you were? It's possible there was a mixup between zipped and unzipped mods with different versions and names.

The other problem I see is that you are adding a metamethod to the `string` global named `split`, which is a common function. Your implementation produces a map instead of a list. The normal behavior of split is to produce a list of strings, as the other user found. It is conceivable they had another mod installed that overwrote string:split with a different implementation of it between when your mod loaded and when it used the function. (This is another reason factorio-current.log should be included in all bug reports.)

Just speaking from experience, I strongly suspect that moving `split` to a local function, rather than a global metamethod, and fixing the folder/name/version issues will resolve the problem.
ChaosGaming_
Manual Inserter
Manual Inserter
Posts: 3
Joined: Sat Oct 26, 2024 11:45 am
Contact:

Re: [2.0.11] Lua Key/Value Tables Don't Work For Some Users.

Post by ChaosGaming_ »

I renamed the workspace when I started working on 2.0.0 which is why the file within the .zip and the info.json names don't match. However, I ensured we both had the same version and code within the files and that the correct function was being called, but the table results still differed.

I fixed the potential for alternate functions being called in 2.1.2 by prefixing the required lua with the mod name

Code: Select all

require("__Unlimited-Resources-Oil-Refresh-Cargo-Ship-Compat__.scripts.string")
We tested this version and the previous one with print functions in the string.lua function being called to ensure UR's method was getting called.
Post Reply

Return to “1 / 0 magic”