Модуль:Entity Sprite: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
-- Загрузка данных | -- Кэш для данных и индекса | ||
local cachedData = nil | |||
local cachedSpritePathIndex = nil | |||
-- Загрузка данных с кэшированием | |||
local function loadData(filePath) | local function loadData(filePath) | ||
if cachedData then | |||
return cachedData | |||
end | |||
local page = mw.title.new(filePath) | local page = mw.title.new(filePath) | ||
local content = page and page:getContent() | local content = page and page:getContent() | ||
cachedData = content and mw.text.jsonDecode(content) or nil | |||
return cachedData | |||
end | end | ||
-- | -- Функция проверки равенства двух таблиц остаётся без изменений | ||
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) | ||
local i = 0 | local i = 0 | ||
| Строка 40: | Строка 47: | ||
end | end | ||
for k, v in pairs(t1) do | for k, v in pairs(t1) do | ||
if t2[k] == nil or not deepEqual(v, t2[k]) then | if t2[k] == nil or not deepEqual(v, t2[k]) then | ||
| Строка 47: | Строка 53: | ||
end | end | ||
for k, | for k, _ in pairs(t2) do | ||
if t1[k] == nil then | if t1[k] == nil then | ||
return false | return false | ||
| Строка 108: | Строка 114: | ||
end | end | ||
-- Создаём индекс для путей | -- Создаём индекс для путей с кэшированием | ||
local function createSpritePathIndex(data) | local function createSpritePathIndex(data) | ||
if cachedSpritePathIndex then | |||
return cachedSpritePathIndex | |||
end | |||
local index = {} | local index = {} | ||
for _, entry in ipairs(data) do | for _, entry in ipairs(data) do | ||
| Строка 117: | Строка 126: | ||
end | end | ||
end | end | ||
cachedSpritePathIndex = index | |||
return index | return index | ||
end | end | ||
-- Обновляем основную функцию | -- Обновляем основную функцию генерации шаблона | ||
local function generateTemplate(entry, param, secondaryParam, data, spritePathIndex) | local function generateTemplate(entry, param, secondaryParam, data, spritePathIndex) | ||
local spritePath = getSpritePath(entry) | local spritePath = getSpritePath(entry) | ||
| Строка 138: | Строка 148: | ||
elseif param == "path" then | elseif param == "path" then | ||
if secondaryParam then | if secondaryParam then | ||
local id = spritePathIndex[secondaryParam] | local id = spritePathIndex[secondaryParam] | ||
if id then | if id then | ||
| Строка 163: | Строка 172: | ||
local path = params.Path | local path = params.Path | ||
local entry = nil | local entry = nil | ||
if id and id ~= "" then | if id and id ~= "" then | ||
| Строка 174: | Строка 182: | ||
end | end | ||
if entry then | if entry then | ||
spritePath = getSpritePath(entry) | spritePath = getSpritePath(entry) | ||
| Строка 182: | Строка 189: | ||
end | end | ||
if not path or path == "" then | if not path or path == "" then | ||
path = "Resources/Textures/" .. (spritePath or "") | path = "Resources/Textures/" .. (spritePath or "") | ||
end | end | ||
return mw.getCurrentFrame():preprocess( | return mw.getCurrentFrame():preprocess( | ||
"{{Файл\n" .. | "{{Файл\n" .. | ||
| Строка 209: | Строка 214: | ||
end | end | ||
local spritePathIndex = createSpritePathIndex(data) | local spritePathIndex = createSpritePathIndex(data) | ||
| Строка 224: | Строка 228: | ||
elseif param == "image" and secondaryParam then | elseif param == "image" and secondaryParam then | ||
for _, entry in ipairs(data) do | for _, entry in ipairs(data) do | ||
if entry.id == secondaryParam then | if tostring(entry.id) == tostring(secondaryParam) then | ||
return getSpritePath(entry) or "Ошибка: Спрайт не найден." | return getSpritePath(entry) or "Ошибка: Спрайт не найден." | ||
end | end | ||
| Строка 239: | Строка 243: | ||
return table.concat(result, "\n") | return table.concat(result, "\n") | ||
else | else | ||
return generateDefaultTemplate(data, frame.args) | return generateDefaultTemplate(data, frame.args) | ||
end | end | ||