Beispiel #1
0
        /**
         * @brief 从Xml节点中读取字符串数组
         *
         * @param node xml节点
         * @param nodeName 节点名字
         * @param defualtVal 默认值
         * @param isMust 是否强制不能为空
         *
         * @return
         */
        public static List <string> ExtractStringList(DBC_Row node, string nodeName, string defualtVal, bool isMust)
        {
            List <string> result = new List <string>();

            if (node == null || !node.HasFields)
            {
                //if (isMust)
                //{
                //  string errorInfo = string.Format("ExtractStringList Error node:{0} nodeName:{1}", node.RowIndex, nodeName);
                //  LogSystem.Assert(false, errorInfo);
                //}

                return(result);
            }

            string nodeText = node.SelectFieldByName(nodeName);

            if (Helper.StringIsNullOrEmpty(nodeText))
            {
                //if (isMust)
                //{
                //  string errorInfo = string.Format("ExtractStringList Error node:{0} nodeName:{1}", node.RowIndex, nodeName);
                //  LogSystem.Assert(false, errorInfo);
                //}
            }
            else
            {
                result = Converter.ConvertStringList(nodeText);
            }

            return(result);
        }
Beispiel #2
0
        public bool CollectDataFromDBC(string file, string rootLabel)
        {
            bool result = true;

            DBC document = new DBC();

            document.Load(HomePath.GetAbsolutePath(file));

            for (int index = 0; index < document.RowNum; index++)
            {
                DBC_Row node = document.GetRowByIndex(index);
                if (node != null)
                {
                    TData  data = new TData();
                    bool   ret  = data.CollectDataFromDBC(node);
                    string info = string.Format("DataTableMgr.CollectDataFromDBC collectData Row:{0} failed!", index);
                    LogMgr.UnityLog(info);
                    //CoreEntry.gLogMgr.Log(LogLevel.ERROR, "Excepton", info);

                    if (ret)
                    {
                        m_DataContainer.Add(data);
                    }
                    else
                    {
                        result = false;
                    }
                }
            }

            return(result);
        }
Beispiel #3
0
        /**
         * @brief 从Xml节点中读取字符串
         *
         * @param node xml节点
         * @param nodeName 节点名字
         * @param defualtVal 默认值
         * @param isMust 是否强制不能为空
         *
         * @return
         */
        public static string ExtractString(DBC_Row node, string nodeName, string defualtVal, bool isMust)
        {
            string result = defualtVal;

            if (node == null || !node.HasFields || node.SelectFieldByName(nodeName) == null)
            {
                //if (isMust)
                //{
                //  string errorInfo = string.Format("ExtactString Error node:{0} nodeName:{1}", node.RowIndex, nodeName);
                //  LogSystem.Assert(false, errorInfo);
                //}

                return(result);
            }

            string nodeText = node.SelectFieldByName(nodeName);

            if (Helper.StringIsNullOrEmpty(nodeText))
            {
                //if (isMust)
                //{
                //  string errorInfo = string.Format("ExtactString Error node:{0} nodeName:{1}", node.RowIndex, nodeName);
                //  LogSystem.Assert(false, errorInfo);
                //}
            }
            else
            {
                result = nodeText;
            }

            return(result);
        }
Beispiel #4
0
        /**
         * @brief 从Xml节点中抽取所有以prefix为前缀的节点
         *
         * @param node xml节点
         * @param prefix 前缀字符串
         *
         * @return
         */
        public static List <string> ExtractNodeByPrefix(DBC_Row node, string prefix)
        {
            if (node == null || !node.HasFields)
            {
                return(null);
            }

            return(node.SelectFieldsByPrefix(prefix));
        }
Beispiel #5
0
        /**
         * @brief 返回文本,根据行、列号
         *
         * @param rowIndex
         * @param colIndex
         *
         * @return
         */
        public string GetField(int rowIndex, int colIndex)
        {
            if (rowIndex < 0 || rowIndex >= m_RowNum ||
                colIndex < 0 || colIndex >= m_ColumNum)
            {
                return("");
            }

            DBC_Row dbRow = GetRowByIndex(rowIndex);

            if (dbRow != null)
            {
                return(dbRow.SelectFieldByIndex(colIndex));
            }

            return("");
        }
Beispiel #6
0
        /**
         * @brief 返回文本根据行关键字和列名
         *
         * @param id
         * @param headerName
         *
         * @return
         */
        public string GetField(string id, string headerName)
        {
            if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(headerName))
            {
                return("");
            }

            if (m_HashData != null && m_HashData.ContainsKey(id))
            {
                DBC_Row row = m_HashData[id];
                if (row != null)
                {
                    return(row.SelectFieldByName(headerName));
                }
            }

            return("");
        }
