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 ➜ Lua ➜ Converting a Python plugin into Lua

Converting a Python plugin into Lua

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


Pages: 1 2  

Posted by Nexes   (65 posts)  Bio
Date Tue 07 Mar 2006 11:31 PM (UTC)

Amended on Tue 07 Mar 2006 11:59 PM (UTC) by Nexes

Message
Ok, I have the following plugin for Python:

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Sunday, March 13, 2005, 4:11 PM -->
<!-- MuClient version 3.65 -->

<!-- Plugin "PromptCatcher_Achaea" generated by Plugin Wizard -->

<muclient>
<plugin
   name="PromptCatcher_Achaea"
   author="Keldar"
   id="8f85913ad96bd5bc255e434a"
   language="Python"
   purpose="Terminates prompts with newlines"
   date_written="2005-03-13 16:10:15"
   requires="3.65"
   version="1.0"
   >

</plugin>

<script>
<![CDATA[

import re


lastHealth = None
lastMana = None
pat1 = re.compile(u"(\-\xff\xf9)($|\x0d\x0a)?")
pat2 = re.compile("^\x0d\x0a")
terminated = False
prompt = re.compile('(\x1b[(?:32|1;33|1;31)m)([0-9]+)(h,\x1b[(?:0;)?37m)(\x1b[(?:32|1;33|1;31)m)( [0-9]+)(m\x1b[(?:0;)?37m [c]?[e]?[x]?[k]?[d]?[b]?\-)')


def repl(mo):
    global terminated
    if mo.groups()[1] == "":
        terminated = True
    return "-\x0d\x0a"


def repl2(mo):
    global lastHealth
    global lastMana
    currHealth, currMana = int(mo.groups()[1]), int(mo.groups()[4])
    if lastHealth is None:
        
lastHealth = currHealth
    if lastMana is None:
        lastMana = currMana
    healthDiff, manaDiff = currHealth - lastHealth, currMana - lastMana
    lastHealth, lastMana = currHealth, currMana
    if healthDiff > 0:
        healthDiff = "\x1b[37m[\x1B[1;32m+%s\x1b[0;37m]%s" % (str(healthDiff), mo.groups()[0])
    elif healthDiff < 0:
        healthDiff = "\x1b[37m[\x1b[1;31m%s\x1b[0;37m]%s" % (str(healthDiff), mo.groups()[0])
    else:
        healthDiff = ""
    if manaDiff > 0:
        manaDiff = "\x1b[37m[\x1B[1;32m+%s\x1b[0;37m]%s" % (str(manaDiff), mo.groups()[3])
    elif manaDiff < 0:
        manaDiff = "\x1b[37m[\x1B[1;31m%s\x1b[0;37m]%s" % (str(manaDiff), mo.groups()[3])
    else:
        manaDiff = ""
    return "%s%s%s%s%s%s%s%s" % (mo.groups()[0], mo.groups()[1], healthDiff, mo.groups()[2], mo.groups()[3], mo.groups()[4], manaDiff, mo.groups()[5])
        
    
def OnPluginPacketReceived(packet):
    packet = prompt.sub(repl2, packet)
    global terminated
    global pat1
    global repl
    global pat2
    if terminated:
        packet = pat2.sub("",packet)
        terminated = False
    packet = pat1.sub(repl, packet)
    return packet


]]>
</script>
</muclient>

And I wanted to convert it to Lua so I could give it to people without worrying about them having Python, and I also wanted to modify it further. But as I read more about Lua's Regex, I learned that it really has minimal support and I don't think I'd want to try to do what this plugin is doing in it. Also, I read that Lua does not support unicode and I was worried that this plugin uses Unicode but I wasn't sure.

So, my questions are: does the Mushclient supplied Regular Expression support in Lua work with Unicode? Does this plugin even use unicode? Or is this plugin just not possible in Lua?

And, maybe, could someone just rewrite this for Lua really quickly? *hope*
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Tue 07 Mar 2006 11:49 PM (UTC)
Message
You need to properly escape forum codes like [b] in your code by writing \[b\], otherwise your code doesn't come out right at all -- look how it's all bold. :)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #2 on Tue 07 Mar 2006 11:54 PM (UTC)
Message
That's strange... Why can't you edit your posts?

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Nexes   (65 posts)  Bio
Date Reply #3 on Tue 07 Mar 2006 11:57 PM (UTC)

Amended on Tue 07 Mar 2006 11:59 PM (UTC) by Nexes

Message
I think it's cause I don't login and just supply my login information in the reply window. Dunno though.

EDIT: Yup it is. Deleted the junk.
Top

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

I was worried that this plugin uses Unicode but I wasn't sure


It doesn't seem to, to me. Why are you worried about Unicode? If you are working with normal characters, that most MUDs send, you won't have a big problem.

Quote:

does the Mushclient supplied Regular Expression support in Lua work with Unicode?


For a discussion about Unicode in Lua, see:

http://lua-users.org/wiki/LuaUnicode

The first thing to consider is that there are two forms of regular expression matching you can do - that native Lua kind, and the PCRE kind, which is available as a MUSHclient extension.

The PCRE matching is UTF-8 compliant, I believe, so it should work with UTF-8 Unicode strings.

From what I can make of the plugin, Unicode is not a huge worry.

- Nick Gammon

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

Posted by Nexes   (65 posts)  Bio
Date Reply #5 on Wed 08 Mar 2006 06:19 AM (UTC)

Amended on Wed 08 Mar 2006 06:22 AM (UTC) by Nexes

