Enhance configuration loading: implement file existence check and JSON parsing for dynamic channel settings

This commit is contained in:
MayaTheShy
2026-03-22 01:38:47 -04:00
parent 06dbaf5756
commit caf0ec85a1

View File

@@ -4,7 +4,7 @@
-- Orders and smelter controls are sent to the master.
-------------------------------------------------
-- Configuration
-- Default configuration (overridden by .client_config)
-------------------------------------------------
local BROADCAST_CHANNEL = 4200 -- master sends state on this channel
@@ -20,6 +20,38 @@ local CLIENT_BARREL_NAME = "" -- e.g. "minecraft:barrel_3"
local DROPPER_ANNOUNCE_INTERVAL = 30 -- seconds between dropper announcements
-------------------------------------------------
-- Load config from file if present
-------------------------------------------------
local CLIENT_CONFIG_FILE = ".client_config"
local function loadConfig()
if not fs.exists(CLIENT_CONFIG_FILE) then return end
local f = fs.open(CLIENT_CONFIG_FILE, "r")
local data = f.readAll()
f.close()
local ok, cfg = pcall(textutils.unserialiseJSON, data)
if not ok or not cfg then
print("[WARN] Failed to parse " .. CLIENT_CONFIG_FILE)
return
end
-- Channels
if cfg.broadcastChannel then BROADCAST_CHANNEL = cfg.broadcastChannel end
if cfg.orderChannel then ORDER_CHANNEL = cfg.orderChannel end
if cfg.clientChannel then CLIENT_CHANNEL = cfg.clientChannel end
-- Peripherals
if cfg.monitorSide then MONITOR_SIDE = cfg.monitorSide end
if cfg.smelterMonitorSide then SMELTER_MONITOR_SIDE = cfg.smelterMonitorSide end
if cfg.dropperName then CLIENT_DROPPER_NAME = cfg.dropperName end
if cfg.barrelName then CLIENT_BARREL_NAME = cfg.barrelName end
-- Timing
if cfg.dropperAnnounceInterval then DROPPER_ANNOUNCE_INTERVAL = cfg.dropperAnnounceInterval end
print("[CONFIG] Loaded from " .. CLIENT_CONFIG_FILE)
end
loadConfig()
-------------------------------------------------
-- State (received from master)
-------------------------------------------------