glitchdetector icon

FiveM Lua File Struct Indexer

glitchdetector | PRO | 08/06/20 06:45:26 PM UTC | 0 ⭐ | 1665 👁️ | Never ⏰ | []
Lua |

2.48 KB

|

None

|

0 👍

/

0 👎

local function ends_with(str, ending)
   return ending == "" or str:sub(-#ending) == ending
end
local version = GetConvar("version", "win32")
local isWindowsVersion = ends_with(version, "win32") or ends_with(version, "win64")
-- can't test
 
local basedir = GetResourcePath("runcode")
 
-- Finds all files in the specified basedir
-- Optional search in subdirectories using subdirs
-- Absolute enables absolute paths, subdirs force absolute paths
function GetFilesInDirectory(basedir, subdirs, absolute)
    if subdirs then absolute = false end -- prevent prefixing path
    local files = {}
    local call = ("dir \"%s\" /a-D /b%s"):format(basedir, subdirs and " /s" or "")
    if not isWindowsVersion then
        -- make call a linux ls command or w/e
    end
    for fname in io.popen(call):lines() do 
        files[#files + 1] = (absolute and basedir .. "/" or "") .. fname
    end
    return files
end
 
-- Alternative that gets files and folders in a pre-formatted structure
function GetStructureFromDirectory(basedir)
    local structure = {}
    if isWindowsVersion then
        local function step(currentdir)
            local stepStructure = {}
            local folders = GetFoldersInDirectory(currentdir)
            for _, folder in next, folders do
                if not stepStructure.folders then stepStructure.folders = {} end
                stepStructure.folders[folder] = step(currentdir .. "/" .. folder)
            end
            local files = GetFilesInDirectory(currentdir)
            stepStructure.files = files
            return stepStructure
        end
        structure = step(basedir)
    else
        -- idk linux man
    end
    return structure
end
 
-- Finds all folders in the specified basedir
-- Absolute enables absolute paths
function GetFoldersInDirectory(basedir, absolute)
    local folders = {}
    local call = ("dir \"%s\" /aD /b"):format(basedir)
    if not isWindowsVersion then
        -- make call a linux ls command or w/e
    end
    for fname in io.popen(call):lines() do 
        folders[#folders + 1] = (absolute and basedir .. "/" or "") .. fname
    end
    return folders
end
 
print("files", json.encode(GetFilesInDirectory(basedir, true, true)))
local folders = GetFoldersInDirectory(basedir, true)
print("folders", json.encode(folders))
for _, folder in next, folders do
    print("->", folder, json.encode(GetFilesInDirectory(folder)))
end
 
print("struct", json.encode(GetStructureFromDirectory(basedir)))

Comments