Triggering Events in .NET for ISXEQ

Moderator: MacroQuest Developers

spanners
a lesser mummy
a lesser mummy
Posts: 56
Joined: Wed Jul 04, 2007 3:02 pm

Triggering Events in .NET for ISXEQ

Post by spanners » Sun Sep 07, 2008 12:48 am

I have been playing around with the .NET wrapper for ISXEQ and discovered 2 ways in which incomming chat messages can trigger actions within the .NET app.

The first method is to hook the "EQ Chat" events in ISXEQ within the .NET application and then applying regular expressions to the text, matching on your events of interest.

The second method is to define a custom event within a lavishscript script and tie that event to a trigger. Inside the .NET application you hook the custom event. This method frees you from parsing the text inside the .NET application.

The ISXEQ.NET.dll was built using Visual Studios Express 2005 for C++ and placed into the InnerSpace directory.

The example was built using Visual Studios 2008 Express for C#. I created a console project, targetted the runtime as .NET 2.0 and had the following for References:
ISXEQ.NET
Lavish.InnerSpace
System

Place all 3 of the files in your InnerSpace/scripts directory

Program.cs

Code: Select all

using System;
using System.Threading;
using System.Text.RegularExpressions;
using LavishScriptAPI;
using IS = InnerSpaceAPI.InnerSpace;

namespace Example
{
    class Program
    {

        private const String testEventRegex = @".*fire test event 1 :([^:]+):.*";

        private static void EQChatText(object sender, LSEventArgs e)
        {
            String chatText = Convert.ToString(e.Args[0]);

            Match eventMatch = Regex.Match(chatText, testEventRegex);
            if (eventMatch.Success)
            {
                IS.Echo("Test event 1 matched, param is " + eventMatch.Groups[1]);
            }
        }

        private static void TestEvent2(object sender, LSEventArgs e)
        {
            string param = Convert.ToString(e.Args[0]);
            IS.Echo("Test event 2 matched, param is " + param);
        }

        static void Main()
        {
            LavishScriptAPI.LavishScript.Events.AttachEventTarget("EQ Chat", EQChatText);
            LavishScriptAPI.LavishScript.Events.AttachEventTarget("Test Event 2", TestEvent2);

            Thread.Sleep(60000);

            LavishScriptAPI.LavishScript.Events.DetachEventTarget("EQ Chat", EQChatText);
            LavishScriptAPI.LavishScript.Events.DetachEventTarget("Test Event 2", TestEvent2);

            LavishScriptAPI.LavishScript.ExecuteCommand("echo ending event testing");
        }
    }
}

Program.xml

Code: Select all

<DotNetScript>
  <References>
    <string>Lavish.InnerSpace.dll</string>
    <string>ISXEQ.NET.dll</string>
    <string>C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll</string>
  </References>
  <SourceFiles>
    <string>Program.cs</string>
  </SourceFiles>
  <Language>C#</Language>
</DotNetScript>
Note you may have to change the path to your .NET System.dll.

testevent.iss

Code: Select all

#macro ProcessQueue()
if ${QueuedCommands}
  while ${QueuedCommands}
    ExecuteQueued
#endmac 


function Event_TestEvent2(string line, string param)
{
	echo "Test Event 2 Fired (lavish script): [${param}]"
	Event[Test Event 2]:Execute[${param}]
}


function main()
{
	ext -require isxeq

	AddTrigger Event_TestEvent2 "@*@fire test event 2 :@param@:@*@"

	LavishScript:RegisterEvent[Test Event 2]
	
	do
  	{
    		ProcessQueue()
    		WaitFrame
		wait 5
   	}
  	while ${Spawn[${Me}](exists)}
}

function atexit()
{
	Event[Test Event 2]:Unregister

	echo ${Script.Filename}.iss ended
} 
Once you run the testevent.iss, launch the .NET application with runscript program, and you will have 60 seconds to mess around firing the events before the .NET application closes. To test the two types of event firing, simple enter a string matching:
fire test event 1 :<param to parse out>:
OR
fire test event 2 :<param to parse out>:

Test event 1 fires by hooking the EQ Chat events and matching a regular expression: private const String testEventRegex = @".*fire test event 1 :([^:]+):.*";

The method EQChatText is tied to all incomming EQ chat events. When the regular expression is matched, the param is parsed out and output: IS.Echo("Test event 1 matched, param is " + eventMatch.Groups[1]);

Test event 2 fires because the lavishscript script defines a new event: LavishScript:RegisterEvent[Test Event 2] and ties this event firing to a trigger:
...
function Event_TestEvent2(string line, string param)
{
Event[Test Event 2]:Execute[${param}]
...
AddTrigger Event_TestEvent2 "@*@fire test event 2 :@param@:@*@"

The method TestEvent2 is tied to the event firing in the .NET application by: LavishScriptAPI.LavishScript.Events.AttachEventTarget("Test Event 2", TestEvent2);


spanners