Модуль:Песочница/Pok: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| (не показано 9 промежуточных версий этого же участника) | |||
| Строка 2: | Строка 2: | ||
local JsonPaths = require('Module:JsonPaths') | local JsonPaths = require('Module:JsonPaths') | ||
local function | local function getSpritePath(entry) | ||
return entry.sprite | |||
end | |||
local function getSpriteStates(entry) | |||
local result = {} | |||
if | 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 | ||
end | end | ||
elseif entry.state and entry.sprite then | |||
table.insert(result, { | |||
state = tostring(entry.state or ""), | |||
sprite = entry.sprite | |||
}) | |||
end | end | ||
return (#result > 0) and result or nil | |||
end | end | ||
local function | local function getPrefix(id, project) | ||
if project ~= "" and JsonPaths.has(id, project) then | |||
return project .. ":" | |||
end | end | ||
return | return "" | ||
end | end | ||
local | local MAX_CHECKS = 100 | ||
local checkCount = 0 | |||
local function fileExists(name) | |||
if checkCount >= MAX_CHECKS then | |||
return false | |||
end | end | ||
checkCount = checkCount + 1 | |||
local title = mw.title.new(name, "File") | |||
return title and title.exists | |||
end | end | ||
local function generateRepeatTemplate(data) | local function generateRepeatTemplate(data, project) | ||
local spriteGroups = {} | local spriteGroups = {} | ||
for | for id, entry in pairs(data) do | ||
local found = false | local found = false | ||
for _, group in pairs(spriteGroups) do | 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, entry) | table.insert(group, { id = id, entry = entry }) | ||
found = true | found = true | ||
break | break | ||
| Строка 142: | Строка 66: | ||
if not found then | if not found then | ||
table.insert(spriteGroups, {entry}) | table.insert(spriteGroups, { | ||
{ id = id, entry = entry } | |||
}) | |||
end | end | ||
end | end | ||
local result = {} | local result = {} | ||
for _, group in pairs(spriteGroups) do | for _, group in pairs(spriteGroups) do | ||
if #group > 1 then | if #group > 1 then | ||
local idLinks = {} | local idLinks = {} | ||
for _, | |||
table.insert(idLinks, "[[:Файл:" .. | for _, obj in pairs(group) do | ||
local id = obj.id | |||
local prefix = getPrefix(id, project) | |||
table.insert(idLinks, "[[:Файл:" .. prefix .. id .. ".png]]") | |||
end | end | ||
table.insert(result, mw.getCurrentFrame():preprocess( | table.insert(result, mw.getCurrentFrame():preprocess( | ||
"{{Entity Sprite/Repeat|" .. table.concat(idLinks, " ") .. "|" .. group[1].id .. "}}" | "{{Entity Sprite/Repeat|" .. | ||
table.concat(idLinks, " ") .. | |||
"|" .. group[1].id .. | |||
"}}" | |||
)) | )) | ||
end | end | ||
| Строка 162: | Строка 97: | ||
end | end | ||
local function generateTemplate(entry, | local function generateTemplate(id, entry, baseUrl, project) | ||
local spritePath = getSpritePath(entry) | local spritePath = getSpritePath(entry) | ||
if not | if not id or not spritePath then | ||
return nil | return nil | ||
end | 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 | end | ||
stateStr = table.concat(links, ", ") | |||
end | end | ||
return | return mw.getCurrentFrame():preprocess( | ||
"{{Песочница/Pok|файл=" .. prefix .. id .. | |||
"|id=" .. id .. | |||
"|путь=" .. baseUrl .. spritePath .. | |||
"|state=" .. stateStr .. | |||
"}}" | |||
) | |||
end | end | ||
function p.main(frame) | function p.main(frame) | ||
local action = frame.args[1] | local action = frame.args[1] | ||
local | local json = frame.args.json or "prototype/sprite.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 | if not spriteData or type(spriteData) ~= "table" then | ||
return "Ошибка: | return "Ошибка загрузки JSON: " .. dataPage | ||
end | end | ||
if action == "repeat" then | if action == "repeat" then | ||
return generateRepeatTemplate(spriteData) | return generateRepeatTemplate(spriteData, project) | ||
elseif action == "image" then | elseif action == "image" then | ||
local result = {} | local result = {} | ||
for | |||
local | for id, entry in pairs(spriteData) do | ||
local prefix = getPrefix(id, project) | |||
local skip = false | |||
if checkFile then | |||
local fileName = prefix .. id .. ".png" | |||
if fileExists(fileName) then | |||
skip = true | |||
end | |||
end | |||
if not skip then | |||
local t = generateTemplate(id, entry, baseUrl, project) | |||
if t then | |||
table.insert(result, t) | |||
end | |||
end | |||
end | end | ||
return table.concat(result, "\n") | return table.concat(result, "\n") | ||
end | end | ||
return nil | |||
end | end | ||
return p | return p | ||