Модуль:Песочница/Pok: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 65: | Строка 65: | ||
end | end | ||
return nil | return nil | ||
end | end | ||
| Строка 82: | Строка 75: | ||
local spritePath = getSpritePath(entry) | local spritePath = getSpritePath(entry) | ||
local iconBlock = findFieldInsensitive(entry, "Icon") | local iconBlock = findFieldInsensitive(entry, "Icon") | ||
local spriteBlock = findFieldInsensitive(entry, "Sprite") | local spriteBlock = findFieldInsensitive(entry, "Sprite") | ||
| Строка 133: | Строка 125: | ||
end | end | ||
local function generateRepeatTemplate(data) | local function getTextureBaseUrl(project) | ||
if project == "Goob" then | |||
return "https://github.com/space-syndicate/Goob-Station/blob/master/Resources/Textures/" | |||
end | |||
return "https://github.com/space-syndicate/space-station-14/blob/master/Resources/Textures/" | |||
end | |||
local function generateRepeatTemplate(data, project) | |||
local spriteGroups = {} | local spriteGroups = {} | ||
| Строка 158: | Строка 158: | ||
local idLinks = {} | local idLinks = {} | ||
for _, entry in pairs(group) do | for _, entry in pairs(group) do | ||
table.insert(idLinks, "[[:Файл:" .. entry.id .. ".png]]") | local prefix = (project ~= "" and JsonPaths.has(entry.id)) and (project .. ":") or "" | ||
table.insert(idLinks, "[[:Файл:" .. prefix .. entry.id .. ".png]]") | |||
end | end | ||
table.insert(result, mw.getCurrentFrame():preprocess( | table.insert(result, mw.getCurrentFrame():preprocess( | ||
| Строка 169: | Строка 170: | ||
end | end | ||
local function generateTemplate(entry, baseUrl, project) | |||
local function generateTemplate(entry, | |||
local spritePath = getSpritePath(entry) | local spritePath = getSpritePath(entry) | ||
if not entry.id or not spritePath then | if not entry.id or not spritePath then | ||
| Строка 183: | Строка 176: | ||
end | end | ||
local prefix = (project ~= "" and JsonPaths.has(entry.id)) and (project .. ":") or "" | |||
local states = getSpriteStates(entry) | |||
local stateStr = "" | |||
if states then | |||
local links = {} | |||
for _, item in ipairs(states) do | |||
local url = baseUrl .. item.sprite .. "/" .. item.state .. ".png" | |||
table.insert(links, "[" .. url .. " " .. item.state .. "]") | |||
end | end | ||
stateStr = table.concat(links, ", ") | |||
end | end | ||
return | return mw.getCurrentFrame():preprocess( | ||
"{{Entity Sprite/Image|" .. prefix .. entry.id .. | |||
"|" .. baseUrl .. spritePath .. | |||
"|" .. stateStr .. "}}" | |||
) | |||
end | end | ||
function p.main(frame) | function p.main(frame) | ||
local action = frame.args[1] | local action = frame.args[1] | ||
local project = JsonPaths.project( | local project = JsonPaths.project() | ||
local baseUrl = getTextureBaseUrl(project) | local baseUrl = getTextureBaseUrl(project) | ||
local dataPage = JsonPaths.get("prototype/sprite.json") | |||
local spriteData = mw.loadData(dataPage) | local spriteData = mw.loadData(dataPage) | ||
if not spriteData or type(spriteData) ~= "table" then | if not spriteData or type(spriteData) ~= "table" then | ||
return "Ошибка: | return "Ошибка загрузки JSON: " .. dataPage | ||
end | end | ||
if action == "repeat" then | if action == "repeat" then | ||
return generateRepeatTemplate(spriteData) | return generateRepeatTemplate(spriteData, project) | ||
elseif action == "image" then | elseif action == "image" then | ||
local result = {} | local result = {} | ||
for _, entry in pairs(spriteData) do | for _, entry in pairs(spriteData) do | ||
local | local t = generateTemplate(entry, baseUrl, project) | ||
if | if t then | ||
table.insert(result, | table.insert(result, t) | ||
end | end | ||
end | end | ||
return table.concat(result, "\n") | return table.concat(result, "\n") | ||
end | end | ||
return nil | |||
end | end | ||
return p | return p | ||
Версия от 22:00, 21 марта 2026
Для документации этого модуля может быть создана страница Модуль:Песочница/Pok/doc
local p = {}
local JsonPaths = require('Module:JsonPaths')
local function deepEqual(t1, t2)
if t1 == t2 then return true end
if type(t1) ~= "table" or type(t2) ~= "table" then return false end
local function isArray(t)
local count = 0
for k in pairs(t) do
if type(k) ~= "number" then return false end
count = count + 1
end
return count == #t
end
if isArray(t1) and isArray(t2) then
if #t1 ~= #t2 then return false end
for i = 1, #t1 do
if not deepEqual(t1[i], t2[i]) then
return false
end
end
return true
end
for k, v in pairs(t1) do
if t2[k] == nil or not deepEqual(v, t2[k]) then
return false
end
end
for k, v in pairs(t2) do
if t1[k] == nil or not deepEqual(v, t1[k]) then
return false
end
end
return true
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 iconField = findFieldInsensitive(entry, "Icon")
local spriteField = findFieldInsensitive(entry, "Sprite")
if iconField and iconField.sprite then
return iconField.sprite
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
return layer.sprite
end
end
end
return nil
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 getTextureBaseUrl(project)
if project == "Goob" then
return "https://github.com/space-syndicate/Goob-Station/blob/master/Resources/Textures/"
end
return "https://github.com/space-syndicate/space-station-14/blob/master/Resources/Textures/"
end
local function generateRepeatTemplate(data, project)
local spriteGroups = {}
for _, entry in pairs(data) do
local found = false
for _, group in pairs(spriteGroups) do
if deepEqual(findFieldInsensitive(entry, "Sprite"), findFieldInsensitive(group[1], "Sprite")) and
deepEqual(entry.EntityStorageVisuals, group[1].EntityStorageVisuals) and
deepEqual(findFieldInsensitive(entry, "Icon"), findFieldInsensitive(group[1], "Icon")) then
table.insert(group, entry)
found = true
break
end
end
if not found then
table.insert(spriteGroups, { entry })
end
end
local result = {}
for _, group in pairs(spriteGroups) do
if #group > 1 then
local idLinks = {}
for _, entry in pairs(group) do
local prefix = (project ~= "" and JsonPaths.has(entry.id)) and (project .. ":") or ""
table.insert(idLinks, "[[:Файл:" .. prefix .. entry.id .. ".png]]")
end
table.insert(result, mw.getCurrentFrame():preprocess(
"{{Entity Sprite/Repeat|" .. table.concat(idLinks, " ") .. "|" .. group[1].id .. "}}"
))
end
end
return table.concat(result, "\n")
end
local function generateTemplate(entry, baseUrl, project)
local spritePath = getSpritePath(entry)
if not entry.id or not spritePath then
return nil
end
local prefix = (project ~= "" and JsonPaths.has(entry.id)) and (project .. ":") or ""
local states = getSpriteStates(entry)
local stateStr = ""
if states then
local links = {}
for _, item in ipairs(states) do
local url = baseUrl .. item.sprite .. "/" .. item.state .. ".png"
table.insert(links, "[" .. url .. " " .. item.state .. "]")
end
stateStr = table.concat(links, ", ")
end
return mw.getCurrentFrame():preprocess(
"{{Entity Sprite/Image|" .. prefix .. entry.id ..
"|" .. baseUrl .. spritePath ..
"|" .. stateStr .. "}}"
)
end
function p.main(frame)
local action = frame.args[1]
local project = JsonPaths.project()
local baseUrl = getTextureBaseUrl(project)
local dataPage = JsonPaths.get("prototype/sprite.json")
local spriteData = mw.loadData(dataPage)
if not spriteData or type(spriteData) ~= "table" then
return "Ошибка загрузки JSON: " .. dataPage
end
if action == "repeat" then
return generateRepeatTemplate(spriteData, project)
elseif action == "image" then
local result = {}
for _, entry in pairs(spriteData) do
local t = generateTemplate(entry, baseUrl, project)
if t then
table.insert(result, t)
end
end
return table.concat(result, "\n")
end
return nil
end
return p