Refactor transaction handling on startup

Changed the config option for handling transactions on startup from a simple boolean (clear transactions) to an enum ('keep', 'delete', 'rollback') for more granular control. Implemented the 'rollback' logic to revert transactions on startup. Added validation and error logging for the new config value.
```
This commit is contained in:
Yves-Marie Haussonne 2024-09-26 14:54:03 +02:00
parent 92eb472212
commit 8d9713e552
3 changed files with 40 additions and 3 deletions

View File

@ -2,4 +2,4 @@ worldedit_bigschems_max_lengths (Max sizes of chuncks) String { x: 256, y: 256,
worldedit_bigschems_failfast (fail fast tests) bool false
worldedit_bigschems_stop_server (stop test server after tests) bool true
worldedit_bigschems_max_undo (max undo length) int 10
worldedit_bigschems_clear_transactions (Clear existing transactions on startup) bool true
worldedit_bigschems_transactions_on_start (indicates what to do with lingering transactions on start : 'keep', keep them, 'delete": delete them, 'rollback': rollback transactions) enum rollback delete,keep,rollback

View File

@ -12,13 +12,34 @@ local transactions_path = worldedit_bigschems.transactions_path
local undo = dofile(worldedit_bigschems.modpath.worldedit_bigschems_undo .. "/undo.lua")
worldedit_bigschems.undo = undo
local clear_transactions = minetest.settings:get_bool("worldedit_bigschems_clear_transactions", true)
local transactions_on_start = minetest.settings:get("worldedit_bigschems_transactions_on_start")
if transactions_on_start == nil then
transactions_on_start = 'rollback'
end
if not string.find("|keep|delete|rollback|", transactions_on_start) then
minetest.log('error', "the value of worldedit_bigschems_transactions_on_start ('"..transactions_on_start.."') is not in keep,delete,rollback. Set it to the default value 'rollback'.")
transactions_on_start = 'rollback'
end
--TODO check path exists
minetest.mkdir(transactions_path)
if clear_transactions then
if transactions_on_start == 'delete' then
undo.clearTransactions()
elseif transactions_on_start == 'rollback' then
local transactionList = {}
for _,t in ipairs(undo.state.transactions) do
table.insert(transactionList, t)
end
for i=#transactionList,1,-1 do
local t = transactionList[i]
local transIndex = undo.findTransaction(t)
if transIndex ~= nil then
undo.removeTransaction(transIndex)
local trans = undo.Transaction.new(t)
trans:rollback()
end
end
end
dofile(worldedit_bigschems.modpath.worldedit_bigschems_undo .. "/commands.lua")

View File

@ -227,4 +227,20 @@ function undo.getTransaction(cmd)
return Transaction:new { command = cmd }
end
-- t is a table
function undo.findTransaction(t)
local self_index = nil
for i, v in ipairs(undo.state.transactions) do
if v.folder_name == t.folder_name then
self_index = i
break
end
end
return self_index
end
function undo.removeTransaction(i)
return table.remove(undo.state.transactions, i)
end
return undo