Example #1
0
 public PPtr <MonoBehaviour>[] GetStateBeahviours(int stateMachineIndex, int stateIndex)
 {
     if (IsReadStateMachineBehaviourVectorDescription(File.Version))
     {
         int layerIndex = Controller.GetLayerIndexByStateMachineIndex(stateMachineIndex);
         StateMachineConstant stateMachine = Controller.StateMachineArray[stateMachineIndex].Instance;
         StateConstant        state        = stateMachine.StateConstantArray[stateIndex].Instance;
         uint stateID = state.GetID(File.Version);
         foreach (KeyValuePair <StateKey, StateRange> pair in StateMachineBehaviourVectorDescription.StateMachineBehaviourRanges)
         {
             StateKey key = pair.Key;
             if (key.LayerIndex == layerIndex && key.StateID == stateID)
             {
                 StateRange             range = pair.Value;
                 PPtr <MonoBehaviour>[] stateMachineBehaviours = new PPtr <MonoBehaviour> [range.Count];
                 for (int i = 0; i < range.Count; i++)
                 {
                     int index = (int)StateMachineBehaviourVectorDescription.StateMachineBehaviourIndices[range.StartIndex + i];
                     stateMachineBehaviours[i] = StateMachineBehaviours[index];
                 }
                 return(stateMachineBehaviours);
             }
         }
     }
     return(new PPtr <MonoBehaviour> [0]);
 }
 public void Update(IPublisher publisher)
 {
     if (StateRange.Contains((publisher as EventManager).State))
     {
         using (StreamWriter streamWriter = new StreamWriter(LogFilePath, true)) {
             streamWriter.WriteLine(string.Format(Message, (publisher as EventManager).State));
         }
     }
 }
 private PPtr <MonoBehaviour>[] GetStateBehaviours(StateRange range)
 {
     PPtr <MonoBehaviour>[] stateMachineBehaviours = new PPtr <MonoBehaviour> [range.Count];
     for (int i = 0; i < range.Count; i++)
     {
         int index = (int)StateMachineBehaviourVectorDescription.StateMachineBehaviourIndices[range.StartIndex + i];
         stateMachineBehaviours[i] = StateMachineBehaviours[index];
     }
     return(stateMachineBehaviours);
 }
 /// <summary>
 /// Unions this state range with (other intersect (minVal to maxVal))
 /// </summary>
 public void UnionWith(StateRange other, int minVal, int maxVal)
 {
     foreach (Interval v in other.data)
     {
         int start = Math.Max(v.Start, minVal);
         int end   = Math.Min(v.End, maxVal);
         if (start <= end)
         {
             data.Add(new Interval(start, end));
         }
     }
 }
Example #5
0
		/// <summary>
		/// Initializes the state range logic:
		/// Clears 'ranges' and sets 'ranges[entryPoint]' to the full range (int.MinValue to int.MaxValue)
		/// </summary>
		void InitStateRanges(ILNode entryPoint)
		{
			ranges = new DefaultDictionary<ILNode, StateRange>(n => new StateRange());
			ranges[entryPoint] = new StateRange(int.MinValue, int.MaxValue);
			rangeAnalysisStateVariable = null;
		}
Example #6
0
			/// <summary>
			/// Unions this state range with (other intersect (minVal to maxVal))
			/// </summary>
			public void UnionWith(StateRange other, int minVal, int maxVal)
			{
				foreach (Interval v in other.data) {
					int start = Math.Max(v.Start, minVal);
					int end = Math.Min(v.End, maxVal);
					if (start <= end)
						data.Add(new Interval(start, end));
				}
			}
Example #7
0
			public void UnionWith(StateRange other)
			{
				data.AddRange(other.data);
			}
Example #8
0
        private void InitializeNewStateRange(StateRange range)
        {
            range.StartValue = 500;
            range.EndValue = 1000;

            range.Name = GetStateRangeName(range.StateIndicator);
        }
Example #9
0
        private void CopyStateRange(StateRange range)
        {
            if (_AddButton != null)
            {
                _AddButton.PerformClick();

                StateRangeCollection pc = range.StateIndicator.Ranges;
                StateRange clone = pc[pc.Count - 1];

                range.CopyToItem(clone);
            }
        }
