Skip to content

Commit 5ea5c01

Browse files
authored
Merge pull request #5 from unboundlopez/codex/review-squad-uniform.lua
Improve squad uniform file handling
2 parents ab9af68 + bb030d9 commit 5ea5c01

1 file changed

Lines changed: 25 additions & 8 deletions

File tree

‎squad-uniform.lua‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,43 @@ local json = require('json')
99
local UNIFORM_DIR = dfhack.getDFPath() .. '/dfhack-config/squad_uniform/'
1010

1111
local function ensure_uniform_dir()
12-
if not dfhack.filesystem.isdir(UNIFORM_DIR) then
13-
dfhack.filesystem.mkdir(UNIFORM_DIR)
12+
if dfhack.filesystem.isdir(UNIFORM_DIR) then
13+
return true
1414
end
15+
local ok, err = dfhack.filesystem.mkdir(UNIFORM_DIR)
16+
if not ok then
17+
dfhack.printerr('Failed to create uniform directory: ' .. tostring(err))
18+
end
19+
return ok
1520
end
1621

1722
local function is_valid_name(name)
1823
return name and #name > 0 and not name:find('[^%w%._%s]')
1924
end
2025

2126
local function get_uniform_files()
22-
ensure_uniform_dir()
27+
if not ensure_uniform_dir() then
28+
return {}
29+
end
2330
local files = {}
24-
local list = dfhack.filesystem.listdir(UNIFORM_DIR)
31+
local list, err = dfhack.filesystem.listdir(UNIFORM_DIR)
2532
if list then
2633
for _, file in ipairs(list) do
2734
if file:match('%.dfuniform$') then
2835
table.insert(files, file)
2936
end
3037
end
3138
table.sort(files)
39+
elseif err then
40+
dfhack.printerr('Failed to list uniform files: ' .. tostring(err))
3241
end
3342
return files
3443
end
3544

3645
local function import_uniform_file(filepath)
37-
ensure_uniform_dir()
46+
if not ensure_uniform_dir() then
47+
return false, 'Uniform directory is unavailable.'
48+
end
3849
local file, err = io.open(filepath, 'r')
3950
if not file then
4051
return false, 'Failed to open file for reading: ' .. tostring(err)
@@ -54,7 +65,7 @@ local function import_uniform_file(filepath)
5465
end
5566

5667
local nickname = data.nickname
57-
if not nickname or nickname == '' then
68+
if not nickname or nickname == '' or not is_valid_name(nickname) then
5869
nickname = filepath:match('([^/\\]+)%.dfuniform$') or 'ImportedUniform'
5970
end
6071

@@ -105,7 +116,9 @@ local function import_uniform_file(filepath)
105116
end
106117

107118
local function export_uniform_file(filepath)
108-
ensure_uniform_dir()
119+
if not ensure_uniform_dir() then
120+
return false, 'Uniform directory is unavailable.'
121+
end
109122
local panel = df.global.game.main_interface and df.global.game.main_interface.squad_equipment
110123
if not panel then
111124
return false, 'Squad equipment panel is not available. Please open the Military > Equipment screen.'
@@ -200,7 +213,11 @@ local function ImportUniformDialog()
200213
dialogs.showYesNoPrompt('Delete uniform file?',
201214
'Are you sure you want to delete "' .. fname .. '"?', nil,
202215
function()
203-
os.remove(fname)
216+
local ok, err = os.remove(fname)
217+
if not ok then
218+
dialogs.showMessage('Delete failed', 'Unable to delete "' .. fname .. '": ' .. tostring(err))
219+
return
220+
end
204221
dfhack.println('Deleted: ' .. fname)
205222
local list = get_dlg().subviews.list
206223
local filter = list:getFilter()

0 commit comments

Comments
 (0)