Exemple #1
0
        public void Assign(string value)
        {
            string error = null;
            object temp;

            if (TypeIndex == 1)
            {
                // do float assignment
                temp = float.Parse(value) * this.Multiplier;
            }
            else if (TypeIndex == 2)
            {
                // do double assignment
                temp = double.Parse(value) * this.Multiplier;
            }
            else
            {
                var t = (this.Field == null ? this.Property.PropertyType : this.Field.FieldType);
                if ((temp = ArbitraryParameterParser.ArbitraryParameterParse(t, value, ref error)) == null)
                {
                    throw new XTMFRuntimeException("Unable to convert value!");
                }
            }

            if (this.Field != null)
            {
                this.Field.SetValue(AssignTo, temp);
            }
            else
            {
                this.Property.SetValue(AssignTo, temp, null);
            }
        }
        internal bool Validate(string newData)
        {
            string error = null;
            var    res   = ArbitraryParameterParser.ArbitraryParameterParse(this.Type, newData, ref error);

            if (res == null)
            {
                MessageBox.Show(error, "Invalid Data", MessageBoxButton.OK, MessageBoxImage.Error);
                return(false);
            }
            return(true);
        }
Exemple #3
0
        public void BlendedAssignment(string value, float ammount)
        {
            var t = (Field != null ? Field.FieldType : Property.PropertyType);

            if (t == typeof(float))
            {
                double temp;
                if (double.TryParse(value, out temp))
                {
                    // do float assignment
                    CurrentBlendNumber += temp * ammount;
                }
            }
            else if (t == typeof(double))
            {
                double temp;
                if (double.TryParse(value, out temp))
                {
                    // do float assignment
                    CurrentBlendNumber += temp * ammount;
                }
            }
            else if (t == typeof(bool))
            {
                // take the "true'est value
                bool temp;
                if (bool.TryParse(value, out temp))
                {
                    this.CurrentBlendBool = this.CurrentBlendBool | temp;
                }
            }
            else
            {
                string error = null;
                object temp;
                if ((temp = ArbitraryParameterParser.ArbitraryParameterParse(t, value, ref error)) == null)
                {
                    throw new XTMFRuntimeException("Unable to convert value!");
                }
                if (this.Field != null)
                {
                    this.Field.SetValue(Parent.Mode, temp);
                }
                else
                {
                    this.Property.SetValue(Parent.Mode, temp, null);
                }
            }
        }
        public void TestStringParsing()
        {
            string error = null;
            var    obj   = ArbitraryParameterParser.ArbitraryParameterParse(typeof(string), "12345.123", ref error);

            if (!(obj is string))
            {
                Assert.Fail("We should be able to parse a string!");
            }
            obj = ArbitraryParameterParser.ArbitraryParameterParse(typeof(string), "asdbasd.321", ref error);
            if (obj == null || error != null)
            {
                Assert.Fail("Strings should always be \"parsed\"");
            }
        }
        public void TestIntegerParsing()
        {
            string error = null;
            var    obj   = ArbitraryParameterParser.ArbitraryParameterParse(typeof(int), "12345", ref error);

            if (!(obj is int))
            {
                Assert.Fail("We should be able to parse a number!");
            }
            obj = ArbitraryParameterParser.ArbitraryParameterParse(typeof(int), "asdbasd", ref error);
            if (obj != null || error == null)
            {
                Assert.Fail("We need to make sure that invalid data doesn't get parsed");
            }
        }
        public void TestDateTimeParsing()
        {
            string error = null;
            var    obj   = ArbitraryParameterParser.ArbitraryParameterParse(typeof(DateTime), "10:00 AM", ref error);

            if (!(obj is DateTime))
            {
                Assert.Fail("We should be able to parse a DateTime!");
            }
            obj = ArbitraryParameterParser.ArbitraryParameterParse(typeof(DateTime), "123211231:123:321", ref error);
            if (obj != null || error == null)
            {
                Assert.Fail("We need to make sure that invalid data doesn't get parsed");
            }
        }
        public void TestTestStructParsing()
        {
            string error = null;
            var    obj   = ArbitraryParameterParser.ArbitraryParameterParse(typeof(TestStruct), "10:00 AM", ref error);

            if (!(obj is TestStruct))
            {
                Assert.Fail("We should be able to parse a TestStruct!");
            }
            obj = ArbitraryParameterParser.ArbitraryParameterParse(typeof(TestStruct), "", ref error);
            if (obj != null || error == null)
            {
                Assert.Fail("We need to make sure that invalid data doesn't get parsed");
            }
        }
        public static void AssignValue(IConfiguration config, IModuleParameter parameter, string value)
        {
            string error = null;
            object trueValue;

            if (parameter == null)
            {
                throw new XTMFRuntimeException(parameter.BelongsTo?.Module, "The parameter was null!");
            }
            if ((trueValue = ArbitraryParameterParser.ArbitraryParameterParse(parameter.Type, value, ref error)) != null)
            {
                AssignValueNoTypeCheck(config, parameter, trueValue);
            }
            else
            {
                throw new XTMFRuntimeException(parameter.BelongsTo?.Module, "We were unable to assign the value of '" + value + "' to the parameter " + parameter.Name);
            }
        }
