Модуль:Entity Sprite: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
Строка 4: | Строка 4: | ||
local function loadData(filePath) | local function loadData(filePath) | ||
local page = mw.title.new(filePath) | local page = mw.title.new(filePath) | ||
if not page then | if not page then return nil end | ||
local content = page:getContent() | local content = page:getContent() | ||
if not content then | if not content then return nil end | ||
return mw.text.jsonDecode(content) | return mw.text.jsonDecode(content) | ||
end | end | ||
Строка 20: | Строка 16: | ||
end | end | ||
-- Используем preprocess, чтобы активировать шаблон для имени | |||
local nameTemplate = frame:preprocess(string.format("{{#invoke:Entity Lookup|getname|%s}}", entry.id)) | local nameTemplate = frame:preprocess(string.format("{{#invoke:Entity Lookup|getname|%s}}", entry.id)) | ||
-- Генерируем строку шаблона с минимизированными тегами | |||
return string.format( | return string.format( | ||
[[ | [[ | ||
<div class="e-s_i">%s</div><div class="e-s_s">Id = %s<br>Путь = Resources/Textures/%s<br></div><div class="copy-icon">📋</div><br> | <div class="e-s_i">%s</div><div class="e-s_s">Id = %s<br>Путь = Resources/Textures/%s<br></div><div class="copy-icon">📋</div><br> | ||
]], | ]], | ||
nameTemplate, | nameTemplate, entry.id, entry.Sprite.sprite | ||
) | ) | ||
end | end | ||
Строка 40: | Строка 36: | ||
end | end | ||
local result = | -- Используем конкатенацию без временного хранения всех данных в массиве | ||
local result = "" | |||
local append = function(s) result = result .. s end | |||
-- Проходим по всем элементам и генерируем | -- Проходим по всем элементам и генерируем строки | ||
for _, entry in ipairs(data) do | for _, entry in ipairs(data) do | ||
local template = generateTemplate(entry, frame) | local template = generateTemplate(entry, frame) | ||
if template then | if template then | ||
append(template) -- Генерируем и добавляем строку | |||
end | end | ||
end | end | ||
return | return result -- Возвращаем сгенерированный результат | ||
end | end | ||
return p | return p |
Версия от 18:44, 20 января 2025
local p = {}
-- Загрузка данных
local function loadData(filePath)
local page = mw.title.new(filePath)
if not page then return nil end
local content = page:getContent()
if not content then return nil end
return mw.text.jsonDecode(content)
end
-- Функция генерации текста для одного элемента JSON
local function generateTemplate(entry, frame)
if not entry.id or not entry.Sprite or not entry.Sprite.sprite then
return nil
end
-- Используем preprocess, чтобы активировать шаблон для имени
local nameTemplate = frame:preprocess(string.format("{{#invoke:Entity Lookup|getname|%s}}", entry.id))
-- Генерируем строку шаблона с минимизированными тегами
return string.format(
[[
<div class="e-s_i">%s</div><div class="e-s_s">Id = %s<br>Путь = Resources/Textures/%s<br></div><div class="copy-icon">📋</div><br>
]],
nameTemplate, entry.id, entry.Sprite.sprite
)
end
-- Основная функция модуля
function p.main(frame)
-- Загрузка данных из JSON-файла
local data = loadData('User:IanComradeBot/prototypes/entity sprite.json')
if not data or type(data) ~= 'table' then
return 'Ошибка: Невозможно загрузить данные из JSON.'
end
-- Используем конкатенацию без временного хранения всех данных в массиве
local result = ""
local append = function(s) result = result .. s end
-- Проходим по всем элементам и генерируем строки
for _, entry in ipairs(data) do
local template = generateTemplate(entry, frame)
if template then
append(template) -- Генерируем и добавляем строку
end
end
return result -- Возвращаем сгенерированный результат
end
return p