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

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

View File

@@ -5,7 +5,7 @@
-- Requires a wired modem attached to the turtle.
-------------------------------------------------
-- Configuration
-- Default configuration (overridden by .turtle_config)
-------------------------------------------------
-- Modem channels (must match inventoryManager.lua)
@@ -15,6 +15,29 @@ local CRAFT_REPLY_CHANNEL = 4204 -- turtle -> master (craft results)
-- Crafting grid slots in a turtle's 4x4 inventory
local CRAFT_SLOTS = {1, 2, 3, 5, 6, 7, 9, 10, 11}
-------------------------------------------------
-- Load config from file if present
-------------------------------------------------
local TURTLE_CONFIG_FILE = ".turtle_config"
local function loadConfig()
if not fs.exists(TURTLE_CONFIG_FILE) then return end
local f = fs.open(TURTLE_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 " .. TURTLE_CONFIG_FILE)
return
end
if cfg.craftChannel then CRAFT_CHANNEL = cfg.craftChannel end
if cfg.craftReplyChannel then CRAFT_REPLY_CHANNEL = cfg.craftReplyChannel end
print("[CONFIG] Loaded from " .. TURTLE_CONFIG_FILE)
end
loadConfig()
-------------------------------------------------
-- Setup
-------------------------------------------------