Skip to content

Commit 274e7c3

Browse files
committed
Add load-save script and adjust gui/load-screen to work with it
Closes DFHack/dfhack#755
1 parent c551008 commit 274e7c3

2 files changed

Lines changed: 86 additions & 3 deletions

File tree

‎gui/load-screen.lua‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,6 @@ function state_change_handler(evt)
469469
if evt ~= SC_VIEWSCREEN_CHANGED then return end
470470
table.remove(focus_stack, 1)
471471
table.insert(focus_stack, dfhack.gui.getCurFocus())
472-
cur_focus = dfhack.gui.getCurFocus()
473472
if enabled and focus_stack[1] == 'title' and focus_stack[2] == 'loadgame' then
474473
mkscreen()
475474
end
@@ -482,8 +481,12 @@ function init()
482481
end
483482

484483
function mkscreen()
485-
scr = load_screen()
486-
scr:show()
484+
local parent = dfhack.gui.getCurViewscreen()
485+
-- Create this screen only above a viewscreen_loadgamest that isn't loading a game
486+
if df.viewscreen_loadgamest:is_instance(parent) and parent.loading == 0 then
487+
scr = load_screen()
488+
scr:show()
489+
end
487490
end
488491

489492
if initialized == nil then

‎load-save.lua‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
-- load a save non-interactively - intended to be run on startup
2+
--[====[
3+
4+
load-save
5+
=========
6+
7+
When run on the title screen or "load game" screen, loads the save with the
8+
given folder name without requiring interaction.
9+
10+
Example: ``load-save region``
11+
12+
This can also be run when starting DFHack from the command line::
13+
14+
./dfhack +load-game region1
15+
16+
(This is currently untested on Windows)
17+
18+
]====]
19+
20+
local gui = require 'gui'
21+
22+
local folder_name = ({...})[1] or qerror("No folder name given")
23+
24+
local loadgame_screen = dfhack.gui.getViewscreenByType(df.viewscreen_loadgamest, 0)
25+
if not loadgame_screen then
26+
local title_screen = dfhack.gui.getViewscreenByType(df.viewscreen_titlest, 0)
27+
if not title_screen then
28+
qerror("Can't find title or load game screen")
29+
end
30+
local found = false
31+
for idx, item in pairs(title_screen.menu_line_id) do
32+
if item == df.viewscreen_titlest.T_menu_line_id.Continue then
33+
found = true
34+
title_screen.sel_menu_line = idx
35+
break
36+
end
37+
end
38+
if not found then
39+
qerror("Can't find 'Continue Playing' option")
40+
end
41+
gui.simulateInput(title_screen, 'SELECT')
42+
end
43+
44+
loadgame_screen = dfhack.gui.getViewscreenByType(df.viewscreen_loadgamest, 0) or
45+
qerror("Can't find load game screen")
46+
47+
local found = false
48+
for idx, save in pairs(loadgame_screen.saves) do
49+
if save.folder_name == folder_name then
50+
found = true
51+
loadgame_screen.sel_idx = idx
52+
break
53+
end
54+
end
55+
if not found then
56+
qerror("Can't find save: " .. folder_name)
57+
end
58+
59+
--[[
60+
If gui/load-screen is active, it will be inserted later this frame and prevent
61+
the viewscreen_loadgamest from working. To work around this, SELECT is fed to
62+
the screen one frame later.
63+
]]
64+
function triggerLoad(loadgame_screen)
65+
-- Get rid of child screens (e.g. gui/load-screen)
66+
if loadgame_screen.child then
67+
local child = loadgame_screen.child
68+
while child.child do
69+
child = child.child
70+
end
71+
while child ~= loadgame_screen do
72+
child = child.parent
73+
dfhack.screen.dismiss(child.child)
74+
end
75+
end
76+
77+
gui.simulateInput(loadgame_screen, 'SELECT')
78+
end
79+
80+
dfhack.timeout(1, 'frames', function() triggerLoad(loadgame_screen) end)

0 commit comments

Comments
 (0)