Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local fs = require("filesystem")
- local shell = require("shell")
- local term = require("term")
- local serialization = require("serialization")
- local function setTextColor(color)
- term.gpu().setForeground(color)
- end
- local function resetTextColor()
- term.gpu().setForeground(0xFFFFFF)
- end
- local function clearScreen()
- term.clear()
- end
- local function listModels()
- local models = {}
- for file in fs.list("/home/shape3d/") do
- if file:match("%.3d$") then
- local filePath = "/home/shape3d/" .. file
- local fileContent = io.open(filePath, "r")
- if fileContent then
- local content = fileContent:read("*a")
- fileContent:close()
- local model = serialization.unserialize(content)
- if model and model.label then
- table.insert(models, {label = model.label, file = filePath})
- end
- end
- end
- end
- table.sort(models, function(a, b) return a.label < b.label end)
- return models
- end
- local function parseColorCodes(text)
- local colorCodes = {
- ["§0"] = 0x000000, ["§1"] = 0x0000AA, ["§2"] = 0x00AA00, ["§3"] = 0x00AAAA,
- ["§4"] = 0xAA0000, ["§5"] = 0xAA00AA, ["§6"] = 0xFFAA00, ["§7"] = 0xAAAAAA,
- ["§8"] = 0x555555, ["§9"] = 0x5555FF, ["§a"] = 0x55FF55, ["§b"] = 0x55FFFF,
- ["§c"] = 0xFF5555, ["§d"] = 0xFF55FF, ["§e"] = 0xFFFF55, ["§f"] = 0xFFFFFF
- }
- local textWithoutCodes = text:gsub("§.", "")
- local coloredTextParts = {}
- local lastColor = 0xFFFFFF
- for code, subText in text:gmatch("(§.)([^§]*)") do
- if colorCodes[code] then
- lastColor = colorCodes[code]
- end
- table.insert(coloredTextParts, {color = lastColor, text = subText})
- end
- if #coloredTextParts == 0 then
- table.insert(coloredTextParts, {color = lastColor, text = textWithoutCodes})
- end
- return textWithoutCodes, coloredTextParts
- end
- local function displayModelInfo(model, texture, colorMode, color, vertical, horizontal)
- clearScreen()
- local label = model.label or "Без имени"
- local tooltip = model.tooltip or "Оригинальное"
- local _, labelParts = parseColorCodes(label)
- local tooltipLines = {}
- for line in tooltip:gmatch("[^\n]+") do
- table.insert(tooltipLines, line)
- end
- setTextColor(0xFFFFFF)
- io.write("Выбранная модель: \"")
- for _, part in ipairs(labelParts) do
- setTextColor(part.color)
- io.write(part.text)
- end
- setTextColor(0xFFFFFF)
- print("\"")
- resetTextColor()
- print()
- setTextColor(0xAAAAAA) -- Серый цвет
- print("- Описание:")
- for _, line in ipairs(tooltipLines) do
- local _, parts = parseColorCodes(line)
- for _, part in ipairs(parts) do
- setTextColor(part.color)
- io.write(part.text)
- end
- print() -- Перенос строки
- end
- print()
- resetTextColor()
- print("Найденные текстуры: " .. (model.texture or "Оригинальная"))
- print("Найденные цвета: " .. (model.color or "Оригинальный"))
- print("Поворот по вертикали: " .. vertical)
- print("Поворот по горизонтали: " .. horizontal)
- print()
- setTextColor(0xAAAAAA) -- Серый цвет
- print("Нажмите Enter чтобы вернуться в главное меню")
- resetTextColor()
- end
- local function selectModel(models)
- clearScreen()
- setTextColor(0xFF00FF) -- Ярко фиолетовый цвет
- print("Доступные модели:")
- resetTextColor()
- print()
- for i, model in ipairs(models) do
- setTextColor(0xFFFFFF) -- Белый цвет для номеров
- io.write(i .. ". ")
- resetTextColor()
- local _, parts = parseColorCodes(model.label)
- for _, part in ipairs(parts) do
- setTextColor(part.color)
- io.write(part.text)
- end
- setTextColor(0xAAAAAA) -- Серый цвет для имени файла
- print(" - " .. model.file:match("/home/(.+)$"))
- resetTextColor()
- end
- print()
- print()
- setTextColor(0xAAAAAA) -- Серый цвет
- print("Введите Enter чтобы выйти из программы")
- resetTextColor()
- print()
- setTextColor(0xFFFF00) -- Желтый цвет
- print("Выберите номер модели:")
- resetTextColor()
- local choice = tonumber(term.read())
- if choice and choice >= 1 and choice <= #models then
- return models[choice]
- elseif choice == nil then
- return "exit"
- else
- return nil
- end
- end
- local function readTexture(model)
- setTextColor(0xFFFF00)
- print("Найденные текстуры которые вы можете изменить:")
- resetTextColor()
- print()
- local textures = model.textures or {}
- for i, texture in ipairs(textures) do
- print(i .. ". " .. texture)
- end
- print()
- print("Введите номер текстуры для замены:")
- local choice = tonumber(term.read())
- if choice and choice >= 1 and choice <= #textures then
- setTextColor(0xFFFF00)
- print("Введите текстуру которая заменит \"" .. textures[choice] .. "\"")
- resetTextColor()
- local newTexture = term.read():gsub("\n", "")
- if newTexture == "" then
- return
- else
- model.textures[choice] = newTexture
- setTextColor(0x00FF00)
- print("Текстура \"" .. textures[choice] .. "\" была заменена на \"" .. newTexture .. "\"")
- resetTextColor()
- end
- else
- setTextColor(0xFF0000)
- print("Неверный выбор")
- resetTextColor()
- end
- end
- local function readColorMode()
- setTextColor(0xFFFF00)
- print("Режим выбора цвета:")
- setTextColor(0xFFFFFF)
- print("1. Сохранить оригинальные цвета из файла модели")
- print("2. Очистить цвета модели")
- print("3. Установить HEX цвет")
- resetTextColor()
- local choice = tonumber(term.read())
- if choice and (choice == 1 or choice == 2 or choice == 3) then
- return choice
- else
- print("Неверный выбор")
- return nil
- end
- end
- local function readColor()
- setTextColor(0xFFFF00)
- print("Введите цвет текстуры:")
- print()
- setTextColor(0xAAAAAA) -- Серый цвет
- print("Введите номер цвета без #. Например \"E23737\"")
- print("Нажмите Enter чтобы выйти без изменений")
- resetTextColor()
- local color = term.read():gsub("\n", "")
- if color == "" then
- return nil
- else
- return tonumber(color, 16)
- end
- end
- local function readRotation(prompt)
- setTextColor(0xFFFF00)
- print(prompt)
- resetTextColor()
- local rotation = tonumber(term.read())
- if rotation == 0 or rotation == 90 or rotation == 180 or rotation == 270 or rotation == 360 then
- return rotation
- else
- print("Неверный выбор")
- return nil
- end
- end
- local function transformShape(shape, vertical, horizontal, texture, colorMode, color)
- local x1, y1, z1, x2, y2, z2 = shape[1], shape[2], shape[3], shape[4], shape[5], shape[6]
- -- Vertical rotation
- if vertical == 90 then
- y1, z1 = z1, 16 - y1
- y2, z2 = z2, 16 - y2
- elseif vertical == 180 then
- y1, z1 = 16 - y1, 16 - z1
- y2, z2 = 16 - y2, 16 - z2
- elseif vertical == 270 then
- y1, z1 = 16 - z1, y1
- y2, z2 = 16 - z2, y2
- elseif vertical == 360 then
- -- No change for 360 degrees as it is same as 0 degrees
- end
- -- Horizontal rotation
- if horizontal == 90 then
- x1, z1 = z1, 16 - x1
- x2, z2 = z2, 16 - x2
- elseif horizontal == 180 then
- x1, z1 = 16 - x1, 16 - z1
- x2, z2 = 16 - x2, 16 - z2
- elseif horizontal == 270 then
- x1, z1 = 16 - z1, x1
- x2, z2 = 16 - z2, x2
- elseif horizontal == 360 then
- -- No change for 360 degrees as it is same as 0 degrees
- end
- if texture then
- shape.texture = texture
- end
- if colorMode == 2 then
- shape.tint = nil
- elseif colorMode == 3 and color then
- shape.tint = color
- end
- return {x1, y1, z1, x2, y2, z2, texture = shape.texture, tint = shape.tint, state = shape.state}
- end
- local function modifyModel(model, texture, colorMode, color, vertical, horizontal)
- for i, shape in ipairs(model.shapes) do
- model.shapes[i] = transformShape(shape, vertical, horizontal, texture, colorMode, color)
- end
- return model
- end
- local function changeLabel(model)
- while true do
- clearScreen()
- setTextColor(0xFFFF00)
- print("Введите название модели:")
- resetTextColor()
- print()
- setTextColor(0xFFFFFF)
- print("Максимум 30 символов с учетом цветовых кодов")
- print("Используйте цветовые коды с помощью &")
- resetTextColor()
- print()
- setTextColor(0xAAAAAA)
- print("Нажмите Enter чтобы выйти без изменений")
- resetTextColor()
- print()
- local newLabel = term.read():gsub("\n", "")
- if newLabel == "" then
- return model.label
- else
- -- Подсчет количества символов без учета цветовых кодов
- local textOnly = newLabel:gsub("&.", "") -- Удаление цветовых кодов для проверки длины
- if #textOnly > 30 then
- setTextColor(0xFF6363) -- Красный цвет
- print("Название превышает количество символов, попробуйте снова")
- resetTextColor()
- os.sleep(2) -- Задержка для отображения сообщения
- else
- newLabel = newLabel:gsub("&", "§")
- model.label = newLabel
- setTextColor(0x66FF63) -- Зеленый цвет
- print("Название установлено!")
- resetTextColor()
- os.sleep(2) -- Задержка для отображения сообщения
- return model.label
- end
- end
- end
- end
- local function changeTooltip(model)
- clearScreen()
- setTextColor(0xFFFF00)
- print("Введите описание модели:")
- resetTextColor()
- local newTooltip = term.read():gsub("\n", "")
- if newTooltip == "" then
- return model.tooltip
- else
- -- Заменяем & на § для цветовых кодов
- newTooltip = newTooltip:gsub("&", "§")
- -- Обрабатываем перенос строки с указанием цвета
- newTooltip = newTooltip:gsub("#§", "\n§")
- -- Обрабатываем просто перенос строки
- newTooltip = newTooltip:gsub("#", "\n")
- model.tooltip = newTooltip
- end
- return model.tooltip
- end
- local function handleModelSave(model, modelPath)
- clearScreen()
- setTextColor(0xFFFF00)
- print("Будет напечатана модель: \"" .. model.label .. "\"")
- resetTextColor()
- print()
- print("Описание:")
- print(model.tooltip or "Оригинальное")
- print()
- print("Текстура: " .. (model.texture or "Оригинальная"))
- print("Цвет: " .. (model.color or "Оригинальный"))
- print("Поворот по вертикали: 0")
- print("Поворот по горизонтали: 0")
- print()
- print("Что делать с внесенными изменениями?")
- print("1. Создать как новый файл модели")
- print("2. Создать копию как copy-" .. fs.name(modelPath))
- setTextColor(0xFFFF00)
- print("3. Обновить оригинальный файл модели")
- resetTextColor()
- print("4. Отменить печать")
- local choice = tonumber(term.read())
- if choice == 1 then
- setTextColor(0xFFFF00)
- print("Введите только название новой модели без указания формата")
- resetTextColor()
- local newName = term.read():gsub("\n", "")
- local newModelPath = "/home/shape3d/" .. newName .. ".3d"
- local file = io.open(newModelPath, "w")
- if not file then
- print("Не удалось создать новый файл модели")
- return
- end
- file:write(serialization.serialize(model))
- file:close()
- setTextColor(0x00FF00)
- print("Модель была сохранена как " .. newModelPath)
- resetTextColor()
- elseif choice == 2 then
- local newModelPath = "/home/shape3d/copy-" .. fs.name(modelPath)
- local file = io.open(newModelPath, "w")
- if not file then
- print("Не удалось создать копию модели")
- return
- end
- file:write(serialization.serialize(model))
- file:close()
- elseif choice == 3 then
- local file = io.open(modelPath, "w")
- if not file then
- print("Не удалось обновить файл модели")
- return
- end
- file:write(serialization.serialize(model))
- file:close()
- elseif choice == 4 then
- return
- else
- print("Неверный выбор")
- return
- end
- end
- local function displayMenu(model, texture, colorMode, color, vertical, horizontal)
- displayModelInfo(model, texture, colorMode, color, vertical, horizontal)
- print()
- setTextColor(0xAAAAAA) -- Серый цвет
- print("0. Выбрать другую модель")
- resetTextColor()
- print("1. Найти и изменить текстуры")
- print("2. Найти и изменить цвета")
- print("3. Изменить название модели")
- print("4. Изменить описание модели")
- print("5. Повернуть по вертикали")
- print("6. Повернуть по горизонтали")
- setTextColor(0xFFFF00) -- Желтый цвет
- print("7. Начать печать модели")
- resetTextColor()
- setTextColor(0xAAAAAA) -- Серый цвет
- print("Введите Enter чтобы выйти из программы")
- resetTextColor()
- end
- local function main()
- local models = listModels()
- if #models == 0 then
- print("Нет доступных моделей в формате .3d")
- return
- end
- local modelChoice = selectModel(models)
- if modelChoice == "exit" then
- return
- elseif not modelChoice then
- return
- end
- local modelName = modelChoice.label
- local modelPath = modelChoice.file
- local file = io.open(modelPath, "r")
- if not file then
- print("Не удалось открыть файл модели")
- return
- end
- local modelData = file:read("*a")
- file:close()
- local model = serialization.unserialize(modelData)
- if model == nil then
- print("Ошибка при загрузке модели")
- return
- end
- local texture = nil
- local colorMode = 1
- local color = nil
- local vertical = 0
- local horizontal = 0
- while true do
- displayMenu(model, texture, colorMode, color, vertical, horizontal)
- local choice = tonumber(term.read())
- if choice == nil then
- return
- elseif choice == 0 then
- modelChoice = selectModel(models)
- if modelChoice == "exit" then
- return
- elseif not modelChoice then
- return
- end
- modelName = modelChoice.label
- modelPath = modelChoice.file
- file = io.open(modelPath, "r")
- if not file then
- print("Не удалось открыть файл модели")
- return
- end
- modelData = file:read("*a")
- file:close()
- model = serialization.unserialize(modelData)
- if model == nil then
- print("Ошибка при загрузке модели")
- return
- end
- texture = nil
- colorMode = 1
- color = nil
- vertical = 0
- horizontal = 0
- elseif choice == 1 then
- texture = readTexture()
- elseif choice == 2 then
- colorMode = readColorMode()
- if colorMode == 3 then
- color = readColor()
- end
- elseif choice == 3 then
- model.label = changeLabel(model)
- elseif choice == 4 then
- model.tooltip = changeTooltip(model)
- elseif choice == 5 then
- vertical = readRotation("Введите градус поворота по Вертикали (0, 90, 180, 270, 360):")
- if vertical == nil then
- vertical = 0
- end
- elseif choice == 6 then
- horizontal = readRotation("Введите градус поворота по Горизонтали (0, 90, 180, 270, 360):")
- if horizontal == nil then
- horizontal = 0
- end
- elseif choice == 7 then
- local newModel = modifyModel(model, texture, colorMode, color, vertical, horizontal)
- fs.makeDirectory("/home/modified-3d")
- local newModelPath = "/home/modified-3d/modified_" .. fs.name(modelPath)
- file = io.open(newModelPath, "w")
- if not file then
- print("Не удалось создать новый файл модели")
- return
- end
- file:write(serialization.serialize(newModel))
- file:close()
- setTextColor(0x00FF00)
- print("Модифицированная модель сохранена как: " .. newModelPath)
- resetTextColor()
- handleModelSave(newModel, modelPath)
- print("Запуск печати...")
- os.execute("/home/3d " .. newModelPath)
- break
- else
- print("Неверный выбор")
- end
- end
- end
- main()
Add Comment
Please, Sign In to add comment