is custom package.loaders a bad idea?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
jonraphaelson
Prole
Posts: 3
Joined: Thu Sep 28, 2023 2:58 pm
Location: Lafayette, CO
Contact:

is custom package.loaders a bad idea?

Post 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
User avatar
dusoft
Party member
Posts: 521
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: is custom package.loaders a bad idea?

Post 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.
jonraphaelson
Prole
Posts: 3
Joined: Thu Sep 28, 2023 2:58 pm
Location: Lafayette, CO
Contact:

Re: is custom package.loaders a bad idea?

Post by jonraphaelson »

yeah, I guess `require` itself isn't really pulling anything; that library looks nice, I'll dig into it, thank you!
User avatar
dusoft
Party member
Posts: 521
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: is custom package.loaders a bad idea?

Post by dusoft »

Try this fixed version.
Attachments
cargo.lua
updated to fix errors
(2.7 KiB) Downloaded 53 times
Ross
Citizen
Posts: 99
Joined: Tue Mar 13, 2018 12:12 pm
Contact:

Re: is custom package.loaders a bad idea?

Post 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.
jonraphaelson
Prole
Posts: 3
Joined: Thu Sep 28, 2023 2:58 pm
Location: Lafayette, CO
Contact:

Re: is custom package.loaders a bad idea?

Post 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!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 5 guests