26
2017
09

Commom(十)—— ConfigReader

 目录为:Assets/Scripts/Commom/
 ConfigReader.cs

里面代码可以参考一下.

using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;


public class CConfigReader
{
    //.txt
    //.html
    //.htm
    //.xml
    //.bytes
    //.json
    //.csv
    //.yaml
    //.fnt
    //上面这些格式的文件在拖入unity3d里面时都会变成TextAsset资源
    private TextAsset m_pcText = null;
    private string m_pcString = null;
    private Int32 m_n32ReadPos = 0;
    static StringBuilder s_pcTempBuilder = null;

    //是否是普通字符,而不是这些转义字符
    private Boolean IsValidChar(Char cTempChar)
    {
        if ('\t' == cTempChar || '\n' == cTempChar || 'r' == cTempChar)
        {
            return false;
        }

        return true;
    }

    //判断是否是换行
    private Boolean IsChangeLineChar(Char cTempChar)
    {
        if ('\n' == cTempChar || '\r' == cTempChar)
        {
            return true;
        }

        return false;
    }

    //构造函数
    public CConfigReader()
    {
        m_pcText = null;
        m_pcString = null;
        m_n32ReadPos = 0;

        if (null == s_pcTempBuilder)
        {
            s_pcTempBuilder = new StringBuilder ();
        }
    }

    public Int32 Init(string paConfigFile)
    {
        //这里的ResourcesManger后面再看
        ResourceUnit texUnit = ResourcesManager.Instance.loadImmediate (paConfigFile, ResourceType.ASSET);

        //主要的TextAsset资源
        m_pcText = texUnit.Asset as TextAsset;

        m_n32ReadPos = 0;
        if (null == m_pcText)
        {
            return (Int32)EErrorCode.eEC_LoadConfigFileFail;
        }

        //text
        m_pcString = m_pcText.text;

        return (Int32)EErrorCode.eNormal;
    }

    public Int32 Uninit()
    {
        //释放TextAsset资源
        Resources.UnloadAsset (m_pcText);
        m_pcText = null;
        m_pcString = null;
        return (Int32)EErrorCode.eNormal;
    }

    //是否到了文件尾
    public bool Eof()
    {
        if (m_n32ReadPos >= m_pcString.Length)
        {
            return true;
        }

        return false;
    }

    public Int32 GetTextLength()
    {
        return m_pcString.Length;
    }

    //读取字符
    public Char ReadChar()
    {
        if (Eof())
        {
            return '\0';
        }

        return m_pcString [m_n32ReadPos++];
    }

    //当前readPos处的字符
    public Char CurChar()
    {
        if (Eof())
        {
            return '\0';
        }

        return m_pcString [m_n32ReadPos];
    }

    //跳过字符
    public Boolean SkipChar()
    {
        if (Eof())
        {
            return false;
        }

        m_n32ReadPos++;
        return true;
    }

    //----------------------------------------------------

    //查找本文本string里是否有和pcMark相同的字符串
    //然后readPos会到那里
    public Boolean ReadTo(string pcMark)
    {
        string pcReadStr = ReadWords ();
        Boolean bFind = false;
        while (!Eof())
        {
            //比较是否相等
            if (pcReadStr == pcMark)
            {
                bFind = true;
                break;
            }

            pcReadStr = ReadWords ();
        }

        return bFind;
    }

    //读取一行
    public string ReadLine()
    {
        s_pcTempBuilder.Length = 0;
        Char cTempChar = ReadChar ();

        //不是换行符,不是文件尾
        while (!IsChangeLineChar(cTempChar) && !Eof())
        {
            s_pcTempBuilder.Append (cTempChar);
            cTempChar = ReadChar ();
        }

        if (s_pcTempBuilder.Length > 0)
        {
            return s_pcTempBuilder.ToString ();
        }
        else
        {
            return null;
        }
    }

    //跳过一行
    public Boolean SkipLine()
    {
        Char cTempChar = ReadChar ();

        while (!IsChangeLineChar(cTempChar) && !Eof())
        {
            cTempChar = ReadChar ();
        }

        return true;
    }

    //读取words
    public string ReadWords()
    {
        //默认字母数字都读取
        Boolean bOnlyDigitF = false;
        string de = ReadWordsHe (bOnlyDigitF);
        return de;
    }

    public string ReadWords(Boolean bOnlyDigitT)
    {
        string de = ReadWordsHe (bOnlyDigitT);
        return de;
    }

