Модуль:Entity Sprite/all: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
local JsonPaths = require('Module:JsonPaths') | local JsonPaths = require('Module:JsonPaths') | ||
local fileExistsCache = {} | |||
local function getSpritePath(entry) | local function getSpritePath(entry) | ||
return entry.sprite | |||
end | end | ||
local function getSpriteStates(entry) | local function getSpriteStates(entry) | ||
local result = {} | |||
if entry.layers and entry.sprite then | |||
for _, layer in ipairs(entry.layers) do | |||
if layer.visible ~= false then | |||
table.insert(result, { | |||
state = tostring(layer.state or entry.state or ""), | |||
sprite = entry.sprite | |||
}) | |||
end | |||
end | |||
elseif entry.state and entry.sprite then | |||
table.insert(result, { | |||
state = tostring(entry.state or ""), | |||
sprite = entry.sprite | |||
}) | |||
end | |||
return (#result > 0) and result or nil | |||
end | end | ||
local function getPrefix(id, project) | local function getPrefix(id, project) | ||
if project ~= "" and JsonPaths.has(id, project) then | |||
return project .. ":" | |||
end | |||
return "" | |||
end | end | ||
local function | local function batchFileExists(names) | ||
local result = {} | |||
local batch = {} | |||
local seen = {} | |||
local function flush() | |||
if #batch == 0 then | |||
return | |||
end | |||
local titles = mw.title.newBatch(batch, 6):lookupExistence():getTitles() | |||
for i, title in ipairs(titles) do | |||
local name = batch[i] | |||
fileExistsCache[name] = title and title.exists or false | |||
end | |||
batch = {} | |||
seen = {} | |||
end | |||
for _, name in ipairs(names) do | |||
if fileExistsCache[name] == nil and not seen[name] then | |||
seen[name] = true | |||
table.insert(batch, name) | |||
if #batch == 25 then | |||
flush() | |||
end | |||
end | |||
end | |||
flush() | |||
for _, name in ipairs(names) do | |||
result[name] = fileExistsCache[name] or false | |||
end | |||
return result | |||
end | end | ||
local function generateRepeatTemplate(data, project) | local function generateRepeatTemplate(data, project) | ||
local spriteGroups = {} | |||
for id, entry in pairs(data) do | |||
local found = false | |||
for _, group in pairs(spriteGroups) do | |||
local g = group[1] | |||
if entry.sprite == g.entry.sprite and entry.state == g.entry.state then | |||
table.insert(group, { id = id, entry = entry }) | |||
found = true | |||
break | |||
end | |||
end | |||
if not found then | |||
table.insert(spriteGroups, { | |||
{ id = id, entry = entry } | |||
}) | |||
end | |||
end | |||
local result = {} | |||
for _, group in pairs(spriteGroups) do | |||
if #group > 1 then | |||
local idLinks = {} | |||
for _, obj in pairs(group) do | |||
local id = obj.id | |||
local prefix = getPrefix(id, project) | |||
table.insert(idLinks, "[[:Файл:" .. prefix .. 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 | end | ||
local function generateTemplate(id, entry, baseUrl, project) | local function generateTemplate(id, entry, baseUrl, project) | ||
local spritePath = getSpritePath(entry) | |||
if not id or not spritePath then | |||
return nil | |||
end | |||
local prefix = getPrefix(id, project) | |||
local states = getSpriteStates(entry) | |||
local stateStr = "" | |||
if states then | |||
local links = {} | |||
for _, item in ipairs(states) do | |||
local url = baseUrl .. item.sprite .. "/" .. item.state .. ".png" | |||
table.insert(links, "[" .. url .. " " .. item.state .. "]") | |||
end | |||
stateStr = table.concat(links, ", ") | |||
end | |||
return mw.getCurrentFrame():preprocess( | |||
"{{Entity Sprite/Image|файл=" .. prefix .. id .. | |||
"|id=" .. id .. | |||
"|путь=" .. baseUrl .. spritePath .. | |||
"|state=" .. stateStr .. | |||
"}}" | |||
) | |||
end | end | ||
function p.main(frame) | function p.main(frame) | ||
local action = frame.args[1] | |||
local json = frame.args.json or "sprite_entity.json" | |||
local checkFile = frame.args.checkFile or "" | |||
if checkFile == "" then checkFile = false end | |||
local project = JsonPaths.project() | |||
local baseUrl = JsonPaths.git() .. "/Resources/Textures/" | |||
local dataPage = JsonPaths.get(json) | |||
local spriteData = mw.loadData(dataPage) | |||
if not spriteData or type(spriteData) ~= "table" then | |||
return "Ошибка загрузки JSON: " .. dataPage | |||
end | |||
if action == "repeat" then | |||
return generateRepeatTemplate(spriteData, project) | |||
elseif action == "image" then | |||
local items = {} | |||
local fileNames = {} | |||
for id, entry in pairs(spriteData) do | |||
local prefix = getPrefix(id, project) | |||
local fileName = prefix .. id .. ".png" | |||
table.insert(items, { | |||
id = id, | |||
entry = entry, | |||
fileName = fileName | |||
}) | |||
if checkFile then | |||
table.insert(fileNames, fileName) | |||
end | |||
end | |||
local existsMap = nil | |||
if checkFile then | |||
existsMap = batchFileExists(fileNames) | |||
end | |||
local result = {} | |||
for _, item in ipairs(items) do | |||
local skip = false | local skip = false | ||
if checkFile | if checkFile and existsMap[item.fileName] then | ||
skip = true | |||
end | end | ||
if not skip then | if not skip then | ||
local t = generateTemplate(item.id, item.entry, baseUrl, project) | |||
if t then | |||
table.insert(result, t) | |||
end | |||
end | end | ||
end | |||
return table.concat(result, "\n") | |||
end | |||
return nil | |||
end | end | ||
return p | return p | ||