Example #10
0
        private void DrawStateText(Graphics g, Rectangle r, StateRange range)
        {
            string text = (range != null)
                ? range.Text : GetOverrideString();

            if (string.IsNullOrEmpty(text) == false)
            {
                using (StringFormat sf = new StringFormat())
                {
                    SetStringAlignment(sf);

                    Color color = _TextColor;

                    if (range != null)
                    {
                        color = range.TextColor;

                        int dx = (int)(r.Width * range.TextHorizontalOffset);
                        int dy = (int)(r.Height * range.TextVerticalOffset);

                        r.Offset(dx, dy);
                    }
                    else
                    {
                        int dx = (int)(r.Width * _TextHorizontalOffset);
                        int dy = (int)(r.Height * _TextVerticalOffset);

                        r.Offset(dx, dy);
                    }

                    using (Brush br = new SolidBrush(color))
                        g.DrawString(text, AbsFont, br, r, sf);
                }
            }
        }
Example #11
0
        private void DrawStateImage(Graphics g, Rectangle r, StateRange range)
        {
            Image image = (range != null) ? range.Image : _Image;

            if (image != null)
            {
                if (AutoSize == true)
                {
                    g.DrawImage(image, r);
                }
                else
                {
                    r.X += (r.Width - image.Width) / 2;
                    r.Y += (r.Height - image.Height) / 2;

                    g.DrawImageUnscaled(image, r);
                }
            }
            else
            {
                GradientFillColor fillColor =
                    (range != null) ? range.BackColor : BackColor;

                using (GraphicsPath path = GetStatePath(r))
                    RenderBackPath(g, path, r, fillColor);
            }
        }
