Модуль:Entity Sprite: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
local cache = mw.cache.new('EntitySpriteCache') | local cache = nil | ||
if mw.cache and mw.cache.new then | |||
cache = mw.cache.new('EntitySpriteCache') | |||
else | |||
cache = { | |||
get = function(key) | |||
return nil | |||
end, | |||
set = function(key, value) | |||
end | |||
} | |||
end | |||
-- Переменные для кэширования в пределах модуля | -- Переменные для кэширования в пределах модуля | ||
Строка 70: | Строка 81: | ||
end | end | ||
-- Загрузка JSON-данных с кэшированием через mw.cache | -- Загрузка JSON-данных с кэшированием через mw.cache или переменными модуля | ||
local function loadData(filePath) | local function loadData(filePath) | ||
if cachedData then | if cachedData then | ||
Строка 203: | Строка 214: | ||
entry = dataById[tostring(id)] | entry = dataById[tostring(id)] | ||
else | else | ||
for _, item in ipairs(data) do | for _, item in ipairs(data) do | ||
if tostring(item.id) == tostring(id) then | if tostring(item.id) == tostring(id) then | ||
Строка 257: | Строка 267: | ||
return nil | return nil | ||
elseif param == "image" and secondaryParam then | elseif param == "image" and secondaryParam then | ||
if dataById and dataById[tostring(secondaryParam)] then | if dataById and dataById[tostring(secondaryParam)] then | ||
local entry = dataById[tostring(secondaryParam)] | local entry = dataById[tostring(secondaryParam)] |
Версия от 12:15, 3 февраля 2025
local p = {}
local cache = nil
if mw.cache and mw.cache.new then
cache = mw.cache.new('EntitySpriteCache')
else
cache = {
get = function(key)
return nil
end,
set = function(key, value)
end
}
end
-- Переменные для кэширования в пределах модуля
local cachedData = nil
local cachedSpritePathIndex = nil
local dataById = nil
-- Функция глубокого сравнения таблиц
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 i = 0
for _ in pairs(t) do
i = i + 1
if t[i] == nil then return false end
end
return true
end
if isArray(t1) and isArray(t2) then
if #t1 ~= #t2 then return false end
local matched = {}
for _, v1 in ipairs(t1) do
local found = false
for j, v2 in ipairs(t2) do
if not matched[j] and deepEqual(v1, v2) then
matched[j] = true
found = true
break
end
end
if not found 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, _ in pairs(t2) do
if t1[k] == nil 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 ipairs(entry.Sprite.layers) do
if layer.sprite then
return layer.sprite
end
end
end
return nil
end
-- Загрузка JSON-данных с кэшированием через mw.cache или переменными модуля
local function loadData(filePath)
if cachedData then
return cachedData
end
cachedData = cache:get('data')
dataById = cache:get('dataById')
if cachedData and dataById then
return cachedData
end
local page = mw.title.new(filePath)
local content = page and page:getContent()
cachedData = content and mw.text.jsonDecode(content) or nil
if cachedData and type(cachedData) == "table" then
dataById = {}
for _, item in ipairs(cachedData) do
dataById[tostring(item.id)] = item
end
cache:set('data', cachedData)
cache:set('dataById', dataById)
end
return cachedData
end
-- Создание индекса для путей спрайтов с кэшированием
local function createSpritePathIndex(data)
if cachedSpritePathIndex then
return cachedSpritePathIndex
end
cachedSpritePathIndex = cache:get('spritePathIndex')
if cachedSpritePathIndex then
return cachedSpritePathIndex
end
local index = {}
for _, entry in ipairs(data) do
local spritePath = getSpritePath(entry)
if spritePath then
index[spritePath] = entry.id
end
end
cachedSpritePathIndex = index
cache:set('spritePathIndex', cachedSpritePathIndex)
return cachedSpritePathIndex
end
-- Генерация шаблона repeat
local function generateRepeatTemplate(data)
local spriteGroups = {}
for _, entry in ipairs(data) do
local found = false
for key, 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
spriteGroups[entry.id] = { entry }
end
end
local result = {}
for _, group in pairs(spriteGroups) do
if #group > 1 then
local idLinks = {}
for _, entry in ipairs(group) do
table.insert(idLinks, "[[:Файл:" .. entry.id .. ".png]]")
end
local firstId = group[1].id
table.insert(result, mw.getCurrentFrame():preprocess("{{Entity Sprite/Repeat|" .. table.concat(idLinks, " ") .. "|" .. firstId .. "}}"))
end
end
return table.concat(result, "\n")
end
-- Генерация шаблона для image и path
local function generateTemplate(entry, param, secondaryParam, spritePathIndex)
local spritePath = getSpritePath(entry)
if not entry.id or not spritePath then
return nil
end
if param == "image" then
if secondaryParam then
if tostring(entry.id) == tostring(secondaryParam) then
return spritePath
end
return nil
else
return mw.getCurrentFrame():preprocess("{{Entity Sprite/Image|" .. entry.id .. "|" .. spritePath .. "}}")
end
elseif param == "path" then
if secondaryParam then
local id = spritePathIndex[secondaryParam]
if id then
return id
end
return nil
else
return mw.getCurrentFrame():preprocess("{{Entity Sprite/Path|" .. entry.id .. "|" .. spritePath .. "}}")
end
end
return nil
end
-- Генерация шаблона по умолчанию
local function generateDefaultTemplate(data, params)
local id = params.Id
local description = params.Description or ""
local servers = params.Servers or ""
local source = params.Source or ""
local tags = params.Tags or ""
local spritePath = nil
local path = params.Path
local entry = nil
if id and id ~= "" and dataById then
entry = dataById[tostring(id)]
else
for _, item in ipairs(data) do
if tostring(item.id) == tostring(id) then
entry = item
break
end
end
end
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
return mw.getCurrentFrame():preprocess(
"{{Файл\n" ..
"|Описание = " .. description .. "\n" ..
"|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
local spritePathIndex = createSpritePathIndex(data)
if param == "repeat" then
return generateRepeatTemplate(data)
elseif param == "path" and secondaryParam then
for _, entry in ipairs(data) do
local template = generateTemplate(entry, param, secondaryParam, spritePathIndex)
if template then
return template
end
end
return nil
elseif param == "image" and secondaryParam then
if dataById and dataById[tostring(secondaryParam)] then
local entry = dataById[tostring(secondaryParam)]
return getSpritePath(entry) or "Ошибка: Спрайт не найден."
else
return "Ошибка: ID не найден."
end
elseif param == "image" or param == "path" then
local result = {}
for _, entry in ipairs(data) do
local template = generateTemplate(entry, param, secondaryParam, spritePathIndex)
if template then
table.insert(result, template)
end
end
return table.concat(result, "\n")
else
return generateDefaultTemplate(data, frame.args)
end
end
return p