Модуль:Песочница/Pok: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| (не показано 11 промежуточных версий этого же участника) | |||
| Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
local JsonPaths = require('Module:JsonPaths') | local JsonPaths = require('Module:JsonPaths') | ||
local function getSpritePath(entry) | local function getSpritePath(entry) | ||
return entry.sprite | |||
end | end | ||
| Строка 70: | Строка 9: | ||
local result = {} | 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 | ||
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 | |||
local function getPrefix(id, project) | |||
if project ~= "" and JsonPaths.has(id, project) then | |||
return project .. ":" | |||
end | |||
return "" | |||
end | |||
local MAX_CHECKS = 100 | |||
local checkCount = 0 | |||
if | local function fileExists(name) | ||
if checkCount >= MAX_CHECKS then | |||
return false | |||
end | end | ||
checkCount = checkCount + 1 | |||
local | local title = mw.title.new(name, "File") | ||
return title and title.exists | |||
return | |||
end | end | ||
| Строка 136: | Строка 52: | ||
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 | ||
| Строка 149: | Строка 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 _, | |||
local prefix = ( | for _, obj in pairs(group) do | ||
table.insert(idLinks, "[[:Файл:" .. prefix . | 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 | ||
| Строка 170: | Строка 97: | ||
end | end | ||
local function generateTemplate(entry, baseUrl, project) | 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 = ( | local prefix = getPrefix(id, project) | ||
local states = getSpriteStates(entry) | local states = getSpriteStates(entry) | ||
| Строка 191: | Строка 118: | ||
return mw.getCurrentFrame():preprocess( | return mw.getCurrentFrame():preprocess( | ||
"{{ | "{{Песочница/Pok|файл=" .. prefix .. id .. | ||
"|" .. baseUrl .. spritePath .. | "|id=" .. id .. | ||
"|" .. stateStr .. "}}" | "|путь=" .. baseUrl .. spritePath .. | ||
"|state=" .. stateStr .. | |||
"}}" | |||
) | ) | ||
end | end | ||
| Строка 199: | Строка 128: | ||
function p.main(frame) | function p.main(frame) | ||
local action = frame.args[1] | local action = frame.args[1] | ||
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 project = JsonPaths.project() | ||
local baseUrl = | local baseUrl = JsonPaths.git() .. "/Resources/Textures/" | ||
local dataPage = JsonPaths.get(json) | |||
local dataPage = JsonPaths.get( | |||
local spriteData = mw.loadData(dataPage) | local spriteData = mw.loadData(dataPage) | ||
| Строка 212: | Строка 143: | ||
if action == "repeat" then | if action == "repeat" then | ||
return generateRepeatTemplate(spriteData, project) | return generateRepeatTemplate(spriteData, project) | ||
elseif action == "image" then | elseif action == "image" then | ||
local result = {} | local result = {} | ||
for | |||
local t = generateTemplate(entry, baseUrl, project) | 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 | ||
Текущая версия от 00:39, 22 марта 2026
Для документации этого модуля может быть создана страница Модуль:Песочница/Pok/doc
local p = {}
local JsonPaths = require('Module:JsonPaths')
local function getSpritePath(entry)
return entry.sprite
end
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
local function getPrefix(id, project)
if project ~= "" and JsonPaths.has(id, project) then
return project .. ":"
end
return ""
end
local MAX_CHECKS = 100
local checkCount = 0
local function fileExists(name)
if checkCount >= MAX_CHECKS then
return false
end
checkCount = checkCount + 1
local title = mw.title.new(name, "File")
return title and title.exists
end
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
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(
"{{Песочница/Pok|файл=" .. prefix .. id ..
"|id=" .. id ..
"|путь=" .. baseUrl .. spritePath ..
"|state=" .. stateStr ..
"}}"
)
end
function p.main(frame)
local action = frame.args[1]
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
return "Ошибка загрузки JSON: " .. dataPage
end
if action == "repeat" then
return generateRepeatTemplate(spriteData, project)
elseif action == "image" then
local result = {}
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
return table.concat(result, "\n")
end
return nil
end
return p