Модуль:Entity Sprite: различия между версиями

мНет описания правки
мНет описания правки
Строка 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()
     return content and mw.text.jsonDecode(content) or nil
     cachedData = content and mw.text.jsonDecode(content) or nil
    return cachedData
end
end


Строка 72: Строка 80:
end
end


-- Генерация шаблона repeat
-- Генерация шаблона repeat с оптимизированным вызовом preprocess (один раз в конце)
local function generateRepeatTemplate(data)
local function generateRepeatTemplate(data)
     local spriteGroups = {}
     local spriteGroups = {}
Строка 101: Строка 109:
             end
             end
             local firstId = group[1].id
             local firstId = group[1].id
             table.insert(result, mw.getCurrentFrame():preprocess("{{Entity Sprite/Repeat|" .. table.concat(idLinks, " ") .. "|" .. firstId .. "}}"))
             table.insert(result, "{{Entity Sprite/Repeat|" .. table.concat(idLinks, " ") .. "|" .. firstId .. "}}")
         end
         end
     end
     end


     return table.concat(result, "\n")
     return mw.getCurrentFrame():preprocess(table.concat(result, "\n"))
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: Строка 128:
         end
         end
     end
     end
     return index
    cachedSpritePathIndex = index
     return cachedSpritePathIndex
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: Строка 149:
     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