Exemple #9
0
        public static void AssignValue(IModuleParameter parameter, string value)
        {
            string error = null;
            object trueValue;

            if (parameter == null)
            {
                throw new XTMFRuntimeException("The parameter was null!");
            }
            var currentStructure = parameter.BelongsTo;

            if (currentStructure == null)
            {
                throw new XTMFRuntimeException("The parameter doesn't belong to any module!");
            }
            if (value == null)
            {
                throw new XTMFRuntimeException("The value was null!");
            }
            if (currentStructure.Module == null)
            {
                throw new XTMFRuntimeException("The currentstructure.Module was null!");
            }
            if ((trueValue = ArbitraryParameterParser.ArbitraryParameterParse(parameter.Type, value, ref error)) != null)
            {
                parameter.Value = trueValue;
                var type = currentStructure.Module.GetType();
                if (parameter.OnField)
                {
                    var field = type.GetField(parameter.VariableName);
                    field.SetValue(currentStructure.Module, trueValue);
                }
                else
                {
                    var field = type.GetProperty(parameter.VariableName);
                    field.SetValue(currentStructure.Module, trueValue, null);
                }
            }
            else
            {
                throw new XTMFRuntimeException("We were unable to assign the value of '" + value + "' to the parameter " + parameter.Name);
            }
        }
Exemple #10
0
 private bool SetValue(string value, ref string error)
 {
     if (this.ContainedIn == null)
     {
         var temp = ArbitraryParameterParser.ArbitraryParameterParse(AttachedParameter.Type, value, ref error);
         if (temp == null)
         {
             return(false);
         }
         AttachedParameter.Value = temp;
     }
     else
     {
         if (!this.ContainedIn.SetValue(this.After, ref error))
         {
             return(false);
         }
     }
     return(true);
 }
Exemple #11
0
        public void LoadData()
        {
            var ret = new LabeledData <T>();

            using (var reader = new CsvReader(LoadFrom, true))
            {
                //burn the header
                reader.LoadLine();
                string error      = null;
                int    lineNumber = 0;
                // load the data
                while (reader.LoadLine(out int columns))
                {
                    lineNumber++;
                    if (columns >= 2)
                    {
                        reader.Get(out string label, 0);
                        if (typeof(T) == typeof(float))
                        {
                            LabeledData <float> fRet = ret as LabeledData <float>;
                            reader.Get(out float parsedData, 1);
                            Add(fRet, label, parsedData);
                        }
                        else
                        {
                            reader.Get(out string data, 1);
                            var parsedData = ArbitraryParameterParser.ArbitraryParameterParse(typeof(T), data, ref error);
                            if (parsedData == null || error != null)
                            {
                                throw new XTMFRuntimeException(this, $"In '{Name}' we were unable to parse the data in line number {lineNumber}!\r\n{error}");
                            }
                            Add(ret, label, (T)parsedData);
                        }
                    }
                }
            }
            _Data  = ret;
            Loaded = true;
        }