Пример #1
0
        /// <summary>
        /// Adds the named unit to the specified group.
        /// </summary>
        /// <param name="unitName">Name of the unit.</param>
        /// <param name="groupName">Name of the group to add the unit to.</param>
        /// <returns>Unit result value.</returns>
        private UnitResult AddUnitToGroup(string unitName, string groupName)
        {
            UnitEntry unit  = this.m_Units[unitName];
            UnitGroup group = this.m_UnitGroups[groupName];

            // Make sure the unit exists.
            if (unit == null)
            {
                return(UnitResult.UnitNotFound);
            }

            // Make sure the group exists.
            if (group == null)
            {
                return(UnitResult.GroupNotFound);
            }

            // Add the unit.
            group.AddUnit(unit);

            return(UnitResult.NoError);
        }
Пример #2
0
 /// <summary>
 /// Adds a unit to the group.
 /// </summary>
 /// <param name="unit">Unit to add to the group.</param>
 /// <returns>Unit result value.</returns>
 public UnitResult AddUnit(UnitEntry unit)
 {
     m_Units[unit.Name] = unit;
     return UnitResult.NoError;
 }
Пример #3
0
        /// <summary>
        /// Parses an XML node that represents a unit.
        /// </summary>
        /// <param name="groupName">Name of the group this unit is in.</param>
        /// <param name="unitnode">The node containing the unit information.</param>
        /// <returns>A unit result value.</returns>
        private UnitResult ParseUnitXMLNode(string filePath, UnitGroup group, XmlNode unitnode)
        {
            int i = 0;

            UnitEntry unit = new UnitEntry();

            // Make sure the unit has a name.
            if (unitnode.Attributes["name"] == null)
            {
                SendUnitFileWarning("found a unit in group '{0}' with no name, ignored.", filePath, new object[] { group.Name });
                return(UnitResult.GenericError);
            }

            // Store off the name.
            unit.Name          = unitnode.Attributes["name"].Value;
            unit.DefaultSymbol = unit.Name.ToLower();

            // Don't allow duplicate units.
            if (GetUnitByName(unit.Name) != null)
            {
                SendUnitFileWarning("duplicate unit with name '{0}' was found and ignored.", filePath, new object[] { unit.Name });
                return(UnitResult.UnitExists);
            }

            // Get every unit property.
            for (i = 0; i < unitnode.ChildNodes.Count; i++)
            {
                XmlNode unitprop = unitnode.ChildNodes[i];

                //Ignore comments.
                if (unitprop.Name.ToLower() == "#comment")
                {
                    continue;
                }

                try
                {
                    if (unitprop.Name.ToLower() == "multiply")
                    {
                        double x;
                        if (ParseNumberString(unitprop.InnerText, out x) != UnitResult.NoError)
                        {
                            throw new System.Exception();
                        }

                        unit.Multiplier = x;

                        //unit.m_Multiplier = Convert.ToDouble(unitprop.InnerText);
                    }
                    else
                    {
                        if (unitprop.Name.ToLower() == "add")
                        {
                            unit.Adder = Convert.ToDouble(unitprop.InnerText);
                        }
                        else
                        {
                            if (unitprop.Name.ToLower() == "preadd")
                            {
                                unit.PreAdder = Convert.ToDouble(unitprop.InnerText);
                            }
                        }
                    }
                }
                catch
                {
                    SendUnitFileWarning("unit '{0}' has invalid '{1}' value. Unit skipped.", filePath, new object[] { unit.Name, unitprop.Name });
                    return(UnitResult.GenericError);
                }

                // Parse the symbol properties.
                if (unitprop.Name.ToLower() == "symbol")
                {
                    //Put the value into the symbol table
                    if ((unitprop.InnerText != "") && (unitprop.InnerText != null))
                    {
                        if (this.m_SymbolTable[unitprop.InnerText] != null)
                        {
                            SendUnitFileWarning("while parsing unit '{0}' - a duplicate symbol was found and ignored ({1}).", filePath, new object[] { unit.Name, unitprop.InnerText });
                        }
                        else
                        {
                            this.m_SymbolTable[unitprop.InnerText] = unit;

                            // Is this unit the default unit?
                            if (unitprop.Attributes["default"] != null)
                            {
                                unit.DefaultSymbol = unitprop.InnerText;
                            }
                        }
                    }
                    else
                    {
                        SendUnitFileWarning("unit '{0}' has an invalid symbol specified, symbol skipped.", filePath, new object[] { unit.Name });
                    }
                }
            }

            // Add the unit to the unit table.
            m_Units[unit.Name] = unit;

            // Add the unit to the group
            AddUnitToGroup(unit.Name, group.Name);

            // All done!
            return(UnitResult.NoError);
        }
        /// <summary>
        /// Parses an XML node that represents a unit.
        /// </summary>
        /// <param name="groupName">Name of the group this unit is in.</param>
        /// <param name="unitnode">The node containing the unit information.</param>
        /// <returns>A unit result value.</returns>
        private UnitResult ParseUnitXMLNode(string filePath, UnitGroup group, XmlNode unitnode)
        {
            int i = 0;

            UnitEntry unit = new UnitEntry();

            //Make sure the unit has a name
            if (unitnode.Attributes["name"] == null)
            {
                SendUnitFileWarning("found a unit in group '{0}' with no name, ignored.", filePath, new object[] { group.Name });
                return UnitResult.GenericError;
            }

            //Store off the name
            unit.Name = unitnode.Attributes["name"].Value;
            unit.DefaultSymbol = unit.Name.ToLower();

            //Dont allow duplicate units
            if (GetUnitByName(unit.Name) != null)
            {
                SendUnitFileWarning("duplicate unit with name '{0}' was found and ignored.", filePath, new object[] { unit.Name });
                return UnitResult.UnitExists;
            }

            //Get every unit property
            for (i = 0; i < unitnode.ChildNodes.Count; i++)
            {
                XmlNode unitprop = unitnode.ChildNodes[i];

                //Ignore comments.
                if (unitprop.Name.ToLower() == "#comment")
                    continue;

                try
                {
                    if (unitprop.Name.ToLower() == "multiply")
                    {
                        double x;
                        if (ParseNumberString(unitprop.InnerText, out x) != UnitResult.NoError)
                            throw new System.Exception();

                        unit.Multiplier = x;

                        //unit.m_Multiplier = Convert.ToDouble(unitprop.InnerText);
                    }
                    else if (unitprop.Name.ToLower() == "add")
                        unit.Adder = Convert.ToDouble(unitprop.InnerText);
                    else if (unitprop.Name.ToLower() == "preadd")
                        unit.PreAdder = Convert.ToDouble(unitprop.InnerText);
                }
                catch
                {
                    SendUnitFileWarning("unit '{0}' has invalid '{1}' value. Unit skipped.", filePath, new object[] { unit.Name, unitprop.Name });
                    return UnitResult.GenericError;
                }

                //Parse the symbol properties
                if (unitprop.Name.ToLower() == "symbol")
                {
                    //Put the value into the symbol table
                    if ((unitprop.InnerText != "") && (unitprop.InnerText != null))
                    {
                        if (this.m_SymbolTable[unitprop.InnerText] != null)
                            SendUnitFileWarning("while parsing unit '{0}' - a duplicate symbol was found and ignored ({1}).", filePath, new object[] { unit.Name, unitprop.InnerText });
                        else
                        {
                            this.m_SymbolTable[unitprop.InnerText] = unit;

                            //Is this unit the default unit?
                            if (unitprop.Attributes["default"] != null)
                                unit.DefaultSymbol = unitprop.InnerText;
                        }
                    }
                    else
                        SendUnitFileWarning("unit '{0}' has an invalid symbol specified, symbol skipped.", filePath, new object[] { unit.Name });
                }
            }

            //Add the unit to the unit table.
            this.m_Units[unit.Name] = unit;

            //Add the unit to the group
            AddUnitToGroup(unit.Name, group.Name);

            //All done!
            return UnitResult.NoError;
        }