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

мНет описания правки
мНет описания правки
 
(не показана 21 промежуточная версия этого же участника)
Строка 1: Строка 1:
local p = {}
local p = {}


-- Загрузка данных
local function loadData(filePath)
    local page = mw.title.new(filePath)
    local content = page and page:getContent()
    return content and mw.text.jsonDecode(content) or nil
end
-- Проверка равенства двух таблиц
local function deepEqual(t1, t2)
local function deepEqual(t1, t2)
     if t1 == t2 then return true end
     if t1 == t2 then return true end
Строка 47: Строка 39:
end
end


-- Получение пути спрайта
local function findFieldInsensitive(tbl, fieldName)
    for key, value in pairs(tbl) do
        if type(key) == "string" and mw.ustring.lower(key) == mw.ustring.lower(fieldName) then
            return value
        end
    end
    return nil
end
 
local function getSpritePath(entry)
local function getSpritePath(entry)
     if entry.Sprite and entry.Sprite.sprite then
     local iconField = findFieldInsensitive(entry, "Icon")
         return entry.Sprite.sprite
    local spriteField = findFieldInsensitive(entry, "Sprite")
     elseif entry.Icon and entry.Icon.sprite then
   
         return entry.Icon.sprite
    if iconField and iconField.sprite then
     elseif entry.Sprite and entry.Sprite.layers then
         return iconField.sprite
         for _, layer in pairs(entry.Sprite.layers) do
     elseif spriteField and spriteField.sprite then
         return spriteField.sprite
     elseif spriteField and spriteField.layers then
         for _, layer in pairs(spriteField.layers) do
             if layer.sprite then
             if layer.sprite then
                 return layer.sprite
                 return layer.sprite
Строка 63: Строка 66:
end
end


-- Генерация шаблона repeat
local function isStateFirstKey(layer)
    for k, _ in pairs(layer) do
        return k == "state"
    end
    return false
end
 