Example #12
0
        private IList <StateRange> FilterState(IEnumerable <string> expressions, string[] entityLimit)
        {
            var stateData = _context.TimeAndStates
                            .Join(_context.EntityStateKeys, state => state.Key, key => key.Key, (state, key) => new { state, key });

            if (entityLimit != null)
            {
                stateData = stateData.Where(x => entityLimit.Contains(x.key.Entity));
            }


            DateTime?afterLimit = null;

            var postFilters = new List <Func <StateRange, bool> >();
            var postUpdates = new List <Action <StateRange> >();

            foreach (var expression in expressions)
            {
                if (expression.StartsWith("State."))
                {
                    var e = expression.Replace("State.", "");

                    string comparer = "";

                    if (e.Contains("="))
                    {
                        comparer = "=";
                    }
                    if (e.Contains("<"))
                    {
                        comparer = "<";
                    }
                    if (e.Contains(">"))
                    {
                        comparer = ">";
                    }
                    if (e.Contains(">="))
                    {
                        comparer = ">=";
                    }
                    if (e.Contains("<="))
                    {
                        comparer = "<=";
                    }

                    var var   = e.Split(new string[] { comparer }, StringSplitOptions.RemoveEmptyEntries)[0].Trim();
                    var value = e.Split(new string[] { comparer }, StringSplitOptions.RemoveEmptyEntries)[1];

                    if (comparer == "=")
                    {
                        stateData = stateData.Where(x => x.state.Key == var && (x.state.Value == value || x.state.PriorValue == value));
                        postFilters.Add(x => x.Key == var && x.Value == value);
                    }

                    if (comparer == "<")
                    {
                        int intVal = Convert.ToInt32(value);
                        stateData = stateData.Where(x => x.state.Key == var && (x.state.NumericValue < intVal || x.state.NumericPriorValue < intVal));
                        postFilters.Add(x => x.Key == var && Convert.ToDecimal(x.Value) < Convert.ToDecimal(value));
                    }

                    if (comparer == "<=")
                    {
                        int intVal = Convert.ToInt32(value);
                        stateData = stateData.Where(x => x.state.Key == var && (x.state.NumericValue <= intVal || x.state.NumericPriorValue <= intVal));
                        postFilters.Add(x => x.Key == var && Convert.ToDecimal(x.Value) <= Convert.ToDecimal(value));
                    }

                    if (comparer == ">")
                    {
                        int intVal = Convert.ToInt32(value);
                        stateData = stateData.Where(x => x.state.Key == var && (x.state.NumericValue > intVal || x.state.NumericPriorValue > intVal));
                        postFilters.Add(x => x.Key == var && Convert.ToDecimal(x.Value) > Convert.ToDecimal(value));
                    }


                    if (comparer == ">=")
                    {
                        int intVal = Convert.ToInt32(value);
                        stateData = stateData.Where(x => x.state.Key == var && (x.state.NumericValue >= intVal || x.state.NumericPriorValue >= intVal));
                        postFilters.Add(x => x.Key == var && Convert.ToDecimal(x.Value) >= Convert.ToDecimal(value));
                    }
                }

                if (expression.StartsWith("After"))
                {
                    var e     = expression.Replace("After", "");
                    var var   = e.Split('=')[0];
                    var value = DateTime.Parse(e.Split('=')[1]);
                    stateData  = stateData.Where(x => x.state.On > value || (x.key.LastChange < value && x.state.On == x.key.LastChange));
                    afterLimit = value;

                    postUpdates.Add((x) =>
                    {
                        if (x.Start < value)
                        {
                            x.Start = value;
                        }
                    }
                                    );
                }

                if (expression.StartsWith("Before"))
                {
                    var e     = expression.Replace("Before", "");
                    var var   = e.Split('=')[0];
                    var value = DateTime.Parse(e.Split('=')[1]);
                    stateData = stateData.Where(x => x.state.On < value);

                    postUpdates.Add((x) =>
                    {
                        if (x.End > value || x.End == null)
                        {
                            x.End = value;
                        }
                    }
                                    );
                }
            }

            var finalData = stateData.ToList();

            var ret = new List <StateRange>();

            var entities = finalData.Select(x => x.state.Entity).Distinct();

            foreach (var entity in entities)
            {
                foreach (var key in finalData.Where(x => x.state.Entity == entity).Select(x => x.state.Key).Distinct())
                {
                    var ordered = finalData.Where(x => x.state.Entity == entity && x.state.Key == key).OrderBy(x => x.state.On);

                    StateRange c       = null;
                    bool       isFirst = true;
                    foreach (var o in ordered)
                    {
                        if (isFirst && afterLimit != null && o.state.On > afterLimit && o.state.PriorValue != null)
                        {
                            ret.Add(new StateRange()
                            {
                                Entity = entity,
                                Key    = key,
                                Value  = o.state.PriorValue,
                                Start  = afterLimit.Value,
                                End    = o.state.On.AddSeconds(-1)
                            });
                        }

                        if (c == null)
                        {
                            c = new StateRange()
                            {
                                Entity = entity,
                                Key    = key,
                                Value  = o.state.Value,
                                Start  = o.state.On
                            };

                            ret.Add(c);
                        }
                        else
                        {
                            c.End = o.state.On.AddSeconds(-1);

                            c = new StateRange()
                            {
                                Entity = entity,
                                Key    = key,
                                Value  = o.state.Value,
                                Start  = o.state.On
                            };

                            ret.Add(c);
                        }

                        isFirst = false;
                    }
                }
            }

            foreach (var filter in postFilters)
            {
                ret = ret.Where(filter).ToList();
            }

            foreach (var update in postUpdates)
            {
                foreach (var r in ret)
                {
                    update.Invoke(r);
                }
            }

            return(ret);
        }
        int AssignStateRanges(List <ILNode> body, int bodyLength, bool forDispose)
        {
            if (bodyLength == 0)
            {
                return(0);
            }
            for (int i = 0; i < bodyLength; i++)
            {
                StateRange nodeRange = ranges[body[i]];
                nodeRange.Simplify();

                ILLabel label = body[i] as ILLabel;
                if (label != null)
                {
                    ranges[body[i + 1]].UnionWith(nodeRange);
                    continue;
                }

                ILTryCatchBlock tryFinally = body[i] as ILTryCatchBlock;
                if (tryFinally != null)
                {
                    if (!forDispose || tryFinally.CatchBlocks.Count != 0 || tryFinally.FaultBlock != null || tryFinally.FinallyBlock == null)
                    {
                        throw new YieldAnalysisFailedException();
                    }
                    ranges[tryFinally.TryBlock].UnionWith(nodeRange);
                    if (tryFinally.TryBlock.Body.Count != 0)
                    {
                        ranges[tryFinally.TryBlock.Body[0]].UnionWith(nodeRange);
                        AssignStateRanges(tryFinally.TryBlock.Body, tryFinally.TryBlock.Body.Count, forDispose);
                    }
                    continue;
                }

                ILExpression expr = body[i] as ILExpression;
                if (expr == null)
                {
                    throw new YieldAnalysisFailedException();
                }
                switch (expr.Code)
                {
                case ILCode.Switch:
                {
                    SymbolicValue val = Eval(expr.Arguments[0]);
                    if (val.Type != SymbolicValueType.State)
                    {
                        throw new YieldAnalysisFailedException();
                    }
                    ILLabel[] targetLabels = (ILLabel[])expr.Operand;
                    for (int j = 0; j < targetLabels.Length; j++)
                    {
                        int state = j - val.Constant;
                        ranges[targetLabels[j]].UnionWith(nodeRange, state, state);
                    }
                    StateRange nextRange = ranges[body[i + 1]];
                    nextRange.UnionWith(nodeRange, int.MinValue, -1 - val.Constant);
                    nextRange.UnionWith(nodeRange, targetLabels.Length - val.Constant, int.MaxValue);
                    break;
                }

                case ILCode.Br:
                case ILCode.Leave:
                    ranges[(ILLabel)expr.Operand].UnionWith(nodeRange);
                    break;

                case ILCode.Brtrue:
                {
                    SymbolicValue val = Eval(expr.Arguments[0]);
                    if (val.Type == SymbolicValueType.StateEquals)
                    {
                        ranges[(ILLabel)expr.Operand].UnionWith(nodeRange, val.Constant, val.Constant);
                        StateRange nextRange = ranges[body[i + 1]];
                        nextRange.UnionWith(nodeRange, int.MinValue, val.Constant - 1);
                        nextRange.UnionWith(nodeRange, val.Constant + 1, int.MaxValue);
                    }
                    else if (val.Type == SymbolicValueType.StateInEquals)
                    {
                        ranges[body[i + 1]].UnionWith(nodeRange, val.Constant, val.Constant);
                        StateRange targetRange = ranges[(ILLabel)expr.Operand];
                        targetRange.UnionWith(nodeRange, int.MinValue, val.Constant - 1);
                        targetRange.UnionWith(nodeRange, val.Constant + 1, int.MaxValue);
                    }
                    else
                    {
                        throw new YieldAnalysisFailedException();
                    }
                    break;
                }

                case ILCode.Nop:
                    ranges[body[i + 1]].UnionWith(nodeRange);
                    break;

                case ILCode.Ret:
                    break;

                case ILCode.Stloc:
                {
                    SymbolicValue val = Eval(expr.Arguments[0]);
                    if (val.Type == SymbolicValueType.State && val.Constant == 0 && rangeAnalysisStateVariable == null)
                    {
                        rangeAnalysisStateVariable = (ILVariable)expr.Operand;
                    }
                    else
                    {
                        throw new YieldAnalysisFailedException();
                    }
                    goto case ILCode.Nop;
                }

                case ILCode.Call:
                    // in some cases (e.g. foreach over array) the C# compiler produces a finally method outside of try-finally blocks
                    if (forDispose)
                    {
                        MethodDefinition mdef = GetMethodDefinition(expr.Operand as MethodReference);
                        if (mdef == null || finallyMethodToStateInterval.ContainsKey(mdef))
                        {
                            throw new YieldAnalysisFailedException();
                        }
                        finallyMethodToStateInterval.Add(mdef, nodeRange.ToEnclosingInterval());
                    }
                    else
                    {
                        throw new YieldAnalysisFailedException();
                    }
                    break;

                default:
                    if (forDispose)
                    {
                        throw new YieldAnalysisFailedException();
                    }
                    else
                    {
                        return(i);
                    }
                }
            }
            return(bodyLength);
        }
 /// <summary>
 /// Initializes the state range logic:
 /// Clears 'ranges' and sets 'ranges[entryPoint]' to the full range (int.MinValue to int.MaxValue)
 /// </summary>
 void InitStateRanges(ILNode entryPoint)
 {
     ranges                     = new DefaultDictionary <ILNode, StateRange>(n => new StateRange());
     ranges[entryPoint]         = new StateRange(int.MinValue, int.MaxValue);
     rangeAnalysisStateVariable = null;
 }
 public void UnionWith(StateRange other)
 {
     data.AddRange(other.data);
 }