/// <summary>
        /// Evaluates a query with static scope.
        /// </summary>
        public RSValue EvaluateQuery(IRSRuntimeEntity inEntity, string inQueryId, params object[] inArgs)
        {
            RSQueryInfo    queryInfo = Library.GetQuery(inQueryId);
            ExecutionScope scope     = CreateScope(inEntity, RSValue.Null, TableUtils.GetRuleFlags(queryInfo.Flags));

            using (new SharedRef <ExecutionScope>(scope))
            {
                return(EvaluateQuery(inEntity, queryInfo, InternalScriptUtils.Convert(inArgs), scope));
            }
        }
        /// <summary>
        /// Performs an action with static scope.
        /// </summary>
        public object PerformAction(IRSRuntimeEntity inEntity, string inActionId, params object[] inArgs)
        {
            RSActionInfo   actionInfo = Library.GetAction(inActionId);
            ExecutionScope scope      = CreateScope(inEntity, RSValue.Null, TableUtils.GetRuleFlags(actionInfo.Flags));

            using (new SharedRef <ExecutionScope>(scope))
            {
                return(PerformAction(inEntity, actionInfo, InternalScriptUtils.Convert(inArgs), scope).Value);
            }
        }
        internal bool CloneScopeIfNecessary(ExecutionScope inScope, RuleFlags inFlags, out ExecutionScope outScope)
        {
            if ((inFlags & RuleFlags.UsesRegisters) != 0)
            {
                outScope = CloneScope(inScope, inFlags);
                return(true);
            }

            outScope = inScope;
            return(false);
        }
        /// <summary>
        /// Evaluates a query with static scope.
        /// </summary>
        public RSValue EvaluateQuery(string inQueryId, params object[] inArgs)
        {
            RSQueryInfo    queryInfo = Library.GetQuery(inQueryId);
            ExecutionScope scope     = m_StaticScope;

            CloneScopeIfNecessary(scope, TableUtils.GetRuleFlags(queryInfo.Flags), out scope);

            using (new SharedRef <ExecutionScope>(scope))
            {
                return(EvaluateQuery(null, queryInfo, InternalScriptUtils.Convert(inArgs), scope));
            }
        }
 internal void RecycleScope(ExecutionScope inScope)
 {
     Assert.True(inScope != null && inScope.m_Environment == this);
     if ((inScope.m_Type & ExecutionScope.Type.Registers) != 0)
     {
         m_RegisterScopePool.Add(inScope);
     }
     else
     {
         m_LocalScopePool.Add(inScope);
     }
 }
        /// <summary>
        /// Performs an action with static scope.
        /// </summary>
        public object PerformAction(string inActionId, params object[] inArgs)
        {
            RSActionInfo   actionInfo = Library.GetAction(inActionId);
            ExecutionScope scope      = m_StaticScope;

            CloneScopeIfNecessary(scope, TableUtils.GetRuleFlags(actionInfo.Flags), out scope);

            using (new SharedRef <ExecutionScope>(scope))
            {
                return(PerformAction(null, actionInfo, InternalScriptUtils.Convert(inArgs), scope).Value);
            }
        }
        internal void TriggerRS(IRSRuntimeEntity inEntity, RSTriggerId inTriggerId, RSValue inArgument, bool inbForce)
        {
            if (!inbForce && inEntity.IsLocked())
            {
                return;
            }

            ExecutionScope scope = CreateScope(inEntity, inArgument, 0);

            using (new SharedRef <ExecutionScope>(scope))
            {
                inEntity?.RuleTable?.EvaluateTrigger(inTriggerId, scope);
            }
        }
        public RSEnvironment(RSLibrary inDatabase, IRSRuntimeEntityMgr inEntityMgr, IRSRuleTableMgr inTableMgr, IRSDebugLogger inLogger)
        {
            Library  = inDatabase;
            Entities = inEntityMgr;
            Tables   = inTableMgr;
            Logger   = inLogger;

            if (Entities != null)
            {
                Entities.Context = this;
            }

            m_StaticScope = new ExecutionScope(this, ExecutionScope.Type.Registers);
            m_StaticScope.Initialize(null, RSValue.Null);

            m_LocalScopePool    = new List <ExecutionScope>();
            m_RegisterScopePool = new List <ExecutionScope>();
        }
        public void PrewarmPools(int inScopeCount, int inRegisterScopeCount)
        {
            if (m_LocalScopePool.Capacity < inScopeCount)
            {
                m_LocalScopePool.Capacity = inScopeCount;
            }

            while (m_LocalScopePool.Count < inScopeCount)
            {
                ExecutionScope scope = new ExecutionScope(this, ExecutionScope.Type.Pooled);
                m_LocalScopePool.Add(scope);
            }

            if (m_RegisterScopePool.Capacity < inRegisterScopeCount)
            {
                m_RegisterScopePool.Capacity = inRegisterScopeCount;
            }

            while (m_RegisterScopePool.Count < inRegisterScopeCount)
            {
                ExecutionScope scope = new ExecutionScope(this, ExecutionScope.Type.Pooled | ExecutionScope.Type.Registers);
                m_RegisterScopePool.Add(scope);
            }
        }
