1function RunSaveGame(name, menu)
2	HexTiles = nil
3	HexTiles = {}
4	RawTiles = nil
5	RawTiles = {}
6	LoadedGame = true
7	local saved_globals = {}
8	saved_globals.Achievements = copy(Achievements)
9	saved_globals.Preferences = copy(wyr.preferences)
10	Achievements = nil
11	wyr.preferences = nil
12	if (menu ~= nil) then
13		if (SaveGame(name) == -1) then
14			local confirm = WarGameMenu(panel(4))
15			confirm:resize(288,128)
16			confirm:addLabel(_("Cannot save game to file:"), 288 / 2, 11)
17			confirm:addLabel(name, 288 / 2, 31)
18			confirm:addHalfButton(_("~!OK"), "o", 1 * (288 / 3), 128 - 16 - 27, function() confirm:stop() end)
19			confirm:run(false)
20		else
21			UI.StatusLine:Set(_("Saved game to:") .. " " .. name)
22			menu:stop()
23		end
24	else
25		SaveGame(name)
26	end
27	Achievements = copy(saved_globals.Achievements)
28	wyr.preferences = copy(saved_globals.Preferences)
29	LoadedGame = false
30end
31
32function RunConfirmErase(name, menu)
33  local confirm = WarGameMenu(panel(4))
34
35  confirm:resize(288,128)
36
37  confirm:addLabel(name, 288 / 2, 11)
38  confirm:addLabel(_("File exists, are you sure?"), 288 / 2, 31)
39
40  confirm:addHalfButton(_("~!Yes"), "y", 1 * (288 / 3) - 90, 128 - 16 - 27,
41    function()
42        confirm:stop()
43        RunSaveGame(name, menu)
44    end)
45
46  confirm:addHalfButton(_("~!No"), "n", 3 * (288 / 3) - 116, 128 - 16 - 27,
47    function() confirm:stop() end)
48
49  confirm:run(false)
50end
51
52function RunSaveMenu()
53  local menu = WarGameMenu(panel(3))
54  menu:resize(384, 256)
55
56  menu:addLabel(_("Save Game"), 384 / 2, 11)
57
58  local t = menu:addTextInputField("game.sav",
59    (384 - 300 - 18) / 2, 11 + 24, 318)
60
61  local browser = menu:addBrowser("~save", ".sav.gz$",
62    (384 - 300 - 18) / 2, 11 + 24 + 22, 318, 126)
63  local function cb(s)
64    t:setText(browser:getSelectedItem())
65  end
66  browser:setActionCallback(cb)
67
68  menu:addHalfButton(_("~!Save"), "s", (384 - 300 - 18) / 2, 256 - 16 - 27,
69    function()
70      local name = t:getText()
71      -- check for an empty string
72      if (string.len(name) == 0) then
73        return
74      end
75      -- strip .gz
76      if (string.find(name, ".gz$") ~= nil) then
77        name = string.sub(name, 1, string.len(name) - 3)
78      end
79      -- append .sav
80      if (string.find(name, ".sav$") == nil) then
81        name = name .. ".sav"
82      end
83      -- replace invalid chars with underscore
84      local t = {"\\", "/", ":", "*", "?", "\"", "<", ">", "|"}
85      table.foreachi(t, function(k,v) name = string.gsub(name, v, "_") end)
86
87      if (browser:exists(name .. ".gz")) then
88        RunConfirmErase(name, menu)
89      else
90        RunSaveGame(name, menu)
91      end
92    end)
93
94	local sortByCheckBox
95	sortByCheckBox = menu:addImageCheckBox(_("Show Latest First"), (384 - 300 - 18) / 2, 256 - 16 - 27 - 25,
96	function()
97		wyr.preferences.SortSaveGamesByTime = sortByCheckBox:isMarked()
98		SavePreferences()
99
100		if (wyr.preferences.SortSaveGamesByTime) then
101			browser:sortByTime()
102		else
103			browser:sortByName()
104		end
105	end)
106	sortByCheckBox:setMarked(wyr.preferences.SortSaveGamesByTime)
107	if (wyr.preferences.SortSaveGamesByTime) then
108		browser:sortByTime()
109	end
110
111	menu:addHalfButton(_("~!Cancel"), "c", 384 - ((384 - 300 - 18) / 2) - 106, 256 - 16 - 27,
112		function() menu:stop() end)
113
114	menu:run(false)
115end
116
117