Converting a Python plugin into Lua

Posted by Nexes on Tue 07 Mar 2006 11:31 PM — 16 posts, 68,254 views.

#0
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*
Amended on Tue 07 Mar 2006 11:59 PM by Nexes
USA #1
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. :)
USA #2
That's strange... Why can't you edit your posts?
#3
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.
Amended on Tue 07 Mar 2006 11:59 PM by Nexes
Australia Forum Administrator #4
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.
#5
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?
Amended on Wed 08 Mar 2006 06:22 AM by Nexes
#6
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.
Australia Forum Administrator #7
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

#8
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?
Australia Forum Administrator #9
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.
#10
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. :(
#11
*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?
Amended on Thu 09 Mar 2006 04:54 AM by Nexes
#12
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
Australia Forum Administrator #13
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

#14
I was using the PCRE ones, but they don't have a gsub. I guess I will though
#15
Okies, I got it working with the PCRE versions. Had to use three while loops but...whatever, can't have everything I guess. Thanks for your help!