Exemple #10
0
 /// <summary>
 /// Converts an RSValue to its closest mapped C# type.
 /// </summary>
 static public object ToObject(RSValue inValue, ExecutionScope inContext)
 {
     return(ToObject(inValue.GetInteropType(), inValue, inContext));
 }
Exemple #11
0
 /// <summary>
 /// Converts an RSValue to the given C# type.
 /// </summary>
 static public T ToObject <T>(RSValue inValue, ExecutionScope inContext)
 {
     return((T)ToObject(typeof(T), inValue, inContext));
 }
Exemple #12
0
 internal IEnumerable <RSValue> EvaluateQueries(IEnumerable <IRSRuntimeEntity> inEntities, RSQueryInfo inQuery, RSValue[] inArguments, ExecutionScope inContext)
 {
     inQuery.PrepArguments(inArguments, inContext);
     foreach (var entity in inEntities)
     {
         yield return(inQuery.InvokeWithCachedArgs(entity, inContext));
     }
 }
Exemple #13
0
 internal IEnumerable <ActionResult> PerformActions(IEnumerable <IRSRuntimeEntity> inEntities, RSActionInfo inAction, RSValue[] inArguments, ExecutionScope inContext)
 {
     inAction.PrepArguments(inArguments, inContext);
     foreach (var entity in inEntities)
     {
         yield return(inAction.InvokeWithCachedArgs(entity, inContext));
     }
 }
Exemple #14
0
 internal ActionResult PerformAction(IRSRuntimeEntity inEntity, RSActionInfo inAction, RSValue[] inArguments, ExecutionScope inContext)
 {
     return(inAction.Invoke(inEntity, inArguments, inContext));
 }
