Модуль:Entity Sprite/all: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| (не показана 21 промежуточная версия этого же участника) | |||
| Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
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) | ||
local iconField = findFieldInsensitive(entry, "Icon") | |||
return | local spriteField = findFieldInsensitive(entry, "Sprite") | ||
elseif | |||
return | if iconField and iconField.sprite then | ||
elseif | return iconField.sprite | ||
for _, layer in pairs( | 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 | ||
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 | 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 | 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 | |||
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 | end | ||
if param == "image" then | if param == "image" then | ||
local states = getSpriteStates(entry) | |||
local stateStr = "" | local stateStr = "" | ||
if | if states then | ||
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 | 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 | local spriteData = mw.loadData(dataPage) | ||
if not | if not spriteData or type(spriteData) ~= "table" then | ||
return | return "Ошибка: Невозможно загрузить данные из JSON (" .. dataPage .. ")." | ||
end | end | ||
if | if action == "repeat" then | ||
return generateRepeatTemplate( | return generateRepeatTemplate(spriteData) | ||
elseif | elseif action == "image" then | ||
local result = {} | local result = {} | ||
for _, entry in pairs( | for _, entry in pairs(spriteData) do | ||
local template = generateTemplate(entry, | local template = generateTemplate(entry, action) | ||
if template then | if template then | ||
table.insert(result, template) | table.insert(result, template) | ||