AresChat / sb0t

GNU Affero General Public License v3.0
7 stars 9 forks source link

new serverEvent. return always canScript False #48

Closed adahox closed 7 years ago

adahox commented 7 years ago

Hi hollow,

i've created a new server event but for some reason it alway return canScript = false. what sould i do to make it return true?

Nemo2478 commented 7 years ago

Hi adahox,

are talking about JS? Which Server-Event are you referring to?

adahox commented 7 years ago

sorry hollow, let me explain better.

sb0t server has a class called ServerEvents : IExtension. I have created a method on to do some stuff but for some reason when i call this method it's work ok but when i check if it can script like code bellow, it always returns false.

if (this.CanScript) { JSScript[] scripts = ScriptManager.Scripts.ToArray();

Nemo2478 commented 7 years ago

Hellow adahox,

could you show your full method-code?

adahox commented 7 years ago

`public void CustomDataSending(IUser client, IUser target, IPrivateDataCustom msg) { if(this.CanScript) { JSScript[] scripts = ScriptManager.Scripts.ToArray();

            foreach (JSScript s in scripts)
            {
                Objects.JSUser u = s.GetUser(client);
                Objects.JSUser t = s.GetUser(target);

                if (u != null && t != null)
                    try
                    {
                        bool result = s.JS.CallGlobalFunction<bool>("onCustomDataBefore", u, t,
                            new Objects.JSDC(s.JS.Object.InstancePrototype, msg));

                        if (!result)
                        {
                            msg.Cancel = true;
                            return;
                        }
                    }
                    catch (Jurassic.JavaScriptException e)
                    {
                        ErrorDispatcher.SendError(s.ScriptName, e.Message, e.LineNumber);
                    }
                    catch { }
            }
        }
    }`
hollow87 commented 7 years ago

Might be a silly question but take a look at line 34 in scripting/ServerEvents.cs https://github.com/AresChat/sb0t/blob/78a40c5dee02b8dc8ec514eb1223fb1757f99374/scripting/ServerEvents.cs#L34

Also you can link to your fork

adahox commented 7 years ago

i saw this line but i'm not undestanding how it handles my method

hollow87 commented 7 years ago

You just adding the method to that file will not cause it to fire...It must also be in the interface project iconnect/IExtension.cs as well as in the core core/Events.cs

Plus the core must call into the Event. For example lets take the AvatarReceived js event

First the code path is in the core https://github.com/AresChat/sb0t/blob/61c0e5e1d62eabaf4e5fb69666850a2629f02631/core/TCPProcessor.cs#L613-L627

From there the Events class still in the core assembly https://github.com/AresChat/sb0t/blob/78a40c5dee02b8dc8ec514eb1223fb1757f99374/core/Events.cs#L219-L243

At which points if DefaultCommands is true then it goes into the commands assembly

Based on the result from the commands assembly the code now enters the scripting assembly https://github.com/AresChat/sb0t/blob/78a40c5dee02b8dc8ec514eb1223fb1757f99374/scripting/ServerEvents.cs#L251-L278

Based on the result from that code now goes through the extension assemblies.

That is how a server event is processed.