diff options
| author | 1F616EMO <root@1f616emo.xyz> | 2026-02-06 13:29:29 +0800 |
|---|---|---|
| committer | orwell <orwell@bleipb.de> | 2026-03-22 17:34:55 +0100 |
| commit | 83c571032c9f91c78ac17965b355285b5ed60332 (patch) | |
| tree | f099e79c65eac5f7cc50f7527755de7e88fce26f /advtrains_luaautomation | |
| parent | 5368267878c49f2d1c79fbaa54b47c2cd563c9d4 (diff) | |
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 <root@1f616emo.xyz>
Diffstat (limited to 'advtrains_luaautomation')
| -rw-r--r-- | advtrains_luaautomation/active_common.lua | 46 |
1 files changed, 46 insertions, 0 deletions
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: <env 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={} |
