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

Новая страница: «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) if t1 == t2 then return true end if type(t1) ~= "table" or type(t2) ~= "table" then return false end -- Если это ма...»
 
мНет описания правки
 
(не показаны 24 промежуточные версии этого же участника)
Строка 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
     if type(t1) ~= "table" or type(t2) ~= "table" then return false end
     if type(t1) ~= "table" or type(t2) ~= "table" then return false end


    -- Если это массивы, проверяем их содержимое без учета порядка
     local function isArray(t)
     local function isArray(t)
         for _, v in pairs(t) do
        local count = 0
             if type(v) == "nil" then return false end
         for k in pairs(t) do
             if type(k) ~= "number" then return false end
            count = count + 1
         end
         end
         return true
         return count == #t
     end
     end


     if isArray(t1) and isArray(t2) then
     if isArray(t1) and isArray(t2) then
         if #t1 ~= #t2 then return false end
         if #t1 ~= #t2 then return false end
         local matched = {}
         for i = 1, #t1 do
        for _, v1 in pairs(t1) do
             if not deepEqual(t1[i], t2[i]) then
             local found = false
                 return false
            for _, v2 in pairs(t2) do
                if not matched[v2] and deepEqual(v1, v2) then
                    matched[v2] = true
                    found = true
                    break
                 end
             end
             end
            if not found then return false end
         end
         end
         return true
         return true
     end
     end


    -- Если это таблицы, проверяем их содержимое
     for k, v in pairs(t1) do
     for k, v in pairs(t1) do
         if not deepEqual(v, t2[k]) then
         if t2[k] == nil or not deepEqual(v, t2[k]) then
             return false
             return false
         end
         end
Строка 46: Строка 31:


     for k, v in pairs(t2) do
     for k, v in pairs(t2) do
         if not deepEqual(v, t1[k]) then
         if t1[k] == nil or not deepEqual(v, t1[k]) then
             return false
             return false
         end
         end
Строка 54: Строка 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
Строка 70: Строка 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 = {}
Строка 77: Строка 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
Строка 98: Строка 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
Строка 105: Строка 168:
end
end


-- Обновляем основную функцию
local function generateTemplate(entry, param)
local function generateTemplate(entry, param, secondaryParam, 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
Строка 113: Строка 175:


     if param == "image" then
     if param == "image" then
         if secondaryParam then
        local states = getSpriteStates(entry)
             if tostring(entry.id) == tostring(secondaryParam) then
        local stateStr = ""
                 return spritePath
         if states then
            end
             local baseUrl = "https://github.com/space-syndicate/space-station-14/blob/master/Resources/Textures/"
            return nil
            local links = {}
        else
            for _, item in ipairs(states) do
            return mw.getCurrentFrame():preprocess("{{Entity Sprite/Image|" .. entry.id .. "|" .. spritePath .. "}}")
                 local spritePath = item.sprite
        end
                local stateName = item.state
    elseif param == "path" then
                local url = baseUrl .. spritePath .. "/" .. stateName .. ".png"
        if secondaryParam then
                table.insert(links, "[" .. url .. " " .. stateName .. "]")
            for _, e in pairs(data) do
                if getSpritePath(e) == secondaryParam then
                    return e.id
                end
             end
             end
             return nil
             stateStr = table.concat(links, ", ")
         end
         end
         return mw.getCurrentFrame():preprocess("{{Entity Sprite/Path|" .. entry.id .. "|" .. spritePath .. "}}")
         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


Строка 136: Строка 198:
end
end


-- Генерация шаблона по умолчанию
function p.main(frame)
local function generateDefaultTemplate(data, params)
     local action = frame.args[1]
     local id = params.Id
     local mode = frame.args[2]
    local description = params.Description or ""
    local servers = params.Servers or ""
    local source = params.Source or ""
     local tags = params.Tags or ""


     local spritePath = nil
     local dataPage
    local path = params.Path
    if mode == "item" then
 
        dataPage = "Модуль:IanComradeBot/prototypes/entity sprite/item.json/data"
     local entry = nil
     elseif mode == "clothing" then
    if id and id ~= "" then
         dataPage = "Модуль:IanComradeBot/prototypes/entity sprite/clothing.json/data"
         for _, item in pairs(data) do
    elseif mode == "structure" then
            if tostring(item.id) == tostring(id) then
        dataPage = "Модуль:IanComradeBot/prototypes/entity sprite/structure.json/data"
                entry = item
     elseif mode == "mob" then
                break
         dataPage = "Модуль:IanComradeBot/prototypes/entity sprite/mob.json/data"
            end
     elseif mode == "other" then
        end
         dataPage = "Модуль:IanComradeBot/prototypes/entity sprite/other.json/data"
     end
    else
 
        dataPage = "Модуль:IanComradeBot/prototypes/entity sprite.json/data"
    if entry then
         spritePath = getSpritePath(entry)
        if not spritePath then
            return ""
        end
    end
   
     if not path or path == "" then
         path = "Resources/Textures/" .. (spritePath or "")
     end
     end


    return mw.getCurrentFrame():preprocess(
     local spriteData = mw.loadData(dataPage)
        "{{Файл\n" ..
     if not spriteData or type(spriteData) ~= "table" then
        "|Описание = " .. description .. "\n" ..
         return "Ошибка: Невозможно загрузить данные из JSON (" .. dataPage .. ")."
        "|Id      = " .. id .. "\n" ..
        "|Сервера  = " .. servers .. "\n" ..
        "|Источник = " .. source .. "\n" ..
        "|Путь    = " .. path .. "\n" ..
        "|Теги    = " .. tags .. "\n" ..
        "}}\n"
    )
end
 
function p.main(frame)
     local param = frame.args[1]
    local secondaryParam = frame.args[2]
 
    local data = loadData('User:IanComradeBot/prototypes/entity sprite.json')
     if not data or type(data) ~= 'table' then
         return 'Ошибка: Невозможно загрузить данные из JSON.'
     end
     end


     if param == "repeat" then
     if action == "repeat" then
         return generateRepeatTemplate(data)
         return generateRepeatTemplate(spriteData)
     elseif param == "path" and secondaryParam then
     elseif action == "image" then
        for _, entry in ipairs(data) do
            local template = generateTemplate(entry, param, secondaryParam, data)
            if template then
                return template
            end
        end
        return nil
    elseif param == "image" or param == "path" then
         local result = {}
         local result = {}
         for _, entry in pairs(data) do
         for _, entry in pairs(spriteData) do
             local template = generateTemplate(entry, param, secondaryParam, data)
             local template = generateTemplate(entry, action)
             if template then
             if template then
                 table.insert(result, template)
                 table.insert(result, template)
Строка 209: Строка 234:
         return table.concat(result, "\n")
         return table.concat(result, "\n")
     else
     else
         return generateDefaultTemplate(data, frame.args)
         return nil
     end
     end
end
end


return p
return p