Save state

Posted by Tsunami on Fri 08 Sep 2006 05:41 PM — 5 posts, 21,162 views.

USA #0
My understanding is that save state should be called only ONCE when a plugin is uninstalled.

It is possible, although unlikely that a plugin will change its state during the OnPluginSaveState callback. Thus if this function is called a second time during plugin save state, a different, and perhaps unwanted state would be saved. This is precisely what happens in my plugin.

I allow user 'plugins' to be added to my own plugin. A list of current plugins is stored in an table, so that the same plugins can be loaded next timer. In the OnPluginSaveState function, each plugin is uninstalled, giving it a chance to save it's own state. The list of plugins is serialized before the plugins are uninstalled. However, by the end of the OnPluginSaveState function, the table which holds current plugins is empty, since all plugins have technically been removed. Thus when OnPluginSaveState is called for a second time, the empty table is serialized, and on restarting MUSHclient, none of the appropriate plugins are loaded by my plugin.

It would be easy enough for me to change it so that the state was not changed during the course of OnPluginSaveState. However, doing some testing, I have found that upon closing MUSHclient, OnPluginSaveState appears to be called once. If the 'some world variables have changed... do you wish to save..?' dialog pops up, on clicking yes, state is saved again for the plugins, and it appears that perhaps the save state fuction is called a third time somewhere else during the close process, although I am not sure about the last one.
Australia Forum Administrator #1
Plugins save their state under two circumstances:

  • The world is being saved - thus if a user is connected for a long time, and wishes to save their world "just in case" it also saves the plugin states as well.
  • The plugin is being removed. Plugins are removed as a side-effect of the world being closed.


Thus if you save the world, and then close it, then the plugins will save their state twice.

I think it is a design flaw for the "save document" code in your plugin to change it. As a general rule in almost any program I can think of, you can always save the document to disk without worrying about it changing.

I suppose I could keep a flag "does the state need to be changed?". But the whole idea of the OnPluginSaveState callback is that you might need to serialize from internal variables (eg. Lua variables) into MUSHclient variables before saving. Thus MUSHclient doesn't really know if the extra save is necessary or not.

I suggest you change the OnPluginSaveState code so it can be safely called repeatedly. For instance, keep a separate list of the "user plugins" you are uninstalling, and work through that. But make it so that you can always re-save.
Australia Forum Administrator #2
A more logical place for you might be OnPluginClose - that is called when the plugin is being closed, which should only happen once.
USA #3
Is OnPluginClose called when MUSHclient is closed. I don't believe it is; I have a plugin that takes around 10 seconds to save state, but there is no delay at all on closing MUSHclient.
Australia Forum Administrator #4
It is called in the plugin destructor, so yes, it should occur on MUSHclient close.

I made a test by playing a sound in the OnPluginClose, and closed MUSHclient, and I heard the sound. :)