    //bOnlyDigitTF:表示是否只读取数字字符
    //这个函数基本会是只读一行,然后传回string
    //有点奇怪
    public string ReadWordsHe(Boolean bOnlyDigitTF)
    {
        s_pcTempBuilder.Length = 0;

        //当前的字符是特殊字符(\t,\n,\r), 并且不是文件尾
        while(!IsValidChar(CurChar()) && !Eof())
        {
            SkipChar ();
        }

        //是普通字符
        while(!Eof() && IsValidChar(CurChar()))
        {
            if (bOnlyDigitTF)
            {
                //是否是数字字符
                if (Char.IsDigit(CurChar()))
                {
                    s_pcTempBuilder.Append (CurChar ());
                }
            }
            else
            {
                s_pcTempBuilder.Append (CurChar ());
            }

            SkipChar ();
        }

        if (s_pcTempBuilder.Length > 0)
        {
            return s_pcTempBuilder.ToString ();
        }
        else
        {
            return null;
        }
    }

    //读取字符到Array里面
    public Boolean ReadCharArray(char[] pszCharArray, Int32 n32ArraySize)
    {
        //如果是三个转义字符的其中一个
        while (!IsValidChar(CurChar()) && !Eof())
        {
            SkipChar ();
        }

        Int32 n32Index = 0;
        //是普通字符
        while(!Eof() && IsValidChar(CurChar()))
        {
            pszCharArray [n32Index++] = CurChar ();
            SkipChar ();
            if (n32Index >= n32ArraySize)
            {
                break;
            }
        }

        if (n32Index < n32ArraySize)
        {
            return false;
        }

        return true;
    }

    //读取Boolean
    public Boolean ReadBoolean()
    {
        Int32 n32Value = 0;
        //只读数字
        string pcTempStr = ReadWords (true);
        if (null != pcTempStr)
        {
            n32Value = Convert.ToInt32 (pcTempStr);
        }

        if (0 == n32Value)
        {
            return false;
        }

        return true;
    }

    //读取的words转换为byte
    public byte ReadByte()
    {
        byte bValue = 0;
        string pcTempStr = ReadWords ();
        if (null != pcTempStr)
        {
            bValue = Convert.ToByte (pcTempStr);
        }

        return bValue;
    }

    public Int16 ReadInt16()
    {
        Int16 n16Value = 0;
        string pcTempStr = ReadWords (true);
        if (null != pcTempStr)
        {
            n16Value = Convert.ToInt16 (pcTempStr);
        }

        return n16Value;
    }

    public UInt16 ReadUInt16()
    {
        UInt16 un16Value = 0;
        string pcTempStr = ReadWords (true);
        if (null != pcTempStr)
        {
            un16Value = Convert.ToUInt16 (pcTempStr);
        }

        return un16Value;
    }

    public Int32 ReadInt32()
    {
        Int32 n32ReadValue = 0;
        string pcTempStr = ReadWords (true);
        if (null != pcTempStr)
        {
            n32ReadValue = Convert.ToInt32 (pcTempStr);
        }

        return n32ReadValue;
    }

    public UInt32 ReadUInt32()
    {
        UInt32 un32ReadValue = 0;
        string pcTempStr = ReadWords (true);
        if (null != pcTempStr)
        {
            un32ReadValue = Convert.ToUInt32 (pcTempStr);
        }

        return un32ReadValue;
    }

    public Int64 ReadInt64()
    {
        Int64 n64ReadValue = 0;
        string pcTempStr = ReadWords ();
        if (null != pcTempStr)
        {
            n64ReadValue = Convert.ToInt64 (pcTempStr);
        }

        return n64ReadValue;
    }

    public UInt64 ReadUInt64()
    {
        UInt64 un64ReadValue = 0;
        string pcTempStr = ReadWords (true);
        if (null != pcTempStr)
        {
            un64ReadValue = Convert.ToUInt64 (pcTempStr);
        }

        return un64ReadValue;
    }

    public float ReadFloat()
    {
        float fReadValue = 0f;
        string pcTempStr = ReadWords (true);
        if (null != pcTempStr)
        {
            fReadValue = (float)Convert.ToDouble (pcTempStr);
        }

        return fReadValue;
    }

    public Double ReadDouble()
    {
        Double dReadValue = 0f;
        string pcTempStr = ReadWords (true);
        if (null != pcTempStr)
        {
            dReadValue = (Double)Convert.ToDouble (pcTempStr);
        }

        return dReadValue;
    }
}
上一篇:Da 下一篇:【Android开发点滴】Android Studio常用快捷键