Модуль:Entity Sprite/all: различия между версиями
Pok (обсуждение | вклад) Новая страница: «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 -- Если это ма...» |
Pok (обсуждение | вклад) мНет описания правки |
||
| (не показаны 24 промежуточные версии этого же участника) | |||
| Строка 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 | ||
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 | local count = 0 | ||
if type( | for k in pairs(t) do | ||
if type(k) ~= "number" then return false end | |||
count = count + 1 | |||
end | end | ||
return | 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 | ||
for i = 1, #t1 do | |||
if not deepEqual(t1[i], t2[i]) then | |||
return false | |||
end | 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) | ||
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 | ||
| Строка 70: | Строка 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 = {} | ||
| Строка 77: | Строка 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 | ||
| Строка 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 | |||
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 | local states = getSpriteStates(entry) | ||
local stateStr = "" | |||
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 | end | ||
stateStr = table.concat(links, ", ") | |||
end | end | ||
return mw.getCurrentFrame():preprocess("{{Entity Sprite/ | 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 action = frame.args[1] | |||
local | local mode = frame.args[2] | ||
local | |||
local | 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 | end | ||
local spriteData = mw.loadData(dataPage) | |||
if not spriteData or type(spriteData) ~= "table" then | |||
return "Ошибка: Невозможно загрузить данные из JSON (" .. dataPage .. ")." | |||
local | |||
if not | |||
return | |||
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) | ||
| Строка 209: | Строка 234: | ||
return table.concat(result, "\n") | return table.concat(result, "\n") | ||
else | else | ||
return | return nil | ||
end | end | ||
end | end | ||
return p | return p | ||