Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ General ➜ ImportXML to 'extend' plugins

ImportXML to 'extend' plugins

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Tsunami   USA  (204 posts)  Bio
Date Mon 06 Mar 2006 01:56 AM (UTC)
Message
I was attempting to allow users of one of my plugins to enable a certain parts at their discretion. Enabling a section would load an assortment to triggers and aliases as well as associated script functions. I've tried this a couple different ways so far, but the problem is always that the triggers are matched, but they cannot find the script function to call in response. So, say user wants to load an extension called 'ext', then dofile('ext.lua') is called. ext.lua would look something like this:


function called_by_trigger()
  dostuff()
end

world.ImportXML(
[[
<triggers>
  <trigger
   enabled="n"
   group="Group1"
   match="^Match on this\.$"
   regexp="y"
   script="called_by_trigger"
   sequence="100"
  >
  </trigger>
</triggers>
]]
)


This didn't work, so I tried placing the trigger function inside the ImportXML function using <script> tags as if in a plugin. Still no go. Any suggestions on a way to get this to work? I would technically like this to be removable, but since triggers and aliases I wish to load must have specific groups and names, I have no way to distinguish them from other triggers and aliases. Still, this is only a secondary and minor concern, first I'd like to get this to work. Thanks!
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #1 on Mon 06 Mar 2006 06:49 AM (UTC)
Message
Quote:

This didn't work ...


In what sense did it not work? Syntax error? Trigger not added?

Can you make a short test plugin to show the context in which you are calling ext.lua?

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Tsunami   USA  (204 posts)  Bio
Date Reply #2 on Tue 07 Mar 2006 09:42 PM (UTC)
Message
The triggers matched, but were unable to find the script functions to call. Either the functions are not being parsed, which seems unlikely, or they are somehow in a different space from the triggers? The function which loads ext.lua is pretty simple, called from an alias. This function itself is located in a .lua file which has itself been included through require().


