lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


From: lua-l-bounces@lists.lua.org <lua-l-bounces@lists.lua.org> on behalf of Sean Conner <sean@conman.org>
> Does it ever occur to you to actually *try* to implement your ideas?  
> This idea, with a separate resource path, could be done in pure Lua.
> Add a package.rpath (for resource path), then replace require() to look for two
> parameters and handle it appropriately?

how about this:

---------- resource.lua
if not package.rpath then
	local path = {}
	for file in package.path:gmatch "[^;]*?" do
		if not path[file] then
			path[file] = 1
			table.insert(path,file);
		end
	end
	package.rpath = table.concat(path, ";")
end
local function resource (fname)
	local r = package.searchpath(fname, package.rpath, ":")
	if r then
		return function()
			local f = io.open(r)
			local t = f:read "*a"
			f:close()
			return t
		end
	end
end
table.insert(package.searchers, 2, resource)
return resource
----------

example of use:
require "resource"
icon = require "package:gopher.icon"