Модуль:Entity Sprite/all: различия между версиями

мНет описания правки
мНет описания правки
Строка 39: Строка 39:
end
end


local function findFieldInsensitive(tbl, fieldName)
local function getInsensitiveKey(t, target)
     for key, value in pairs(tbl) do
    target = target:lower()
         if type(key) == "string" and mw.ustring.lower(key) == mw.ustring.lower(fieldName) then
     for k, v in pairs(t) do
             return value
         if type(k) == "string" and k:lower() == target then
             return v
         end
         end
     end
     end
Строка 49: Строка 50:


local function getSpritePath(entry)
local function getSpritePath(entry)
     local iconField = findFieldInsensitive(entry, "Icon")
     local iconField = getInsensitiveKey(entry, "Icon")
     local spriteField = findFieldInsensitive(entry, "Sprite")
     local spriteField = getInsensitiveKey(entry, "Sprite")
      
      
     if iconField and iconField.sprite then
     if iconField and iconField.sprite then
Строка 67: Строка 68:


local function getSpriteStates(entry)
local function getSpriteStates(entry)
    local result = {}
     local spriteBlock = getInsensitiveKey(entry, "Sprite")
     local spriteBlock = findFieldInsensitive(entry, "Sprite")
     if spriteBlock and spriteBlock.layers then
     if spriteBlock and spriteBlock.layers then
        local states = {}
         for _, layer in ipairs(spriteBlock.layers) do
         for _, layer in ipairs(spriteBlock.layers) do
             if layer.visible == false then
             if layer.visible == false then
            elseif layer.state and layer.sprite then
                table.insert(result, { state = layer.state, sprite = layer.sprite })
             elseif layer.state then
             elseif layer.state then
                 table.insert(result, { state = layer.state, sprite = getSpritePath(entry) })
                 table.insert(states, layer.state)
             end
             end
        end
        if #states > 0 then
            return states
         end
         end
     end
     end
    if #result > 0 then
     local iconBlock = getInsensitiveKey(entry, "Icon")
        return result
    end
 
     local iconBlock = findFieldInsensitive(entry, "Icon")
     if iconBlock and iconBlock.state then
     if iconBlock and iconBlock.state then
         return { { state = iconBlock.state, sprite = getSpritePath(entry) } }
         return { iconBlock.state }
     end
     end
     if spriteBlock and spriteBlock.state then
     if spriteBlock and spriteBlock.state then
         return { { state = spriteBlock.state, sprite = getSpritePath(entry) } }
         return { spriteBlock.state }
     end
     end
     return nil
     return nil
end
end
Строка 101: Строка 97:
         local found = false
         local found = false
         for _, group in pairs(spriteGroups) do
         for _, group in pairs(spriteGroups) do
             if deepEqual(findFieldInsensitive(entry, "Sprite"), findFieldInsensitive(group[1], "Sprite")) and
             if deepEqual(getInsensitiveKey(entry, "Sprite"), getInsensitiveKey(group[1], "Sprite")) and
               deepEqual(entry.EntityStorageVisuals, group[1].EntityStorageVisuals) and
               deepEqual(entry.EntityStorageVisuals, group[1].EntityStorageVisuals) and
               deepEqual(findFieldInsensitive(entry, "Icon"), findFieldInsensitive(group[1], "Icon")) then
               deepEqual(getInsensitiveKey(entry, "Icon"), getInsensitiveKey(group[1], "Icon")) then
                 table.insert(group, entry)
                 table.insert(group, entry)
                 found = true
                 found = true