Page 1 of 1

is custom package.loaders a bad idea?

Posted: Thu Sep 28, 2023 3:07 pm
by jonraphaelson
Hey y'all, I'm playing around with the driver parts of my application, just trying new things out, and I ended up making this little loader which allows using `require` to load non-lua (non-code) files, kind of like we do in webpack.

Using it, you're able to do the following:

Code: Select all

-- insert just before normal loader
-- glsl is the file extension, newShader is the callback (gets the file content)
table.insert(package.loaders, 2, extension_loader("glsl", love.graphics.newShader))

function love.load()
  myshader = require("some.mod.path!glsl")
  -- note the path, myshader is a shader object
end
This feels nice and not too clever, which makes me concerned, because I can find almost no references to anything much like this in the forums or wiki, which makes me think this must be a bad idea for reasons that I don't understand, otherwise I imagine folk would stumble into it and talk about it.

As such, as this a bad idea?

Code: Select all

local extension_loader = function(ext, cb)
   return function(modname)
      -- only handle paths that end in !ext (eg. my.mod.effect!glsl)
      if (not string.find(modname, "!"..ext.."$")) then
         return nil
      end

      -- normalize the path
      local modpath = modname
      modpath = string.gsub(modpath, "%.", "/")
      modpath = string.gsub(modpath, "!", ".")

      -- read and return a partially applied callback 
      local content, _ = love.filesystem.read(modpath)
      if content then
         return function() cb(content) end, modpath
      else
         return "no file found named `"..modpath.."` (using `"..ext.."` loader)"
      end
   end
end
Thanks, y'all!
- Jon

Re: is custom package.loaders a bad idea?

Posted: Thu Sep 28, 2023 7:38 pm
by dusoft
If I understand what you are trying to do (asset loading?) then Cargo (https://github.com/bjornbytes/cargo) works in a similar fashion (code is a bit outdated and I had to fix its loader logic). It does not use `require` though.

Re: is custom package.loaders a bad idea?

Posted: Thu Sep 28, 2023 8:14 pm
by jonraphaelson
yeah, I guess `require` itself isn't really pulling anything; that library looks nice, I'll dig into it, thank you!

Re: is custom package.loaders a bad idea?

Posted: Thu Sep 28, 2023 8:37 pm
by dusoft
Try this fixed version.

Re: is custom package.loaders a bad idea?

Posted: Fri Sep 29, 2023 5:31 pm
by Ross
Nah, I don't think there's anything wrong with making a new package loader, that's what it's there for. It's just a bit obscure, and not particularly necessary. If you like it, do it. In this case, love.graphics.newShader can already take a file path, so you're only making a very small shortcut to it.

Re: is custom package.loaders a bad idea?

Posted: Fri Sep 29, 2023 7:55 pm
by jonraphaelson
Ok, thinking more about it, `require` really isn't doing anything special; it's just a built-in function with a lookup priority and caching.

Digging through the code in cargo was really helpful, @dusoft, thank you!

@Ross, yeah, though it gives you path-based caching too. more thinking about it demystified it a bit, and the library suggestion was helpful to get a bigger picture

cheers!