Exemplo n.º 1
0
 public StatechartTransition(TransitionAttribute transition, MethodInfo method, IState context)
 {
     _Transition = transition;
     Action      = (TransitionAction)Delegate.CreateDelegate(typeof(TransitionAction), context, method);
 }
Exemplo n.º 2
0
    public StateContoller(object _obj)
    {
        m_curState    = ANY_STATE;
        m_transitions = new List <TransPair>();
        m_updates     = new Dictionary <short, BasicFnct>();

        MethodInfo[] mthds = _obj.GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);
        // search all fields and find the attribute [TransitionAttribute]
        for (int i = 0; i < mthds.Length; i++)
        {
            TransitionAttribute         attribute       = Attribute.GetCustomAttribute(mthds[i], typeof(TransitionAttribute)) as TransitionAttribute;
            TransitionOverrideAttribute ovrride         = Attribute.GetCustomAttribute(mthds[i], typeof(TransitionOverrideAttribute)) as TransitionOverrideAttribute;
            UpdateAttribute             updateAttribute = Attribute.GetCustomAttribute(mthds[i], typeof(UpdateAttribute)) as UpdateAttribute;

            // if we detect a RememberMe attribute, generate field for dynamic class
            if (attribute != null)
            {
                TransPair p;
                p.m_key = getKey(attribute.m_from, attribute.m_to);

                //IF it already exists do not overrwrite! derrived fncts are handled first! @@actually should add some priority system bc the methodinfo can be any order
                int index;
                if (tryGetValue(p.m_key, out p.m_fnct, out index))
                {
                    //Debug.Log("Trying to define " + mthds[i].Name + " as a transition function.");
                    //Debug.Log("transition already defined from: " + attribute.m_from  + " to " + attribute.m_to + " on " + _obj);
                    //Debug.Log("Ignore this if you've overriden a transition function with TransitionOverrideAttribute");
                }
                else
                {
                    //else add it
                    p.m_fnct = Delegate.CreateDelegate(typeof(BasicFnct), _obj, mthds[i]) as BasicFnct;
                    m_transitions.Add(p);
                }
            }
            // if we detect a RememberMe attribute, generate field for dynamic class
            else if (ovrride != null)
            {
                TransPair p;
                p.m_key = getKey(ovrride.m_from, ovrride.m_to);

                //IF it already exists DO overrwrite!
                int index;
                if (tryGetValue(p.m_key, out p.m_fnct, out index))
                {
                    m_transitions[index] = p;
                }
                else
                {
                    p.m_fnct = Delegate.CreateDelegate(typeof(BasicFnct), _obj, mthds[i]) as BasicFnct;
                    m_transitions.Add(p);
                }
            }
            else if (updateAttribute != null) //otherwise check for state update fnct
            {
                if (updateAttribute != null)
                {
                    //seems like derrived class gets looked at first so we want first thing to not get overwritten...
                    BasicFnct fnc = null;
                    if (!m_updates.TryGetValue(updateAttribute.m_state, out fnc))
                    {
                        m_updates[updateAttribute.m_state] = Delegate.CreateDelegate(typeof(BasicFnct), _obj, mthds[i]) as BasicFnct;
                    }
                }
            }
        }
    }