lundi 23 novembre 2015

C# request to plugin

I'm trying implement simple plugin system to my app, but i have problem with communication with my plugin.

I made interface to my plugin:

public interface IAlgorithm
{
    /// <summary>
    /// New request in current simulation tick - all logic should be run after request
    /// </summary>
    /// <param name="pTime">Simulation time in [ms] from simulation start</param>
    /// <param name="pObject">Manipulating object</param>
    /// <param name="pWorld">Current world</param>
    void Request(int pTime, PhysicalObject pObject, World pWorld);
}

and next I create my plugin to hook keyboard:

 public class Main : IAlgorithm
{
    private int m_Direction;
    private readonly GlobalKeyboardHook m_Hook = new GlobalKeyboardHook();

    public Main()
    {
        m_Hook.HookedKeys.Add(Keys.Up);
        m_Hook.HookedKeys.Add(Keys.Down);
        m_Hook.HookedKeys.Add(Keys.Right);
        m_Hook.HookedKeys.Add(Keys.Left);
        m_Hook.KeyDown += KeyClicked;
    }

    private void KeyClicked(object sender, KeyEventArgs e)
    {
        switch (e.KeyCode)
        {
            case Keys.Down:
                m_Direction = 180;
                break;
            case Keys.Up:
                m_Direction = 0;
                break;
            case Keys.Left:
                m_Direction = 90;
                break;
            case Keys.Right:
                m_Direction = 270;
                break;
        }
    }

    public void Request(int pTime, PhysicalObject pObject, World pWorld)
    {
        pObject.Movement.Power = 100;
        pObject.Movement.Direction = m_Direction;
    }
}

Next in my main program I try to use my plugin like this:

var myAssembly = Assembly.LoadFile(myPath).GetTypes().First(x => x.Name == "Main");
IAlgorithm myAlgorithm = ((IAlgorithm)Activator.CreateInstance(myAssembly ));

and it's working, but if I want make some request to plugin

myAlgorithm.Request(pSimulationTime, myPhysicalObject, pWorld);

after that I don't have any changes in myPhysicalObject

but if I use MessageBox:

myAlgorithm.Request(pSimulationTime, myPhysicalObject, pWorld); 
MessageBox.Show(myPhysicalObject.Movement.Direction)

I can see correct value in MB.

I don't know what is incorrect in my logic and I don't understand why it's working after some pause. Have you some ideas??





Aucun commentaire:

Enregistrer un commentaire