VE Lua Documentation

Press F to search!

loadModulesInDirectory

Definition


-- @/lua/common/extensions.lua:695

local function loadModulesInDirectory(directory, excludeSubdirectories)
  -- log('I', logTag, "loadModulesInDirectory called...")
  --[[ -- Game engine version not working on libbeamng side

  local luaFiles = FS:findFiles(directory, '*.lua', -1, true, false)
  for _,luaFilename in pairs(luaFiles) do
    load(luaFilename:sub(1,-5))  -- strip '.lua'
  end
  ]]

  --local savedPath = package.path
  --package.path = directory .. "/?.lua;".. package.path
  -- addModulePath(directory)
  local filePaths = FS:findFiles(directory, "*.lua", -1, true, false)

  if type(excludeSubdirectories) == 'table' then
    local processed = {}
    for _, file in ipairs(filePaths) do
      local skip = false
      for _,subDir in pairs(excludeSubdirectories) do
        if string.find(file, subDir) then
          skip = true
          break
        end
      end
      if not skip then
        table.insert(processed, file)
      end
    end
    filePaths = processed
  end

  for _, file in ipairs(filePaths) do
    -- find the lua module files now
    if not file then break end
    if FS:fileExists(file) then
      -- loading at a root "" which signifies "global space", maintains backwards compatibility with the old
      -- behaviour of just loading using the filename from the path. This is NECESSARY to not break mods
      -- without it, vehicle extensions like custom_input would come out as custom/input which is wrong
      loadAtRoot(file:sub(1,-5), "")
    end
  end
  --package.path = savedPath
  resolveDependencies()
  table.clear(luaExtensionFuncs)  -- clear the hook function cache
  processLoadedFreshList()
end

Callers

@/lua/vehicle/main.lua
  vehiclePath = path
  extensions.loadModulesInDirectory(path .. "/lua", {"controller", "powertrain", "energyStorage"})
  -- load the extensions at this point in time, so the whole jbeam is parsed already
  extensions.loadModulesInDirectory("lua/vehicle/extensions/auto")