Beispiel #1
0
        /// <summary>
        /// This intermediary will cache the results so that multiple variable
        /// requests within the frame would not result in duplicated code.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public object ProcessVariable(string input, RPMVesselComputer comp)
        {
            input = input.Trim();

            if (RPMGlobals.debugShowVariableCallCount)
            {
                debug_callCount[input] = debug_callCount[input] + 1;
            }

            if (comp == null)
            {
                comp = RPMVesselComputer.Instance(vid);
            }

            if (!variableCache.ContainsKey(input))
            {
                AddVariable(input);
            }

            VariableCache vc = variableCache[input];

            if (vc.cacheable)
            {
                return(vc.value.Get());
            }
            else
            {
                return(vc.evaluator(input, comp));
            }
        }
        internal IEnumerable <VariableDefinition> GetAllVariablesForType(Type t, MappingSides side)
        {
            if (variableCache == null)
            {
                return(Enumerable.Empty <VariableDefinition>());
            }

            return(VariableCache.Where(v => v.Key.Type.IsAssignableFrom(t) && v.Key.Side == side).Select(v => v.Value));
        }
Beispiel #3
0
        public void FixedUpdate()
        {
            if (JUtil.RasterPropMonitorShouldUpdate(vessel) && timeToUpdate)
            {
                UpdateLocalVars();

                RPMVesselComputer comp = RPMVesselComputer.Instance(vid);

                for (int i = 0; i < periodicRandomVals.Count; ++i)
                {
                    periodicRandomVals[i].counter -= refreshDataRate;
                    if (periodicRandomVals[i].counter <= 0)
                    {
                        periodicRandomVals[i].counter = periodicRandomVals[i].period;
                        periodicRandomVals[i].value   = UnityEngine.Random.value;
                    }
                }

                for (int i = 0; i < updatableVariables.Count; ++i)
                {
                    VariableCache vc     = updatableVariables[i];
                    float         oldVal = vc.value.AsFloat();
                    double        newVal;

                    object evaluant = vc.evaluator(vc.value.variableName, comp);
                    if (evaluant is string)
                    {
                        vc.value.isNumeric   = false;
                        vc.value.stringValue = evaluant as string;
                        newVal = 0.0;
                    }
                    else
                    {
                        newVal             = evaluant.MassageToDouble();
                        vc.value.isNumeric = true;
                    }
                    vc.value.numericValue = newVal;

                    if (!Mathf.Approximately(oldVal, (float)newVal) || forceCallbackRefresh == true)
                    {
                        vc.FireCallbacks((float)newVal);
                    }
                }

                ++debug_fixedUpdates;

                forceCallbackRefresh = false;
                timeToUpdate         = false;

                Vessel v = vessel;
                for (int i = 0; i < activeTriggeredEvents.Count; ++i)
                {
                    activeTriggeredEvents[i].Update(v);
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Register for a resource callback.  Resource callbacks provide a boolean that is
        /// updated when the named resource drops above or below 0.01f.
        /// </summary>
        /// <param name="variableName"></param>
        /// <param name="cb"></param>
        public void RegisterResourceCallback(string variableName, Action <bool> cb)
        {
            variableName = variableName.Trim();
            if (!variableCache.ContainsKey(variableName))
            {
                AddVariable(variableName);
            }

            VariableCache vc = variableCache[variableName];

            vc.onResourceDepletedCallbacks += cb;
            cb(vc.value.numericValue < 0.01);
        }
Beispiel #5
0
        /// <summary>
        /// Register a callback to receive notifications when a variable has changed.
        /// Used to prevent polling of low-frequency, high-utilization variables.
        /// </summary>
        /// <param name="variableName"></param>
        /// <param name="cb"></param>
        public void RegisterVariableCallback(string variableName, Action <float> cb)
        {
            variableName = variableName.Trim();
            if (!variableCache.ContainsKey(variableName))
            {
                AddVariable(variableName);
            }

            VariableCache vc = variableCache[variableName];

            vc.onChangeCallbacks += cb;
            cb((float)vc.value.numericValue);
        }
Beispiel #6
0
        /// <summary>
        /// Add a variable to the variableCache
        /// </summary>
        /// <param name="variableName"></param>
        private void AddVariable(string variableName)
        {
            VariableCache vc = new VariableCache();
            bool          cacheable;

            vc.evaluator = GetEvaluator(variableName, out cacheable);
            vc.value     = new VariableOrNumber(variableName, cacheable, this);
            vc.cacheable = cacheable;

            if (vc.value.variableType == VariableOrNumber.VoNType.VariableValue)
            {
                RPMVesselComputer comp  = RPMVesselComputer.Instance(vessel);
                object            value = vc.evaluator(variableName, comp);
                if (value is string)
                {
                    vc.value.stringValue  = value as string;
                    vc.value.isNumeric    = false;
                    vc.value.numericValue = 0.0;

                    // If the evaluator returns the variableName, then we
                    // have an unknown variable.  Change the VoN type to
                    // ConstantString so we don't waste cycles on update to
                    // reevaluate it.
                    if (vc.value.stringValue == variableName && !unrecognizedVariables.Contains(variableName))
                    {
                        vc.value.variableType = VariableOrNumber.VoNType.ConstantString;
                        unrecognizedVariables.Add(variableName);
                        JUtil.LogInfo(this, "Unrecognized variable {0}", variableName);
                    }
                }
                else
                {
                    vc.value.numericValue = value.MassageToDouble();
                    vc.value.isNumeric    = true;
                }
            }

            variableCache.Add(variableName, vc);

            if (vc.value.variableType == VariableOrNumber.VoNType.VariableValue)
            {
                // Only variables that are really variable need to be checked
                // during FixedUpdate.
                updatableVariables.Add(vc);
            }
        }
        /// <summary>
        /// Add a variable to the variableCache
        /// </summary>
        /// <param name="variableName"></param>
        private void AddVariable(string variableName)
        {
            VariableCache vc = new VariableCache();
            bool cacheable;
            vc.evaluator = GetEvaluator(variableName, out cacheable);
            vc.value = new VariableOrNumber(variableName, cacheable, this);
            vc.cacheable = cacheable;

            if (vc.value.variableType == VariableOrNumber.VoNType.VariableValue)
            {
                RPMVesselComputer comp = RPMVesselComputer.Instance(vessel);
                object value = vc.evaluator(variableName, comp);
                if (value is string)
                {
                    vc.value.stringValue = value as string;
                    vc.value.isNumeric = false;
                    vc.value.numericValue = 0.0;

                    // If the evaluator returns the variableName, then we
                    // have an unknown variable.  Change the VoN type to
                    // ConstantString so we don't waste cycles on update to
                    // reevaluate it.
                    if (vc.value.stringValue == variableName && !unrecognizedVariables.Contains(variableName))
                    {
                        vc.value.variableType = VariableOrNumber.VoNType.ConstantString;
                        unrecognizedVariables.Add(variableName);
                        JUtil.LogInfo(this, "Unrecognized variable {0}", variableName);
                    }
                }
                else
                {
                    vc.value.numericValue = value.MassageToDouble();
                    vc.value.isNumeric = true;
                }
            }

            variableCache.Add(variableName, vc);

            if (vc.value.variableType == VariableOrNumber.VoNType.VariableValue)
            {
                // Only variables that are really variable need to be checked
                // during FixedUpdate.
                updatableVariables.Add(vc);
            }
        }