Beispiel #7
0
        /**
         * @brief 从Xml节点中读取布尔值
         *
         * @param node xml节点
         * @param nodeName 节点名字
         * @param defualtVal 默认值
         * @param isMust 是否强制不能为空
         *
         * @return
         */
        public static bool ExtractBool(DBC_Row node, string nodeName, bool defualtVal, bool isMust)
        {
            bool result = defualtVal;

            if (node == null || !node.HasFields || node.SelectFieldByName(nodeName) == null)
            {
                //if (isMust)
                //{
                //  string errorInfo = string.Format("ExtractBool Error node:{0} nodeName:{1}", node.RowIndex, nodeName);
                //  LogSystem.Assert(false, errorInfo);
                //}

                return(result);
            }

            string nodeText = node.SelectFieldByName(nodeName);

            if (nodeText == null)
            {
                return(result);
            }
            if (Helper.StringIsNullOrEmpty(nodeText))
            {
                //if (isMust)
                //{
                //  string errorInfo = string.Format("ExtractBool Error node:{0} nodeName:{1}", node.RowIndex, nodeName);
                //  LogSystem.Assert(false, errorInfo);
                //}
            }
            else
            {
                if (nodeText.Trim().ToLower() == "true" || nodeText.Trim().ToLower() == "1")
                {
                    result = true;
                }

                if (nodeText.Trim().ToLower() == "false" || nodeText.Trim().ToLower() == "0")
                {
                    result = false;
                }
            }

            return(result);
        }
Beispiel #8
0
        /**
         * @brief 从Xml节点中读取数值类型,使用时,必须在函数中指明数值类型
         *          如: int id = ExtractNumeric<int>(xmlNode, "Id", -1, true);
         *
         * @param node xml节点
         * @param nodeName 节点名字
         * @param defualtVal 默认值
         * @param isMust 是否强制不能为空
         *
         * @return
         */
        public static T ExtractNumeric <T>(DBC_Row node, string nodeName, T defualtVal, bool isMust)
        {
            T result = defualtVal;

            if (node == null || !node.HasFields || node.SelectFieldByName(nodeName) == null)
            {
                //if (isMust)
                //{
                //  string errorInfo = string.Format("ExtractNumeric Error node:{0} nodeName:{1}", node.RowIndex, nodeName);
                //  LogSystem.Assert(false, errorInfo);
                //}

                return(result);
            }

            string nodeText = node.SelectFieldByName(nodeName);

            if (Helper.StringIsNullOrEmpty(nodeText))
            {
                //if (isMust)
                //{
                //  string errorInfo = string.Format("ExtractNumeric Error node:{0} nodeName:{1}", node.RowIndex, nodeName);
                //  LogSystem.Assert(false, errorInfo);
                //}
            }
            else
            {
                try {
                    result = (T)Convert.ChangeType(nodeText, typeof(T));
                } catch (System.Exception ex) {
                    string info = string.Format("ExtractNumeric Error node:{0} nodeName:{1} ex:{2} stacktrace:{3}",
                                                node.RowIndex, nodeName, ex.Message, ex.StackTrace);
                    LogMgr.UnityLog(info);
                    //LogSystem.Debug(info);

                    Helper.LogCallStack();
                }
            }

            return(result);
        }
Beispiel #9
0
        /**
         * @brief 从文本文件流中读取
         *
         * @param sr
         *
         * @return
         */
        private bool LoadFromStream_Text(StreamReader sr)
        {
            //--------------------------------------------------------------
            //临时变量
            List <string> vRet    = null;
            string        strLine = "";

            //读第一行,标题行
            strLine = sr.ReadLine();
            //读取失败,即认为读取结束
            if (strLine == null)
            {
                return(false);
            }

            vRet = ConvertStringList(strLine, new string[] { "\t" });
            if (vRet == null || vRet.Count == 0)
            {
                return(false);
            }
            m_Header = vRet;

            //--------------------------------------------------------------
            //初始化
            int nRecordsNum = 0;
            int nFieldsNum  = vRet.Count;

            //--------------------------------------------------------------
            //开始读取
            DBC_Row dbcRow = null;

            do
            {
                vRet   = null;
                dbcRow = null;

                //读取一行
                strLine = sr.ReadLine();
                //读取失败,即认为读取结束
                if (strLine == null)
                {
                    break;
                }

                //是否是注释行
                if (strLine.StartsWith("#"))
                {
                    continue;
                }

                //分解
                vRet = ConvertStringList(strLine, new string[] { "\t" });

                //列数不对
                if (vRet.Count == 0)
                {
                    continue;
                }
                if (vRet.Count != nFieldsNum)
                {
                    //补上空格
                    if (vRet.Count < nFieldsNum)
                    {
                        int nSubNum = nFieldsNum - vRet.Count;
                        for (int i = 0; i < nSubNum; i++)
                        {
                            vRet.Add("");
                        }
                    }
                }

                //第一列不能为空
                if (string.IsNullOrEmpty(vRet[0]))
                {
                    continue;
                }

                dbcRow      = new DBC_Row(this, nRecordsNum);
                dbcRow.Data = vRet;

                m_DataBuf.Add(dbcRow);

                nRecordsNum++;
            } while (true);

            //--------------------------------------------------------
            //生成正式数据库
            m_ColumNum = nFieldsNum;
            m_RowNum   = nRecordsNum;

            //--------------------------------------------------------
            //创建索引
            CreateIndex();

            return(true);
        }