Exemple #15
0
        /// <summary>
        /// Converts an RSValue to the given C# type.
        /// </summary>
        static public object ToObject(Type inType, RSValue inValue, ExecutionScope inContext)
        {
            if (inType == null || inType == typeof(void))
            {
                return(null);
            }

            if (inType == typeof(object))
            {
                return(inValue);
            }

            if (inType == typeof(RSValue))
            {
                return(inValue);
            }

            if (inType.IsEnum)
            {
                return(inValue.AsEnum(inType));
            }

            switch (Type.GetTypeCode(inType))
            {
            case TypeCode.Boolean:
                return(inValue.AsBool);

            case TypeCode.Byte:
                return((byte)inValue.AsInt);

            case TypeCode.Char:
                return((char)inValue.AsInt);

            case TypeCode.Double:
                return((double)inValue.AsFloat);

            case TypeCode.Int16:
                return((Int16)inValue.AsInt);

            case TypeCode.Int32:
                return(inValue.AsInt);

            case TypeCode.Int64:
                return((Int64)inValue.AsInt);

            case TypeCode.SByte:
                return((sbyte)inValue.AsInt);

            case TypeCode.Single:
                return(inValue.AsFloat);

            case TypeCode.String:
                return(inValue.AsString);

            case TypeCode.UInt16:
                return((UInt16)inValue.AsInt);

            case TypeCode.UInt32:
                return((UInt32)inValue.AsInt);

            case TypeCode.UInt64:
                return((UInt64)inValue.AsInt);

            case TypeCode.Object:
            {
                if (inType == typeof(Color))
                {
                    return(inValue.AsColor);;
                }
                if (inType == typeof(Vector2))
                {
                    return(inValue.AsVector2);
                }
                if (inType == typeof(Vector3))
                {
                    return(inValue.AsVector3);
                }
                if (inType == typeof(Vector4))
                {
                    return(inValue.AsVector4);
                }
                if (inType == typeof(RSGroupId))
                {
                    return(inValue.AsGroupId);
                }
                if (inType == typeof(RSTriggerId))
                {
                    return(inValue.AsTriggerId);
                }
                if (typeof(IRSEntity).IsAssignableFrom(inType))
                {
                    return(inContext.ResolveEntity(inValue.AsEntity).ForceSingle());
                }
                break;
            }
            }

            throw new ArgumentException(string.Format("Unable to convert RSValue {0} to object of type {1}", inValue, inType), "inValue");
        }
Exemple #16
0
 internal ExecutionScope CloneScope(ExecutionScope inScope, RuleFlags inFlags)
 {
     Assert.True(inScope != null && inScope.m_Environment == this);
     return(CreateScope(inScope.SelfEntity, inScope.Argument, inFlags));
 }
Exemple #17
0
        internal void EvaluateTrigger(RSTriggerId inTriggerId, ExecutionScope inScope)
        {
            if (!m_Entity.IsAlive())
            {
                return;
            }

            if (OnTrigger != null)
            {
                object arg = RSInterop.ToObject(inScope.Argument, inScope);
                OnTrigger.Invoke(inTriggerId, arg);
            }

            RSRuleData[] rules = m_Table?.Rules;
            int          ruleCount;

            if (rules == null || (ruleCount = rules.Length) <= 0)
            {
                return;
            }

            using (PooledSet <string> triggeredGroups = PooledSet <string> .Alloc())
            {
                for (int i = 0; i < ruleCount; ++i)
                {
                    RSRuleData rule = rules[i];
                    if (rule.TriggerId != inTriggerId)
                    {
                        continue;
                    }

                    if (m_States[i].HasFlag(RuleState.Disabled))
                    {
                        continue;
                    }

                    if (rule.DontInterrupt && m_Routines[i])
                    {
                        continue;
                    }

                    if (!inScope.EvaluateConditions(rule.Conditions, rule.ConditionSubset))
                    {
                        continue;
                    }

                    if (!string.IsNullOrEmpty(rule.RoutineGroup))
                    {
                        if (!triggeredGroups.Add(rule.RoutineGroup))
                        {
                            continue;
                        }

                        StopRuleGroup(rule.RoutineGroup);
                    }

                    if (rule.OnlyOnce)
                    {
                        m_States[i] |= RuleState.Disabled;
                    }

                    if (rule.Actions != null)
                    {
                        ExecutionScope scope = inScope;
                        scope.m_Environment.CloneScopeIfNecessary(scope, rule.Flags, out scope);

                        m_Routines[i].Replace(m_Entity.ProxyObject, scope.PerformActions(rule.Actions))
                        .ExecuteWhileDisabled().SetPhase(m_Entity.ExecutionPhase)
                        .TryManuallyUpdate(0);
                    }
                }
            }
        }
Exemple #18
0
 internal RSValue EvaluateQuery(IRSRuntimeEntity inEntity, RSQueryInfo inQuery, RSValue[] inArgs, ExecutionScope inContext)
 {
     return(inQuery.Invoke(inEntity, inArgs, inContext));
 }