From 83c571032c9f91c78ac17965b355285b5ed60332 Mon Sep 17 00:00:00 2001 From: 1F616EMO Date: Fri, 6 Feb 2026 13:29:29 +0800 Subject: Add code headers to facilitate copy and paste This patch fixes the issue that environment and approach callback mode has to be set across copy and pastes through additional steps. By adding headers in the following format: -- ADVT-Environment-Name: tmp -- ADVT-Approach-Callback-Mode: 1 These settings will be automatically set when clicking "Save". Headers are only read when saved; therefore, codes can still modify the approach callback mode (or any other variables that later uses this header approach). This will speed up code copy and pastes, especially when handling codes that require approach callback. Signed-off-by: 1F616EMO --- advtrains_luaautomation/active_common.lua | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/advtrains_luaautomation/active_common.lua b/advtrains_luaautomation/active_common.lua index da0a99b..63ccfcd 100644 --- a/advtrains_luaautomation/active_common.lua +++ b/advtrains_luaautomation/active_common.lua @@ -87,6 +87,52 @@ function ac.on_receive_fields(pos, formname, fields, player) -- discard all schedules for this node advtrains.lines.sched.discard_all(advtrains.encode_pos(pos)) end + + -- Parse code headers + --[[ + Valid code headers: + * ADVT-Environment-Name: + * ADVT-Approach-Callback-Mode: <0/1/2> + ]] + local errmsg_lines = {} + local code_headers = {} + for k, v in nodetbl.code:gmatch("%-%- ADVT%-([%w%-]+): ([^\n\r]*)") do + code_headers[k] = v + end + + if code_headers["Environment-Name"] then + local env = code_headers["Environment-Name"]:trim() + if not atlatc.envs[env] then + errmsg_lines[#errmsg_lines+1] = S("Invalid environment name in header: @1", env) + else + nodetbl.env = env + end + code_headers["Environment-Name"] = nil + end + + if code_headers["Approach-Callback-Mode"] then + local mode_raw = code_headers["Approach-Callback-Mode"]:trim() + if mode_raw == "0" or mode_raw == "1" or mode_raw == "2" then + nodetbl.data = nodetbl.data or {} + nodetbl.data.__approach_callback_mode = tonumber(mode_raw) + else + errmsg_lines[#errmsg_lines+1] = S("Invalid approach callback mode in header: @1", mode_raw) + end + code_headers["Approach-Callback-Mode"] = nil + end + + local invalid_headers = {} + for k in pairs(code_headers) do + invalid_headers[#invalid_headers+1] = k + end + if #invalid_headers > 0 then + table.sort(invalid_headers) + errmsg_lines[#errmsg_lines+1] = S("Invalid attribute headers: @1", table.concat(invalid_headers, ", ")) + end + + if #errmsg_lines > 0 then + nodetbl.err = core.formspec_escape(table.concat(errmsg_lines, "; ")) + end end if fields.cle then nodetbl.data={} -- cgit v1.2.3