function load_class(name,output,wildcs)
  dofile(path .. 'modules\scripts\defences\' .. string.lower(wildcs[1]) .. '.lua')
  world.Note('SUCCESSFUL CLASS LOAD.')
end


So the structure is like this. Main plugin file is Main.xml. Mail.xml uses XML include statements to include Secondary.xml. Secondary.xml contains triggers, aliases, and just barely enough script to require('SecondaryCode.lua'). SecondaryCode.lua contains the function above, which is called by an alias located in Secondary.xml. The file that the load_class function attempts to load is basically just what was in my previous post. Thanks!

of secondary importance, is there a way to determine whether dofile is succesful? I'll have to stop being lazy and go look at the lua error catching docs, heh.
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #3 on Tue 07 Mar 2006 11:24 PM (UTC)
Message
For how dofile works, see:

http://www.gammon.com.au/scripts/doc.php?general=lua_base


It raises an error, however the snippet there shows how you could use loadfile and catch errors yourself.


As for your problem, personally I wouldn't do the dofile inside an alias, I think you have deferred its execution too long.

When MUSHclient loads triggers etc. it checks to see if the script function exists. Making it exist later on will not work.

The simple solution is to put the dofile command outside a function, that is at global scope. Then it is executed immediately. Putting it inside a function means it will not be executed until the function is executed, which is too late.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Tsunami   USA  (204 posts)  Bio
Date Reply #4 on Wed 08 Mar 2006 05:06 AM (UTC)
Message
I'm not sure thats the problem. The functions called by the triggers in ext.lua are either already defined (no problem with those) or are defined in ext.lua, which means they should be loaded at the same time as any triggers in ext.lua. No triggers outside ext.lua call anything inside ext.lua, since ext.lua's existance is not guarenteed.
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #5 on Wed 08 Mar 2006 05:14 AM (UTC)
Message
Quote:

SecondaryCode.lua contains the function above, which is called by an alias located in Secondary.xml.


I'm not sure what you mean by this. An alias is normally done when you type something.


Perhaps make up a small example to demonstrate the exact problem, and post the contents of all the files. I think it is something structural you are doing. All things in the plugin go into the same script space.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Tsunami   USA  (204 posts)  Bio
Date Reply #6 on Wed 08 Mar 2006 04:29 PM (UTC)

Amended on Wed 08 Mar 2006 04:37 PM (UTC) by Tsunami

Message
What I meant was the user types in 'loadfile ext' or something which calls the function. But in any case, here's all my files, extraneous stuff stripped out:

Main.xml:
Quote:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Sunday, February 06, 2005, 3:53:56 PM -->
<!-- MuClient version 3.70 -->

<!--
Comments:
-->

<muclient>
<plugin
  name="Main"
  author="Tsunami"
  id="c3fda3e0e77a5fbebf3c5e10"
  language="Lua"
  save_state="y"
  date_written="2006-02-06 15:53:56"
  requires="3.7"
  version="4.0"
>
</plugin>

<include name="modules\Secondary.xml"/>

<aliases>
...
</aliases>
<triggers>
...
</triggers>

<script>
<![CDATA[

--defined in Secondary.xml
if(not MODULE) then world.Note('ERROR - MISSING MODULE') end

function OnPluginInstall()
...
end
function OnPluginSaveState()
...
end

]]>
</script> 
</muclient>


Secondary.xml:
Quote:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE secondary>
<triggers>
...
</triggers>
<aliases>
...
<alias
   script="load_class"
   match="^LoadClass (\w+)$"
   enabled="y"
   regexp="y"
>
</alias>
...
</aliases>

<script>
<![CDATA[

_,_,path = string.find(GetPluginInfo(GetPluginID(),6),'^(.+\)')
require(path .. 'modules\scripts\secondary.lua')

if(CTRL_DEBUG) then
  world.Note('MODULE LOADED.')
end

]]>
</script>


secondary.lua:
Quote:
MODULE = true

function load_class(name,output,wildcs)
  if(class_loaded) then
    world.Note('CLASS ALREADY LOADED. YOU MUST RELOAD PLUGIN FIRST.')
  else
    require(path .. 'modules\scripts\exts\' .. string.lower(wildcs[1]) .. '.lua')
    world.Note('SUCCESSFULL LOAD OF CLASS.')
    class_loaded = true
  end
end


And suppose there is a file called ext.lua in modules\scripts\exts\ it would look like this:
Quote:
function on_trigger(name,output,wildcs)
...
end

world.ImportXML(
[[
<triggers>
  <trigger
   enabled="y"
   name="Name"
   group="Group"
   match="^blahblahblah$"
   regexp="y"
   script="on_trigger"
   sequence="100"
  >
  </trigger>
</triggers>
]]
)



Thats the structure of all my files, everything works fine except for trying to load ext.lua on demand. To do so, the user would type 'LoadClass ext'. Thanks!
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #7 on Thu 09 Mar 2006 12:20 AM (UTC)
Message
There is a test in the "Load World" function (which is also used for ImportXML), that it doesn't find script entry points if a plugin is doing ImportXML. I can't remember the reason for it, and am reluctant to take the test out.

A work-around for you is to manually set the script function in an extra line, this correctly sets the entry point, like this (in ext.lua) :


function on_trigger(name,output,wildcs)
  Note ("In on_trigger")
end

world.ImportXML(
[[
<triggers>
  <trigger
   enabled="y"
   name="Name"
   group="Group"
   match="^blahblahblah$"
   regexp="y"
   sequence="100"
  >
  </trigger>
</triggers>
]]
)

SetTriggerOption ("Name", "script", "on_trigger")


In this case I have removed the script line from the actual ImportXML, and done the equivalent afterwards. This works, and I can correctly process the trigger.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Tsunami   USA  (204 posts)  Bio
Date Reply #8 on Thu 09 Mar 2006 06:52 PM (UTC)

Amended on Thu 09 Mar 2006 06:56 PM (UTC) by Tsunami

Message
Ok, thanks for the work around, I'll be using that. It might get a little messy since some of the triggers will have basically fluff names while the others have necessary ones, but it should be workable. However, I still haven't gotten xpcall to work for some reason, I'll be posting on that later when I got some time. Thanks!
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


25,939 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.