Skip to content

Instantly share code, notes, and snippets.

@MCJack123
Created April 8, 2022 00:32
Show Gist options
  • Save MCJack123/8e048a6e51bcdf1bb3b65115bdf506a5 to your computer and use it in GitHub Desktop.
Save MCJack123/8e048a6e51bcdf1bb3b65115bdf506a5 to your computer and use it in GitHub Desktop.
TUI configuration utility for CraftOS-PC (requires Tamperer: https://github.com/Fatboychummy-CC/Tamperer)
local tamperer = require "tamperer"
settings.save(".settings")
settings.clear()
local cfg = {
name = "Configuration",
info = "Configure CraftOS-PC's behavior",
bigInfo = "",
colors = {
bg = {
main = "black",
},
fg = {
main = "white",
title = "yellow",
info = "lightGray",
listInfo = "gray",
listTitle = "white",
bigInfo = "lightGray",
selector = "yellow",
arrowDisabled = "gray",
arrowEnabled = "white",
input = "yellow",
error = "red",
}
},
platform = "computer",
final = "Exit",
settings = {},
selections = {}
}
local descriptions = {
checkUpdates = "Check for updates",
configReadOnly = "Lock the configuration",
debug_enable = "Enable debug & debugger",
disable_lua51_features = "Disable deprecated fn's",
dropFilePath = "Paste path on drag & drop",
extendMargins = "Show colored margins",
http_blacklist = "Blocked HTTP URLs",
http_enable = "Enable the HTTP API",
http_websocket_enable = "Enable HTTP WebSockets",
http_whitelist = "Allowed HTTP URLs",
ignoreHotkeys = "Disable function keys",
isColor = "Toggle advanced features",
keepOpenOnShutdown = "Do not close when shutting down",
logErrors = "Log errors to console",
monitorsUseMouseEvents = "Use mouse_* for monitors",
mount_mode = "Permissions for mounts",
romReadOnly = "Lock the ROM for writing",
showFPS = "Show framerate",
showMountPrompt = "Ask to mount directories",
snapToSize = "Automatically resize to fit",
snooperEnabled = "Send crash reports",
standardsMode = "Enhance compatibility",
startFullscreen = "Start window fullscreen",
useDFPWM = "Enable DFPWM emulation",
useHardwareRenderer = "Use hardware rendering",
useVsync = "Enable V-sync (HW only)",
useWebP = "Enable WebP screenshots",
vanilla = "Disable CraftOS-PC APIs",
abortTimeout = "Time to wait for yield",
clockSpeed = "Maximum framerate",
computerSpaceLimit = "Limit of computer space",
computerHeight = "Height of computer",
computerWidth = "Width of computer",
defaultHeight = "Height of terminals",
defaultWidth = "Width of terminals",
default_computer_settings = "Default settings",
initialComputer = "First computer booted",
maxNotesPerTick = "Maximum notes per 1/20s",
maxOpenPorts = "Maximum modem ports open",
maxRecordingTime = "Maximum GIF length",
maximumFilesOpen = "Maximum open files",
mouse_move_throttle = "Time between mouse_move",
preferredHardwareDriver = "Preferred render backend",
recordingFPS = "GIF recording framerate"
}
local longDescriptions = {
abortTimeout = "Maximum time to wait for a program to yield before forcibly terminating",
checkUpdates = "Whether to check for updates at program start",
computerSpaceLimit = "The amount of space available to the computer (only affects standards mode)",
clockSpeed = "Terminal refresh rate",
configReadOnly = "Whether the configuration settings should not be able to be changed inside CraftOS",
computerWidth = "Default width of the current computer in characters",
computerHeight = "Default height of the current computer in characters",
debug_enable = "Whether to enable the `debug` API",
default_computer_settings = "A string with some default settings to set in the `settings` API",
defaultWidth = "The default width of new terminals",
defaultHeight = "The default height of new terminals",
disable_lua51_features = "Whether to disable some Lua 5.1-only features in the BIOS",
dropFilePath = "If set, dropping a file on the window will paste its path rather than copying it into the computer",
extendMargins = "Whether to extend colored margins to the edges of the screen, independent of standards mode",
http_blacklist = "An array of IP addresses or hostnames to block; this takes priority over the whitelist",
http_enable = "Whether to enable the `http` API",
http_max_download = "The maximum size of an HTTP response to accept, in bytes",
http_max_requests = "The maximum number of HTTP requests to make at a time",
http_max_upload = "The maximum size of an entire HTTP request, including POST data and headers, in bytes",
http_max_websocket_message = "The maximum size of one WebSocket message, in bytes",
http_max_websockets = "The maximum number of open WebSocket connections to allow",
http_timeout = "The amount of time to wait for an HTTP request/response before terminating, in milliseconds",
http_websocket_enabled = "Whether to enable WebSocket support in the http API",
http_whitelist = "An array of IP addresses or hostnames to allow; any not in the list will be blocked",
ignoreHotkeys = "Whether to ignore hotkeys such as F2, F3, F11",
initialComputer = "The computer to start when opening CraftOS-PC",
isColor = "Whether the emulated computer is an Advanced Computer (color) or Standard Computer (no color)",
keepOpenOnShutdown = "Whether to leave computer windows open after the computer shuts down; it can be rebooted with Ctrl+R",
logErrors = "Whether to log all Lua errors to the console",
maximumFilesOpen = "Maximum number of files that can be open at once",
maxNotesPerTick = "Maximum number of notes that can be played through a speaker every 1/20 of a second",
maxRecordingTime = "The maximum number of seconds that a recording will run until it auto-stops",
maxOpenPorts = "The maximum number of ports (channels) that may be open on a single modem",
monitorsUseMouseEvents = "Whether monitors should send `mouse_*` events (v2.3.4 and below behavior)",
mount_mode = "Specifies security settings for mounting directories",
mouse_move_throttle = "The amount of throttling placed on the `mouse_move` event",
preferredHardwareDriver = "The preferred driver to use with the hardware renderer.",
recordingFPS = "The framerate of GIF recordings. This value should be a divisor of `clockSpeed`.",
showFPS = "Whether to show the framerate (unimplemented)",
snapToSize = "Whether to automatically snap window size to the nearest character size, keeping margins normalized",
snooperEnabled = "Whether to automatically send crash reports to the developer's server",
standardsMode = "Enables a few super-compatibility options, such as 50ms timing and colored margins.",
startFullscreen = "Whether to start the computer in fullscreen mode",
useDFPWM = "Whether to convert audio played through speaker.playAudio to DFPWM first",
useHardwareRenderer = "Whether to use the GPU for hardware-accelerated rendering",
useVsync = "Whether to enable Vsync with the hardware renderer",
useWebP = "Whether to take screenshots and recordings in WebP format (uses less data, but recordings don't work on Discord)",
vanilla = "Whether to disable all CraftOS-PC-specific features"
}
local types = {"boolean", "number", "string", "table"}
local list = {}
for _,v in ipairs(config.list()) do if v ~= "showMountPrompt" then table.insert(list, v) end end
table.sort(list)
for _,v in ipairs(list) do
if config.get(v) ~= nil then
print(v)
local tp = config.getType(v)
settings.define(v, {type = tp})
settings.set(v, config.get(v))
if (tp == "string" or tp == "number") and v ~= "mount_mode" then
table.insert(cfg.settings, {
setting = v,
title = v:sub(1, 16),
tp = tp,
bigInfo = longDescriptions[v] or "",
location = ".config"
})
else
table.insert(cfg.selections, {
title = v:sub(1, 16),
option = v,
info = descriptions[v] or v,
bigInfo = longDescriptions[v] or ""
})
end
end
end
local stop
repeat
stop = true
local res = tamperer.display(cfg)
if type(res) == "number" and res <= #cfg.selections then
print(res)
stop = false
local opt = cfg.selections[res].option
local tp = config.getType(opt)
if opt == "mount_mode" then
local s = tamperer.display {
name = "mount_mode",
info = "Permissions for mounts",
bigInfo = "",
colors = cfg.colors,
platform = "computer",
final = "Back",
selections = {
{
title = "none",
info = "Disable mounting",
bigInfo = ""
},
{
title = "ro strict",
info = "Only allow read-only mounting",
bigInfo = ""
},
{
title = "ro",
info = "Mount read-only by default",
bigInfo = ""
},
{
title = "rw",
info = "Mount read-write by default",
bigInfo = ""
}
}
}
if s and s <= 4 then settings.set("mount_mode", s - 1) end
elseif tp == "boolean" then
local s = tamperer.display {
name = opt:sub(1, 16),
info = descriptions[opt] or opt:sub(1, 24),
bigInfo = "",
colors = cfg.colors,
platform = "computer",
final = "Back",
selections = {
{
title = "True",
info = "Enable this option",
bigInfo = ""
},
{
title = "False",
info = "Disable this option",
bigInfo = ""
}
}
}
if s and s <= 2 then settings.set(opt, s == 1) end
end
end
until stop
local msg
for _,v in ipairs(list) do
if config.get(v) ~= nil and config.get(v) ~= settings.get(v) then
local m = config.set(v, settings.get(v))
if m and (m:find("CraftOS%-PC") or msg == nil) then msg = m end
end
end
settings.clear()
settings.load(".settings")
term.clear()
term.setCursorPos(1, 1)
if msg then print(msg) end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment