Skip to content

Commit 04be92e

Browse files
committed
Added simple path/filename validation field type
Allows specifying what it accepts (folder, filename, filename extensions) Supports having the path/filename relative to the current mod folder
1 parent 7d5ef41 commit 04be92e

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

src/ui/forms/fields/path.lua

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
local utils = require("utils")
2+
local mods = require("mods")
3+
local loadedState = require("loaded_state")
4+
local stringField = require("ui.forms.fields.string")
5+
6+
local pathField = {}
7+
8+
pathField.fieldType = "path"
9+
10+
function pathField.getElement(name, value, options)
11+
-- Add extra options and pass it onto string field
12+
13+
local allowEmpty = options.allowEmpty
14+
local allowMissingPath = options.allowMissingPath
15+
local allowFolders = options.allowFolders
16+
local allowFiles = options.allowFiles
17+
local allowedExtensions = options.allowedExtensions
18+
local relativeToMod = options.relativeToMod
19+
20+
options.validator = function(filename)
21+
local fieldEmpty = filename == nil or #filename == 0
22+
23+
if fieldEmpty then
24+
return allowEmpty ~= false
25+
end
26+
27+
if relativeToMod ~= false then
28+
local modPath = mods.getFilenameModPath(loadedState.filename)
29+
30+
if not modPath then
31+
return false
32+
end
33+
34+
filename = utils.joinpath(modPath, filename)
35+
end
36+
37+
local attributes = utils.pathAttributes(filename)
38+
39+
if not attributes and allowMissingPath ~= false then
40+
return false
41+
end
42+
43+
local attributeMode = attributes.mode or "missing"
44+
45+
if attributeMode == "directory" and not allowFolders then
46+
return false
47+
end
48+
49+
if attributeMode == "file" then
50+
local fileExtension = utils.fileExtension(filename)
51+
52+
if allowFiles == false then
53+
return false
54+
end
55+
56+
if allowedExtensions and not utils.contains(fileExtension, allowedExtensions) then
57+
return false
58+
end
59+
end
60+
61+
-- TODO - Custom validator at this point?
62+
63+
return true
64+
end
65+
66+
return stringField.getElement(name, value, options)
67+
end
68+
69+
return pathField

0 commit comments

Comments
 (0)