Модуль:Entity Sprite/all: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 13: | Строка 13: | ||
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: | Строка 39: | ||
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 | ||
Версия от 16:40, 5 февраля 2025
Для документации этого модуля может быть создана страница Модуль:Entity Sprite/all/doc
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
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 getSpritePath(entry)
if entry.Sprite and entry.Sprite.sprite then
return entry.Sprite.sprite
elseif entry.Icon and entry.Icon.sprite then
return entry.Icon.sprite
elseif entry.Sprite and entry.Sprite.layers then
for _, layer in pairs(entry.Sprite.layers) do
if layer.sprite then
return layer.sprite
end
end
end
return nil
end
-- Генерация шаблона repeat
local function generateRepeatTemplate(data)
local spriteGroups = {}
for _, entry in pairs(data) do
local found = false
for _, group in pairs(spriteGroups) do
if deepEqual(entry.Sprite, group[1].Sprite) and
deepEqual(entry.EntityStorageVisuals, group[1].EntityStorageVisuals) and
deepEqual(entry.Icon, 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
table.insert(idLinks, "[[:Файл:" .. 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, param, data)
local spritePath = getSpritePath(entry)
if not entry.id or not spritePath then
return nil
end
if param == "image" then
return mw.getCurrentFrame():preprocess("{{Entity Sprite/Image|" .. entry.id .. "|" .. spritePath .. "}}")
elseif param == "path" then
return mw.getCurrentFrame():preprocess("{{Entity Sprite/Path|" .. entry.id .. "|" .. spritePath .. "}}")
end
return nil
end
function p.main(frame)
local param = frame.args[1]
local data = loadData('User:IanComradeBot/prototypes/entity sprite.json')
if not data or type(data) ~= 'table' then
return 'Ошибка: Невозможно загрузить данные из JSON.'
end
if param == "repeat" then
return generateRepeatTemplate(data)
elseif param == "image" or param == "path" then
local result = {}
for _, entry in pairs(data) do
local template = generateTemplate(entry, param, data)
if template then
table.insert(result, template)
end
end
return table.concat(result, "\n")
else
return nil
end
end
return p