Skip to content
Snippets Groups Projects
Commit f19c5db7 authored by Peter Nerlich's avatar Peter Nerlich
Browse files

sync patch for release-2.1.4

parents
Branches
No related merge requests found
patched/
[submodule "advtrains"]
path = advtrains
url = https://git.bananach.space/advtrains.git
# advtrains_luaautomation_sync
A patch for the Minetest mod *advtrains_luaautomation* (which belongs to the *advtrains* modpack).
*advtrains* adds trains and railway systems with interlocking to Minetest and *advtrains_luaautomation* is the component that enables players to program their own logic for setting routes and switches, e.g. when a train passes a special lua rail.
This is organized in sandboxed environments.
The code used in these environments can grow very complex very quickly, making it difficult to maintain (and maybe even collaborate on) within the limited interface the Minetest formspec API can provide.
This repository can be used to patch the *advtrains_luaautomation* mod to introduce a "Sync" button that fetches the init code for an environment from an external URL.
This way, the environment code can be developed using external tools and collaborated on using version control systems, and brought up to date inside the game with a click of a button (e.g. when pointing to a raw file in master on GitLab).
The mod must be declared in `secure.http_mods` to be able to use the http API.
Ensure your destination is reachable from within your Minetest servers network and your content doesn't need special cookies to view it.
## Creating the patched `advtrains_luaautomation` mod
1. Run `create_env.sh` to initialize the submodule *advtrains*, fetch the newest changes and create a working copy of *advtrains_luaautomation* in the directory `patched/`.
2. Run `apply_patch.sh` to apply the `sync.patch` file to `patched/`.
3. Copy `patched/` to your Minetest mods folder and rename it to `advtrains_luaautomation_sync/`.
4. Disable the original *advtrains_luaautomation* of the *advtrains* modpack and enable *advtrains_luaautomation_sync* for your world.
This can be done directly in your worlds `world.mt` or from within your Minetest client (the latter for local games only).
5. Ensure *advtrains_luaautomation_sync* is listed in your `minetest.conf` (e.g. `secure.http_mods = advtrains_luaautomation_sync,other_mod`)
Otherwise, the sync option will be disabled.
## Creating patches for new `advtrains` versions
As the original code base changes over the course of the continued development, the old patches cannot be cleanly applied and/or may no longer have the desired effect.
So the patch has to be continually verified to work with and, if necessary, adapted to new versions of *advtrains*.
1. Run `clean.sh` to delete `patched/` and reset `sync.patch` and the submodule to the commit recorded in HEAD of the superproject.
1. Run `create_env.sh` to ensure the submodule *advtrains* is initialized, fetch the newest changes and create a working copy of *advtrains_luaautomation* in the directory `patched/`.
You might patch it with `apply_patch.sh` and make a backup of `patched/` to e.g. `patched-2.3.0/` for reference.
2. Checkout the submodule in `advtrains/` to the desired commit to make the patch for.
3. Run `create_env.sh` again.
This won't change the currently checked out commit but replace `patched/` with a new working copy of `advtrains/advtrains_luaautomation/`.
You can also copy this manually.
6. Try to apply the old patch with `apply_patch.sh`.
This will most likely fail, leaving `.orig` and `.rej` files in `patched/`.
Even if it doesn't, it might well be that other changes compromise the functionality of the patch.
Study the patch and the diff for the original code base well and try to evaluate places where it might break.
Testing the functionality is a necessity.
5. Modify the files in `patched/`.
Fix the parts where the old patch couldn't be applied and where you have to add more changes to restore functionality.
6. Happy? Everything runs as planned?
Extract the minimal patch with `generate_patch.sh` and commit the result.
Tag it with the version number of *advtrains* if your patch was for a release.
Congrats! You're done! (Don't forget to push the commit though)
---
All shell scripts can be run from any directory, they will always execute where they are stored: In the project root.
advtrains @ b616ae44
Subproject commit b616ae4425b26d0163e9d5f5cb98121fa103e003
#!/usr/bin/env sh
cd $(dirname "${BASH_SOURCE[0]}")
SCRIPT_DIR=$(pwd)
[ ! -d patched ] &&
echo "Path patched/ doesn't exist!" &&
echo "Run create_env.sh first before applying the patch." &&
exit 1 ||
echo "patch -p0 < sync.patch" &&
patch -p0 < sync.patch
#!/usr/bin/env sh
cd $(dirname "${BASH_SOURCE[0]}")
SCRIPT_DIR=$(pwd)
git submodule update
rm -rf patched
git checkout sync.patch
#!/usr/bin/env sh
cd $(dirname "${BASH_SOURCE[0]}")
SCRIPT_DIR=$(pwd)
[ ! -f advtrains/init.lua ] &&
git submodule update --init ||
git submodule foreach git fetch
[ ! -d patched ] &&
yn="y" ||
read -p "Do you really want to overwrite any files in patched/ with the current contents of advtrains/advtrains_luaautomation [y/N]? " yn
case $yn in
[Yy]* )
echo "Copying 'advtrains/advtrains_luaautomation/' to 'patched/'..." &&
cp -r advtrains/advtrains_luaautomation patched
break;;
* )
echo "Aborting."
exit;;
esac
#!/usr/bin/env sh
cd $(dirname "${BASH_SOURCE[0]}")
SCRIPT_DIR=$(pwd)
[ ! -d patched ] &&
echo "Path patched/ doesn't exist!" &&
echo "Run create_env.sh first and modify the files inside before creating a patch." &&
exit 1 ||
echo "diff -ruN advtrains/advtrains_luaautomation/ patched/ > sync.patch" &&
diff -ruN advtrains/advtrains_luaautomation/ patched/ > sync.patch
diff -ruN advtrains/advtrains_luaautomation/chatcmds.lua patched/chatcmds.lua
--- advtrains/advtrains_luaautomation/chatcmds.lua 2021-10-12 19:04:17.221486017 +0200
+++ patched/chatcmds.lua 2021-10-12 22:43:01.077577672 +0200
@@ -4,6 +4,12 @@
--position helper.
--punching a node will result in that position being saved and inserted into a text field on the top of init form.
local punchpos={}
+local http
+
+local function setup_http(_http)
+ http = _http
+end
+table.insert(atlatc.setup_http, setup_http)
minetest.register_on_punchnode(function(pos, node, player, pointed_thing)
local pname=player:get_player_name()
@@ -22,6 +28,30 @@
.."button[4,0;2,1;save;Save] button[6,0;2,1;del;Delete Env.] field[8.1,0.5;2,1;punchpos;Last punched position;"..pp.."]"
.."textarea[0.2,1;10,10;code;Environment initialization code;"..minetest.formspec_escape(code).."]"
.."label[0,9.8;"..err.."]"
+ local msg = "Sync"
+ if env.sync_err then
+ msg = msg..", failed"
+ end
+ if not http then
+ msg = msg.." (disabled)"
+ end
+ form = form.."button[6.5,9.6;3,0.5;sync;"..msg.."]"
+ return form
+end
+
+local function get_sync_form(env, pname, message)
+ local url = env.sync_url or ""
+ local msg = env.sync_err and "ERROR: "..env.sync_err or message or ""
+ local form = "size[10,3]"
+ .."field[.5,.6;9.5,1;sync_url;URL;"..url.."]"
+ .."label[.5,1.2;"..minetest.formspec_escape("Be careful! This replaces the current init code for env "..env.name.." with the contents at the URL!").."]"
+ .."button[7.5,2;2,1;urlsave;Save URL only]"
+ .."label[4,0;"..msg.."]"
+ if http then
+ form = form.."button[.5,2;3,1;replace;Replace init code] button[4,2;3,1;replacerun;Replace and Run init code]"
+ else
+ form = form.."label[4,2;Sync disabled]"
+ end
return form
end
@@ -62,6 +92,44 @@
return
end
+ envname=string.match(formname, "^atlatc_sync_(.+)$")
+ if envname then
+ local env=atlatc.envs[envname]
+ if not env then return end
+
+ if fields.replace or fields.replacerun or fields.urlsave then
+ if fields.sync_url ~= env.sync_url then
+ env.sync_url = fields.sync_url
+ env.sync_err = nil
+ end
+ end
+ if env.syncing then return end
+
+ local old_code = env.init_code
+
+ local function cb(res)
+ if fields.replacerun and not env.sync_err then
+ env:run_initcode()
+ end
+ env.syncing = nil
+ local no_change = res and res.succeeded and res.data == old_code
+
+ if fields.urlsave or env.sync_err or no_change then
+ minetest.show_formspec(pname, formname, get_sync_form(env, pname, no_change and "Init code was identical to remote content." or nil))
+ else
+ minetest.show_formspec(pname, "atlatc_envsetup_"..envname, get_init_form(env, pname))
+ end
+ end
+
+ if fields.replace or fields.replacerun then
+ env.syncing = true
+ env:sync_initcode(cb)
+ else
+ cb()
+ end
+ return
+ end
+
envname=string.match(formname, "^atlatc_envsetup_(.+)$")
if not envname then return end
@@ -81,4 +149,7 @@
env:run_initcode()
minetest.show_formspec(pname, formname, get_init_form(env, pname))
end
+ if fields.sync then
+ minetest.show_formspec(pname, "atlatc_sync_"..envname, get_sync_form(env, pname))
+ end
end)
diff -ruN advtrains/advtrains_luaautomation/environment.lua patched/environment.lua
--- advtrains/advtrains_luaautomation/environment.lua 2021-10-12 19:04:17.221486017 +0200
+++ patched/environment.lua 2021-10-12 22:43:01.077577672 +0200
@@ -1,6 +1,13 @@
-------------
-- lua sandboxed environment
+local http
+
+local function setup_http(_http)
+ http = _http
+end
+table.insert(atlatc.setup_http, setup_http)
+
-- function to cross out functions and userdata.
-- modified from dump()
function atlatc.remove_invalid_data(o, nested)
@@ -34,11 +41,12 @@
self.fdata={}
self.init_code=data.init_code or ""
self.step_code=data.step_code or ""
+ self.sync_url=data.sync_url or ""
end,
save = function(self)
-- throw any function values out of the sdata table
self.sdata = atlatc.remove_invalid_data(self.sdata)
- return {sdata = self.sdata, init_code=self.init_code, step_code=self.step_code}
+ return {sdata = self.sdata, init_code=self.init_code, step_code=self.step_code, sync_url=self.sync_url}
end,
}
@@ -326,6 +334,32 @@
end
end
end
+function env_proto:sync_initcode(cb)
+ if self.sync_url and self.sync_url~="" then
+ if http then
+ http.fetch({
+ url = self.sync_url,
+ timeout = 5
+ }, function(res)
+ if res.completed then
+ if res.succeeded then
+ self.init_code = res.data
+ self.sync_err = nil
+ elseif res.timeout then
+ self.sync_err = "timeout"
+ else
+ self.sync_err = tostring(res.code)
+ end
+ if cb then
+ cb(res)
+ end
+ end
+ end)
+ else
+ -- no http :(
+ end
+ end
+end
--- class interface
@@ -356,7 +390,11 @@
env:run_stepcode()
end
end
-
+function atlatc.sync_initcode()
+ for envname, env in pairs(atlatc.envs) do
+ env:sync_initcode()
+ end
+end
diff -ruN advtrains/advtrains_luaautomation/init.lua patched/init.lua
--- advtrains/advtrains_luaautomation/init.lua 2021-10-12 19:04:17.221486017 +0200
+++ patched/init.lua 2021-10-12 22:43:01.077577672 +0200
@@ -12,7 +12,7 @@
--Privilege
--Only trusted players should be enabled to build stuff which can break the server.
-atlatc = { envs = {}}
+atlatc = { envs = {}, setup_http = {} }
minetest.register_privilege("atlatc", { description = "Player can place and modify LUA ATC components. Grant with care! Allows to execute bad LUA code.", give_to_singleplayer = false, default= false })
@@ -23,9 +23,9 @@
end
end
-local mp=minetest.get_modpath("advtrains_luaautomation")
+local mp=minetest.get_modpath("advtrains_luaautomation_sync")
if not mp then
- error("Mod name error: Mod folder is not named 'advtrains_luaautomation'!")
+ error("Mod name error: Mod folder is not named 'advtrains_luaautomation_sync'!")
end
dofile(mp.."/environment.lua")
dofile(mp.."/interrupt.lua")
@@ -97,6 +97,17 @@
end
+local http = minetest.request_http_api()
+if http then
+ minetest.log("info", "advtrains_luaautomation_sync has http!")
+ for _,v in ipairs(atlatc.setup_http) do
+ v(http)
+ end
+else
+ minetest.log("error", "advtrains_luaautomation_sync HAS NO HTTP! sync will be disabled!")
+end
+
+
-- globalstep for step code
local timer, step_int=0, 2
diff -ruN advtrains/advtrains_luaautomation/operation_panel.lua patched/operation_panel.lua
--- advtrains/advtrains_luaautomation/operation_panel.lua 2021-10-12 18:52:03.743798735 +0200
+++ patched/operation_panel.lua 2021-10-12 22:43:01.077577672 +0200
@@ -4,7 +4,7 @@
end
-minetest.register_node("advtrains_luaautomation:oppanel", {
+minetest.register_node(":advtrains_luaautomation:oppanel", {
drawtype = "normal",
tiles={"atlatc_oppanel.png"},
description = "LuaAutomation operation panel",
diff -ruN advtrains/advtrains_luaautomation/pcnaming.lua patched/pcnaming.lua
--- advtrains/advtrains_luaautomation/pcnaming.lua 2021-10-12 18:52:03.743798735 +0200
+++ patched/pcnaming.lua 2021-10-12 22:43:01.077577672 +0200
@@ -22,7 +22,7 @@
error("Invalid position supplied to " .. (func_name or "???")..": " .. dump(pos))
end
-minetest.register_craftitem("advtrains_luaautomation:pcnaming",{
+minetest.register_craftitem(":advtrains_luaautomation:pcnaming",{
description = attrans("Passive Component Naming Tool\n\nRight-click to name a passive component."),
groups = {cracky=1}, -- key=name, value=rating; rating=1..3.
inventory_image = "atlatc_pcnaming.png",
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment