Advertisement
glitchdetector

FiveM Lua File Struct Indexer

Aug 6th, 2020
1,376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.48 KB | None | 0 0
  1. local function ends_with(str, ending)
  2.    return ending == "" or str:sub(-#ending) == ending
  3. end
  4. local version = GetConvar("version", "win32")
  5. local isWindowsVersion = ends_with(version, "win32") or ends_with(version, "win64")
  6. -- can't test
  7.  
  8. local basedir = GetResourcePath("runcode")
  9.  
  10. -- Finds all files in the specified basedir
  11. -- Optional search in subdirectories using subdirs
  12. -- Absolute enables absolute paths, subdirs force absolute paths
  13. function GetFilesInDirectory(basedir, subdirs, absolute)
  14.     if subdirs then absolute = false end -- prevent prefixing path
  15.     local files = {}
  16.     local call = ("dir \"%s\" /a-D /b%s"):format(basedir, subdirs and " /s" or "")
  17.     if not isWindowsVersion then
  18.         -- make call a linux ls command or w/e
  19.     end
  20.     for fname in io.popen(call):lines() do
  21.         files[#files + 1] = (absolute and basedir .. "/" or "") .. fname
  22.     end
  23.     return files
  24. end
  25.  
  26. -- Alternative that gets files and folders in a pre-formatted structure
  27. function GetStructureFromDirectory(basedir)
  28.     local structure = {}
  29.     if isWindowsVersion then
  30.         local function step(currentdir)
  31.             local stepStructure = {}
  32.             local folders = GetFoldersInDirectory(currentdir)
  33.             for _, folder in next, folders do
  34.                 if not stepStructure.folders then stepStructure.folders = {} end
  35.                 stepStructure.folders[folder] = step(currentdir .. "/" .. folder)
  36.             end
  37.             local files = GetFilesInDirectory(currentdir)
  38.             stepStructure.files = files
  39.             return stepStructure
  40.         end
  41.         structure = step(basedir)
  42.     else
  43.         -- idk linux man
  44.     end
  45.     return structure
  46. end
  47.  
  48. -- Finds all folders in the specified basedir
  49. -- Absolute enables absolute paths
  50. function GetFoldersInDirectory(basedir, absolute)
  51.     local folders = {}
  52.     local call = ("dir \"%s\" /aD /b"):format(basedir)
  53.     if not isWindowsVersion then
  54.         -- make call a linux ls command or w/e
  55.     end
  56.     for fname in io.popen(call):lines() do
  57.         folders[#folders + 1] = (absolute and basedir .. "/" or "") .. fname
  58.     end
  59.     return folders
  60. end
  61.  
  62. print("files", json.encode(GetFilesInDirectory(basedir, true, true)))
  63. local folders = GetFoldersInDirectory(basedir, true)
  64. print("folders", json.encode(folders))
  65. for _, folder in next, folders do
  66.     print("->", folder, json.encode(GetFilesInDirectory(folder)))
  67. end
  68.  
  69. print("struct", json.encode(GetStructureFromDirectory(basedir)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement