The Twilight Sun

Aspect (plugin infrastructure)

by Jonathan on Mar.23, 2010, under Aspect

This is still entirely conceptual at the moment, so don’t get too excited. ;) Also, if you’re not too into technical stuff, you might want to read only the first paragraph.

I’m planning on making Aspect extensible to a certain extent. Users will be able to create plugins written in Lua, run on the server in a highly sandboxed environment, with a specialized API so the plugins can do useful stuff. One such API will probably be a widget interface, to create and manage HTML widgets on the browser side. This means you could create plugins that manage a visual set of stat gauges, or show a visual map, etc etc. in a similar vein to MUSHclient and Mudlet.

Now, plugins will be run on the server. This is because (a) I don’t think you can run Javascript sandboxed very easily, (b) it makes it much harder to communicate between the plugins and Aspect and the MUD, and (c) Lua is just so much nicer to script this kind of stuff with. For speed and manageability, I’ve also decided that all plugins will share the same Lua interpreter space. You have to be careful with security in all cases here, because third-party scripts will be running on the Aspect server. You don’t want arbitrary plugins to share player info, or clash with eachother in other ways.

Obviously I have to only give scripts access to utilities I want them to have access to. But what if the plugin goes into an infinite loop, or just takes too much time to execute? In the best case, it will lag everyone else connected to Aspect. That’s clearly not a good thing!

There is functionality in Lua to set a “hook” which can be called automatically every so often. Theoretically, I can use this to halt execution, go do other stuff, then come back to the plugins. But we also want some way to keep plugins completely separate, and we don’t want any one plugin to hog all the time so no other plugin gets a chance. It’s a bit of a sticky situation.

First of all, we can isolate each plugin by starting each one in a coroutine, and setting the coroutine functions environment to its own special table. Coroutines are like threads, but they’re cooperative rather than preemptive. That is to say, a coroutine has to say “Okay, I’m going to take a break now” or “Hey, you, do some work” explicitly in order for another coroutine to begin. And changing the environment keeps the plugins from sharing any state.

Now we’re down to one more problem (or at least the last major one for now). I mentioned before that I don’t want any one plugin to hog all the time. I also mentioned hook functions. It just so happens that you can set a hook function separately for each coroutine. If we could yield from that hook, it would be almost like preemptive threads. And while you can’t yield from a hook set with debug.sethook (in Lua), you can from a hook set with lua_sethook (in C). So I just have to figure out how to write C-based hook functions to yield every so often.

So that’s a rather verbose explanation of how I want to implement plugins. Well, it’s 2am and I needed to talk. :P Tl;dr summary: every plugin will be a completely isolated Lua coroutine that shares execution time with other plugins. Plugins will be able to do a lot of interesting things, like create and manage visual widgets on the user’s browser, like status bars and maps and stuff, and could even augment/replace the default output window and input bar. It’ll be extremely extensible.

Now if only I could figure out how lua_yield works.
~Jonathan

:,

1 Comment for this entry

  • Tethryn

    Please, please, PLEASE make an Autosipper! T_T Also, I have MUSH 4.61 now, and I have ONLY your plugins installed (ATCP, Gauges, Compass, and Map), but none of them seem to be working. Is there any way to fix this?

Leave a Reply

*

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...