[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Plugins
. . -> [Subject]  OnPluginPacketReceived & Null Characters

OnPluginPacketReceived & Null Characters

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


Posted by Craig   (5 posts)  [Biography] bio
Date Tue 05 Oct 2010 04:03 AM (UTC)

Amended on Tue 05 Oct 2010 05:02 AM (UTC) by David Haley

Message
I have an issue where I am trying to use the OnPluginPacketReceived function to reformat the MUD's packet output, but the MUD's packet output contains null characters mid way through the string. The string variable comes through in the scripting language containing all the characters prior to the null character but deletes any characters after the null character (assuming it is a null terminated string). Do you know of any workaround so I can have access to the characters AFTER the null character?

Example of packet:


..+..*..Ok...You   0d 00 2b 0d 00 2a 0d 0a 4f 6b 2e 0d 0a 59 6f 75
 weave flows of    20 77 65 61 76 65 20 66 6c 6f 77 73 20 6f 66 20
Earth to create    45 61 72 74 68 20 74 6f 20 63 72 65 61 74 65 20
a meal.....* HP:   61 20 6d 65 61 6c 2e 0d 0a 0d 0a 2a 20 48 50 3a
Healthy SP:Full    48 65 61 6c 74 68 79 20 53 50 3a 46 75 6c 6c 20
MV:Fresh >         4d 56 3a 46 72 65 73 68 20 3e 20


(Edited to add code tags.)
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #1 on Tue 05 Oct 2010 05:05 AM (UTC)
Message
What language are you using here?

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Craig   (5 posts)  [Biography] bio
Date Reply #2 on Tue 05 Oct 2010 05:16 AM (UTC)
Message
in this case I am using JScript. I believe Lua behaves the same way though. I have not tried the other scripting languages yet.

Goal is to add a newline after prompt as seen in some other examples, except I am being hindered by the null characters.

here is my code:


function OnPluginPacketReceived(s)
{
	if (s.match(/[\*o]? HP:\w+?(?: SP:\w+?)? MV:\w+? .*?> /))
	{
		return s.replace(/[\*o]? HP:\w+?(?: SP:\w+?)? MV:\w+? .*?> /g, '$&\r\n');
	}
}
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #3 on Tue 05 Oct 2010 05:22 AM (UTC)
Message
Craig said:
in this case I am using JScript. I believe Lua behaves the same way though.

Lua is fine with null bytes in its strings. Testing Javascript in Chrome (console.debug("foo\0bar".length)) suggests Javascript is also fine with it (though admittedly JScript != V8). It sounds like MUSHclient may not be accounting for the null bytes when it passes the data to the callback.

Craig said:
here is my code:

function OnPluginPacketReceived(s)
{
	if (s.match(/[\*o]? HP:\w+?(?: SP:\w+?)? MV:\w+? .*?> /))
	{
		return s.replace(/[\*o]? HP:\w+?(?: SP:\w+?)? MV:\w+? .*?> /g, '$&\r\n');
	}
}

I have to say, I know Lua's match functions had (have?) a problem with embedded nulls, even though Lua itself is fine with it. It may be the case with JScript too.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Craig   (5 posts)  [Biography] bio
Date Reply #4 on Tue 05 Oct 2010 05:28 AM (UTC)
Message
ahh, it could be that the problem is not with the scripting language then, but the underlying language that mushclient is programmed in (c/c++)??? Pretty sure c strings are null terminated. Perhaps they are getting truncated there before the variable is ever passed to the scripting language.

just a guess... could be way off. apologies if i am wrong.
[Go to top] top

Posted by Craig   (5 posts)  [Biography] bio
Date Reply #5 on Tue 05 Oct 2010 05:39 AM (UTC)
Message
Twisol said:

I have to say, I know Lua's match functions had (have?) a problem with embedded nulls, even though Lua itself is fine with it. It may be the case with JScript too.


I don't think it is an issue with the match function because I have manually iterated each character in a for loop checking the ascii codes... the string just cuts off directly before the null character.
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #6 on Tue 05 Oct 2010 06:14 AM (UTC)

Amended on Tue 05 Oct 2010 06:15 AM (UTC) by Twisol

Message
Craig said:
ahh, it could be that the problem is not with the scripting language then, but the underlying language that mushclient is programmed in (c/c++)??? Pretty sure c strings are null terminated. Perhaps they are getting truncated there before the variable is ever passed to the scripting language.

You should understand that a "C-string" is just a convention; it's not hardwired into the language. :) Lua, Python, etc. are all themselves written in C or C++. The problem, I'm guessing, is that MUSHclient is not passing the string's length to the scripting engine, so it assumes it's a C-string and takes up until the first null byte it finds.

Craig said:
I don't think it is an issue with the match function because I have manually iterated each character in a for loop checking the ascii codes... the string just cuts off directly before the null character.

Good idea! Yeah, it sounds like a MUSHclient problem (or maybe it's an issue with WSH, which is the interface MUSHclient uses for non-Lua languages. That seems more likely IMHO).

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Nick Gammon   Australia  (22,982 posts)  [Biography] bio   Forum Administrator
Date Reply #7 on Tue 05 Oct 2010 06:33 AM (UTC)
Message
Twisol said:

You should understand that a "C-string" is just a convention; it's not hardwired into the language. :) Lua, Python, etc. are all themselves written in C or C++. The problem, I'm guessing, is that MUSHclient is not passing the string's length to the scripting engine, so it assumes it's a C-string and takes up until the first null byte it finds.


Twisol is correct. Somehow that slipped through, which is strange because people have been using OnPluginPacketReceived for a while now.

A minor change lets the correct length be passed to the Lua script engine. However I'm not too hopeful about Jscript, because the interface to it uses C-style strings internally.

I've made a change in version 4.64 to the Lua part, that won't however change the way Jscript works.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (22,982 posts)  [Biography] bio   Forum Administrator
Date Reply #8 on Tue 05 Oct 2010 09:46 PM (UTC)
Message
Version 4.64 is now available. I recommend doing packet manipulation in a Lua plugin. There are examples around of doing similar things to what you describe.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Craig   (5 posts)  [Biography] bio
Date Reply #9 on Tue 05 Oct 2010 10:02 PM (UTC)
Message
Amazingly fast turnaround!

I just verified that it works in Lua but not in Jscript as you suspected. Still working on converting the regex to Lua, but I will get it...

Thansk for your help!
[Go to top] 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.


24,627 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]