Message
Yay! I'll use the MUSHClient supplied PCRE one then because Lua's is...meh. For things like "^\x0d\x0a" I would use the exact same thing in Lua right? Or does Lua have another way of using escape charachters?
Top

Posted by Nexes   (65 posts)  Bio
Date Reply #6 on Wed 08 Mar 2006 10:56 PM (UTC)
Message
Yay! I got it to match lines! But it's not working properly *grumble* I have to find some way of emulating Pythons .sub functions.
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #7 on Thu 09 Mar 2006 12:24 AM (UTC)
Message
I'm not sure what Python "sub" does, but if it is string substitution, check out string.gsub:


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


- Nick Gammon

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

Posted by Nexes   (65 posts)  Bio
Date Reply #8 on Thu 09 Mar 2006 03:41 AM (UTC)
Message
When I try to use Lua's gsub, I can't properly represent /x1b.

How do I represent special charachters in Lua's pattern system?
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #9 on Thu 09 Mar 2006 04:12 AM (UTC)
Message
Inside a string, you can use \027 to represent an escape (that is, decimal 27 is the same as hex 1B).

So, a string like this:


"\xff\xf9"


In Lua would be:


"\255\249"



Get out your scientific calculator to work out the correspondence between hex and decimal.

- Nick Gammon

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

Posted by Nexes   (65 posts)  Bio
Date Reply #10 on Thu 09 Mar 2006 04:22 AM (UTC)
Message
Ahh! Why didn't they just say use a base 10 number *sigh* I could have done that.

Before you posted that I was resigned to concatenating something like string.char(27) and so on to the pattern. I was getting very depressed at that thought because it was so ugly and I wasn't sure it would work anyways. :(
Top

Posted by Nexes   (65 posts)  Bio
Date Reply #11 on Thu 09 Mar 2006 04:53 AM (UTC)

Amended on Thu 09 Mar 2006 04:54 AM (UTC) by Nexes

Message
*sigh* This is very frustrating. This is what I have so far:
lastHealth = 0
lastMana = 0
lastWillpower = 0
lastEndurance = 0
terminated = false

function repl(...)
if arg[2] == "" then
terminated = true
end
return "-\13\10"
end --repl

function repl2(...)
    table.foreach(arg, Note)
    currHealth = arg[2]
    currMana = arg[5]
    if lastHealth == 0 then
        
lastHealth = currHealth
    end
    if lastMana == 0 then
        lastMana = currMana
    end
    healthDiff = currHealth - lastHealth
    manaDiff = currMana - lastMana
    lastHealth = currHealth
    lastMana = currMana
    if healthDiff > 0 then
        healthDiff = "\x1b[37m[\x1B[1;32m+" .. healthDiff .. "\x1b[0;37m]" .. arg[1]
    elseif healthDiff < 0 then
        healthDiff = "\x1b[37m[\x1b[1;31m" .. healthDiff .. "\x1b[0;37m]" .. arg[1]
    else
        healthDiff = ""
    end
    if manaDiff > 0 then
        manaDiff = "\x1b[37m[\x1B[1;32m+" .. manaDiff .. "\x1b[0;37m]" .. arg[4]
    elseif manaDiff < 0 then
        manaDiff = "\x1b[37m[\x1B[1;31m" .. manaDiff .. "\x1b[0;37m]" .. arg[4]
    else
        manaDiff = ""
    end
    return arg[1] .. arg[2] .. healthDiff .. arg[3] .. arg[4] .. arg[5] .. manaDiff .. arg[6]
end --repl2

function repl3(...)
return ""
end --repl3

function OnPluginPacketReceived(packet)
packet = string.gsub(packet, "(\27%[(?:32|1;33|1;31)m)([0-9]+)(h,\27%[(?:0;)?37m)(\27%[(?:32|1;33|1;31)m)( [0-9]+)(m\27%[(?:0;)?37m [c]?[e]?[x]?[k]?[d]?[b]?\-)",
                    repl2)
if terminated then
	packet = string.gsub(packet, "^\13\10", "")
	terminated = false
end
-- \x0a == 10 \xod == 13 \xff == 255 \xf9 == 249 \x1b == 27
packet = string.gsub(packet, "(\-\255\249)($|\13\10)?", repl)
return packet
end --PacketReceived
]]>


Can someone tell me what's wrong with it?
Top

Posted by Nexes   (65 posts)  Bio
Date Reply #12 on Thu 09 Mar 2006 05:13 AM (UTC)
Message
I think I've isolated a problem at this line:

packet = string.gsub(packet, "(\-\255\249)($|\13\10)?", repl)

It was actually getting to repl if I took out the ($|\13\10)? part, but I can't figure out what's wrong with it. I tried replacing it with (\10|\13\10)? and (\13|\13\10)? and even (.*)? but for some reason if that second part is there it never ever reaches repl. And this is just ignoring the other big pattern, but I decided to do this part first, small steps :p
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #13 on Thu 09 Mar 2006 08:10 PM (UTC)
Message
It is hard to tell from what you posted exactly what the regexp is trying to do.

However I will point out the that "native" Lua regexps do not support the "|" symbol (meaning this OR that). If your use of | is for that purpose, you should switch to the PCRE regexps.

See:

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

This describes how to use the PCRE regular expressions in MUSHclient (Lua).

Also see this forum page:

http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=4905


- Nick Gammon

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

Posted by Nexes   (65 posts)  Bio
Date Reply #14 on Thu 09 Mar 2006 09:04 PM (UTC)
Message
I was using the PCRE ones, but they don't have a gsub. I guess I will though
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.


48,170 views.

This is page 1, subject is 2 pages long: 1 2  [Next page]

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.