From b9109af5050ecbe4ced061466d72d5eb60bc77b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branislav=20Setl=C3=A1k?= Date: Sat, 14 Oct 2023 04:49:03 +0200 Subject: [PATCH 1/8] corrupt jobs deleted from units --- fix/corrupt-jobs.lua | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 fix/corrupt-jobs.lua diff --git a/fix/corrupt-jobs.lua b/fix/corrupt-jobs.lua new file mode 100644 index 0000000000..a158d9642b --- /dev/null +++ b/fix/corrupt-jobs.lua @@ -0,0 +1,21 @@ +-- Deletes corrupted jobs from global job list + +local utils = require("utils") + +local count = 0 + +functioning_job_list = {} + +for _, job in ipairs(df.global.job_list) do + if job.id != -1 then + functioning_job_list[#functioning_job_list+1] = job + end +end + +for _, v in ipairs(df.global.world.units.all) do + if v.job.current_job then + if utils.linear_index(functioning_job_list, v.job.current_job) == nil then + v.job.current_job = nil + end + end +end From 5952f23a21896f986b641557147c8bf455635063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branislav=20Setl=C3=A1k?= Date: Sat, 14 Oct 2023 14:49:12 +0200 Subject: [PATCH 2/8] finished script and added it to control panel --- docs/fix/corrupt-jobs.rst | 15 +++++++++++++++ fix/corrupt-jobs.lua | 21 +++++++-------------- gui/control-panel.lua | 3 +++ 3 files changed, 25 insertions(+), 14 deletions(-) create mode 100644 docs/fix/corrupt-jobs.rst diff --git a/docs/fix/corrupt-jobs.rst b/docs/fix/corrupt-jobs.rst new file mode 100644 index 0000000000..36c74d5dd3 --- /dev/null +++ b/docs/fix/corrupt-jobs.rst @@ -0,0 +1,15 @@ +fix/corrupt-jobs +================== + +.. dfhack-tool:: + :summary: Removes jobs with an id of -1 from units. + :tags: fort bugfix + +This fix ensures that no units have a job with an id of -1 set as their current job. + +Usage +----- + +:: + + fix/corrupt-jobs diff --git a/fix/corrupt-jobs.lua b/fix/corrupt-jobs.lua index a158d9642b..bb43cbff32 100644 --- a/fix/corrupt-jobs.lua +++ b/fix/corrupt-jobs.lua @@ -1,21 +1,14 @@ --- Deletes corrupted jobs from global job list - -local utils = require("utils") +-- Deletes corrupted jobs from affected units local count = 0 -functioning_job_list = {} - -for _, job in ipairs(df.global.job_list) do - if job.id != -1 then - functioning_job_list[#functioning_job_list+1] = job +for _, unit in ipairs(df.global.world.units.all) do + if unit.job.current_job and unit.job.current_job.id == -1 then + unit.job.current_job = nil + count = count + 1 end end -for _, v in ipairs(df.global.world.units.all) do - if v.job.current_job then - if utils.linear_index(functioning_job_list, v.job.current_job) == nil then - v.job.current_job = nil - end - end +if count > 0 then + print(('removed %d corrupted job(s) from affected units'):format(count)) end diff --git a/gui/control-panel.lua b/gui/control-panel.lua index 1d642f0402..e8a731ee88 100644 --- a/gui/control-panel.lua +++ b/gui/control-panel.lua @@ -140,6 +140,9 @@ local REPEATS = { ['empty-wheelbarrows']={ desc='Empties wheelbarrows which have rocks stuck in them.', command={'--time', '1', '--timeUnits', 'days', '--command', '[', 'fix/empty-wheelbarrows', '-q', ']'}}, + ['corrupt-jobs']={ + desc='Removes corrupt jobs from affected units.', + command={'--time', '1', '--timeUnits', 'days', '--command', '[', 'fix/corrupt-jobs', ']'}}, } local REPEATS_LIST = {} for k in pairs(REPEATS) do From e626d72a386e9a558f22530369487920a3cc971a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branislav=20Setl=C3=A1k?= Date: Sat, 14 Oct 2023 15:01:22 +0200 Subject: [PATCH 3/8] fixed trailing whitespace --- fix/corrupt-jobs.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fix/corrupt-jobs.lua b/fix/corrupt-jobs.lua index bb43cbff32..ffa1dd64e7 100644 --- a/fix/corrupt-jobs.lua +++ b/fix/corrupt-jobs.lua @@ -3,10 +3,10 @@ local count = 0 for _, unit in ipairs(df.global.world.units.all) do - if unit.job.current_job and unit.job.current_job.id == -1 then + if unit.job.current_job and unit.job.current_job.id == -1 then unit.job.current_job = nil count = count + 1 - end + end end if count > 0 then From 5e2dddd4e06e896cde18e0c0d76d8f374dd900e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branislav=20Setl=C3=A1k?= Date: Sat, 14 Oct 2023 20:41:41 +0200 Subject: [PATCH 4/8] fixed doc and made the script run on load --- docs/fix/corrupt-jobs.rst | 4 ++-- fix/corrupt-jobs.lua | 28 +++++++++++++++++++++------- gui/control-panel.lua | 3 --- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/docs/fix/corrupt-jobs.rst b/docs/fix/corrupt-jobs.rst index 36c74d5dd3..1894a59857 100644 --- a/docs/fix/corrupt-jobs.rst +++ b/docs/fix/corrupt-jobs.rst @@ -1,11 +1,11 @@ fix/corrupt-jobs -================== +================ .. dfhack-tool:: :summary: Removes jobs with an id of -1 from units. :tags: fort bugfix -This fix ensures that no units have a job with an id of -1 set as their current job. +This fix cleans up corrupt jobs so they don't cause crashes. It runs automatically on fort load, so you don't have to run it manually. Usage ----- diff --git a/fix/corrupt-jobs.lua b/fix/corrupt-jobs.lua index ffa1dd64e7..afa6a82b09 100644 --- a/fix/corrupt-jobs.lua +++ b/fix/corrupt-jobs.lua @@ -1,14 +1,28 @@ -- Deletes corrupted jobs from affected units -local count = 0 +local GLOBAL_KEY = 'corrupt-jobs' -for _, unit in ipairs(df.global.world.units.all) do - if unit.job.current_job and unit.job.current_job.id == -1 then - unit.job.current_job = nil - count = count + 1 +function remove_bad_jobs() + local count = 0 + + for _, unit in ipairs(df.global.world.units.all) do + if unit.job.current_job and unit.job.current_job.id == -1 then + unit.job.current_job = nil + count = count + 1 + end + end + + if count > 0 then + print(('removed %d corrupted job(s) from affected units'):format(count)) end end -if count > 0 then - print(('removed %d corrupted job(s) from affected units'):format(count)) +dfhack.onStateChange[GLOBAL_KEY] = function(sc) + if sc == SC_MAP_LOADED then + remove_bad_jobs() + end end + +if dfhack_flags.module then + return +end \ No newline at end of file diff --git a/gui/control-panel.lua b/gui/control-panel.lua index e8a731ee88..1d642f0402 100644 --- a/gui/control-panel.lua +++ b/gui/control-panel.lua @@ -140,9 +140,6 @@ local REPEATS = { ['empty-wheelbarrows']={ desc='Empties wheelbarrows which have rocks stuck in them.', command={'--time', '1', '--timeUnits', 'days', '--command', '[', 'fix/empty-wheelbarrows', '-q', ']'}}, - ['corrupt-jobs']={ - desc='Removes corrupt jobs from affected units.', - command={'--time', '1', '--timeUnits', 'days', '--command', '[', 'fix/corrupt-jobs', ']'}}, } local REPEATS_LIST = {} for k in pairs(REPEATS) do From 0b02f6b7526a591e84dc3800c391c6ed4dc36b89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branislav=20Setl=C3=A1k?= Date: Sat, 14 Oct 2023 20:50:41 +0200 Subject: [PATCH 5/8] fixed enf of file --- fix/corrupt-jobs.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fix/corrupt-jobs.lua b/fix/corrupt-jobs.lua index afa6a82b09..9e998ad01a 100644 --- a/fix/corrupt-jobs.lua +++ b/fix/corrupt-jobs.lua @@ -25,4 +25,4 @@ end if dfhack_flags.module then return -end \ No newline at end of file +end From 62bf06f248c22ba335b97eaf3d7726a20e2dc580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branislav=20Setl=C3=A1k?= Date: Sat, 14 Oct 2023 20:57:24 +0200 Subject: [PATCH 6/8] added module bool --- fix/corrupt-jobs.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/fix/corrupt-jobs.lua b/fix/corrupt-jobs.lua index 9e998ad01a..b3c5965ccc 100644 --- a/fix/corrupt-jobs.lua +++ b/fix/corrupt-jobs.lua @@ -1,4 +1,5 @@ -- Deletes corrupted jobs from affected units +--@module = true local GLOBAL_KEY = 'corrupt-jobs' From 55ab19fa6ba6d1f0e8b8a43c7e61f641bf4023df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branislav=20Setl=C3=A1k?= Date: Sat, 14 Oct 2023 21:11:33 +0200 Subject: [PATCH 7/8] added changelog entry --- changelog.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/changelog.txt b/changelog.txt index d03ae453cc..04544ea87c 100644 --- a/changelog.txt +++ b/changelog.txt @@ -28,6 +28,7 @@ Template for new versions: ## New Tools - `add-recipe`: (reinstated) add reactions to your civ (e.g. for high boots if your civ didn't start with the ability to make high boots) +- `corrupt-jobs`: removes corrupted jobs from units at world load ## New Features - `drain-aquifer`: gained ability to drain just above or below a certain z-level From 625923175034aed61aa0f9bea9c272358588120e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branislav=20Setl=C3=A1k?= Date: Sat, 14 Oct 2023 21:18:11 +0200 Subject: [PATCH 8/8] added correct path --- changelog.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.txt b/changelog.txt index 04544ea87c..66b3b15d32 100644 --- a/changelog.txt +++ b/changelog.txt @@ -28,7 +28,7 @@ Template for new versions: ## New Tools - `add-recipe`: (reinstated) add reactions to your civ (e.g. for high boots if your civ didn't start with the ability to make high boots) -- `corrupt-jobs`: removes corrupted jobs from units at world load +- `fix/corrupt-jobs`: removes corrupted jobs from units at world load ## New Features - `drain-aquifer`: gained ability to drain just above or below a certain z-level