Page 1 of 1

A centralized script system

Posted: Sun Aug 07, 2011 4:43 pm
by chris
I'm unsure whether a similar incarnation of this script exists, but if not, here is a way to share scripts easily across sessions. To use it, make a script with the following code in it, and make sure it has a sysLoadEvent handler. Then, instead of importing scripts across profiles and having to do it over and over again with updates, write your code in .lua files, and store them in a common repository. My respository is my mudlet home dir+shared/. To load a script saved in here, do: loadShared('package name'). For larger packages with many subfiles, you can create a directory and load all the files within that directory with: loadSharedFolder('3k Mapper') (3K mapper being my shared directory).

What doesn't work: event handlers that are bound. Everything else works for me (including Geyser stuff so you can share your tabbed overlays easily, and have their load method bound to the resizewindow to keep them in the right proportions!)
Code: [show] | [select all] lua
function getBaseDir()
	local dir = getMudletHomeDir()
	local loc = string.find(dir, "[\\/]profiles[\\/]")
	local basedir = string.sub(dir, 0, loc)
	return basedir
end

function loadShared(filename)
	local newpath = getBaseDir()..'shared/'
	dofile(newpath..filename..'.lua')
end

function scandir(dirname)
--from http://www.wellho.net/resources/ex.php4?item=u112/dlisting
	callit = os.tmpname()
	os.execute("ls -a1 "..dirname .. " >"..callit)
	f = io.open(callit,"r")
	rv = f:read("*all")
	f:close()
	os.remove(callit)
	tabby = {}
	local from = 1
	local delim_from, delim_to = string.find( rv, "\n", from )
	while delim_from do
		table.insert( tabby, string.sub( rv, from , delim_from-1 ) )
		from = delim_to + 1
		delim_from, delim_to = string.find( rv, "\n", from )
	end
	-- table.insert( tabby, string.sub( rv, from ) )
	-- Comment out eliminates blank line on end!
	return tabby
	end

function loadSharedFolder(folder)
	local linuxfolder = string.gsub(folder,' ', '\\ ')
	local basedir = getBaseDir()..'shared/'
	local files = scandir(basedir..linuxfolder)
	local mudletdir = basedir..folder..'/'
	for i,v in pairs(files) do
		if v ~= '.' and v ~= '..' then
			dofile(mudletdir..v)
		end
	end
end

Re: A centralized script system

Posted: Sun Aug 07, 2011 4:46 pm
by chris
Also, the directory listing is very specific to linux. Windows users will need to see how to get lua to list directories, and if you do please post it and i'll update it to address windows/linux specifics.

Re: A centralized script system

Posted: Mon Aug 08, 2011 3:45 am
by Omni
can use the registerAnonymousEventHandler() function for event handlers.

Re: A centralized script system

Posted: Tue Feb 05, 2013 2:06 pm
by Panorama
Hey, I just started trying out Mudlet and I've been looking for something like this to keep scripts in sync between multiple profiles. Is this still the best way or have you found a better solution since then?

Re: A centralized script system

Posted: Tue Feb 05, 2013 2:44 pm
by chris
Modules have replaced this system.

Re: A centralized script system

Posted: Tue Feb 05, 2013 3:45 pm
by Panorama
Got some pointers about modules? There doesn't seem to be much on the wiki.

Re: A centralized script system

Posted: Wed Feb 06, 2013 2:59 am
by chris
Sure, install by Toolbox -> Module Manager

If you choose to Save & Sync them, any changes you make while in the mudlet-editor will propagate to other modules. Priority is the load order, in case you have modules that depend on each other.