local function getSpriteStates(entry)
    local result = {}
 
    local function addState(state, sprite)
        table.insert(result, { state = state, sprite = sprite })
    end
 
    local spritePath = getSpritePath(entry)
 
    local iconBlock = findFieldInsensitive(entry, "Icon")
    local spriteBlock = findFieldInsensitive(entry, "Sprite")
 
    if iconBlock and iconBlock.state then
        addState(iconBlock.state, iconBlock.sprite or spritePath)
    else
        if spriteBlock and spriteBlock.layers then
            for _, layer in ipairs(spriteBlock.layers) do
                if layer.visible ~= false then
                    local stateName = layer.state or (iconBlock and iconBlock.state) or "icon"
                    local s = layer.sprite or (iconBlock and iconBlock.sprite) or spritePath
                    addState(stateName, s)
                    break
                end
            end
        elseif spriteBlock and spriteBlock.state then
            addState(spriteBlock.state, spriteBlock.sprite or spritePath)
        end
    end
 
    if spriteBlock and spriteBlock.layers then
        for _, layer in ipairs(spriteBlock.layers) do
            local alreadyAdded = false
            for _, r in ipairs(result) do
                local layerState = layer.state or "icon"
                if r.state == layerState then
                    alreadyAdded = true
                    break
                end
            end
 
            if not alreadyAdded and layer.visible ~= false then
                local stateName = layer.state or "icon"
                local s = layer.sprite or (iconBlock and iconBlock.sprite) or spritePath
 
                if s then
                    addState(stateName, s)
                end
            end
        end
    end
 
    if #result == 0 and (iconBlock and (iconBlock.sprite or iconBlock.state) or spritePath) then
        local s = (iconBlock and iconBlock.sprite) or spritePath
        addState("icon", s)
    end
 
    return (#result > 0) and result or nil
end
 
local function generateRepeatTemplate(data)
local function generateRepeatTemplate(data)
     local spriteGroups = {}
     local spriteGroups = {}
Строка 70: Строка 138:
         local found = false
         local found = false
         for _, group in pairs(spriteGroups) do
         for _, group in pairs(spriteGroups) do
             if deepEqual(entry.Sprite, group[1].Sprite) and
             if deepEqual(findFieldInsensitive(entry, "Sprite"), findFieldInsensitive(group[1], "Sprite")) and
               deepEqual(entry.EntityStorageVisuals, group[1].EntityStorageVisuals) and
               deepEqual(entry.EntityStorageVisuals, group[1].EntityStorageVisuals) and
               deepEqual(entry.Icon, group[1].Icon) then
               deepEqual(findFieldInsensitive(entry, "Icon"), findFieldInsensitive(group[1], "Icon")) then
                 table.insert(group, entry)
                 table.insert(group, entry)
                 found = true
                 found = true
Строка 91: Строка 159:
                 table.insert(idLinks, "[[:Файл:" .. entry.id .. ".png]]")
                 table.insert(idLinks, "[[:Файл:" .. entry.id .. ".png]]")
             end
             end
             table.insert(result, mw.getCurrentFrame():preprocess("{{Entity Sprite/Repeat|" .. table.concat(idLinks, " ") .. "|" .. group[1].id .. "}}"))
             table.insert(result, mw.getCurrentFrame():preprocess(
                "{{Entity Sprite/Repeat|" .. table.concat(idLinks, " ") .. "|" .. group[1].id .. "}}"
            ))
         end
         end
     end
     end
Строка 98: Строка 168:
end
end


-- Функция генерации шаблона по записи
local function generateTemplate(entry, param)
local function generateTemplate(entry, param, data)
     local spritePath = getSpritePath(entry)
     local spritePath = getSpritePath(entry)
     if not entry.id or not spritePath then
     if not entry.id or not spritePath then
         return nil
         return nil
    end
    -- Определяем state
    local state = nil
    if entry.Sprite and entry.Sprite.state then
        state = entry.Sprite.state
    elseif entry.Sprite and entry.Sprite.layers then
        for _, layer in pairs(entry.Sprite.layers) do
            if layer.state then
                state = layer.state
                break
            end
        end
     end
     end


     if param == "image" then
     if param == "image" then
        local states = getSpriteStates(entry)
         local stateStr = ""
         local stateStr = ""
         if state then
         if states then
             stateStr = "(" .. state .. ")"
             local baseUrl = "https://github.com/space-syndicate/space-station-14/blob/master/Resources/Textures/"
            local links = {}
            for _, item in ipairs(states) do
                local spritePath = item.sprite
                local stateName = item.state
                local url = baseUrl .. spritePath .. "/" .. stateName .. ".png"
                table.insert(links, "[" .. url .. " " .. stateName .. "]")
            end
            stateStr = table.concat(links, ", ")
         end
         end
         return mw.getCurrentFrame():preprocess("{{Entity Sprite/Image|" .. entry.id .. "|" .. spritePath .. stateStr .. "}}")
         return mw.getCurrentFrame():preprocess(
            "{{Entity Sprite/Image|" .. entry.id ..
            "|https://github.com/space-syndicate/space-station-14/blob/master/Resources/Textures/" .. spritePath ..
            "|" .. stateStr .. "}}"
        )
     end
     end


Строка 130: Строка 199:


function p.main(frame)
function p.main(frame)
     local param = frame.args[1]
     local action = frame.args[1]
    local mode = frame.args[2]
 
    local dataPage
    if mode == "item" then
        dataPage = "Модуль:IanComradeBot/prototypes/entity sprite/item.json/data"
    elseif mode == "clothing" then
        dataPage = "Модуль:IanComradeBot/prototypes/entity sprite/clothing.json/data"
    elseif mode == "structure" then
        dataPage = "Модуль:IanComradeBot/prototypes/entity sprite/structure.json/data"
    elseif mode == "mob" then
        dataPage = "Модуль:IanComradeBot/prototypes/entity sprite/mob.json/data"
    elseif mode == "other" then
        dataPage = "Модуль:IanComradeBot/prototypes/entity sprite/other.json/data"
    else
        dataPage = "Модуль:IanComradeBot/prototypes/entity sprite.json/data"
    end


     local data = loadData('User:IanComradeBot/prototypes/entity sprite.json')
     local spriteData = mw.loadData(dataPage)
     if not data or type(data) ~= 'table' then
     if not spriteData or type(spriteData) ~= "table" then
         return 'Ошибка: Невозможно загрузить данные из JSON.'
         return "Ошибка: Невозможно загрузить данные из JSON (" .. dataPage .. ")."
     end
     end


     if param == "repeat" then
     if action == "repeat" then
         return generateRepeatTemplate(data)
         return generateRepeatTemplate(spriteData)
     elseif param == "image" or param == "path" then
     elseif action == "image" then
         local result = {}
         local result = {}
         for _, entry in pairs(data) do
         for _, entry in pairs(spriteData) do
             local template = generateTemplate(entry, param, data)
             local template = generateTemplate(entry, action)
             if template then
             if template then
                 table.insert(result, template)
                 table.insert(result, template)